home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / autopsp.zip / LOC / OI.BAK < prev    next >
Text File  |  1994-01-24  |  6KB  |  220 lines

  1. // CObjectInfo
  2. // This is the method definition file for the Object Information class.
  3.  
  4. #include "oi.h"
  5.  
  6. #define ON  1
  7. #define OFF 0
  8.  
  9. #define TRUE  1
  10. #define FALSE 0
  11.  
  12.  
  13. CObjectInfo::CObjectInfo(void)
  14. {
  15.   OLOC=0;
  16.   ONUMC=0;
  17.   OMTHD=0;
  18.   strcpy(ObjectName,"");
  19. }
  20.  
  21. void CObjectInfo::AddFile(char *filename)
  22. {
  23.   FN *temp;
  24.  
  25.   Add(new FN,LL_ADD_AFTER);
  26.   Next();
  27.   temp=(FN*)Retrieve(LL_DATA);
  28.   strcpy(temp->FileName,filename);
  29. }
  30.  
  31. int  CObjectInfo::Count_OLOC(void)
  32. {
  33.   FILE *input;                    // Source file, stream pointer
  34.   char file[35];
  35.   char opnfile[60];
  36.   FN   *temp;
  37.  
  38.   char line[120];                 // A single line from the program.
  39.   char *chr;                      // Points to a particular character
  40.   int  LOC=0;                     // Lines of code found so far
  41.   int  NUMC=0;                    // Number of comment lines.
  42.   char PrevChr;                   // Holds last non-white space character
  43.   int  PDepth=0;                  // Current paranthesis depth
  44.   int  Error=0;                   // If 1 then an error was encountered.
  45.   int  CMode=0;                   // If 1 then the code is in a multi-line comment.
  46.   char FirstChr;                  // First non-Whitespace char on line.
  47.   int  GotFirst;                  // Flag for wether or not you've got the first chr.
  48.   int  quote=OFF;                 // Flag for string constants.
  49.   int  brkt=0;                    // Bracket depth.
  50.   char qtype=' ';                 // is it a " or a ' ?
  51.  
  52.                   // Attempt to open source file.
  53.  
  54.   Restart();
  55.  
  56.   do{
  57.  
  58.     temp=(FN*)Retrieve(LL_DATA);
  59.     strcpy(opnfile,temp->FileName);
  60.  
  61.     if (NULL==(input=fopen(opnfile,"rt"))){
  62.       printf("Unable to open input file: %s\n", temp->FileName);
  63.       printf("File was accessable and now is not.  Program aborts.\n");
  64.       exit(1);                      // Abort program
  65.     }
  66.  
  67.     LOC=0;
  68.     NUMC=0;
  69.     brkt=0;
  70.     PDepth=0;
  71.     while (!feof(input)){           // While more lines of code to process.
  72.       fgets(line,120,input);        // Get a line of code into 'line'
  73.       chr=line;                     // Point at the start of line.
  74.       FirstChr=NULL;
  75.       GotFirst=0;
  76.  
  77.       while (*chr!='\n'){           // While !EOLN
  78.     if(!CMode){                 // If not in a comment.
  79.       switch (*chr){            // Decide what token to process.
  80.         case ';':               // Process ;s
  81.          if (PrevChr!='}' && PrevChr!=';'){
  82.            ++LOC;           // Counts as a valid line of code.
  83.          }
  84.          break;
  85.         case '{':               // Process {s
  86.          if (quote==OFF){
  87.            ++LOC;             // Counts as a valid line of code.
  88.            ++brkt;
  89.          }
  90.          break;
  91.         case '}':
  92.          if (quote==OFF){
  93.            --brkt;
  94.          }
  95.          break;
  96.         case '(':               // Process (
  97.          if (quote==OFF){
  98.            if (PDepth>0){     // Determines if it's valid.
  99.              ++LOC;           // Counts as a valid line of code.
  100.            }
  101.            ++PDepth;
  102.          }
  103.          break;             // Keep track of depth.
  104.         case ')':
  105.          if (quote==OFF){
  106.            --PDepth;          // Decriment the ( depth.
  107.            if(PDepth<0){
  108.              PDepth=0;
  109. //                     Error=1;         // Should never happen with valid code.
  110.            }
  111.          }
  112.          break;
  113.         case '"':
  114.          if (qtype=='"' || qtype==' '){
  115.            if (quote==ON){    // If in a " string this must be the end of it.
  116.              quote=OFF;       //   so turn off the " flag.
  117.              qtype=' ';
  118.            }
  119.            else{
  120.              quote=ON;        // else, this is the begining of the quote.
  121.              qtype='"';
  122.            }
  123.          }
  124.          break;
  125.         case '\'':
  126.          if (qtype=='\'' || qtype==' '){
  127.            if (quote==ON){    // If in a ' string this must be the end of it.
  128.              quote=OFF;       //   so turn off the ' flag.
  129.              qtype=' ';
  130.            }
  131.            else{
  132.              quote=ON;        // else, this is the begining of the quote.
  133.              qtype='\'';
  134.            }
  135.          }
  136.          break;
  137.         case '#':               // Handle the #
  138.          if (FirstChr==NULL){
  139.            ++LOC;           // if # is first thing on line ++LOC
  140.          }
  141.         case '/':               // Handle the coming comment.
  142.          if (quote==OFF){   // If not in a quote.
  143.            if (*(chr+1)=='/'){// Single line comment.
  144.              ++NUMC;          // Count as a comment line.
  145.              *(chr+1)='\n';   // Force end of line.
  146.            }
  147.            else if (*(chr+1)=='*'){
  148.              CMode=1;         // Change to multiline comment mode.
  149.            }
  150.          }
  151.          break;
  152.         case ':':
  153.          if (quote==OFF && brkt==0){     // If not in quotes
  154.            if (PrevChr==':'){ // And a :: then it's a method define.
  155.              ++OMTHD;         // Incr # methods in object.
  156.            }
  157.          }
  158.          break;
  159.       }
  160.     }
  161.     if (CMode){                 // If in a multi-line comment.
  162.       ++NUMC;                   // Count this as a comment line.
  163.       if (NULL!=strstr(chr,"*/")){
  164.         chr=strstr(chr,"*/");   // Find possible end of comment.
  165.         CMode=0;                // End multi-line comment processing.
  166.       }
  167.       else {                    // Goto end of line.
  168.         *(chr+1)='\n';          // Force end of line.
  169.       }
  170.     }
  171.     if (0==strchr("\n\r\t ",*chr)){
  172.       PrevChr=*chr;             // PrevChr = last non-whitespace character
  173.       if (!GotFirst){
  174.         FirstChr=PrevChr;
  175.         GotFirst=1;
  176.       }
  177.     }
  178.     ++chr;                      // increment character pointer.
  179.       }
  180.     }
  181.  
  182.                   // Add to the sum of object lines of code.
  183.     OLOC+=LOC;
  184.     ONUMC+=NUMC;
  185.  
  186.     if (Error || PDepth>0 || brkt>0 ){
  187.       printf("An error was detected in your source code.\n");
  188.       printf("  You may want to ensure that it compiles properly.\n");
  189.     }
  190.  
  191.     fclose(input);
  192.     Next();
  193.  
  194.   } while(AtEnd == FALSE);
  195.   return(OLOC);
  196. }
  197.  
  198.  
  199. void CObjectInfo::Print_OLOC(FILE *out)
  200. {
  201.  
  202.   fprintf(out,"%-15s OMethods=%3d OLOC=%6d ONUMC=%6d NUMC/OLOC=%f\n",
  203.           ObjectName,
  204.           OMTHD,
  205.       OLOC,
  206.           ONUMC,
  207.           (float)ONUMC/(float)OLOC);
  208. }
  209.  
  210. char *CObjectInfo::WhatIsYourName(void)
  211. {
  212.   return ObjectName;
  213. }
  214.  
  215. void CObjectInfo::YourNameIs(char *name)
  216. {
  217.   strcpy(ObjectName,name);
  218. }
  219.  
  220.