-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathutils.c
283 lines (242 loc) · 6.31 KB
/
utils.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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
#include "utils.h"
void *w_malloc(size_t size) {
void* res = malloc(size);
if(res == NULL) {
printf("malloc error.\n");
exit(-1);
}
return res;
}
void *w_calloc(size_t size) {
void* res = calloc(size, 1);
if(res == NULL) {
printf("calloc error.\n");
exit(-1);
}
return res;
}
char *aapts(char* str) {
char* alloc_str = w_malloc(strlen(str)+1);
strncpy(alloc_str, str, strlen(str));
*(alloc_str+strlen(str)) = 0;
return alloc_str;
}
bool strs_identical(char* str1, char* str2) {
if(str1 == NULL || str2 == NULL) print_err_and_exit("string cmp error, one or both are NULL", -4);
if(strlen(str1) == strlen(str2) && strcmp(str1, str2) == 0)
return true;
return false;
}
char* c_itoa(int num, char *str) {
if(str == NULL) {
return NULL;
}
sprintf(str, "%d", num);
return str;
}
void print_title(char* msg) {
printf(ANSI_COLOR_MAGENTA "\n\t %s" ANSI_COLOR_RESET "\n", msg);
}
void print_info(char* msg) {
printf(ANSI_COLOR_CYAN "%s" ANSI_COLOR_RESET "\n", msg);
}
void print_success(char* msg) {
printf(ANSI_COLOR_GREEN "%s" ANSI_COLOR_RESET "\n", msg);
}
void print_warn(char* warning, int warning_code) {
printf(ANSI_COLOR_YELLOW "%s" ANSI_COLOR_RESET "\n", warning);
}
void print_err(char* err, int err_code) {
printf(ANSI_COLOR_RED "%s" ANSI_COLOR_RESET "\n", err);
}
void print_err_and_exit(char* err, int err_code) {
printf(ANSI_COLOR_RED "%s" ANSI_COLOR_RESET "\n", err);
exit(err_code);
}
node* init_node(void* item) {
node* node = w_malloc(sizeof(node));
node->item = item;
node->next = NULL;
return node;
}
node* create_node(void* item) {
node* n = w_malloc(sizeof(node));
n->item = item;
n->next = NULL;
return n;
}
int count_node_list(node** head) {
if(head == NULL) return 0;
node* cur_node = *head;
int i = 0;
while(cur_node != NULL) { i++; }
return i;
}
node* get_node(node** head, int pos) {
if(pos == 0) return *head;
node* cur_node = *head;
int i = 1;
while(cur_node->next != NULL && i != pos) {
cur_node = cur_node->next;
i++;
}
return cur_node;
}
void insert_node(node** head, node* new) {
if(head == NULL) return;
node* cur_node = *head;
if(cur_node == NULL) {
cur_node = new;
return;
}
while(cur_node->next != NULL) {
cur_node = cur_node->next;
}
cur_node->next = new;
}
node** insert_node_create_head_if_needed(node** head, node* new) {
node** new_head = head;
if(new_head == NULL) {
new_head = w_malloc(sizeof(node*));
*new_head = new;
return new_head;
}
node* cur_node = *new_head;
if(cur_node == NULL) {
*new_head = new;
return new_head;
}
while(cur_node->next != NULL) {
cur_node = cur_node->next;
}
cur_node->next = new;
return new_head;
}
void remove_node(node** head, int pos) {
if(head == NULL) return;
if(pos == 0) *head = (*head)->next;
int i = 1;
node* prev = *head;
node* cur_node = (*head)->next;
while(cur_node != NULL && i != pos) {
prev = cur_node;
cur_node = cur_node->next;
}
if(i == pos) {
if(cur_node->next == NULL) {
prev->next = NULL;
free(cur_node);
return;
}
prev->next = cur_node->next;
free(cur_node);
}
}
void link_node(node** head, node* link_node, int pos) {
if(head == NULL) return;
if(pos == 0) {
link_node->next = *head;
*head = link_node;
return;
}
int i = 1;
node* cur_node = *head;
while(cur_node != NULL && i != pos) {
cur_node = cur_node->next;
i++;
}
if(i == pos)
cur_node = link_node;
}
void free_node_to_end(node* n) {
if(n->next == NULL) {
free(n);
return;
}
free_node_to_end(n->next);
free(n);
}
void free_list(node** head) {
free_node_to_end(*head);
}
node** init_node_list() {
node** head = w_malloc(sizeof(node*));
*head = w_malloc(sizeof(node));
return head;
}
bool is_exception_char(char ch) {
if(ch == 0x00 || ch == 0x40)
return true;
return false;
}
bool is_printable_ascii(char ch) {
return ((ch & ~0x7f) == 0 ) && (isprint(ch) || isspace(ch));
// return (ch >= 32 && ch <= 126);
}
bool is_letter_or_number(char ch) {
return (ch >= 65 && ch <= 90) || (ch >= 97 && ch <=122) || (ch >= 48 && ch <= 57);
}
bool is_letter(char ch) {
return (ch >= 65 && ch <= 90) || (ch >= 97 && ch <=122);
}
char* string_remove_special_chars(char* str) {
int len = strlen(str);
int new_len = 0;
char* ch = str;
char new[len];
for(int i=0; i < len; i++) {
if(is_letter_or_number(*(ch+i))) {
new[i] = *(ch+i);
new_len++;
}
}
new[new_len] = 0;
return aapts(new);
}
bool is_string(byte* str, int len) {
int null_counter = 0;
int row_nulls = 0;
int non_printable = 0;
int letters = 0;
int printable = 0;
int spaces = 0;
char last = *(str+(len));
for(int i=0; i < len; i++) {
char ch = *(str+i);
if(ch == 0) { null_counter++; row_nulls++; } else { row_nulls = 0; }
//if((row_nulls > 8 && len > 4)) return false;
if((row_nulls > 8)) return false;
if(is_letter_or_number(ch)) printable++;
if(is_letter(ch)) letters++;
if(ch == 0x20) spaces++;
if(!is_printable_ascii(ch) && !is_exception_char(ch))
non_printable++;
}
if(non_printable >= (((float)len)/10)*6) {return false;}
//if(null_counter >= 5) return false;
if(letters <= 1 || printable == 0) {return false;}
//if(letters > 10 && spaces == 0) return false;
//if(letters > 1 && last == 0) return true;
// if(last != 0 && last != 0x23) {return false;}
//if(letters <= (len-null_counter)/2) return false;
if(letters < (null_counter/2)) {return false;}
return true;
}
int count_lines(FILE* file)
{
char buf[BUF_SIZE];
int counter = 0;
for(;;)
{
size_t res = fread(buf, 1, BUF_SIZE, file);
if (ferror(file))
return -1;
int i;
for(i = 0; i < res; i++)
if (buf[i] == '\n')
counter++;
if (feof(file))
break;
}
return counter;
}