home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / SQZH102.ZIP / sqzh.c < prev    next >
C/C++ Source or Header  |  1993-01-05  |  5KB  |  220 lines

  1. /* Squeezes header files */
  2. /* Written Geo 18-Dec-90 */
  3. /* OS/2 version 1.0 Nick Bethmann, 22 Apr 92 */
  4. /* OS/2 version 1.01 Nick Bethmann, 6 Jun 92, updated command line */
  5. /* FAPI version 1.02 Nick Bethmann, 6 Jan 93, char immediately after '/' was dupped! */
  6.  
  7. #define INCL_DOSFILEMGR
  8. #include <os2.h>
  9.  
  10. #include <stdio.h>
  11. #include <ctype.h>
  12. #include <fcntl.h>
  13. #include <io.h>
  14. #include <share.h>
  15. #include <sys\types.h>
  16. #include <sys\stat.h>
  17. #include <direct.h>
  18. #include <string.h>
  19.  
  20. #include "token.h"
  21.  
  22. char basedir[CCHMAXPATH];
  23. char targetdir[CCHMAXPATH];
  24. char curdir[CCHMAXPATH];
  25.  
  26.  
  27. BOOL NEAR process( char *infile,
  28.                    char *outfile)
  29. {
  30. int         fi,fo;
  31. unsigned    bytes;
  32. PFBUFFER    ib,ob;
  33. FILESTATUS  info1;
  34. FILESTATUS  info2;
  35.  
  36.   fi = sopen( infile, O_BINARY | O_RDONLY, SH_DENYWR );
  37.   if ( fi == -1) {
  38.     printf( "ERROR: failed to OPEN %s\n", infile);
  39.     return( FALSE );
  40.   }
  41.  
  42.   fo = sopen( outfile, O_BINARY | O_RDWR, SH_DENYWR );
  43.   if (fo == -1) {
  44.     fo = sopen( outfile, O_BINARY | O_RDWR | O_CREAT | O_TRUNC, SH_DENYWR,
  45.                 S_IREAD | S_IWRITE);
  46.     if (fo == -1) {
  47.       DosClose(fi);
  48.       printf( "ERROR: failed to CREATE %s\n", outfile);
  49.       return( FALSE );
  50.     }
  51.   }
  52.  
  53.         /* don't squeeze if file already squeezed */
  54.   DosQFileInfo((HFILE)fi, 1, (PBYTE)&info1, sizeof(FILESTATUS));
  55.   DosQFileInfo((HFILE)fo, 1, (PBYTE)&info2, sizeof(FILESTATUS));
  56.  
  57.         /* NOTE: only hpfs stores creation, so... don't check */
  58.   if ( memcmp( &info1.fdateLastWrite,
  59.                &info2.fdateLastWrite, sizeof(FDATE) + sizeof(FTIME)) != 0 ||
  60.        info1.cbFile == info2.cbFile ) {
  61.     DosWrite( 1, ".", 1, &bytes );
  62.     DosNewSize( (HFILE)fo, (ULONG)0 );
  63.     ib = createLex( fi );
  64.     ob = createLex( fo );
  65.  
  66.     processFiles( ib, ob );
  67.  
  68.        /* stamp date time of file */
  69.     DosBufReset( (HFILE)fo );
  70.     DosQFileInfo((HFILE)fo, 1, (PBYTE)&info2, sizeof(FILESTATUS));
  71.     info1.cbFile      = info2.cbFile;
  72.     info1.cbFileAlloc = info2.cbFileAlloc;
  73.     DosSetFileInfo((HFILE)fo, 1, (PBYTE)&info1, sizeof(FILESTATUS));
  74.  
  75.     closeLex(ob);
  76.     closeLex(ib);
  77.     return( TRUE );
  78.   }else {
  79.     DosClose(fi);
  80.     DosClose(fo);
  81.     DosWrite( 1, "-", 1, &bytes );
  82.     return( FALSE );
  83.   }
  84.  
  85. }  /* process */
  86.  
  87.  
  88. void addSlash( char *path )
  89. {
  90. unsigned len = strlen(path);
  91.  
  92.   if ( len > 0 && path[len-1] != '\\' ) {
  93.     path[len] = '\\';
  94.     path[len+1] = 0;
  95.   }
  96.  
  97. }  /* addSlash */
  98.  
  99.  
  100. void removeWild( char *to,
  101.                  char *from )
  102. {
  103. unsigned len = strlen(from);
  104.  
  105.   while ( from[len] != '*' && from[len] != '?' ) len--;
  106.   while ( len > 0 && from[len] != '\\' && from[len] != ':' ) len--;
  107.  
  108.   if ( len == 0 )
  109.     to[0] = 0;
  110.   else {
  111.     memcpy( to, from, len+1 );
  112.     to[len+1] = 0;
  113.   }
  114.  
  115. }  /* removeWild */
  116.  
  117.  
  118. void dirs(char *dirName)
  119. {
  120.  unsigned     searchCount = 1;
  121.  HDIR         hDir = HDIR_CREATE;
  122.  FILEFINDBUF  fb;
  123.  char         wildpath[CCHMAXPATH];
  124.  char         basepath[CCHMAXPATH];
  125.  unsigned     count = 0;
  126.  
  127.   if ( strchr(basedir, '*') != NULL || strchr(basedir, '?') != NULL ) {
  128.     strcpy(wildpath, basedir);
  129.     removeWild( basepath, basedir );
  130.     strcat( basepath, curdir ); 
  131.   }else {
  132.     addSlash( basedir );
  133.     strcpy(basepath, basedir);
  134.     strcpy(wildpath, basedir);
  135.     strcat( wildpath, dirName );
  136.     strcat( wildpath, "\\*.h");
  137.   }
  138.  
  139.  if ( DosFindFirst( wildpath, &hDir, FILE_NORMAL,
  140.                     &fb, sizeof(fb), &searchCount, 0L) == 0 )
  141.  do {
  142.     if (fb.achName[0] == '.')
  143.       continue;
  144.     if (fb.attrFile & FILE_DIRECTORY)
  145.     {
  146.      char *ptr = curdir;
  147.  
  148.       while (*ptr)
  149.         ptr++;
  150.  
  151.       strcat(ptr, "\\");
  152.       strcat(ptr, fb.achName);
  153.       {
  154.       static char dirname[CCHMAXPATH];
  155.  
  156.         sprintf(dirname, "%s%s", targetdir, curdir);
  157.         mkdir(dirname);
  158.       }
  159.       
  160.       dirs(fb.achName);
  161.       *ptr = '\0';
  162.     }
  163.     else
  164.     {
  165.       static char infile[CCHMAXPATH], outfile[CCHMAXPATH];
  166.  
  167.       strcpy( infile, basepath );
  168.       strcat( infile, fb.achName );     
  169.  
  170.       strcpy( outfile, targetdir );
  171.       strcat( outfile, curdir );
  172.       strcat( outfile, fb.achName );
  173.  
  174.       if ( process(infile, outfile) )
  175.         count++;
  176.     }
  177.   }while ( DosFindNext(hDir, &fb, sizeof(fb), &searchCount) == 0 );
  178.  
  179.  DosFindClose(hDir);
  180.  printf( "%u\n", count );
  181.  
  182. } /* dirs */
  183.  
  184.  
  185. int _cdecl main( int argc,
  186.                  char *argv[] )
  187. {
  188. int   i;
  189. char *ptr;
  190.  
  191.   if (argc < 3 ) {
  192.     printf("SqzH, v1.02\nInsufficient args.\nusage : sqzh <target dir> <source dir>... \n");
  193.     return(2);
  194.   }
  195.  
  196.   strcpy(targetdir, argv[1]);
  197.   mkdir(targetdir);
  198.   addSlash( targetdir );
  199.      /* convert all forward slashes to back slashes */
  200.   for (ptr = targetdir; *ptr; ptr++)
  201.     if (*ptr == '/')
  202.       *ptr = '\\';
  203.  
  204.   for ( i=2; i<argc; i++ ) {
  205.     strcpy(basedir, argv[i]);
  206.     for (ptr = basedir; *ptr; ptr++)
  207.       if (*ptr == '/')
  208.         *ptr = '\\';
  209.  
  210.     curdir[0] = '\0';
  211.     dirs(curdir);
  212.   }
  213.  
  214. }  /* main */
  215.  
  216.  
  217.  
  218.  
  219.  
  220.