-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsorter.c
238 lines (206 loc) · 5.08 KB
/
sorter.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
#include "Sorter.h"
char *trim(char *word, int index)
{
char *end;
while(isspace((unsigned char)*word)){
word++;
}
if(*word == 0)
return word;
end = word + index;
while(end > word && isspace((unsigned char)*end)) {
end--;
}
*(end+1) = 0;
return word;
}
char** tokenizer(char* line, size_t num_col){
int i, j, k;
i = 0;//current position in line;
j = 0;//current position in tmp;
k = 0;//current position in result;
char** result = (char**)malloc(sizeof(char*) * (num_col + 1)); //return value;
char* temp = (char*)malloc(500);//store each word;
size_t start_quote = FALSE;//1 quote start, 0 quote end;
//go through each character;
while(i < strlen(line)){
/*reach the start '"' */
if(line[i] == '"' && start_quote == FALSE){
start_quote = TRUE;
}
else if(line[i] == '"' && start_quote == TRUE){
//store value in result
result[k] = (char*) malloc((j + 1) * sizeof(char));
temp = trim(temp, j - 1);
strcpy(result[k], temp);
memset(&temp[0], 0, strlen(temp));
start_quote = FALSE;
j = 0;
k++;
i++;
}
//split by ',' or reach the end of line;
else if((line[i] == ',' || i == strlen(line) - 1) && start_quote != TRUE){
//if there is no character; (eg: ,,)
if(!temp){
temp[0] = '\0';
}
if(i == strlen(line) - 1 && line[i] != '\n' && line[i] != ','){
temp[j] = line[i];
j++;
}
//store value to result;
result[k] = (char*)malloc((j+1) * sizeof(char));
temp = trim(temp, j - 1);
strcpy(result[k], temp);
memset(&temp[0], 0, strlen(temp));
j = 0;
k++;
//if the last character is ',';
if(line[i] == ',' && i == strlen(line) - 1){
temp[0] = '\0';
result[k] = (char*)malloc((j+1) * sizeof(char));
strcpy(result[k], temp);
memset(&temp[0], 0, strlen(temp));
}
}else{
//copy character from line to temp;
if(j == 0){
if(line[i] == ' '){
i++;
continue;
}
}
temp[j] = line[i];
j++;
}
i++;
}
i = 0;
free(temp);
return result;
}
int tok_file(FILE *fp, row* data, int num_col){
row rest_row;
rest_row.row_text = (char*) malloc (sizeof(char) * BUF_SIZE);
size_t curr_col = 0;
size_t curr_row = 0; //current row number in row* data
int i;//loop virable
/*end of declaring variable*/
/*deal with data*/
i = 0;
while(fgets(rest_row.row_text, BUF_SIZE-1, fp) != NULL){
rest_row.row_len = strlen(rest_row.row_text);
rest_row.row_token = (char**) malloc(sizeof(char *) * (num_col+1));
rest_row.row_token = tokenizer(rest_row.row_text, num_col);
data[curr_row++] = rest_row;
}
return curr_row;
}
int main (int argc, char* argv[]){
/*declare variable*/
FILE *fp;
fp = stdin;
//first row:
row first_row;
row *data;
char *token;
char* target;
size_t num_col = 1;
int length;
int i;
int j;
int k;
int num_row;
int target_col;
/*split the 1st token by ','*/
first_row.row_text = (char*) malloc (sizeof(char) * BUF_SIZE);
fgets(first_row.row_text, BUF_SIZE-1, fp);
first_row.row_len = strlen(first_row.row_text);
first_row.row_token = (char**) malloc(sizeof(char *) * first_row.row_len);
token = strtok(first_row.row_text, ",");
first_row.row_token[0] = token;
//split the rest of token in the first row
while(token = strtok(NULL, ",")){
first_row.row_token[num_col++] = token;
}
first_row.num_col = num_col;
//delete the '\n' in the last word;
length = strlen(first_row.row_token[num_col - 1]);
if(first_row.row_token[num_col - 1][length - 1] == '\n'){
first_row.row_token[num_col - 1][length - 2] = '\0';
}
//trim blank space;
i = 0;
while(i < num_col){
first_row.row_token[i] = trim(first_row.row_token[i], strlen(first_row.row_token[i]) - 1);
i++;
}
//deal with rest rows;
data = (row*) malloc (sizeof(row) * 10000);
num_row = tok_file(fp, data, num_col);
if(argv[2]){
target = argv[2];
}else{
printf("Wrong Input");
exit(1);
}
//find the target column number;
target_col = 0;
while(target_col < first_row.num_col){
if(strcmp(first_row.row_token[target_col], target) == 0){
break;
}
target_col++;
}
//no such title in the first row
if(target_col == first_row.num_col){
printf("Wrong input, no such title.\n");
exit(1);
}
mergeSort(data, target_col, num_row);
i = 0;
//print the first row:
while(i < num_col){
printf("%s",first_row.row_token[i]);
if(i != num_col - 1){
printf(",");
}else{
printf("\n");
}
i++;
}
//print the rest row;
i = 0;
j = 0;
next:
while(i < num_row){
while(j < num_col){
//if(data[i].comma){
for(k = 0; k < strlen(data[i].row_token[j]); k++){
if(data[i].row_token[j][k] == ',' && j != num_col - 1){
printf("\"%s\",", data[i].row_token[j]);
j++;
break;
}
if(data[i].row_token[j][k] == ',' && j == num_col - 1){
printf("\"%s\"", data[i].row_token[j]);
i++;
j = 0;
goto next;
}
}
//}
printf("%s",data[i].row_token[j]);
if(j != num_col - 1){
printf(",");
}else{
printf("\n");
}
j++;
}
i++;
j = 0;
}
return 0;
}