home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / autopsp.zip / LOC / LOC4CPP.CPP < prev    next >
C/C++ Source or Header  |  1994-01-24  |  7KB  |  276 lines

  1. /* MAIN object*/
  2. // This program counts the lines of code in a project by reading
  3. //   each source file in a directory, determining which object
  4. //   that files contains a part of and then totaling all the object
  5. //   lines of code, reporting them, and from that finding the
  6. //   total lines of code and reporting it.
  7.  
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <dos.h>
  11.  
  12. /* ARG!!! Hack Hack Hack!!!*/
  13. char ReadDir[80];
  14.  
  15. #include "e:\pspsrc\llist.h"
  16. #include "oi.h"
  17.  
  18. int  FoundObjectInList(LList *ObjL, char *name);
  19. void BuildList(LList *ObjL);
  20. int  CountList(LList *ObjL);
  21. void PrintList(LList *ObjL, int TLOC);
  22.  
  23. LList       ObjectList;         // List of Objects
  24. void main(int argc,char *argv[])
  25. {
  26.   int         TLOC=0;
  27.  
  28.   if (argc==0){
  29.     printf("Please supply the directory to scan as a parameter.\n");
  30.     exit (0);
  31.   }
  32.  
  33.   strcpy(ReadDir,argv[1]);        // Just slip the parm in here.
  34.   if (strlen(ReadDir)==0){
  35.     FILE *out;
  36.     if(NULL==(out=fopen("data2.dat","wt"))){
  37.       printf("Error:  Can't write results.");
  38.       exit(1);
  39.     }
  40.     fprintf(out,"Please supply a source directory.\n");
  41.     fclose(out);
  42.     exit(1);
  43.   }
  44.  
  45.                   // Scan all files in said directory,
  46.                   //   Everything with the proper extension
  47.                   //   is assigned to an object.
  48.   BuildList(&ObjectList);
  49.  
  50.                   // Go through all objects and inform
  51.                   //   them that they should count their
  52.                   //   OLOC.  Returns total counted.
  53.   TLOC=CountList(&ObjectList);
  54.  
  55.                   // Have each object print it's name and count,
  56.                   //   and then prints the total.
  57.   PrintList(&ObjectList, TLOC);
  58. }
  59.  
  60. int  FoundObjectInList(LList *ObjL, char *name)
  61. {
  62.   CObjectInfo *temp;              // Pointer to an Object Info class
  63.  
  64.   if (ObjL->NumNodes==0){
  65.     return(0);
  66.   }
  67.  
  68.   ObjL->Restart();                    // Start at start of list.
  69.   do{
  70.     temp=(CObjectInfo*)ObjL->Retrieve(LL_DATA);
  71.     if (0==strcmp(temp->WhatIsYourName(),name)){
  72.       return(1);
  73.     }
  74.     ObjL->Next();
  75.                   // Check objects until out of objects
  76.                   // or name is found.
  77.   } while (ObjL->AtEnd == FALSE);
  78.   return(0);
  79. }
  80.  
  81. void BuildList(LList *ObjL)
  82. {
  83.   char   dirtemp[60];
  84.   char   dirname[60];
  85.   char   opnfile[60];
  86.   char   tempfile[60];
  87.   int    readtype=0;
  88.  
  89. //  DIR    *dir;
  90. //  struct dirent *ent;
  91.   struct find_t ffblk;
  92.  
  93.   FILE   *srcfile;
  94.   char   *ext;
  95.   char   *name;
  96.   char   *endname;
  97.  
  98.   char   Object[35];
  99.   char   line[80];
  100.   char   fullfile[60];
  101.   int    done;
  102.   int    flag=0;
  103.  
  104.   char   cmntchrs[55]="\t\n /*`~!@#$%^&*()[]{}\|;':<?>,.";
  105.  
  106.   CObjectInfo *temp;        // Pointer to an Object Info class
  107.  
  108. /*
  109.   if(-1==getcurdir(0,dirtemp)){
  110.     printf("Error reading the current directory.\n");
  111.   }
  112.   sprintf(dirname,"\\%s",dirtemp);
  113.  
  114. //  strcpy(dirname,ReadDir);        // Slid the parm in here.
  115.  
  116.   if ((dir = opendir(dirname)) == NULL)  {
  117.     perror("Unable to access directory!\n");
  118.     exit(1);
  119.   }
  120. */
  121.  
  122.   strcpy(dirname,ReadDir);        // Slid the parm in here.
  123.   strcat(dirname,"\\*.h");
  124.  
  125.   done = _dos_findfirst(dirname,_A_NORMAL,&ffblk);
  126.   while (readtype<4){
  127.     while ((done!=0)&&(flag==0)){
  128.       readtype++;
  129.       if (readtype==1){
  130.     strcpy(dirname,ReadDir);        // Slid the parm in here.
  131.     strcat(dirname,"\\*.hpp");
  132.     done = _dos_findfirst(dirname,_A_NORMAL,&ffblk);
  133.       }
  134.       if (readtype==2){
  135.     strcpy(dirname,ReadDir);        // Slid the parm in here.
  136.     strcat(dirname,"\\*.c");
  137.     done = _dos_findfirst(dirname,_A_NORMAL,&ffblk);
  138.       }
  139.       if (readtype==3){
  140.     strcpy(dirname,ReadDir);        // Slid the parm in here.
  141.     strcat(dirname,"\\*.cpp");
  142.     done = _dos_findfirst(dirname,_A_NORMAL,&ffblk);
  143.       }
  144.       if (readtype==4) flag=1;
  145.     }
  146.  
  147.     if (done==0){
  148.       strupr(ffblk.name);
  149.       strcpy(tempfile,ffblk.name);
  150.  
  151.       ext=tempfile;
  152.       if (ext!=NULL){
  153.     ext=strchr( ext,'.');
  154.       }
  155.     }
  156.     else {
  157.       ext=NULL;
  158.     }
  159.  
  160.     if( ext != NULL &&
  161.        (0==strcmp(ext,".C")   ||
  162.     0==strcmp(ext,".CPP") ||
  163.     0==strcmp(ext,".H")   ||
  164.     0==strcmp(ext,".HPP") )){
  165.  
  166.       strcpy(opnfile,ReadDir);
  167.       strcat(opnfile,"\\");
  168.       strcat(opnfile,ffblk.name);
  169.  
  170.  
  171.       if(NULL==(srcfile=fopen(opnfile,"rt"))){
  172.     printf("Unable to open file %s.",opnfile);
  173.       }
  174.       else{
  175.  
  176.     name=NULL;
  177.     while (name==NULL && !feof(srcfile)){
  178.       fgets(line,80,srcfile);
  179.       name=line;
  180.  
  181. //          what=strcspn(line,"\t\n /*");// Points to first non-comment character
  182.       while (NULL!=strchr(cmntchrs,*name) && name!=NULL){
  183.         name++;
  184.       }
  185.     }
  186.  
  187.     if (name!=NULL){
  188.       endname=name;
  189.       while (NULL==strchr(cmntchrs,*endname) && endname!=NULL){
  190.         endname++;
  191.       }
  192.  
  193.       *endname=NULL;          // Null terminate the name.
  194.  
  195.       strcpy(Object,name);    // Copy the name into Object.
  196.  
  197.                   // Check if there is already an object,
  198.                   //   of this name...
  199.  
  200.                   // Found Object returns 1 if the object is
  201.                   //   in the list and 0 if not.  The list
  202.                   //   cur pointer is left pointing to the
  203.                   //   correct object.
  204.       if(1==FoundObjectInList(ObjL, name)){
  205.                   // Add this file name to the object
  206.         temp=(CObjectInfo*)ObjL->Retrieve(LL_DATA);
  207.         strcpy(fullfile,ReadDir);
  208.         strcat(fullfile,"\\");
  209.         strcat(fullfile,ffblk.name);
  210.         temp->AddFile(fullfile);
  211.       }
  212.       else{
  213.                   // Create this object & add the file name.
  214.         ObjL->Add(new CObjectInfo,LL_ADD_AFTER);
  215.         ObjL->Next();
  216.         temp=(CObjectInfo*)ObjL->Retrieve(LL_DATA);
  217.  
  218.         strcpy(fullfile,ReadDir);
  219.         strcat(fullfile,"\\");
  220.         strcat(fullfile,ffblk.name);
  221.         temp->AddFile(fullfile);
  222.         temp->YourNameIs(Object);
  223.       }
  224.     }
  225.     fclose(srcfile);
  226. //        printf("%15s holds object %s...\n",ent->d_name,name);
  227.       }
  228.     }
  229.     if (readtype != 4){
  230.       done = _dos_findnext(&ffblk);
  231.     }
  232.   }
  233. }
  234.  
  235. int  CountList(LList *ObjL)
  236. {
  237.   CObjectInfo *temp;              // Pointer to an Object Info class
  238.   int TLOC=0;
  239.  
  240.   ObjectList.Restart();                    // Start at start of list.
  241.   do{
  242.     temp=(CObjectInfo*)ObjectList.Retrieve(LL_DATA);
  243.  
  244.     TLOC += temp->Count_OLOC();
  245.  
  246.     ObjectList.Next();
  247.                   // Count objects until out of objects
  248.   } while (ObjectList.AtEnd == FALSE);
  249.  
  250.   return(TLOC);                   // Return the total.
  251. }
  252.  
  253. void PrintList(LList *ObjL, int TLOC)
  254. {
  255.   CObjectInfo *temp;              // Pointer to an Object Info class
  256.   FILE *out;
  257.  
  258.   if(NULL==(out=fopen("data2.dat","wt"))){
  259.     printf("Error:  Can't write results.");
  260.     exit(1);
  261.   }
  262.  
  263.   ObjL->Restart();                    // Start at start of list.
  264.   do{
  265.     temp=(CObjectInfo*)ObjL->Retrieve(LL_DATA);
  266.  
  267.     temp->Print_OLOC(out);
  268.  
  269.     ObjL->Next();
  270.                   // Count objects until out of objects
  271.   } while (ObjL->AtEnd == FALSE);
  272.  
  273.   fprintf(out,"\nTotal LOC:     %d\n",TLOC);
  274.   fclose(out);
  275. }
  276.