home *** CD-ROM | disk | FTP | other *** search
/ C!T ROM 2 / ctrom_ii_b.zip / ctrom_ii_b / PROGRAM / C / UNIQUE / UNIQUE.CPP < prev    next >
C/C++ Source or Header  |  1991-12-18  |  5KB  |  122 lines

  1. /*  unique.cpp  */
  2. /*
  3.     this filter reads a text file, line at a time, and outputs only the
  4.     first occurence of identical lines.
  5.  
  6.     written on 13 November 1991 by Ed Beroset
  7. */
  8.  
  9. /****************************************************************************
  10. *                               includes                                    *
  11. ****************************************************************************/
  12. #include <iostream.h>
  13. #include <fstream.h>
  14. #include <iomanip.h>
  15. #include <string.h>
  16. #include <process.h>
  17. #include <ctype.h>
  18.  
  19. #define VERSION  "1.01"    /* version number of this program  */
  20. #define FALSE 0
  21. #define TRUE (!FALSE)       /* useful constants  */
  22.  
  23. #define SWITCH_CHARS "-/"      /*  command line switch delimiters  */
  24.  
  25. #define MAXLINE 20000
  26.  
  27. /****************************************************************************
  28. *                         function prototypes                               *
  29. ****************************************************************************/
  30. int get_switches(int argc, char *argv[]);
  31. void usage(void);
  32.  
  33. /****************************************************************************
  34. *                           global variables                                *
  35. ****************************************************************************/
  36. int countflag;
  37.  
  38. /****************************************************************************
  39. *                                                                   main()  *
  40. ****************************************************************************/
  41. int main(int argc, char *argv[] )
  42. {
  43.     char a[MAXLINE];    // string A
  44.     char b[MAXLINE];    // string B
  45.     unsigned int i;             // index variable
  46.     int firstarg;
  47.  
  48.     countflag = 0;
  49.  
  50.     firstarg = get_switches(argc,argv);
  51.  
  52.     cin.getline(a,MAXLINE);
  53.     i=0;
  54.     while (cin)    {
  55.         cin.getline(b,MAXLINE);
  56.         i++;
  57.         if (strcmp(a,b))    {
  58.             if (countflag)
  59.                 cout << setw(4) << i << setw(0) << ':';
  60.             cout << a << endl;
  61.             strcpy(a,b);
  62.             i=0;
  63.         }
  64.     }
  65.     return(0);
  66. }
  67.  
  68.  
  69. /****************************************************************************
  70. *                                                           get_switches()  *
  71. *                                                                           *
  72. *   parses the first arguments to extract the appropriate command line      *
  73. *   switches and act on that info accordingly.  Also returns the number     *
  74. *   of the first non-switch arg (e.g. from which argv the main routine      *
  75. *   should start reading)                                                   *
  76. *                                                                           *
  77. ****************************************************************************/
  78. int get_switches(int argc, char *argv[])    {
  79.     char *sw_ptr;
  80.     int done;
  81.     int cur_arg=1;      /*  start from arg AFTER program name  */
  82.     int i;
  83.  
  84.     while ( ((sw_ptr = strpbrk(argv[cur_arg],SWITCH_CHARS)) != NULL) &&
  85.             (cur_arg < argc))   {
  86.         switch (sw_ptr[1])  {
  87.             case '?':   /*  user wants help  */
  88.             case 'H':
  89.             case 'h':
  90.                 usage();
  91.             case 'c':   /*  count frequency of each line  */
  92.             case 'C':
  93.                 countflag=1;
  94.                 break;
  95.             default:
  96.                 cerr << "\aUnknown command line switch: " << sw_ptr << endl;
  97.                 usage();
  98.         }
  99.         cur_arg++;
  100.     }
  101.     return(cur_arg);
  102. }
  103.  
  104. /****************************************************************************
  105. *                                                                  usage()  *
  106. *                                                                           *
  107. *   prints to the stderr device a short text blurb that describes the       *
  108. *   proper usage (including command line switches) of this program.         *
  109. *                                                                           *
  110. ****************************************************************************/
  111. void usage(void)    {
  112.     cerr << "\nUNIQUE.EXE   \ttext file extraction filter  \tversion " << VERSION;
  113.     cerr << "\nCopyright (c) 1991 by Edward J. Beroset    Chapel Hill, NC  USA\n\n";
  114.     cerr << "Syntax is UNIQUE [options] \n\n";
  115.     cerr << "valid options are:\n";
  116.     cerr << "\t-H or -?   displays this screen\n";
  117.     cerr << "\t-C         prepend each line with occurence count\n";
  118.     exit(0);
  119. }
  120.  
  121.  
  122.