-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
716 lines (528 loc) · 19.8 KB
/
Copy pathmain.cpp
File metadata and controls
716 lines (528 loc) · 19.8 KB
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
// ToDo: реализовать ввод вывод в файлы по аргументу cmd
#include <stdio.h>
#include <stdlib.h>
#include <locale.h>
#include <sys\stat.h>
#include <cctype>
#include <ctype.h>
#include <errno.h>
#include <string.h>
#include <assert.h>
#define DBG if(0)
const char INPUT[] = "input.txt";
const char SOUTPUT[] = "straight_output.txt";
const char ROUTPUT[] = "reverse_output.txt";
const char FULLTEXT[] = "full_text.txt";
//TODO починить я
struct String
{
char* chLine;
int length;
};
//!---------------------------------------------------
//! gets size of file in bytes
//!
//! @param [in] fileName-name of file or filepath
//!
//! @return size of file in bytes
//!
//! @note size of file in bytes often is bigger than
//! number of char symbols in file
//!----------------------------------------------------
int getFileSize (const char* fileName);
//!-------------------------------------------------------
//! reads data from file to storage
//!
//! @param [in] fileName-name of file or filepath
//!
//! @return storage consisting data from file
//!
//! @note returns string that is not normal for c
//! format of string is
//!-------------------------------------------------------
char* readToStorage (const char* fileName);
//!---------------------------------------------------------------------------
//! counts number of input symbols in input string
//!
//! @param [in] storage-buffer consisting string
//! @param [in] symb-symbol which we find
//!
//! @return number of string
//!
//!---------------------------------------------------------------------------
int countSymb (const char* storage,const char symb);
//!---------------------------------------------------------------------------
//! deletes every double symbol from input and returns size of new storage without double symb;
//!
//! @param [in] storage-buffer consisting string
//! @param [in] symb-symbol which we want to delete
//!
//! @return size of new storage without double symb;
//!
//!---------------------------------------------------------------------------
int deleteDoubleSymb (const char* storage,const char symb);
//!---------------------------------------------------------------------------
//! deletes every double symbol from input and returns size of new storage without double symb;
//!
//! @param [in] storage-buffer consisting string
//! @param [in] symb-symbol which we want to delete
//!
//! @return size of new storage without double symb;
//!
//!---------------------------------------------------------------------------
int deleteSymb (const char* storage,const char symb);
//!--------------------------------------------------------------------------
//! converts sting to array of Strings
//!
//! @param [in] storage buffer consisting string
//!
//! @param [in] nStr number of string
//!
//! @return array of Strings
//!
//!---------------------------------------------------------------------------
struct String* makeText (const char* storage, const int nStr);
//!---------------------------------------------------------------------------
//! compares strings from beginning
//!
//! @param [in] str1 first string
//! @param [in] str2 second string
//!
//! @return negative number if str1 < str2
//! positive number if str1 > str2
//! zero if str1 == str2
//!---------------------------------------------------------------------------
int strCompareStraight (const char* str1, const char* str2);
//!---------------------------------------------------------------------------
//! compares strings from ending
//!
//! @param [in] str1 first string
//! @param [in] str2 second string
//!
//! @return negative number if str1 < str2
//! positive number if str1 > str2
//! zero if str1 == str2
//!---------------------------------------------------------------------------
int strCompareReverse (const struct String* str1, const struct String* str2);
//!---------------------------------------------------------------------------
//! comparator for sorting using strCompareStraight
//!
//! @param [in] first member for comparing
//! @param [in] second member for comparing
//!
//! @return negative number if str1 < str2
//! positive number if str1 > str2
//! zero if str1 == str2
//!----------------------------------------------------------------------------
int straightComparator (const void* member1, const void* member2);
//!---------------------------------------------------------------------------
//! comparator for sorting using strCompareReverse
//!
//! @param [in] first member for comparing
//! @param [in] second member for comparing
//!
//! @return negative number if str1 < str2
//! positive number if str1 > str2
//! zero if str1 == str2
//!---------------------------------------------------------------------------
int reverseComporator (const void* member1, const void* member2);
//!--------------------------------------------------------------------------
//! writes text from storage to file
//!
//! @param [in] storage buffer with text
//! @param [in] nStr number of strings in buffer
//! @param [in] fileName name of file where text will be written
//!
//!--------------------------------------------------------------------------
void storageOutput (const char* storage, const int nCh, const char* fileName);
//!--------------------------------------------------------------------------
//! writes text from text to file from beginning of every string
//!
//! @param [in] text array of strings
//! @param [in] nCh number of strings in buffer
//! @param [in] fileName name of file where text will be written
//!
//!--------------------------------------------------------------------------
void textOutputStraight (struct String* text,const int nStr, const char* fileName);
//!--------------------------------------------------------------------------
//! writes text from text to file from ending of every string
//!
//! @param [in] text array of strings
//! @param [in] nStr number of strings in buffer
//! @param [in] fileName name of file where text will be written
//!
//!--------------------------------------------------------------------------
void textOutputReverse (struct String* text,const int nStr, const char* fileName);
//!-----------------------------------------------------------------------------------
//! reads string from file and deletes every '\r' and double '\n' from it
//!
//! @param [in] fileName-name of file from which we read
//!
//! @return pointer ti char which is string we read
//!------------------------------------------------------------------------------------
char* prepareStorage (const char* fileName, int* nCh, int* nStr);
//!-----------------------------------------------------------------------------------------
//! sorts array of Strings
//! @param [in] text-pointer to string array
//! @param [in] beg-beginning index
//! @param [in] en-ending index
//!-----------------------------------------------------------------------------------------------
void quickSort (struct String* text, int beg, int en, int (*compare) (const void *, const void *));
//!-------------------------------------------------------------------------------------
//! swaps strings
//! @param [in] text-array of Strings
//! @param [in] l-left index
//! @param [in] f-right index
//!--------------------------------------------------------------------------------------
void sw (struct String* text, int l, int r);
//!--------------------------------------------------------------------------------------
//! gets strings which are filenames
//!
//! @param args - number of arguments
//! @param argv - array of strings
//! @param finput - pointer to input file
//! @param fsoutput - pointer to output file with straight sorting
//! @param froutput - pointer to output file with reverse sorting
//! @param fstorage - pointer to output file with text
//!
//!---------------------------------------------------------------------------------------
void getFileNames (int args, char* argv[], char** finput, char** fsoutput, char** froutput, char** fstorage);
int myisalpha (char symb);
//!-----------------------------------------------
//! unit tests
//!-----------------------------------------------
void testGetFileSize (const char* fileName, const int exampleSize, int testN);
void testReadToStorage (const char* fileName, const char* exampleStr, int nTest);
void testStrCompareStraight (const char* str1, const char* str2, int res, int nTest);
void testStrCompareReverse (const struct String* str1, const struct String* str2, int res, int nTest);
void testQuickSort (struct String* text, int beg, int en, int (*compare) (const void *, const void *), struct String* example, int nTest);
void tests();
int main(int argc, char *argv[])
{
setlocale (LC_ALL, "Russian");
char* finput = "";
char* fsoutput = "";
char* froutput = "";
char* fstorage = "";
tests();
getFileNames ( argc, argv, &finput, &fsoutput, &froutput, &fstorage);
int nCh = 0, nStr = 0;
char* storage = prepareStorage ((const char*)finput, &nCh, &nStr);
struct String* text = makeText (storage, nStr);
qsort (text, nStr, sizeof(struct String), straightComparator );
textOutputStraight (text, nStr, fsoutput);
printf("%s", fsoutput);
//qsort (text, nStr, sizeof(struct String), reverseComporator );
quickSort (text, 0, nStr - 1, reverseComporator);
textOutputStraight (text, nStr, froutput);
storageOutput (storage, nCh, fstorage);
free (storage);
free (text);
return 0;
}
//---------------------------------------------------------------------
int getFileSize (const char* fileName)
{
assert (fileName != NULL);
struct stat buff = {};
stat( fileName, &buff);
return buff.st_size;
}
//----------------------------------------------------------------------
char* readToStorage (const char* fileName)
{
assert (fileName != NULL);
FILE* input = fopen (fileName, "rt");
int fileSz = getFileSize(fileName);
char* buffer = (char*) calloc (fileSz + 2 , sizeof (*buffer));
int nCh = fread (buffer, sizeof(char), fileSz, input);
*(buffer + nCh) = '\n';
*(buffer + nCh + 1) = '\0';
fclose(input);
return buffer;
}
//------------------------------------------------------------------------
int countSymb (const char* storage, const char symb)
{
assert (storage != NULL);
int nStr = 0;
int ind = -1;
char* pointerToSymb = NULL;
while ( (pointerToSymb = strchr(storage + ind + 1, symb))!= NULL)
{
nStr++;
ind = pointerToSymb - storage;
}
return nStr;
}
//-------------------------------------------------------------------------
int deleteDoubleSymb (const char* storage, const char symb)
{
assert (storage != NULL);
char* read = (char*) storage;
char* write = (char*) storage;
int nSymb = 0;
if (symb == '\0')
while (*read != '\0')
nSymb++;
else
while (*read != '\0')
{
while (*read == symb && *(read+1) == symb )
read++;
*write++ = *read++;
nSymb++;
}
*write = '\0';
return nSymb;
}
//------------------------------------------------------------------------
int deleteSymb (const char* storage,const char symb)
{
assert (storage != NULL);
char* read = (char*) storage;
char* write = (char*) storage;
int nSymb = 0;
if (symb == '\0')
while (*read != '\0')
nSymb++;
else
while (*read != '\0')
{
while (*read == symb)
read++;
*write++ = *read++;
nSymb++;
}
*write = '\0';
return nSymb + 1;
}
//-------------------------------------------------------------------------
int strCompareStraight (const char* str1, const char* str2)
{
assert (str1 != NULL);
assert (str2 != NULL);
const char* read1 = str1;
const char* read2 = str2;
while (1)
{
while (!(isalpha(*(read1))) && (*(read1) != '\n') && !(isspace(*read1)))
read1++;
while (!(isalpha(*(read2))) && (*(read2) != '\n') && !(isspace(*read2)))
read2++;
unsigned char tempSymb1 = (unsigned char) tolower (*read1);
unsigned char tempSymb2 = (unsigned char) tolower (*read2);
if (tempSymb1 == '\n' || tempSymb2 == '\n')
return tempSymb1 - tempSymb2;
if ((tempSymb1) == (tempSymb2))
{
read1++;
read2++;
continue;
}
return tempSymb1 - tempSymb2;
}
}
//-------------------------------------------------------------------------------------
int strCompareReverse (const String* str1, const String* str2)
{
assert (str1 != NULL);
assert (str2 != NULL);
int nCh1 = str1->length;
int nCh2 = str2->length;
const char* read1 = str1->chLine + nCh1 - 1;
const char* read2 = str2->chLine + nCh2 - 1;
while (1)
{
while (!(isalpha(*(read1))) && (nCh1 > 1) && !(isspace(*read1)))
{
read1--;
nCh1--;
}
while (!(isalpha(*(read2))) && (nCh2 > 1) && !(isspace(*read2)))
{
read2--;
nCh2--;
}
unsigned char tempSymb1 = (unsigned char) tolower (*read1);
unsigned char tempSymb2 = (unsigned char) tolower (*read2);
if ((nCh1 == 1) || (nCh2 == 1))
return tempSymb1 - tempSymb2;
if ((tempSymb1) == (tempSymb2))
{
read1--;
read2--;
continue;
}
return tempSymb1 - tempSymb2;
}
}
//-----------------------------------------------------
struct String* makeText (const char* storage, const int nStr)
{
assert (storage != NULL);
char* read = (char*) storage;
//printf("%s",read);
struct String* tempText = (struct String*) calloc (nStr, sizeof(*tempText));
struct String* returnText = tempText;
int nChInStr = 0;
while (*read != '\0')
{
if (*read == '\n')
{
tempText->chLine = read - nChInStr ;
tempText->length = nChInStr + 1;
tempText++;
nChInStr = -1;
}
nChInStr++;
read++;
}
return returnText;
}
//---------------------------------------------------------------------------------------------
int straightComparator (const void* member1, const void* member2)
{
assert (member1 != NULL);
assert (member2 != NULL);
return strCompareStraight (((struct String*) member1)->chLine, ((struct String*) member2)->chLine);
}
//----------------------------------------------------------------------------------------------
int reverseComporator (const void* member1, const void* member2)
{
assert (member1 != NULL);
assert (member2 != NULL);
return strCompareReverse ((struct String*) member1, (struct String*) member2);
}
//------------------------------------------------------------------------------------------------
void storageOutput (const char* storage, const int nCh, const char* fileName)
{
assert (storage != NULL);
assert (fileName != NULL);
FILE* output = fopen (fileName, "wb");
fwrite (storage, sizeof(char), nCh, output);
fclose (output);
}
//--------------------------------------------------------------------------------------------------
void textOutputStraight (struct String* text, const int nStr, const char* fileName)
{
assert (text != NULL);
assert (fileName != NULL);
FILE* output = fopen (fileName, "wb");
struct String* tempText = text;
for( int i = 0; i < nStr; i++)
{
char* temp = tempText->chLine;
fwrite (tempText->chLine, sizeof(char), tempText->length, output);
tempText++;
}
fclose (output);
}
//---------------------------------------------------------------------------------------------------
char* prepareStorage (const char* fileName, int* nCh, int* nStr)
{
assert (fileName != NULL);
char* storage = readToStorage (fileName);
//deleteSymb (storage, '\r');
*nCh = deleteDoubleSymb (storage, '\n');
*nStr = countSymb (storage, '\n');
return storage;
}
//------------------------------------------------------------------------------------------------------
void testGetFileSize (const char* fileName, const int exampleSize, int testN)
{
int Sz = getFileSize (fileName);
(exampleSize == Sz) ? :
printf ("test number %d:read of file failed\n", testN);
}
//----------------------------------------------------------------------
void sw (struct String* text, int l, int r) {
assert (text);
struct String t = text[l];
text[l] = text[r];
text[r] = t;
}
//-----------------------------------------------------------------------
void quickSort(String v[], int left, int right, int (*compare) (const void *, const void *))
{
assert (v);
if (left >= right)
return;
sw (v, left, (left+right)/2);
int last = left;
for(int i = left + 1; i <= right; i++)
if (compare(v + i, v + left) < 0)
sw (v, ++last, i);
sw (v, left, last);
quickSort (v, left , last - 1, compare);
quickSort (v, last + 1, right , compare);
}
//----------------------------------------------------------------------------------------
void testReadToStorage (const char* fileName, const char* exampleStr, int nTest)
{
(!strcmp(readToStorage (fileName), exampleStr)) ? :
printf ("reading failed test number: %d\n", nTest);
}
//-----------------------------------------------------------------------------------------
void testStrCompareStraight (const char* str1, const char* str2, int res, int nTest)
{
int res1 = strCompareStraight (str1, str2);
if (res1 != 0)
res1 = res1 / abs (res1);
(!(res1 - res) ) ? :
printf ("compairing failed test number: %d\n", nTest);
}
//-------------------------------------------------------------------------------------------
void testStrCompareReverse (const struct String* str1, const struct String* str2, int res, int nTest)
{
int res1 = strCompareReverse(str1, str2);
if (res1 != 0)
res1 = res1 / abs (res1);
(!(res1 - res) ) ? :
printf ("compairing failed test number: %d\n", nTest);
}
//-------------------------------------------------------------------------------------------------
void testQuickSort (struct String* text, int beg, int en, int (*compare) (const void *, const void *), struct String* exampleText, int nTest)
{
quickSort (text, beg, en, compare);
int i = beg;
while (compare(text + i,exampleText + i))
i++;
(!(i-en - 1) ) ? :
printf ("sorting failed test number: %d\n", nTest);
}
//-----------------------------------------------------------------------------------------------
void getFileNames (int args, char* argv[], char** finput, char** fsoutput, char** froutput, char** fstorage)
{
if (args >= 2)
*finput = argv[1];
else
*finput = (char*) INPUT;
if (args >= 3)
*fsoutput = argv[2];
else
*fsoutput = (char*) SOUTPUT;
if (args >= 4)
*froutput = argv[3];
else
*froutput = (char*) ROUTPUT;
if (args >= 5)
*fstorage = argv[4];
else
*fstorage = (char*) FULLTEXT;
}
//-------------------------------------------------------------------------------
int myisalpha (char symb)
{
return (tolower(symb) != 'я') ? isalpha (symb) : 1;
}
void tests()
{
testGetFileSize( "ex1.txt",3 ,1);
testGetFileSize( "ex2.txt",7 ,2);
testGetFileSize( "ex3.txt",10,3);
testReadToStorage( "ex1.txt", "aaa\n",4);
testReadToStorage( "ex2.txt", "aaa\naa\n",5);
testStrCompareStraight ("aaaa" ,"abaa", -1, 6);
testStrCompareStraight ("aaa a c","aaaa", -1, 7);
}