Skip to content

Commit 49df86b

Browse files
committed
bug fixes
fix --alphas --digits fix duplicate remove functionality
1 parent 731ed8c commit 49df86b

File tree

2 files changed

+30
-30
lines changed

2 files changed

+30
-30
lines changed

makefile

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,7 @@
11
CC = gcc
2-
BINS = main test clean
2+
BINS = main
33

44
all: $(BINS)
55

66
main:
77
$(CC) -o build/ascii.exe src/main.c
8-
9-
test:
10-
$(CC) src/test.c -c
11-
12-
# `clean` is not needed
13-
clean:
14-
rm -f build/*.o

src/main.c

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66

77
#define MAX_COL 4
88
#define MAX_ROW 32
9-
#define YEL "\x1B[33m"
10-
#define RESET "\x1B[0m"
9+
#define YEL "\x1b[33m"
10+
#define RESET "\x1b[0m"
1111

1212
typedef struct parameter{
1313

@@ -29,7 +29,7 @@ typedef struct parameter{
2929

3030

3131
void splashScreen(){
32-
printf(" ascii - ASCII character set encoded in octal, decimal, and hexadecimal\n\n"
32+
printf("ascii - ASCII character set encoded in octal, decimal, and hexadecimal\n\n"
3333
"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"
3434
"--------------------------------------------+-----------------------+---------------------+--------------------\n"
3535
"000 0 00 "YEL"NUL (null)"RESET" | 040 32 20 "YEL"SPACE"RESET" | 100 64 40 "YEL"@"RESET" | 140 96 60 "YEL"`"RESET"\n"
@@ -103,22 +103,28 @@ asciiParams parseParameter(int argv, char** args){
103103

104104
static int ascCmp(const void* a, const void* b){return *(char *)a > *(char *)b;}
105105
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';
106+
void manipulateData(asciiParams *params){
107+
108+
char occur[256] = {0};int i;int idx = 0;
117109

110+
for(i = 0; i < strlen(params->content); i++){
111+
if(!occur[params->content[i]]){
112+
if(idx != i){
113+
params->content[idx] = params->content[i];
114+
}
115+
116+
idx++;
117+
occur[params->content[i]] = 1;
118+
}
119+
}
120+
121+
char* tmp = (char *)malloc((idx) * sizeof(char));
122+
strncpy(tmp, params->content, idx);tmp[idx] = '\0';
123+
params->content = tmp;
118124

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);
125+
if(params->order){
126+
if(params->order == 1) qsort(params->content, strlen(params->content), sizeof(char), ascCmp);
127+
else qsort(params->content, strlen(params->content), sizeof(char), desCmp);
122128
}
123129
}
124130

@@ -163,20 +169,21 @@ int main(int argv, char** args){
163169
if(params.showAll){
164170
splashScreen();
165171
return 0;
172+
}else if(params.showAllAlphas && params.showAllDigits){
173+
params.content = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
166174
}else if(params.showAllAlphas){
167-
params.content = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
175+
params.content = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
168176
}else if(params.showAllDigits){
169177
params.content = "0123456789";
170178
}
171179

172180
//@todo: print the data first in the center
173181
// and then print the table output ..
174182

175-
// testing params: will be removed on release
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);
177-
178-
manipulateData(params);
183+
manipulateData(&params);
179184
printData(params);
180185

186+
// free(params.content);
187+
181188
return 0;
182189
}

0 commit comments

Comments
 (0)