home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / snip9707.zip / DOSSORT.C < prev    next >
C/C++ Source or Header  |  1997-07-05  |  3KB  |  123 lines

  1. /* +++Date last modified: 05-Jul-1997 */
  2.  
  3. /*
  4.  
  5.     cl /AL sortit.c
  6.  
  7. ===========================================================
  8.    sort.c       7-31-91  Robert Mashlan
  9.  
  10.    This filter is almost compatible with the MS-DOS filter of the same name.
  11.  
  12.    This filter sorts each line of standard input, disregarding case,
  13.    and sends it to standard output.
  14.  
  15.    optional parameters:  /R    Output in reverse order
  16.                          /+n   Compare at column n, 1 based
  17.  
  18.    example usage:  sort < unsorted.txt > sorted.txt
  19.  
  20.    note: compile in a far data model for maximum capacity ( compact or large )
  21.  
  22. */
  23.  
  24. #include <stdlib.h>
  25. #include <stdio.h>
  26. #include <string.h>
  27.  
  28. #define MAXLINES 10000        /* maximum number of lines to sort */
  29. #define MAXLINE  80           /* maximum line length             */
  30.  
  31. unsigned col     = 0;  /* column to start sort at ( zero based here ) */
  32. int      reverse = 0;  /* reverse order flag                          */
  33.  
  34. /*
  35. **  compare function for qsort
  36. */
  37.  
  38. int cmp( const void *a, const void *b)
  39. {
  40.       int result;
  41.       const char *_a = *(const char **)a;
  42.       const char *_b = *(const char **)b;
  43.  
  44.       /* compare at col if other than zero */
  45.  
  46.       if (col > 0)
  47.       {
  48.             if (strlen(_a) > col)
  49.                   _a += col;
  50.             else  _a = "";
  51.             if (strlen(_b) > col)
  52.                   _b += col;
  53.             else  _b = "";
  54.       }
  55.       result = stricmp(_a,_b);
  56.       return reverse ? -result : result;
  57. }
  58.  
  59. int main(int argc, char *argv[])
  60. {
  61.       static char *lines[MAXLINES];
  62.       int i, nlines=0, no_match;
  63.       char buf[MAXLINE];
  64.  
  65.       /* scan for command line options */
  66.  
  67.       for(i=1;i<argc;i++)
  68.       {
  69.             if(!stricmp(argv[i],"/R"))  /* reverse order option */
  70.                   reverse=1;
  71.             else if (!strnicmp(argv[i],"/+",2))  /* column number option */
  72.             {
  73.                   int n = atoi(argv[i]+1)-1;
  74.  
  75.                   if (n < 0)
  76.                         n = 0;
  77.                   col = (unsigned)n;
  78.             }
  79.             else
  80.             {
  81.                   fputs("Invalid Parameter",stderr);
  82.                   return 1;
  83.             }
  84.       }
  85.       while(1)
  86.       {
  87.             /* read the line */
  88.  
  89.             if(fgets(buf,MAXLINE,stdin)==NULL)
  90.                   break;
  91.  
  92.             /* Check for duplicates here */
  93.  
  94.             no_match = 1;
  95.             for ( i=0; i< nlines; i++ )
  96.             {
  97.                   if (0 == (no_match = memicmp(buf, lines[i],
  98.                         (unsigned int)12)))
  99.                   {
  100.                         break;
  101.                   }
  102.             }
  103.  
  104.             if (no_match)                 /* make a duplicate on the heap */
  105.             {
  106.                   if((lines[nlines++]=strdup(buf))==NULL)
  107.                   {
  108.                         fputs("SORT: Insufficient memory",stderr);
  109.                         return 2;
  110.                   }
  111.             }
  112.       }
  113.       /* sort the lot */
  114.       
  115.       qsort((void *)lines,nlines,sizeof(char *),cmp);
  116.  
  117.       /* display the sorted output */
  118.       
  119.       for (i=0;i<nlines;i++)
  120.             fputs(lines[i],stdout); /* send it to standard output */
  121.       return 0;
  122. }
  123.