home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / INFO / C / R_LA4_02.ZIP / COMCHECK.C < prev    next >
Encoding:
C/C++ Source or Header  |  1988-01-20  |  1.8 KB  |  65 lines

  1. /* COMCHECK.C - From page 581 of "Microsoft C Programming for   */
  2. /* the IBM" by Robert Lafore. Checks for matching comment       */
  3. /* symbols, reports first occurrence of mispairing.             */
  4. /****************************************************************/
  5.  
  6. #include "stdio.h"
  7. #define TRUE 1
  8. #define FALSE 0
  9.  
  10. main(argc, argv)
  11. int argc;
  12. char *argv[];
  13. {
  14. FILE *fptr;
  15. char ch;
  16. int slash = FALSE;
  17. int star = FALSE;
  18. int comments = 0;
  19. int line = 1;
  20. void chcount(), finalcnt();
  21.  
  22.    if(argc != 2)  {
  23.       printf("\nType \"comcheck filename\".");
  24.       exit();
  25.    }
  26.    if((fptr = fopen(argv[1], "r")) == NULL)  {
  27.       printf("\nCan't open file %s", argv[1]);
  28.       exit();
  29.    }
  30.    while((ch = getc(fptr)) != EOF)  {
  31.       switch(ch)  {
  32.          case '*':
  33.             star = TRUE;
  34.             if(slash == TRUE)  {          /*found start comment?*/
  35.                comments++;
  36.                if(comments > 1)  {        /*unmatched start comm*/
  37.                   printf("\nUnexpected start comment, line %d", line);
  38.                   exit();
  39.                }
  40.                slash = star = FALSE;      /*reset flags*/
  41.             }
  42.             break;
  43.          case '/':
  44.             slash = TRUE;
  45.             if(star == TRUE)  {           /*found end comment ?*/
  46.                comments--;
  47.                if(comments < 0)  {        /*unmatched end comment*/
  48.                   printf("\nUnexpected end comment, line %d", line);
  49.                   exit();
  50.                }
  51.                slash = star = FALSE;
  52.             }
  53.             break;
  54.          case '\n':
  55.             line++;
  56.          default:
  57.             slash = star = FALSE;
  58.       }  /*end switch*/
  59.    }  /*end while*/
  60.    fclose(fptr);
  61.    if(comments > 0)           /*open comment at EOF*/
  62.       printf("\nUnmatched open comment.");
  63. }
  64.  
  65.