home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: Java / Java.zip / cind120.zip / CIND.C next >
C/C++ Source or Header  |  1997-02-25  |  7KB  |  259 lines

  1. /*  File: CIND.C   Updated: 25-Feb-1997 14:06
  2. Copyright (c) Fabrizio Aversa
  3. ===========================================================*/
  4. /****************************************************************/
  5. /**                                                            **/
  6. /**                                                            **/
  7. /**          JAVA, C, C++ indention program                    **/
  8. /**                                                            **/
  9. /**                        Fabrizio Aversa                     **/
  10. /**                                                            **/
  11. /**          First release :   30/08/89                        **/
  12. /**          Latest Update :   26/02/97                        **/
  13. /**                                                            **/
  14. /**               Version 1.2                                  **/
  15. /**                                                            **/
  16. /****************************************************************/
  17.  
  18. #define INCL_DOS
  19. #include <os2.h>
  20.  
  21. #include<stdio.h>
  22. #include<stdlib.h>
  23. #include<stdarg.h>
  24. #include<string.h>
  25. #include<time.h>
  26.  
  27. #define MAX_LINE_LEN 200
  28.  
  29. void TimeString1(PSZ);
  30. INT getNextFile(HDIR*, PSZ, PSZ, INT*);
  31.  
  32. void main(int argc, char * argv[])
  33. {
  34.    int  indent= 0 ;
  35.    int i, n, sw= 0;
  36.    long iStatements= 0 ;
  37.    char filename[90], source_name[90], out_name[90], path[90], ch ;
  38.    char ext[90], *pp, act[MAX_LINE_LEN], temps[90] ;
  39.    FILE *source, *out ;
  40.    INT iFileCount = 0;
  41.    HDIR FindHandle;
  42.  
  43.    printf("\nC Indent Utility CIND  ver. 1.2\n") ;
  44.    printf("Copyright (c)  Fabrizio Aversa  1989-97\n") ;
  45.  
  46.    if (argc != 2) {
  47.       printf ("file mask (e.g. *.java) : ");
  48.       fflush(stdout);
  49.       scanf("%s",source_name);
  50.    }
  51.    else
  52.    {
  53.       strcpy(source_name,argv[1]);
  54.    }
  55.  
  56.    printf ("\n");
  57.  
  58.    /* see if source name contains path info */
  59.    *path = 0;
  60.    for (i = strlen(source_name); i >= 0 ; i--) {
  61.       if(*(source_name + i) == '\\') {
  62.          strncpy(path, source_name, i+1);
  63.          break;
  64.       }
  65.    }
  66.  
  67.    while(getNextFile(&FindHandle, source_name, filename, &iFileCount))  {
  68.  
  69.       if ( (pp= strchr(filename,'.')) == NULL) {
  70.          strcpy(ext,".cpp");
  71.       }
  72.       else
  73.       {
  74.          strcpy(ext,pp);
  75.          *pp = '\0' ;
  76.       }
  77.  
  78.       strcpy(out_name,path);
  79.       strcpy(source_name,path);
  80.  
  81.       strcat(out_name,filename);
  82.       strcat(source_name,filename);
  83.  
  84.       strcat(source_name,ext);
  85.       strcat(out_name,".$$$");
  86.  
  87.       printf("%s%s: ", filename, ext);
  88.  
  89.       if ( (source = fopen(source_name,"rb")) == NULL )
  90.       {
  91.          strcpy(filename,"File open error : ");
  92.          strcat(filename,source_name);
  93.          printf ("%s\n\n",filename);
  94.          exit(1);
  95.       }
  96.  
  97.       if ( (out = fopen(out_name,"wb")) == NULL )
  98.       {
  99.          strcpy(filename,"File open error : ");
  100.          strcat(filename,out_name);
  101.          printf ("%s\n\n",filename);
  102.          exit(1);
  103.       }
  104.  
  105.       /* look for header line: get 1 line */
  106.       i= 0;
  107.       while ( (*(act+ i++) = getc(source)) != 10
  108.       && (! feof(source)) && i <= MAX_LINE_LEN ) ;
  109.  
  110.       strcpy (temps, "/*  File:" ) ;
  111.  
  112.       if( act != strstr(act, temps) ) {
  113.          fseek(source, (long)0, SEEK_SET);
  114.       } else {
  115.          /* ignore 2 lines */
  116.          i= 0;
  117.          while ( (*(act+ i++) = getc(source)) != 10
  118.          && (! feof(source)) && i <= MAX_LINE_LEN ) ;
  119.          i= 0;
  120.          while ( (*(act+ i++) = getc(source)) != 10
  121.          && (! feof(source)) && i <= MAX_LINE_LEN ) ;
  122.       }
  123.       /* print header lines */
  124.       TimeString1(act);
  125.       fprintf(out,
  126.       "/*  File: %s%s   Updated: %s\r\nCopyright (c) Fabrizio Aversa\r\n===========================================================*/\r\n"
  127.       ,filename, ext, act);
  128.  
  129.       do { /* scan file until end-of-it */
  130.  
  131.          /* get 1 line */
  132.          i= 0;
  133.          while ( (*(act+ i++) = getc(source)) != 10
  134.          && (! feof(source)) && i <= MAX_LINE_LEN ) ;
  135.  
  136.          if ( i >= MAX_LINE_LEN ) {
  137.             printf ("Line termination not found \n\n");
  138.             exit(1);
  139.          }
  140.  
  141.          /* scan line to update indent value */
  142.          i= 0;
  143.          while ( (ch= *(act+ i++)) != 10) {
  144.  
  145.             /* count semicolons */
  146.          if(ch ==';' || ch =='{') iStatements++;    /* } only for indent !! */
  147.  
  148.             switch (ch) {
  149.                case '{' : sw++ ;
  150.                   continue;
  151.                case '}' : indent -= 3;
  152.             }
  153.          }
  154.  
  155.          /* ignore leading blanks */
  156.          i= 0;
  157.          while ( (*(act+ i)) == ' ' ||  (*(act+ i)) == '\t') i++;
  158.  
  159.          /* indent */
  160.          if ( act[i] != '#' ) {
  161.             n= indent;
  162.             while ( (n--) > 0) putc(' ',out);
  163.          }
  164.          if (sw) {
  165.             indent += sw*3;
  166.             sw= 0 ;
  167.          }
  168.  
  169.          /* print line */
  170.          if ( ! feof(source)) {
  171.             do {
  172.                ch=  *(act+ i++);
  173.                putc(ch, out);
  174.             } while ( ch != 10 );
  175.          }
  176.       } while ( ! feof(source)) ;
  177.  
  178.       fclose(out);
  179.       fclose(source);
  180.  
  181.       remove (source_name) ;
  182.       rename (out_name,source_name) ;
  183.  
  184.       printf("approx. %ld statements found.\n", iStatements) ;
  185.  
  186.    }
  187.  
  188.    printf ("\n");
  189.  
  190. }
  191.  
  192. /*===================================================
  193. Time String
  194. ===================================================  */
  195. void TimeString1(PSZ str)
  196. {
  197.    int i;
  198.    char str1[25];
  199.  
  200.    time_t lt;
  201.    lt=time(NULL);
  202.    for (i=0; i< 24; i++) str1[i] = *(asctime(localtime(<))+i);
  203.  
  204.    memcpy(str, str1+8, 2); /* day */
  205.    str[2]='-';
  206.    memcpy(str+3, str1+4, 3); /* month */
  207.    str[6]='-';
  208.    memcpy(str+7, str1+20, 4); /* year */
  209.    str[11]=' ';
  210.    memcpy(str+12, str1+11, 5); /* time */
  211.    str[17]=0;
  212.  
  213. }
  214.  
  215. /*===================================================
  216. Directory
  217. ===================================================  */
  218. INT getNextFile(HDIR *FindHandle, PSZ pszPath, PSZ pszRet, INT * iCount)
  219. /* returns 0 if no more files */
  220. {
  221.    ULONG FindCount= 1;
  222.    FILEFINDBUF3  FindBuffer;
  223.    APIRET rc;
  224.  
  225.    if((*iCount) == 0) {
  226.  
  227.       *FindHandle= 0x0001;
  228.  
  229.       rc = DosFindFirst(pszPath,     /* File pattern */
  230.       FindHandle, /* Directory search handle */
  231.       0,     /* Search attribute */
  232.       (PVOID) &FindBuffer,   /* Result buffer */
  233.       sizeof(FindBuffer),  /* Result buffer length */
  234.       &FindCount,  /* # of entries to find */
  235.       FIL_STANDARD); /* Return level 1 file info */
  236.  
  237.    } else {
  238.  
  239.       rc = DosFindNext(*FindHandle, /* Directory handle */
  240.       (PVOID) &FindBuffer,  /* Result buffer */
  241.       sizeof(FindBuffer), /* Result buffer length */
  242.       &FindCount);        /* Number of entries to find */
  243.  
  244.    }
  245.  
  246.    if(rc) {
  247.       *pszRet= 0;
  248.       DosFindClose(*FindHandle);
  249.       return 0;
  250.    }
  251.  
  252.    (*iCount)++;
  253.    strcpy(pszRet, FindBuffer.achName);
  254.  
  255.    return 1;
  256. }
  257.  
  258.  
  259.