/* ---------- */ /* najisort.c */ /* ---------- */ /* naji sort functions */ /* this .c file is a part */ /* of libnaji version 0.6.1 */ /* libnaji is based on */ /* the original najitool */ /* both najitool and libnaji */ /* are public domain and are */ /* made by the same author */ /* please read license.txt */ /* made by NECDET COKYAZICI */ /* bblensorts */ /* bblensortl */ /* readforsort */ /* writesorted */ /* lensort_basis */ /* Made by MANUEL LE BOETTE */ #include "libnaji.h" char swap_char_var; #define swap_char(a, b) swap_char_var=a; a=b; b=swap_char_var; int swap_int_var; #define swap_int(a, b) swap_int_var=a; a=b; b=swap_int_var; int cvlen; char *cva; char *cvb; void rcharvar(char *str) { int c; int x; int y; int z; cvlen = strlen(str); c = cvlen-1; cva = newchar(cvlen); cvb = newchar(cvlen); exitnull(cva); exitnull(cvb); strcpy(cva, str); for(x=0; x 0) { cvb[x]--; x--; y=c; while (y > x) { swap_char(cva[x], cva[y]); y--; x++; } for (z=0; zlen) > ((pline + 1)->len)) { buffer = *pline; *pline = *(pline + 1); *(pline + 1) = buffer; sortflag = 1; } pline++; } } while (sortflag != 0); } void bblensortl(FILE * sourcefile, FILE * destfile, struct najiline *plines, struct najiline *plineend) { struct najiline *pline, buffer; int sortflag; do { pline = plines; sortflag = 0; while (pline < plineend) { if ((pline->len) < ((pline + 1)->len)) { buffer = *pline; *pline = *(pline + 1); *(pline + 1) = buffer; sortflag = 1; } pline++; } } while (sortflag != 0); } int readforsort(FILE * sourcefile, FILE * destfile, struct najiline **pplines, struct najiline **pplineend, char *lastchar) { struct najiline line, *pline, *plines; char current; char previous = ' '; int linecharnb = 0; int linenb = 0; int lflong = 1; long filecharnb = 0; while ((current = fgetc(sourcefile)) != EOF) { linecharnb++; filecharnb++; if (current == '\n') { if (previous == '\r') lflong = 2; linenb++; linecharnb = 0; } previous = current; } if (linecharnb > 0) { *lastchar = '0'; linenb++; } else *lastchar = '\n'; if (filecharnb > 0) linenb++; plines = malloc(sizeof(line) * linenb); if (pline == NULL) { fprintf(stderr, "Error allocating %d bytes of memory.", sizeof(line) * linenb); return -1; } rewind(sourcefile); pline = plines - 1; linecharnb = 0; filecharnb = 0; while ((current = fgetc(sourcefile)) != EOF) { linecharnb++; if (current == '\n') { pline++; pline->len = linecharnb - lflong; pline->pos = filecharnb; filecharnb += (long) linecharnb; linecharnb = 0; } } if (linecharnb > 0) { pline++; pline->len = linecharnb; pline->pos = filecharnb; } *pplineend = pline; *pplines = plines; return lflong; } void writesorted(FILE *sourcefile, FILE *destfile, struct najiline *plines, struct najiline *plineend, int lflong, char lastchar) { struct najiline *pline; char current; int i; char cr = '\r'; char lf = '\n'; pline = plines; for (pline = plines; pline <= plineend; pline++) { fseek(sourcefile, pline->pos, SEEK_SET); for (i=0; i < pline->len; i++) { fread(¤t, 1, 1, sourcefile); fwrite(¤t, 1, 1, destfile); } if ((pline < plineend) || (lastchar == '\n')) { if (lflong == 2) fwrite(&cr, 1, 1, destfile); fwrite(&lf, 1, 1, destfile); } } fclose(sourcefile); fclose(destfile); } void lensort_basis(char whichone, char *namein, char *nameout) { struct najiline *plines, *plineend; int lflong; char lastchar; najin(namein); najout(nameout); lflong = readforsort(naji_input, naji_output, &plines, &plineend, &lastchar); if (whichone == 's') bblensorts(naji_input, naji_output, plines, plineend); else bblensortl(naji_input, naji_output, plines, plineend); writesorted(naji_input, naji_output, plines, plineend, lflong, lastchar); free(plines); } void lensorts(char *namein, char *nameout) { lensort_basis('s', namein, nameout); } void lensortl(char *namein, char *nameout) { lensort_basis('l', namein, nameout); }