-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstrings_int.c
241 lines (195 loc) · 5.53 KB
/
strings_int.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
#include "strings_int.h"
//==========================__ISTEXT__==========================
// Checks if given code of a symbol is a letter of English or Russian alphabet
int istext(char c)
{
return 1;
if(c > 'z')
return 2;
else
return (((c >= 'A') && (c <= 'Z')) || ((c >= 'a') && (c <= 'z'))) ? 1 : 0;
}
//==========================__PRINTTEXT__=======================
// Prints given number of strings (as integer arrays) from array
void printtext(char** txt_arr, int num_of_lines)
{
assert(txt_arr != NULL);
if(!txt_arr)
return;
for(int i = 0; i < num_of_lines; ++i)
printf("%s\n", txt_arr[i]);
return;
}
//==========================__PRINT_ORIGINAL_TEXT__=============
// Prints given number of strings (as integer arrays) from the place that used to be a single string (as integer array)
int print_original_text(char* text, int num_of_lines)
{
assert(text != NULL);
if(!text)
return 1;
while(num_of_lines > 0)
{
printf("%c", (*text) ? *text : '\n');
++text;
if(!*text)
--num_of_lines;
}
return 0;
}
//==========================__FGETFILE__========================
// Creates a string (as integer array) which contains entire given file
char* fgetfile(char* filename, int* num_of_lines)
{
assert(filename != NULL);
assert(num_of_lines != NULL);
if(!filename)
return NULL;
if(!num_of_lines)
return NULL;
struct stat st = {};
stat(filename, &st);
int len = (int) st.st_size;
if(!len)
{
printf("No file or it is empty\n");
return NULL;
}
FILE *infile = fopen(filename, "r");
char* text = (char*) calloc(len, sizeof(*text));
char* txt_p = text - 1;
char c;
while((c = getc(infile)) != EOF)
if((*++txt_p = c) == '\n')
++*num_of_lines;
*txt_p = '\0';
fclose(infile);
return text;
}
//==========================__SLICEDTEXT__======================
// Slices given string (as integer array) into continuous set of strings (as integer arrays) by changing '\n' to '\0'
// Creates array of strings (as integer arrays) containing strings (as integer arrays) from the sliced one
char** slicedtext(char* text, int num_of_lines)
{
assert(text != NULL);
if(!text)
return NULL;
char** txt_arr= (char**) calloc(num_of_lines, sizeof(*txt_arr));
char** txt_arr_p = txt_arr;
*txt_arr_p++ = text;
while(*text)
if(*text++ == '\n')
{
*(text - 1) = '\0';
/*while(isspace(*text))
text++;
if(*text == '\0')
text -= 2;*/
*txt_arr_p++ = text;
}
return txt_arr;
}
//==========================__LINE_COMP__=======================
// Compares strings (as integer arrays) from their beginning
// Ignores non-text symbols
int linecomp(const char* a, const char* b)
{
assert(a != b);
assert(a != NULL);
assert(b != NULL);
while(!istext(*a))
a++;
while(!istext(*b))
b++;
while(*a == *b)
{
if(*a == '\0')
return 0;
do
{
++a;
} while(!istext(*a) && *a);
do
{
++b;
} while(!istext(*b) && *b);
}
return *a - *b;
}
//==========================__REV_LINE_COMP__===================
// Compares strings (as integer arrays) from their ending
// Ignores non-text symbols
int rev_linecomp(const char* a, const char* b)
{
assert(a != b);
assert(a != NULL);
assert(b != NULL);
const char* a0 = a;
const char* b0 = b;
while(*a)
a++;
while(*b)
b++;
if (a == a0 || b == b0)
return *a - *b;
while(*a == *b)
{
do
{
--a;
} while(!istext(*a) && a != a0);
do
{
--b;
} while(!istext(*b) && b != b0);
if (a == a0 || b == b0)
break;
}
return *a - *b;
}
//==========================__QSORT_TEXT_ARRAY__================
// Sorts some number of strings (as integer arrays) from given array by given comparing function
// Arg = '+' for detailed info
int qsort_text_array(char** txt_arr, int num_of_lines, int (*line_comp_func)(const char*, const char*), char arg)
{
assert(txt_arr != NULL);
assert(line_comp_func != NULL);
if(!txt_arr || !line_comp_func)
return 0;
//**
if(num_of_lines <= 1)
return 1;
int base = num_of_lines - 1;
int check = 0;
int num_of_self_recourses = 1;
if(arg == '+')
{
printf("___\n"
"-> new reference, <base> = (");
printf("%s", txt_arr[base]);
printf("):\n");
printtext(txt_arr, num_of_lines);
}
while(check < base)
if((*line_comp_func)(txt_arr[check], txt_arr[base]) >= 0)
{
char* t = txt_arr[check];
txt_arr[check] = txt_arr[base - 1];
txt_arr[base - 1] = txt_arr[base];
txt_arr[base] = t;
--base;
if(arg == '+')
{
printf("%s", txt_arr[check]);
printf(" >= ");
printf("%s", txt_arr[base]);
printf("\n");
printtext(txt_arr, num_of_lines);
}
}
else
++check;
//**
num_of_self_recourses += qsort_text_array(txt_arr, base, line_comp_func, arg);
num_of_self_recourses += qsort_text_array(txt_arr + base + 1, num_of_lines - base - 1, line_comp_func, arg);
return num_of_self_recourses;
}