1 /* ---------- */
2 /* najisort.c */
3 /* ---------- */
4
5 /* naji sort functions */
6
7 /* this .c file is a part */
8 /* of libnaji version 0.6.4 */
9
10 /* libnaji is based on */
11 /* the original najitool */
12
13 /* both najitool and libnaji */
14 /* are public domain and are */
15 /* made by the same author */
16 /* please read license.txt */
17
18 /* made by NECDET COKYAZICI */
19
20 /* bblensorts */
21 /* bblensortl */
22 /* readforsort */
23 /* writesorted */
24 /* lensort_basis */
25
26 /* Made by MANUEL LE BOETTE */
27
28
29 #include "libnaji.h"
30
31 char swap_char_var;
32 #define swap_char(a, b) swap_char_var=a; a=b; b=swap_char_var;
33
34 int swap_int_var;
35 #define swap_int(a, b) swap_int_var=a; a=b; b=swap_int_var;
36
37
38 int cvlen;
39 char *cva;
40 char *cvb;
41
42
43
44
45
rcharvar(char * str)46 void rcharvar(char *str)
47 {
48 int c;
49 int x;
50 int y;
51 int z;
52
53
54 cvlen = strlen(str);
55
56 c = cvlen-1;
57
58 cva = newchar(cvlen);
59 cvb = newchar(cvlen);
60 exitnull(cva);
61 exitnull(cvb);
62
63 strcpy(cva, str);
64
65
66 for(x=0; x<cvlen; x++)
67 cvb[x]=cvlen-x;
68
69 for (z=0; z<cvlen; z++)
70 fputc(cva[z], stdout);
71
72 fputc('\n', stdout);
73
74
75 x=c;
76
77 while (x > 0)
78 {
79 cvb[x]--;
80 x--;
81 y=c;
82
83 while (y > x)
84 {
85 swap_char(cva[x], cva[y]);
86 y--;
87 x++;
88 }
89
90 for (z=0; z<cvlen; z++)
91 fputc(cva[z], stdout);
92
93 fputc('\n', stdout);
94
95
96 x=c;
97
98 while (cvb[x] == 0)
99 {
100 cvb[x] = cvlen-x;
101 x--;
102 }
103 }
104 }
105
106
lcharvar(char * str)107 void lcharvar(char *str)
108 {
109 int x;
110 int y;
111 int z;
112
113
114
115 cvlen = strlen(str);
116
117 cva = newchar(cvlen);
118 cvb = newchar(cvlen);
119 exitnull(cva);
120 exitnull(cvb);
121
122 strcpy(cva, str);
123
124
125 for (x=0; x<cvlen; x++)
126 cvb[x] = x;
127
128 cvb[cvlen] = cvlen;
129
130
131 for (z=0; z<cvlen; z++)
132 fputc(cva[z], stdout);
133
134 fputc('\n', stdout);
135
136
137 x=1;
138
139 while (x < cvlen)
140 {
141 cvb[x]--;
142 y=0;
143
144 while (y < x)
145 {
146 swap_char(cva[x], cva[y])
147 y++;
148 x--;
149 }
150
151 for (z=0; z<cvlen; z++)
152 fputc(cva[z], stdout);
153
154 fputc('\n', stdout);
155
156 x=1;
157
158 while (cvb[x] == 0)
159 {
160 cvb[x]=x;
161 x++;
162 }
163
164 }
165
166 }
167
168
169
170
171 /*
172 Sort lines by length.
173
174 - Scan input file to count the lines.
175 - Allocate (12 * lines) bytes of memory.
176 - Re-scan input file to store line lengths and positions.
177 - Sort by length (either shortest or longest first).
178 - Copy input file to output file using line refs.
179
180 * NOTE: Can process both UNIX and Windows text files.
181
182 */
183
184
185
bblensorts(FILE * sourcefile,FILE * destfile,struct najiline * plines,struct najiline * plineend)186 void bblensorts(FILE * sourcefile, FILE * destfile,
187 struct najiline *plines, struct najiline *plineend)
188 {
189
190 struct najiline *pline, buffer;
191 int sortflag;
192
193 do
194 {
195
196 pline = plines;
197 sortflag = 0;
198
199 while (pline < plineend)
200 {
201 if ((pline->len) > ((pline + 1)->len))
202 {
203 buffer = *pline;
204 *pline = *(pline + 1);
205 *(pline + 1) = buffer;
206 sortflag = 1;
207 }
208
209 pline++;
210 }
211
212 } while (sortflag != 0);
213
214
215 }
216
217
218
bblensortl(FILE * sourcefile,FILE * destfile,struct najiline * plines,struct najiline * plineend)219 void bblensortl(FILE * sourcefile, FILE * destfile,
220 struct najiline *plines, struct najiline *plineend)
221 {
222 struct najiline *pline, buffer;
223 int sortflag;
224
225 do
226 {
227
228
229 pline = plines;
230 sortflag = 0;
231
232
233 while (pline < plineend)
234 {
235
236 if ((pline->len) < ((pline + 1)->len))
237 {
238 buffer = *pline;
239 *pline = *(pline + 1);
240 *(pline + 1) = buffer;
241 sortflag = 1;
242 }
243 pline++;
244
245 }
246
247
248
249 } while (sortflag != 0);
250
251
252 }
253
254
255
256
readforsort(FILE * sourcefile,FILE * destfile,struct najiline ** pplines,struct najiline ** pplineend,char * lastchar)257 int readforsort(FILE * sourcefile, FILE * destfile,
258 struct najiline **pplines, struct najiline **pplineend, char *lastchar)
259 {
260 struct najiline line;
261 struct najiline *pline = NULL;
262 struct najiline *plines = NULL;
263 char current;
264 char previous = ' ';
265 int linecharnb = 0;
266 int linenb = 0;
267 int lflong = 1;
268
269 long filecharnb = 0;
270
271 while ((current = fgetc(sourcefile)) != EOF)
272 {
273
274 linecharnb++;
275 filecharnb++;
276
277 if (current == '\n')
278 {
279 if (previous == '\r')
280 lflong = 2;
281
282 linenb++;
283 linecharnb = 0;
284 }
285
286 previous = current;
287 }
288
289 if (linecharnb > 0)
290 {
291 *lastchar = '0';
292 linenb++;
293 }
294
295 else *lastchar = '\n';
296
297 if (filecharnb > 0)
298 linenb++;
299
300 plines = malloc(sizeof(line) * linenb);
301
302 if (plines == NULL)
303 {
304
305 fprintf(stderr, "Error allocating %d bytes of memory.",
306 sizeof(line) * linenb);
307
308 return -1;
309 }
310
311 rewind(sourcefile);
312 pline = plines - 1;
313 linecharnb = 0;
314 filecharnb = 0;
315
316 while ((current = fgetc(sourcefile)) != EOF)
317 {
318 linecharnb++;
319
320 if (current == '\n')
321 {
322 pline++;
323 pline->len = linecharnb - lflong;
324 pline->pos = filecharnb;
325 filecharnb += (long) linecharnb;
326 linecharnb = 0;
327 }
328
329 }
330
331 if (linecharnb > 0)
332 {
333 pline++;
334 pline->len = linecharnb;
335 pline->pos = filecharnb;
336 }
337
338
339 *pplineend = pline;
340 *pplines = plines;
341
342 return lflong;
343 }
344
345
346
writesorted(FILE * sourcefile,FILE * destfile,struct najiline * plines,struct najiline * plineend,int lflong,char lastchar)347 void writesorted(FILE *sourcefile, FILE *destfile, struct najiline *plines,
348 struct najiline *plineend, int lflong, char lastchar)
349 {
350 struct najiline *pline;
351 char current;
352 int i;
353 char cr = '\r';
354 char lf = '\n';
355
356 pline = plines;
357
358 for (pline = plines; pline <= plineend; pline++)
359 {
360
361 fseek(sourcefile, pline->pos, SEEK_SET);
362
363
364 for (i=0; i < pline->len; i++)
365 {
366 fread(¤t, 1, 1, sourcefile);
367 fwrite(¤t, 1, 1, destfile);
368 }
369
370
371 if ((pline < plineend) || (lastchar == '\n'))
372 {
373 if (lflong == 2)
374 fwrite(&cr, 1, 1, destfile);
375
376 fwrite(&lf, 1, 1, destfile);
377 }
378
379
380 }
381
382 fclose(sourcefile);
383 fclose(destfile);
384 }
385
386
387
lensort_basis(char whichone,char * namein,char * nameout)388 void lensort_basis(char whichone, char *namein, char *nameout)
389 {
390 struct najiline *plines, *plineend;
391 int lflong;
392 char lastchar;
393
394 najin(namein);
395 najout(nameout);
396
397 lflong =
398 readforsort(naji_input, naji_output, &plines, &plineend, &lastchar);
399
400 if (whichone == 's')
401 bblensorts(naji_input, naji_output, plines, plineend);
402
403 else
404 bblensortl(naji_input, naji_output, plines, plineend);
405
406 writesorted(naji_input, naji_output, plines, plineend, lflong, lastchar);
407
408 free(plines);
409 }
410
411
412
lensorts(char * namein,char * nameout)413 void lensorts(char *namein, char *nameout)
414 {
415 lensort_basis('s', namein, nameout);
416 }
417
418
lensortl(char * namein,char * nameout)419 void lensortl(char *namein, char *nameout)
420 {
421 lensort_basis('l', namein, nameout);
422 }
423
424
425
426
charsort(char * namein,char * nameout)427 void charsort(char *namein, char *nameout)
428 {
429 unsigned long charsort_buf[256];
430
431 int i=0;
432 int a=0;
433 int b=0;
434
435 for (a=0; a<256; a++)
436 charsort_buf[a] = 0;
437
438 najin(namein);
439 najout(nameout);
440
441
442 while (!(feof(naji_input)))
443 {
444 i = fgetc(naji_input);
445 charsort_buf[i]++;
446 }
447
448
449
450
451 for (a=0; a<256; a++)
452 for (b=0; b<charsort_buf[a]; b++)
453 fputc(a, naji_output);
454
455
456 najinclose();
457 najoutclose();
458 }
459
460
461
lcvfiles(char * namein)462 void lcvfiles(char *namein)
463 {
464 long cvlen;
465 char *cva;
466 char *cvb;
467
468 int x;
469 int y;
470 int z;
471
472 int a;
473
474 long i=0;
475 char buffer[2048];
476
477 long filecount=0;
478
479
480
481 najin(namein);
482
483 cvlen = najinsize();
484
485 cva = newchar(cvlen);
486 cvb = newchar(cvlen);
487 exitnull(cva);
488 exitnull(cvb);
489
490 while (1)
491 {
492 a = fgetc(naji_input);
493
494 if (a == EOF) break;
495
496 cva[i] = a;
497 i++;
498 }
499
500 najinclose();
501
502
503 for (x=0; x<cvlen; x++)
504 cvb[x] = x;
505
506 cvb[cvlen] = cvlen;
507
508
509
510
511
512 sprintf(buffer, "lcv%lu-%s", filecount, namein);
513 najout(buffer);
514 filecount++;
515
516
517
518
519 for (z=0; z<cvlen; z++)
520 fputc(cva[z], naji_output);
521
522 najoutclose();
523
524 x=1;
525
526 while (x < cvlen)
527 {
528 cvb[x]--;
529 y=0;
530
531 while (y < x)
532 {
533 swap_char(cva[x], cva[y])
534 y++;
535 x--;
536 }
537
538 sprintf(buffer, "lcv%lu-%s", filecount, namein);
539 najout(buffer);
540 filecount++;
541
542
543 for (z=0; z<cvlen; z++)
544 fputc(cva[z], naji_output);
545
546 najoutclose();
547
548 x=1;
549
550 while (cvb[x] == 0)
551 {
552 cvb[x]=x;
553 x++;
554 }
555
556 }
557
558
559
560 }
561
562
563
rcvfiles(char * namein)564 void rcvfiles(char *namein)
565 {
566 int a;
567 int c;
568 int x;
569 int y;
570 int z;
571 long cvlen;
572 char *cva;
573 char *cvb;
574
575
576
577 long i=0;
578 char buffer[2048];
579
580 long filecount=0;
581
582
583 najin(namein);
584 cvlen = najinsize();
585
586 c = cvlen-1;
587
588 cva = newchar(cvlen);
589 cvb = newchar(cvlen);
590 exitnull(cva);
591 exitnull(cvb);
592
593
594 while (1)
595 {
596 a = fgetc(naji_input);
597
598 if (a == EOF) break;
599
600 cva[i] = a;
601 i++;
602 }
603
604 najinclose();
605
606
607 sprintf(buffer, "rcv%lu-%s", filecount, namein);
608 najout(buffer);
609 filecount++;
610
611 for(x=0; x<cvlen; x++)
612 cvb[x]=cvlen-x;
613
614 for (z=0; z<cvlen; z++)
615 fputc(cva[z], naji_output);
616
617 najoutclose();
618
619
620 x=c;
621
622 while (x > 0)
623 {
624 cvb[x]--;
625 x--;
626 y=c;
627
628 while (y > x)
629 {
630 swap_char(cva[x], cva[y]);
631 y--;
632 x++;
633 }
634
635 sprintf(buffer, "rcv%lu-%s", filecount, namein);
636 najout(buffer);
637 filecount++;
638
639
640 for (z=0; z<cvlen; z++)
641 fputc(cva[z], naji_output);
642
643 najoutclose();
644
645
646 x=c;
647
648 while (cvb[x] == 0)
649 {
650 cvb[x] = cvlen-x;
651 x--;
652 }
653
654 }
655
656 }
657
658
659
660
661
662
663 /* function pointer declaration */
664 void (*sort_print_global_function_pointer)(char **buffer, unsigned long howmany);
665
666
sortcomp(const void * a,const void * b)667 int sortcomp(const void *a, const void *b)
668 {
669 const char **va = (void *) a;
670 const char **vb = (void *) b;
671 return strcmp(*va, *vb);
672 }
673
674
sort_basis(char * namein)675 void sort_basis(char *namein)
676 {
677 char **buffer = NULL;
678 unsigned long howmany;
679 unsigned long howlong;
680
681 howmany = howl(namein);
682 howlong = longl(namein);
683
684 howlong += 3;
685 howmany ++;
686
687 buffer = naji_lines_alloc(howmany, howlong);
688
689 naji_lines_load(namein, buffer, howmany, howlong);
690
691 qsort( (void*) buffer, howmany, sizeof(buffer[0]), sortcomp);
692
693 (*sort_print_global_function_pointer)(buffer, howmany);
694
695 naji_lines_free(buffer, howmany);
696 }
697
698
sort(char * namein)699 void sort(char *namein)
700 {
701 (sort_print_global_function_pointer) = naji_lines_print;
702 sort_basis(namein);
703 }
704
705
sortlast(char * namein)706 void sortlast(char *namein)
707 {
708 (sort_print_global_function_pointer) = naji_lines_backwards_print;
709 sort_basis(namein);
710 }
711
712