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

  1. // +++Date last modified: 05-Jul-1997
  2.  
  3. /*
  4.  * CMTREMOV.CPP
  5.  * Implementation of comment remover.
  6.  *
  7.  * ver 1.0, 30 Jun 1996
  8.  *
  9.  * Public domain by:
  10.  *   Jari Laaksonen
  11.  *   Arkkitehdinkatu 30 A 2
  12.  *   FIN-33720 Tampere
  13.  *   FINLAND
  14.  *
  15.  *   Fidonet : 2:221/360.20
  16.  *   Internet: jla@to.icl.fi
  17.  */
  18.  
  19. #include "cmtremov.h"
  20.  
  21. void CommentRemover::ProcessActions (Event theEvent)
  22. {
  23.     int rc = 1;
  24.  
  25.     if (theEvent == END_OF_FILE)
  26.     rc= 1;
  27.  
  28.     switch (itsState)
  29.     {
  30.     case NormalInput:
  31.         if (theEvent == FOUND_SLASH)
  32.         {
  33.             rc = 0;                     // Don't print it yet...
  34.         }
  35.         break;
  36.  
  37.     case BeginComment:
  38.         switch (theEvent)
  39.         {
  40.         case FOUND_SLASH:               // Yes, it's a C++ comment...
  41.         case FOUND_STAR:                // Yes, it's a C-style comment.
  42.         case FOUND_BACKSLASH:           // Escaped character.
  43.             rc = 0;
  44.             break;
  45.  
  46.         default:                        // No, just a slash...
  47.             print ('/');                // Print the previous slash.
  48.             break;
  49.         }
  50.         break;
  51.  
  52.     case InsideEscape:
  53.         if (itsPrevState == InCppComment || itsPrevState == InCComment)
  54.             rc = 0;
  55.         break;
  56.  
  57.     case InCppComment:
  58.     case InCComment:
  59.     case StarInCppComment:
  60.     case StarInCComment:
  61.         rc = 0;
  62.         break;
  63.     }
  64.  
  65.     if (theEvent == FOUND_NL)       // Newline can be printed always.
  66.     {
  67.         rc = 1;
  68.     }
  69.  
  70.     if (rc)
  71.         PrintChar();
  72. }
  73.  
  74. int   main (int argc, char **argv)
  75. {
  76.     CommentRemover CommRemv;
  77.  
  78.     if (0 == CommRemv.Init (argc, argv))
  79.     {
  80.         fprintf (stderr, "USAGE: CMTREMOV InFile [OutFile]\n");
  81.     }
  82.     else
  83.     {
  84.         CommRemv.Run();
  85.         CommRemv.Uninit();
  86.  
  87.         fprintf (stderr, "\nOK! %lu lines processed. Last state = %d\n",
  88.             CommRemv.GetLines(), CommRemv.GetLastState()
  89.         );
  90.     }
  91.  
  92.     return 0;
  93. }
  94.