home *** CD-ROM | disk | FTP | other *** search
/ Boldly Go Collection / version40.iso / TS / 05B / PPSTOCK4.ZIP / PPSTOCK.C < prev    next >
Encoding:
C/C++ Source or Header  |  1992-02-24  |  4.1 KB  |  134 lines

  1. /*  PPSTOCK v1.1  by Jim Robeson    copyright (c) 1992
  2.  *  Read the file found in (arg-1),
  3.  *     insert comma-delimited values into the data,
  4.  *  then copy whatever to the file in (arg-2).
  5.  */
  6.  
  7. #include <stdio.h>
  8. #include <string.h>
  9. #define TRUE 1
  10. #define FALSE 0
  11. #define FLDCNT 7
  12.  
  13.   FILE *infile;                    /* the IN file */
  14.   FILE *outfile;                   /* the OUT file */
  15.   char inbuf[BUFSIZ];              /* line buffer for reading from file */
  16.   char outbuf[BUFSIZ];             /* line buffer for output            */
  17.                                    /* BUFSIZ = 512 in stdio.h */
  18. static char vartab[2][FLDCNT] = {
  19.    {25,  8, 10, 10,  9,  8,  7},                        /* length    */
  20.    {2,   1,  1,  1,  1,  1,  2},                        /* char/numr */
  21.                                                         /*  2    1   */
  22.  };
  23. /* ------------------------------------------------------------------ */
  24. main(int argc, char *argv[])              /* main reads command args  */
  25. {
  26.   char *progname;
  27.   char *filein;
  28.   char *fileout;
  29.   int linecnt;
  30.   int ipt,opt;            /* index pointers */
  31.   int fldi, len, frm;
  32.   int first_time;
  33.   int i;
  34.  
  35. /* ------------------------------------------------------------------ */
  36. /*  Display a little how-to "help" if arguments are null or improper  */
  37.  
  38.   if (argc != 3)                /*  should be 2 args */
  39.     {
  40.     printf("\nPPSTOCK v1.1:  A File Copy Program,");
  41.     printf("  by Jim Robeson,  compiled 2-24-92.\n\n");
  42.     printf("This program copies one file to another, inserting\n");
  43.     printf("comma-delimited controls between selected fields.\n");
  44.     printf("File names must be entered on the command line:\n\n");
  45.     printf("PPSTOCK drv:\path\in-file-name drv:\path\out-file-name \n\n");
  46.     exit (1);                              /* exit with errorlevel=1 */
  47.     }
  48.  
  49.   progname = argv[0];
  50.   filein   = argv[1];
  51.   fileout  = argv[2];
  52.  
  53. /* ------------------------------------------------------------------ */
  54.   infile = fopen(filein,"r");          /* open the input file */
  55.   if (infile == NULL)
  56.     {
  57.     fprintf(stderr,"\n\007%s can't open the INPUT file: %s.\n",progname,filein);
  58.     exit (1);
  59.     }
  60.  
  61.   outfile = fopen(fileout,"w");          /* open the output file */
  62.   if (outfile == NULL)
  63.     {
  64.     fprintf(stderr,"\n\007%s can't open the OUTPUT file: %s.\n",progname,fileout);
  65.     exit (1);
  66.     }
  67.  
  68. /* ------------------------------------------------------------------ */
  69. linecnt = 0;
  70. printf ("%4d records processed.\r",linecnt);
  71. while ( fgets(inbuf,BUFSIZ,infile) != NULL )  /* read it */
  72.  { /* READ PROCESSING */
  73.  
  74.    first_time = 0;
  75.    ipt=0;
  76.    opt=0;
  77.  
  78.    for (fldi = 1; fldi <= FLDCNT; fldi++)
  79.     { /* FIELD PROCESSING */
  80.       len = vartab[0][fldi-1];
  81.       frm = vartab[1][fldi-1];
  82.  
  83.       /* FOR THE OUTPUT SIDE: */
  84.       if (first_time == 1)           /* 1 = all but the first */
  85.           { outbuf[opt] = ',';       /* put trailer ',' first */
  86.             opt++;
  87.             if (frm == 2) { outbuf[opt] = '"';
  88.                             opt++;
  89.                           }
  90.           }
  91.       else
  92.           { first_time = 1;        /* only one time, the first. */
  93.             if (frm == 2) { outbuf[opt] = '"';
  94.                             opt++;
  95.                           }
  96.           }
  97.  
  98.       for (i=0;   (i < len) && !( (inbuf[ipt] == '\0') || (inbuf[ipt] == '\n') )  ; i++)
  99.          { outbuf[opt] = inbuf[ipt];
  100.            opt++;
  101.            ipt++;
  102.          }
  103.       if (frm == 2) { outbuf[opt] = '"';
  104.                       opt++;
  105.                     }
  106.       if ( inbuf[ipt] == '\0' ) ipt = ipt - 1;
  107.  
  108.     } /* FIELD PROCESSING loop */
  109.  
  110.  here:
  111.  
  112.     outbuf[opt] = '\n';
  113.     opt++;
  114.     outbuf[opt] = '\0';
  115.  
  116.     linecnt++;
  117.     fprintf(outfile,"%s",outbuf);
  118.     printf ("%4d\r",linecnt);
  119.  
  120.  
  121.  } /* READ PROCESSING loop */
  122.  
  123. /* ------------------------------------------------------------------ */
  124.  
  125.   fclose(infile);
  126.   fclose(outfile);
  127.  
  128.   printf ("%4d\n",linecnt);
  129.  
  130.   exit (0);                      /* job done, get out, errorvalue = 0 */
  131. }
  132.  
  133. /*------------- End of PPSTOCK.C ------------------------------------*/
  134.