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

  1. // +++Date last modified: 05-Jul-1997
  2.  
  3. /*
  4.  * CMTXTRAC.CPP
  5.  * Implementation of comment extractor.
  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 "cmtxtrac.h"
  20.  
  21. void CommentExtractor::ProcessActions (Event theEvent)
  22. {
  23.     int rc = 0;
  24.  
  25.     // This flag is needed when comment starting is continued in next line:
  26.     //   /\
  27.     //   * comment */
  28.     // The BeginComment state is still on after InsideEscape state and
  29.     // without flag the slash would be printed twice.
  30.     static int SlashPrinted = 0;
  31.  
  32.     switch (itsState)
  33.     {
  34.     case NormalInput:
  35.         if (printWhiteSpace && theEvent == FOUND_WHITESPACE)
  36.             rc = 1;                     // Print whitespace.
  37.         break;
  38.  
  39.     case BeginComment:
  40.         switch (theEvent)
  41.         {
  42.         case FOUND_SLASH:               // Yes, it's a C++ comment.
  43.         case FOUND_STAR:                // Yes, it's a C comment.
  44.         case FOUND_BACKSLASH:           // Escaped character.
  45.             rc = 1;
  46.             if (! SlashPrinted)
  47.             {
  48.                 if (printLineNumbers)
  49.                 {
  50.                     PrintLineNumber();
  51.                 }
  52.  
  53.                 // Comment starting slash was not yet printed, so print it now.
  54.                 print ('/');
  55.                 SlashPrinted = 1;
  56.             }
  57.             else
  58.                 SlashPrinted = 0;
  59.  
  60.             break;
  61.         }
  62.         break;
  63.  
  64.     case InsideEscape:
  65.         if (itsPrevState == InCppComment || itsPrevState == InCComment)
  66.             rc = 1;
  67.         break;
  68.  
  69.     case StarInCComment:
  70.         if (theEvent == FOUND_SLASH)
  71.             SlashPrinted = 0;           // End of comment, clear flag.
  72.         rc = 1;
  73.         break;
  74.  
  75.     case InCppComment:
  76.         if (theEvent == FOUND_NL)
  77.             SlashPrinted = 0;           // End of comment, clear flag.
  78.         rc = 1;
  79.         break;
  80.  
  81.     case InCComment:
  82.     case StarInCppComment:
  83.         rc = 1;
  84.         break;
  85.     }
  86.  
  87.     if (theEvent == FOUND_NL)           // Newline can be printed always.
  88.     {
  89.         rc = 1;
  90.     }
  91.  
  92.     if (rc)
  93.         PrintChar();
  94. }
  95.  
  96. int   main (int argc, char **argv)
  97. {
  98.     CommentExtractor CommExtract;
  99.  
  100.     if (0 == CommExtract.Init (argc, argv))
  101.     {
  102.         fprintf (stderr, "USAGE: CMTREMOV InFile [OutFile]\n");
  103.     }
  104.     else
  105.     {
  106.         CommExtract.WhiteSpace (0);
  107.         CommExtract.LineNumbers (1);
  108.         CommExtract.Run();
  109.         CommExtract.Uninit();
  110.  
  111.         fprintf (stderr, "\nOK! %lu lines processed. Last state = %d\n",
  112.             CommExtract.GetLines(), CommExtract.GetLastState()
  113.         );
  114.     }
  115.  
  116.     return 0;
  117. }
  118.