2
2
#include <stdint.h>
3
3
#include <string.h>
4
4
#include <stdbool.h>
5
+ #include <stdlib.h>
5
6
6
7
#define MAX_COL 4
7
8
#define MAX_ROW 32
9
+ #define YEL "\x1B[33m"
10
+ #define RESET "\x1B[0m"
8
11
9
12
typedef struct parameter {
10
13
@@ -15,7 +18,7 @@ typedef struct parameter{
15
18
bool onlyOct ; //--octa shows only octa in output table
16
19
bool onlyDec ; //--dec shows only dec in output table
17
20
bool onlyHex ; //--hex shows only hex in output table
18
- bool onlyChar ; //--char shows only char in output table
21
+ bool onlyChar ; //
19
22
bool _onlyAll ; // if all only* 4 are false this will be set to true
20
23
21
24
char * content ; // " " content / data
@@ -27,76 +30,127 @@ typedef struct parameter{
27
30
28
31
void splashScreen (){
29
32
printf (" ascii - ASCII character set encoded in octal, decimal, and hexadecimal\n\n"
30
- "Oct Dec Hex Char | Oct Dec Hex Char | Oct Dec Hex Char | Oct Dec Hex Char \n"
31
- "------------------------------------------+----------------------+----------------------+ ----------------\n"
32
- "000 0 00 NUL (null) | 000 32 00 SPACE | 000 64 00 @ | 000 96 00 ` \n"
33
- "001 1 01 SOH (start of heading) | 000 33 00 ! | 000 65 00 A | 000 97 00 a \n"
34
- "002 2 02 STX (start of text) | 000 34 00 \" | 000 66 00 B | 000 98 00 b \n"
35
- "003 3 03 ETX (end of text) | 000 35 00 # | 000 67 00 C | 000 99 00 c \n"
36
- "004 4 04 EOT (end of transmission) | 000 36 00 $ | 000 68 00 D | 000 100 00 d \n"
37
- "005 5 05 ENQ (enquiry) | 000 37 00 %% | 000 69 00 E | 000 101 00 e \n"
38
- "006 6 06 ACK (acknowledge) | 000 38 00 & | 000 70 00 F | 000 102 00 f \n"
39
- "007 7 07 BEL (bell) | 000 39 00 ' | 000 71 00 G | 000 103 00 g \n"
40
- "008 8 08 BS (backspace) | 000 40 00 ( | 000 72 00 H | 000 104 00 h \n"
41
- "009 9 09 TAB (horizontal tab) | 000 41 00 ) | 000 73 00 I | 000 105 00 i \n"
42
- "010 10 0A LF (NL line feed, new line) | 000 42 00 * | 000 74 00 J | 000 106 00 j \n"
43
- "011 11 0B VT (vertical tab) | 000 43 00 + | 000 75 00 K | 000 107 00 k \n"
44
- "012 12 0C FF (NP form feed, new page) | 000 44 00 , | 000 76 00 L | 000 108 00 l \n"
45
- "013 13 0D CR (carriage return) | 000 45 00 - | 000 77 00 M | 000 109 00 m \n"
46
- "014 14 0E SO (shift out) | 000 46 00 . | 000 78 00 N | 000 110 00 n \n"
47
- "015 15 0F SI (shift in) | 000 47 00 / | 000 79 00 O | 000 111 00 o \n"
48
- "016 16 10 DLE (data link escape) | 000 48 00 0 | 000 80 00 P | 000 112 00 p \n"
49
- "017 17 11 DC1 (device control 1) | 000 49 00 1 | 000 81 00 Q | 000 113 00 q \n"
50
- "018 18 12 DC2 (device control 2) | 000 50 00 2 | 000 82 00 R | 000 114 00 r \n"
51
- "019 19 13 DC3 (device control 3) | 000 51 00 3 | 000 83 00 S | 000 115 00 s \n"
52
- "020 20 14 DC4 (device control 4) | 000 52 00 4 | 000 84 00 T | 000 116 00 t \n"
53
- "021 21 15 NAK (negative acknowledge) | 000 53 00 5 | 000 85 00 U | 000 117 00 u \n"
54
- "022 22 16 SYN (synchronous idle) | 000 54 00 6 | 000 86 00 V | 000 118 00 v \n"
55
- "023 23 17 ETB (end of trans. block) | 000 55 00 7 | 000 87 00 W | 000 119 00 w \n"
56
- "024 24 18 CAN (cancel) | 000 56 00 8 | 000 88 00 X | 000 120 00 x \n"
57
- "025 25 19 EM (end of medium) | 000 57 00 9 | 000 89 00 Y | 000 121 00 y \n"
58
- "026 26 1A SUB (substitute) | 000 58 00 : | 000 90 00 Z | 000 122 00 z \n"
59
- "027 27 1B ESC (escape) | 000 59 00 ; | 000 91 00 [ | 000 123 00 { \n"
60
- "028 28 1C FS (file separator) | 000 60 00 < | 000 92 00 \\ | 000 124 00 | \n"
61
- "029 29 1D GS (group separator) | 000 61 00 = | 000 93 00 ] | 000 125 00 } \n"
62
- "030 30 1E RS (record separator) | 000 62 00 > | 000 94 00 ^ | 000 126 00 ~ \n"
63
- "031 31 1F US (unit separator) | 000 63 00 ? | 000 95 00 _ | 177 127 00 DEL\n"
64
- "---------------------------------------------------------------------------------------------------------\n" );
33
+ "Oct Dec Hex " YEL "Chr" RESET " | Oct Dec Hex " YEL "Chr" RESET " | Oct Dec Hex " YEL "Chr" RESET " | Oct Dec Hex " YEL "Chr" RESET " \n"
34
+ "-------------------------------------------- +----------------------- +---------------------+---- ----------------\n"
35
+ "000 0 00 " YEL " NUL (null)" RESET " | 040 32 20 " YEL " SPACE" RESET " | 100 64 40 " YEL "@" RESET " | 140 96 60 " YEL "`" RESET " \n"
36
+ "001 1 01 " YEL " SOH (start of heading)" RESET " | 041 33 21 " YEL "!" RESET " | 101 65 41 " YEL "A" RESET " | 141 97 61 " YEL "a" RESET " \n"
37
+ "002 2 02 " YEL " STX (start of text)" RESET " | 042 34 22 " YEL "\"" RESET " | 102 66 42 " YEL "B" RESET " | 142 98 62 " YEL "b" RESET " \n"
38
+ "003 3 03 " YEL " ETX (end of text)" RESET " | 043 35 23 " YEL "#" RESET " | 103 67 43 " YEL "C" RESET " | 143 99 63 " YEL "c" RESET " \n"
39
+ "004 4 04 " YEL " EOT (end of transmission)" RESET " | 044 36 24 " YEL "$" RESET " | 104 68 44 " YEL "D" RESET " | 144 100 64 " YEL "d" RESET " \n"
40
+ "005 5 05 " YEL " ENQ (enquiry)" RESET " | 045 37 25 " YEL "%%" RESET " | 105 69 45 " YEL "E" RESET " | 145 101 65 " YEL "e" RESET " \n"
41
+ "006 6 06 " YEL " ACK (acknowledge)" RESET " | 046 38 26 " YEL "&" RESET " | 106 70 46 " YEL "F" RESET " | 146 102 66 " YEL "f" RESET " \n"
42
+ "007 7 07 " YEL " BEL (bell)" RESET " | 047 39 27 " YEL "'" RESET " | 107 71 47 " YEL "G" RESET " | 147 103 67 " YEL "g" RESET " \n"
43
+ "010 8 08 " YEL " BS (backspace)" RESET " | 050 40 28 " YEL "(" RESET " | 110 72 48 " YEL "H" RESET " | 150 104 68 " YEL "h" RESET " \n"
44
+ "011 9 09 " YEL " TAB (horizontal tab)" RESET " | 051 41 29 " YEL ")" RESET " | 111 73 49 " YEL "I" RESET " | 151 105 69 " YEL "i" RESET " \n"
45
+ "012 10 0A " YEL " LF (NL line feed, new line)" RESET " | 052 42 2A " YEL "*" RESET " | 112 74 4A " YEL "J" RESET " | 152 106 6A " YEL "j" RESET " \n"
46
+ "013 11 0B " YEL " VT (vertical tab)" RESET " | 053 43 2B " YEL "+" RESET " | 113 75 4B " YEL "K" RESET " | 153 107 6B " YEL "k" RESET " \n"
47
+ "014 12 0C " YEL " FF (NP form feed, new page)" RESET " | 054 44 2C " YEL "," RESET " | 114 76 4C " YEL "L" RESET " | 154 108 6C " YEL "l" RESET " \n"
48
+ "015 13 0D " YEL " CR (carriage return)" RESET " | 055 45 2D " YEL "-" RESET " | 115 77 4D " YEL "M" RESET " | 155 109 6D " YEL "m" RESET " \n"
49
+ "016 14 0E " YEL " SO (shift out)" RESET " | 056 46 2E " YEL "." RESET " | 116 78 4E " YEL "N" RESET " | 156 110 6E " YEL "n" RESET " \n"
50
+ "017 15 0F " YEL " SI (shift in)" RESET " | 057 47 2F " YEL "/" RESET " | 117 79 4F " YEL "O" RESET " | 157 111 6F " YEL "o" RESET " \n"
51
+ "020 16 10 " YEL " DLE (data link escape)" RESET " | 060 48 30 " YEL "0" RESET " | 120 80 50 " YEL "P" RESET " | 160 112 70 " YEL "p" RESET " \n"
52
+ "021 17 11 " YEL " DC1 (device control 1)" RESET " | 061 49 31 " YEL "1" RESET " | 121 81 51 " YEL "Q" RESET " | 161 113 71 " YEL "q" RESET " \n"
53
+ "022 18 12 " YEL " DC2 (device control 2)" RESET " | 062 50 32 " YEL "2" RESET " | 122 82 52 " YEL "R" RESET " | 162 114 72 " YEL "r" RESET " \n"
54
+ "023 19 13 " YEL " DC3 (device control 3)" RESET " | 063 51 33 " YEL "3" RESET " | 123 83 53 " YEL "S" RESET " | 163 115 73 " YEL "s" RESET " \n"
55
+ "024 20 14 " YEL " DC4 (device control 4)" RESET " | 064 52 34 " YEL "4" RESET " | 124 84 54 " YEL "T" RESET " | 164 116 74 " YEL "t" RESET " \n"
56
+ "025 21 15 " YEL " NAK (negative acknowledge)" RESET " | 065 53 35 " YEL "5" RESET " | 125 85 55 " YEL "U" RESET " | 165 117 75 " YEL "u" RESET " \n"
57
+ "026 22 16 " YEL " SYN (synchronous idle)" RESET " | 066 54 36 " YEL "6" RESET " | 126 86 56 " YEL "V" RESET " | 166 118 76 " YEL "v" RESET " \n"
58
+ "027 23 17 " YEL " ETB (end of trans. block)" RESET " | 067 55 37 " YEL "7" RESET " | 127 87 57 " YEL "W" RESET " | 167 119 77 " YEL "w" RESET " \n"
59
+ "030 24 18 " YEL " CAN (cancel)" RESET " | 070 56 38 " YEL "8" RESET " | 130 88 58 " YEL "X" RESET " | 170 120 78 " YEL "x" RESET " \n"
60
+ "031 25 19 " YEL " EM (end of medium)" RESET " | 071 57 39 " YEL "9" RESET " | 131 89 59 " YEL "Y" RESET " | 171 121 79 " YEL "y" RESET " \n"
61
+ "032 26 1A " YEL " SUB (substitute)" RESET " | 072 58 3A " YEL ":" RESET " | 132 90 5A " YEL "Z" RESET " | 172 122 7A " YEL "z" RESET " \n"
62
+ "033 27 1B " YEL " ESC (escape)" RESET " | 073 59 3B " YEL ";" RESET " | 133 91 5B " YEL "[" RESET " | 173 123 7B " YEL "{" RESET " \n"
63
+ "034 28 1C " YEL " FS (file separator)" RESET " | 074 60 3C " YEL "<" RESET " | 134 92 5C " YEL "\\" RESET " | 174 124 7C " YEL "|" RESET " \n"
64
+ "035 29 1D " YEL " GS (group separator)" RESET " | 075 61 3D " YEL "=" RESET " | 135 93 5D " YEL "]" RESET " | 175 125 7D " YEL "}" RESET " \n"
65
+ "036 30 1E " YEL " RS (record separator)" RESET " | 076 62 3E " YEL ">" RESET " | 136 94 5E " YEL "^" RESET " | 176 126 7E " YEL "~" RESET " \n"
66
+ "037 31 1F " YEL " US (unit separator)" RESET " | 077 63 3F " YEL "?" RESET " | 137 95 5F " YEL "_" RESET " | 177 127 7F " YEL " DEL" RESET " \n"
67
+ "--------------------------------------------------------------------------------------------------------------- \n" );
65
68
}
66
69
67
70
asciiParams parseParameter (int argv , char * * args ){
68
71
69
72
asciiParams params = {0 };
70
73
71
- for (uint8_t i = 0 ; i < argv ; i ++ ){
74
+ for (uint8_t i = 1 ; i < argv ; i ++ ){
75
+
76
+ if (!(strlen (args [i ]) >= 2 && args [i ][0 ] == '-' && args [i ][1 ] == '-' )){
77
+ if (params .content == NULL ) params .content = args [i ];
78
+ else strcat (params .content , args [i ]);
79
+
80
+ continue ;
81
+ }
82
+
72
83
if (strcmp (args [i ],"--all" ) == 0 ) params .showAll = true;
73
84
else if (strcmp (args [i ],"--digits" ) == 0 ) params .showAllDigits = true;
74
85
else if (strcmp (args [i ],"--alphas" ) == 0 ) params .showAllAlphas = true;
86
+
75
87
else if (strcmp (args [i ],"--octa" ) == 0 ) params .onlyOct = true;
76
88
else if (strcmp (args [i ],"--dec" ) == 0 ) params .onlyDec = true;
77
89
else if (strcmp (args [i ],"--hex" ) == 0 ) params .onlyHex = true;
78
- else if (strcmp (args [i ],"--char" ) == 0 ) params .onlyChar = true;
90
+ // else if(strcmp(args[i],"--char") == 0) params.onlyChar = true;
91
+
79
92
else if (strcmp (args [i ],"--asc" )== 0 ) params .order = 1 ;
80
93
else if (strcmp (args [i ],"--des" )== 0 ) params .order = 2 ;
81
- else params . content = args [ i ];
94
+
82
95
}
83
96
84
- params ._onlyAll = !(params .onlyChar || params .onlyDec || params .onlyHex || params .onlyOct );
97
+ params .onlyChar = 1 ; //hardcoded;
98
+ params ._onlyAll = !(params .onlyDec || params .onlyHex || params .onlyOct );
85
99
86
100
return params ;
87
101
88
102
}
89
103
104
+ static int ascCmp (const void * a , const void * b ){return * (char * )a > * (char * )b ;}
105
+ static int desCmp (const void * a , const void * b ){return * (char * )a < * (char * )b ;}
106
+ void manipulateData (asciiParams params ){
107
+
108
+ char occur [256 ] = {0 };int i ;
109
+ for (i = 0 ; i < strlen (params .content ); i ++ ){
110
+ if (occur [params .content [i ]]){
111
+ memmove (& params .content [i ], & params .content [i + 1 ], strlen (params .content ) - i );
112
+ i -- ;
113
+ }else occur [params .content [i ]] = 1 ;
114
+
115
+ }
116
+ params .content [i ] = '\0' ;
117
+
118
+
119
+ if (params .order ){
120
+ if (params .order == 1 ) qsort (params .content , strlen (params .content ), sizeof (char ), ascCmp );
121
+ else qsort (params .content , strlen (params .content ), sizeof (char ), desCmp );
122
+ }
123
+ }
124
+
90
125
void printData (asciiParams params ){
91
- size_t s = 0 ;
92
- printf ("Oct Dec Hex Char\n-----------------\n" );
126
+
127
+ size_t s = 0 ;char se_line [20 ] = "" ;
128
+ if (params ._onlyAll ) printf ("Oct Dec Hex " YEL "Chr" RESET "\n------------------\n" );
129
+ else {
130
+ if (params .onlyOct ){ printf ("Oct " ); strcat (se_line ,"-----" );}
131
+ if (params .onlyDec ){ printf ("Dec " ); strcat (se_line ,"-----" );}
132
+ if (params .onlyHex ){ printf ("Hex " ); strcat (se_line ,"-----" );}
133
+ if (params .onlyChar ){ printf (YEL "Chr" RESET ); strcat (se_line ,"---" );}
134
+ printf ("\n%s\n" ,se_line );
135
+ }
136
+
93
137
while (strlen (params .content ) - s ){
94
- printf ("%3o %3d %2X %c\n" , params .content [s ], params .content [s ], params .content [s ], params .content [s ]);
138
+ if (params ._onlyAll ) printf ("%3o %3d %2X " YEL "%c" RESET "\n" , params .content [s ], params .content [s ], params .content [s ], params .content [s ]);
139
+ else {
140
+ if (params .onlyOct ) printf ("%3o " , params .content [s ]);
141
+ if (params .onlyDec ) printf ("%3d " , params .content [s ]);
142
+ if (params .onlyHex ) printf (" %2X " , params .content [s ]);
143
+ if (params .onlyChar ) printf (YEL "%c" RESET , params .content [s ]);
144
+ printf ("\n" );
145
+ }
95
146
s ++ ;
96
147
}
97
- printf ("-----------------\n" );
148
+
149
+ if (params ._onlyAll ) printf ("------------------\n" );
150
+ else printf ("%s" ,se_line );
98
151
}
99
152
153
+
100
154
int main (int argv , char * * args ){
101
155
102
156
if (argv <= 1 ){
@@ -109,14 +163,19 @@ int main(int argv, char** args){
109
163
if (params .showAll ){
110
164
splashScreen ();
111
165
return 0 ;
166
+ }else if (params .showAllAlphas ){
167
+ params .content = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" ;
168
+ }else if (params .showAllDigits ){
169
+ params .content = "0123456789" ;
112
170
}
113
171
114
172
//@todo: print the data first in the center
115
173
// and then print the table output ..
116
174
117
175
// testing params: will be removed on release
118
- printf ("showAll:%i showAllAlphas:%i showAllDigits:%i onlyOct:%i onlyDec:%i onlyHex:%i onlyChar:%i;\ncontent: %s\n\n" ,params .showAll , params .showAllAlphas , params .showAllDigits , params .onlyOct , params .onlyDec , params .onlyHex , params .onlyChar , params .content );
176
+ // printf("showAll:%i showAllAlphas:%i showAllDigits:%i onlyOct:%i onlyDec:%i onlyHex:%i onlyChar:%i;\ncontent: %s\n\n",params.showAll, params.showAllAlphas, params.showAllDigits, params.onlyOct, params.onlyDec, params.onlyHex, params.onlyChar, params.content);
119
177
178
+ manipulateData (params );
120
179
printData (params );
121
180
122
181
return 0 ;
0 commit comments