home *** CD-ROM | disk | FTP | other *** search
/ CP/M / CPM_CDROM.iso / lambda / soundpot / p / ut-c.lbr / HCAT.CZ / HCAT.C
Encoding:
C/C++ Source or Header  |  1993-10-25  |  3.1 KB  |  131 lines

  1. /*  hcat.c -- UTOOL. Horizontally concatenate text files.
  2.  
  3.      author: David H. Wolen
  4.      last change: 3/16/83
  5.  
  6.      usage:    hcat file1 n2 file2 n3 file3 ...
  7.  
  8.                hcat file1 31 file2 51 file3 
  9.                     (begin file1 in col 1, file2 in col 31, file3 in col 51)
  10.  
  11.      input:    one or more files
  12.      output:   STDOUT
  13.  
  14.      notes:    1. max of 6 input files
  15.                2. first file starts in col 1
  16.  
  17.      linkage:  a:clink hcat -f dio -ca (uses deff3.crl)
  18. */
  19.  
  20. #include "a:bdscio.h"
  21. #include "dio.h"
  22.  
  23. #define  MAXHCFILS  6    /* max input files */
  24.  
  25. struct fs                /* file data */
  26.      {char ibuf[BUFSIZ];
  27.      char  line[MAXLINE];
  28.      int   iseof;
  29.      int   col;
  30.      };
  31.  
  32. main(argc,argv)
  33. int  argc;
  34. char *argv[];
  35. {
  36.      int  i, nfiles;
  37.      char outline[MAXLINE];
  38.      struct fs fstuff[MAXHCFILS];
  39.  
  40.      dioinit(&argc,argv);
  41.      if(argc/2 > MAXHCFILS)
  42.           error("hcat: too many input files");
  43.  
  44.      /* process arguments */
  45.  
  46.      for(i=0; i < argc/2; i++)
  47.           {if(i == 0)    /* start cols (1 for first file) */
  48.                fstuff[i].col=1;
  49.           else if((fstuff[i].col=atoi(*++argv)) <= 0)
  50.                error("hcat: bad column argument");
  51.  
  52.           if(fopen(*++argv,fstuff[i].ibuf) == ERROR)   /* files */
  53.                error("hcat: can't open file");
  54.  
  55.           fstuff[i].iseof = FALSE;
  56.           }
  57.  
  58.      nfiles=i;
  59.  
  60.      /* process */
  61.  
  62.      while(hcmore(fstuff,nfiles))       /* until all files are at eof */
  63.           {for(i=0; i < nfiles; i++)    /* get line from each file */
  64.                {if(!fstuff[i].iseof)
  65.                     if(!fgets(fstuff[i].line,fstuff[i].ibuf))
  66.                          fstuff[i].iseof=TRUE;
  67.                }
  68.  
  69.           setmem(outline,MAXLINE,' ');
  70.  
  71.           for(i=0; i < nfiles; i++)     /* form output line */
  72.                {if(!fstuff[i].iseof)
  73.                     dohc(fstuff[i].line,fstuff[i].col,outline,MAXLINE);
  74.                }
  75.  
  76.           if(hcmore(fstuff,nfiles))
  77.                {trail(outline,MAXLINE);       /* delete trailing blanks */
  78.                puts(outline);
  79.                }
  80.           }
  81.  
  82.      dioflush();
  83. }
  84.  
  85.  
  86.  
  87. /* hcmore -- return false if all input files at eof; else true */
  88. hcmore(fstuff,nfiles)
  89. struct fs fstuff[];
  90. int  nfiles;
  91. {
  92.      int  i;
  93.  
  94.      for(i=0; i < nfiles; i++)
  95.           {if(!fstuff[i].iseof)
  96.                return(TRUE);
  97.           }
  98.  
  99.      return(FALSE);
  100. }
  101.  
  102.  
  103.  
  104. /* dohc -- insert line into outline starting at col.  Won't insert
  105.      past end of outline.  */
  106. dohc(line,col,outline,maxout)
  107. char *line, *outline;
  108. int  col, maxout;
  109. {
  110.      int  i, j;
  111.  
  112.      for(i=0, j=col-1; j < (maxout-1) && line[i] != '\n'; i++, j++)
  113.           outline[j]=line[i];
  114. }
  115.  
  116.  
  117.  
  118. /* trail -- delete trailing blanks from line */
  119. trail(outline,maxout)
  120. char *outline;
  121. int  maxout;
  122. {
  123.      int  i, nfiles;
  124.  
  125.      for(i=maxout-3; i >= 0; i--)
  126.           if(outline[i] != ' ')
  127.                break;
  128.      outline[++i]='\n';
  129.      outline[++i]='\0';
  130. }
  131.