home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1995 August / NEBULA.mdf / Apps / DevTools / CodeCounter / Source / ConvertObject.m < prev    next >
Encoding:
Text File  |  1994-12-02  |  4.3 KB  |  220 lines

  1.  
  2. #import "ConvertObject.h"
  3. #import <appkit/Form.h>
  4.  
  5. #define EXTENSION_LIST "c", "h", "m", "cc", "e"
  6. @implementation ConvertObject
  7.  
  8. - convertMethod:sender
  9. {
  10.    
  11. typedef    int    Token ;
  12. #define    STOP_INPUT    0
  13. #define    NEWLINE    1
  14. #define START_COMMENT    2
  15. #define END_COMMENT    3
  16. #define    MISC_CHARACTER    4
  17. #define WHITE_SPACE    5
  18.  
  19. typedef char    Bool;
  20. #define True    1
  21. #define False    0
  22.  
  23. typedef int    State;
  24. #define    Code    0
  25. #define Comment    1
  26. #define Quiescent    2
  27.  
  28. #define FNULL    ( (FILE *) 0)
  29. #define CNULL    ( (char *) 0)
  30.  
  31. char objc_comm = 'N';
  32.  
  33.     id myobj;
  34.  
  35.  {
  36.     register Token     input;
  37.     register State    statevar = Quiescent, laststate = Quiescent;
  38.     int    filecount = 0;
  39.     long    cod_linect, com_linect, blnk_linect, comment_ct;
  40.     long    tot_cdline, tot_cmline, tot_bkline, tot_comment;
  41.     long    grand_tot_cdline, grand_tot_cmline, grand_tot_bkline, grand_tot_comment;
  42.     Bool    following_com = False;
  43.     FILE    *fp;
  44.  
  45.   const char *file, *directory;
  46.   const char *const *filenames;
  47.   static const char
  48.   *const wordTypes[] = {EXTENSION_LIST, NULL};
  49.   char fullPathName[MAXPATHLEN];
  50.   id openPanel = [OpenPanel new];
  51.      
  52.   [openPanel allowMultipleFiles:NO];
  53.   if ([openPanel runModalForTypes:wordTypes])
  54.   {
  55.     filenames = [openPanel filenames];
  56.     directory = [openPanel directory];
  57.         file = *(filenames++);
  58.         strcpy(fullPathName, directory);
  59.         strcat(fullPathName, "/");
  60.         strcat(fullPathName, file);
  61.         strcpy(filename, file);    
  62.          }                              
  63.  
  64.     if( strncmp(fullPathName, "\000", 1) == 0 || 
  65. (fp = fopen(fullPathName, "r")) == NULL )
  66.         {
  67.          return NO;
  68.             }
  69.  
  70.     tot_cdline = tot_cmline = tot_bkline = tot_comment = 0;
  71.     grand_tot_cdline = grand_tot_cmline = grand_tot_bkline = grand_tot_comment = 0;
  72.  
  73.     {
  74.         cod_linect = com_linect = blnk_linect = comment_ct = 0;
  75.         filecount++;
  76.  
  77.                   while (!feof(fp)) {
  78.         {
  79.  
  80.  
  81. {
  82.     /* return Token  for char type, taking into account comment delims */
  83.     /* ignores spaces and tabs */
  84.  
  85.     register int    c;
  86.     register Token     retval;
  87.     static int    buf;
  88.     static Bool    inbuf = False;
  89.     do
  90.     {
  91.         if ( inbuf )
  92.         {
  93.             c = buf;
  94.             inbuf = False;
  95.         }
  96.         else
  97.             c = getc(fp);
  98.         
  99.         switch ( c )
  100.         {
  101.         case EOF:
  102.             retval = STOP_INPUT;
  103.             break;
  104.         case '\n':
  105.             retval = NEWLINE;
  106.             break; 
  107.         case '/':
  108.             buf = getc(fp);
  109.             if ( buf == '*' )
  110.                 retval = START_COMMENT;
  111.             else if (buf == '/' ) {
  112.                 objc_comm = 'Y';
  113.                 retval = END_COMMENT; }
  114.             else
  115.             {
  116.                 inbuf = True;
  117.                 retval = MISC_CHARACTER;
  118.             }
  119.             break;
  120.         case '*':
  121.             buf = getc(fp);
  122.             if ( buf == '/' )
  123.                 retval = END_COMMENT;
  124.             else
  125.             {
  126.                 inbuf = True;
  127.                 retval = MISC_CHARACTER;
  128.             }
  129.             break;
  130.         case ' ':
  131.         case '\t':
  132.             retval = WHITE_SPACE;
  133.             break;
  134.         default:
  135.             retval = MISC_CHARACTER;
  136.         }
  137.     }
  138.     while ( retval == WHITE_SPACE );
  139.  
  140.              input = retval;
  141.             switch ( input )
  142.             {
  143.             case NEWLINE:
  144.                 if ( statevar == Code && objc_comm == 'N')
  145.                     cod_linect++;
  146.                 else if ( statevar == Comment || objc_comm == 'Y')
  147.                     {com_linect++;
  148.                     objc_comm = 'N';}
  149.                 /* state is quiescent */
  150.                 else if ( laststate == Comment )
  151.                 {
  152.                     /* if is supposed to catch cases where a comment
  153.                      * follows a line of code
  154.                      */
  155.                     if ( following_com ) 
  156.                         cod_linect++;
  157.                     else
  158.                         com_linect++;
  159.                 }
  160.                 else
  161.                     blnk_linect++;
  162.                 if ( statevar != Comment )
  163.                 {
  164.                     laststate = Quiescent;
  165.                     statevar = Quiescent;
  166.                 }
  167.                 following_com = False;
  168.                 break;
  169.             case START_COMMENT:
  170.                 laststate = statevar;
  171.                 statevar = Comment;
  172.                 break;
  173.             case END_COMMENT:
  174.                 comment_ct++;
  175.                     /* if true, is a comment on same line as code */
  176.                 if ( laststate == Code )
  177.                     following_com = True;
  178.  
  179.                 laststate = Comment;
  180.                 statevar = Quiescent;
  181.                 break;
  182.             case MISC_CHARACTER:
  183.                 if ( statevar == Quiescent )
  184.                 {
  185.                     laststate = statevar;
  186.                     statevar = Code;
  187.                 }
  188.                 break;
  189.             default:
  190.             lines = cod_linect;
  191.             blanks = blnk_linect;
  192.             comments = com_linect;
  193.             }
  194.         }
  195.         tot_cdline += lines;
  196.         tot_cdline += cod_linect;
  197.         tot_cmline += com_linect;
  198.         tot_bkline += blnk_linect;
  199.         tot_comment += comment_ct;
  200.  
  201.         grand_tot_cdline += tot_cdline;
  202.         grand_tot_cmline += tot_cmline;
  203.         grand_tot_bkline += tot_bkline;
  204.         grand_tot_comment += tot_comment;
  205.     }
  206.  
  207. }
  208. }
  209. }
  210.     myobj = [[ConvertObject alloc] init];
  211.  
  212.     [fileOutlet setStringValue:filename];
  213.     [linesOutlet setFloatValue:lines];
  214.     [blnkOutlet setFloatValue:blanks];
  215.     [comOutlet setFloatValue:comments];
  216.     
  217.     return self;
  218. }
  219.  
  220. @end
  221.