home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_300 / 376_01 / os2tool.000 / CUTHUGE.C < prev    next >
C/C++ Source or Header  |  1992-08-29  |  8KB  |  315 lines

  1. /*
  2. * CUTHUGE.C - Cut a huge file into smaller more manageable pieces.
  3. *
  4. * PROGRAMMER:        Martti Ylikoski
  5. * CREATED:        24.8.1992
  6. */
  7. static char *VERSION = "Version  1.0, Copyright(c) Martti Ylikoski, 1992" ;
  8. /*
  9. */
  10.  
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <string.h>
  14. #include <limits.h>
  15. #include <io.h>
  16. #include <ctype.h>
  17. #include <errno.h>
  18. #include <param.h>
  19. #include <paramstd.h>
  20. #define INCL_VIO
  21. #include <os2.h>
  22.  
  23. static char *progname ;
  24.  
  25. extern unsigned long pflags ;
  26.  
  27. ParamEntry pentry[12] = {
  28.      "P", &ParamSetPause, 0,
  29.      "F", &ParamSetFold, 0,
  30.      "V", &ParamSetVerbose, 0,
  31.      "R", &ParamSetReport, 0,
  32.      "S", &ParamSetSubDirs, 0,
  33.      "?", &ParamSetHelp, 1,
  34.      "H", &ParamSetHelp, 1,
  35.      "NOD", &ParamSetNoDefault, 0,
  36.      "TEST", &ParamSetTest, 0,
  37.      "Y", &ParamSetYes, 0,
  38.      "N", &ParamSetTest, 0,
  39.      "\0", NULL, 0
  40. } ;
  41.  
  42. ParamBlock params = {
  43.     "/-",   IGNORECASE | NOPRIORITY | NOTRIGGERSALLOWED ,
  44.     pentry
  45. } ;
  46.  
  47. static int fhnd, writecnt = 1 ;
  48. static int cols, count, stdinassumed ;
  49. #define MAXBUFLEN 512
  50.  
  51. /* local prototypes */
  52. static int cuttext (char *fname, char *base, int clines) ;
  53. FILE *newbase( char *basename, int *nameext, char *outmode) ;
  54. static int cutbin (char *cutfile, char *basename, int cutsize) ;
  55.  
  56. int main(int argc, char *argv[])
  57. {
  58. int i, nargc, binsize, binmode ;
  59. char *base ;
  60.  
  61.    base = "PART" ;
  62.    progname = argv[0] ;
  63.    count = 1000 ;        /* default for lines to include */
  64.    binsize = 32 * 1024 ;
  65.    stdinassumed = FALSE ;
  66.    binmode = FALSE ;
  67.    
  68.  
  69.    ParamHandle(¶ms, &argc, argv) ;
  70.  
  71.    if (pflags & PA_HELP)
  72.    {
  73.       fputs("CUTHUGE - cut a huge file into smaller more manageable pieces.\n", stderr) ;
  74.       fputs(VERSION, stderr) ;
  75.       fputs("\nUsage: cuthuge [file] [-lines] [/BIN] [/B basename] [/H | /?]\n", stderr) ;
  76.       fputs("Where:\n/B = give new basename (default PART)\n", stderr) ;
  77.       fputs("-lines = how many lines to include in each file or the size of each part in bytes in /BIN mode\n/H,/? = Show this help.\n", stderr) ;
  78.       return( 0 ) ;
  79.    }
  80.  
  81.    fputs("CUTHUGE - cut a huge file into smaller more manageable pieces.\n", stderr) ;
  82.    fputs(VERSION, stderr) ;
  83.  
  84.    nargc = argc ;
  85.    for (i = 1 ; i < argc ; i++)
  86.       if (argv[i][0] == '-' || argv[i][0] == '/')
  87.        {
  88.      if (strlen(argv[i]) == 1)
  89.      {
  90.         stdinassumed = TRUE ;
  91.         argv[i] = NULL ;
  92.         nargc -- ;
  93.         continue ;
  94.      }
  95.      else
  96.      if ( isdigit(argv[i][1]) != 0)
  97.      {
  98.         count = atoi(&argv[i][1]) ;
  99.         argv[i] = NULL ;
  100.         nargc -- ;
  101.      }
  102.      else
  103.      if (strcmpi("bin", &argv[i][1]) == 0)
  104.      {
  105.             binmode = TRUE ;
  106.         argv[i] = NULL ;
  107.         nargc -- ;
  108.      }
  109.      else
  110.      if (strcmpi("B", &argv[i][1]) == 0)
  111.      {
  112.         if (argc >= i+1)
  113.             {
  114.                fprintf(stderr, "%s: no base name given with /B\nExiting...\n", progname) ;
  115.                return( 1 ) ;
  116.         }
  117.         if (strlen(argv[i+1]) != 4)
  118.             {
  119.                fprintf(stderr, "%s: base name must be exatly 4 characters long\nExiting...\b", progname) ;
  120.                return( 1 ) ;
  121.         }        
  122.  
  123.             base = argv[i+1] ;
  124.         argv[i] = NULL ;
  125.         nargc -- ;
  126.         argv[i+1] = NULL ;
  127.         nargc -- ;
  128.         i++ ;
  129.      }
  130.       }
  131.  
  132.    if (binmode == TRUE && count < 1024 )
  133.    {
  134.       fputs("Minumum buffer size for /BIN-mode is 1024\nExiting...\n", stderr) ;
  135.       return( 1 ) ;
  136.    }
  137.  
  138.    if (nargc == 1)
  139.    {
  140.       fputs("\nStdin assumed\n", stderr) ;
  141.       if (binmode == FALSE)
  142.          cuttext("-", base, count) ;
  143.      else
  144.         cutbin("-", base, count) ;
  145.    }
  146.    else
  147.    {
  148.       for (i = 1 ; i < argc ; i++)
  149.      if (argv[i] != NULL)
  150.             if (binmode == FALSE)
  151.                cuttext(argv[i], base, count) ;
  152.             else
  153.               cutbin(argv[i], base, count) ;
  154.       if (stdinassumed == TRUE)
  155.          if (binmode == FALSE)
  156.             cuttext("-", base, count) ;
  157.          else
  158.            cutbin("-", base, count) ;
  159.    }
  160.  
  161.    return( 0 ) ;
  162. }
  163.  
  164.  
  165. /*****************************************
  166. *
  167. * cuttext - cut a huge text file into smaller pieces.
  168. *
  169. ******************************************/
  170.  
  171. static int cuttext (char *cutfile, char *basename, int cutlines)
  172. {
  173. FILE *cutfptr, *outfptr ;
  174. char buf[MAXBUFLEN], *line ;
  175. int lineswritten, nameext ;
  176.  
  177.    lineswritten = 0 ;
  178.    nameext = 0 ;
  179.    
  180.    /* open file */
  181.    if (strcmp(cutfile, "-") == 0)
  182.       cutfptr = stdin ;
  183.    else
  184.       if ((cutfptr = fopen(cutfile, "r")) == NULL)
  185.       {
  186.      printf("%s: unable to open file %s for reading...\n", progname, cutfile) ;
  187.      printf("Exiting...\n") ;
  188.      return( 1 ) ;
  189.       }
  190.  
  191.       if (( outfptr = newbase(basename, &nameext, "w")) == NULL)
  192.       {
  193.          fputs("Error opening new parts file...",stderr) ;
  194.          fputs("CUTHUGE terminated...", stderr) ;
  195.          fputs("Possibly temporary files left on disk", stderr) ;
  196.          fclose(cutfptr) ;
  197.          return( 1 ) ;
  198.       }
  199.    
  200.    while (( line = fgets(buf, sizeof(buf), cutfptr)) != NULL)
  201.    {
  202.       fputs(buf, outfptr) ;
  203.       lineswritten++ ;
  204.       if (lineswritten >= cutlines)
  205.       {
  206.          fclose (outfptr) ;
  207.          lineswritten = 0 ;
  208.          if (( outfptr = newbase(basename, &nameext,"w")) == NULL)
  209.          {
  210.             fputs("Error opening new parts file...", stderr) ;
  211.             fputs("CUTHUGE terminated...", stderr) ;
  212.             fputs("Possibly temporary files left on disk", stderr) ;
  213.             fclose(cutfptr) ;
  214.             return( 1 ) ;
  215.          }
  216.       }
  217.    }
  218.  
  219.    /* close file */
  220.    fclose ( cutfptr ) ;
  221. }
  222.  
  223. FILE *newbase( char *basename, int *nameext, char *outmode)
  224. {
  225. char namebuf[100] ;
  226. int i ;
  227. FILE *fptr ;
  228.  
  229.    for( i = *nameext; i <INT_MAX; i++)
  230.    {
  231.       sprintf(namebuf,"%4.4s%4.4d.CUT",basename, i) ;
  232.       if (access(namebuf, 00) != 0) /* does not exist */
  233.          break ;
  234.    }
  235.  
  236.    if (i == INT_MAX)
  237.       return ( NULL ) ;
  238.  
  239.    if ((fptr = fopen(namebuf, outmode)) == NULL)
  240.    {
  241.      return( NULL ) ;
  242.    }
  243.  
  244.    return( fptr ) ;
  245. }
  246.  
  247. /*****************************************
  248. *
  249. * cutbin - cut a huge text file into smaller pieces (binary mode).
  250. *
  251. ******************************************/
  252.  
  253. static int cutbin (char *cutfile, char *basename, int cutsize)
  254. {
  255. FILE *cutfptr, *outfptr ;
  256. char buf[1024] ;
  257. int nameext, overflow, rounds, totalrounds, readcnt ;
  258.  
  259.    rounds = 0 ;
  260.    nameext = 0 ;
  261.    overflow = cutsize % sizeof(buf) ;
  262.    totalrounds = cutsize / sizeof(buf) ;
  263.    
  264.    /* open file */
  265.    if (strcmp(cutfile, "-") == 0)
  266.       cutfptr = stdin ;
  267.    else
  268.       if ((cutfptr = fopen(cutfile, "rb")) == NULL)
  269.       {
  270.      printf("%s: unable to open file %s for reading...\n", progname, cutfile) ;
  271.      printf("Exiting...\n") ;
  272.      return( 1 ) ;
  273.       }
  274.  
  275.       if (( outfptr = newbase(basename, &nameext, "wb")) == NULL)
  276.       {
  277.          fputs("Error opening new parts file...",stderr) ;
  278.          fputs("CUTHUGE terminated...", stderr) ;
  279.          fputs("Possibly temporary files left on disk", stderr) ;
  280.          fclose(cutfptr) ;
  281.          return( 1 ) ;
  282.       }
  283.    
  284.    
  285.    while ( (readcnt = read( fileno(cutfptr), buf, sizeof(buf) )) )
  286.    {
  287.       fwrite(buf, readcnt, (size_t) 1, outfptr) ;
  288.       rounds ++ ;      
  289.       if (feof(cutfptr))
  290.          break ;
  291.  
  292.       if (rounds >= totalrounds)
  293.       {
  294.          readcnt = read(fileno(cutfptr), buf, overflow) ;
  295.          fwrite(buf, readcnt, (size_t) 1, outfptr) ;
  296.          fclose (outfptr) ;
  297.          rounds = 0 ;
  298.          if (feof(cutfptr))
  299.             break ;
  300.          if (( outfptr = newbase(basename, &nameext, "wb")) == NULL)
  301.          {
  302.             fputs("Error opening new parts file...", stderr) ;
  303.             fputs("CUTHUGE terminated...", stderr) ;
  304.             fputs("Possibly temporary files left on disk", stderr) ;
  305.             fclose(cutfptr) ;
  306.             return( 1 ) ;
  307.          }
  308.       }
  309.    }
  310.  
  311.    /* close file */
  312.    fclose ( cutfptr ) ;
  313. }
  314.  
  315.