home *** CD-ROM | disk | FTP | other *** search
/ ARM Club 3 / TheARMClub_PDCD3.iso / hensa / misc / b186_1 / Source / c / colex < prev    next >
Text File  |  1987-09-27  |  2KB  |  110 lines

  1. /*
  2.  
  3.        This file is part of the PDP software package.
  4.          
  5.        Copyright 1987 by James L. McClelland and David E. Rumelhart.
  6.        
  7.        Please refer to licensing information in the file license.txt,
  8.        which is in the same directory with this source file and is
  9.        included here by reference.
  10. */
  11.  
  12.  
  13. /* file: colex.c
  14.  
  15.     Simple program that extracts columns from files consisting of
  16.     lines each with many columns.  
  17.     
  18.     First version implemented by JLM - 5-16-87
  19.     
  20.     Date of last revision 8-12-87.
  21.  
  22. */    
  23.  
  24.  
  25. #include <stdio.h>
  26. #include <ctype.h>
  27. FILE *in, *out, *abut;
  28. int outcol[10];
  29. char inline[BUFSIZ];
  30. char outstr[BUFSIZ];
  31.  
  32.  
  33. nextcol(count) int count; {
  34.     int i;
  35.     char *lp = inline;
  36.     char *sp = outstr;
  37.     
  38.     while(*lp && isspace(*lp)) *lp++;
  39.     for (i = 0; i < count; i++) {
  40.         while(*lp && !isspace(*lp)) *lp++;
  41.         while(*lp &&  isspace(*lp)) *lp++;
  42.     }
  43.     if (*lp == '\0') return (0);
  44.     while(*lp && !isspace(*lp)) *sp++ = *lp++;
  45.     *sp = '\0';
  46.     return(1);
  47. }
  48.     
  49. main(argc,argv) int argc; char **argv; {
  50.     int arg;
  51.     char *lp;
  52.     int oc,noc,len,t;
  53.  
  54.     arg = 3;
  55.  
  56.     if ( (in = fopen(argv[1],"r")) == NULL) {
  57.         fprintf(stderr,"Cannot open input file.\n");
  58.         exit(1);
  59.     }
  60.     if ( (out = fopen(argv[2],"w")) == NULL) {
  61.         fprintf(stderr,"Cannot open output file.\n");
  62.         exit(1);
  63.     }
  64.     if (argc > 3 && sscanf(argv[3],"%d",&t) ==0) {
  65.          if ( (abut = fopen(argv[arg++],"r")) == NULL) {
  66.         fprintf(stderr,"Cannot open file to abut to.\n");
  67.         exit(1);
  68.          }
  69.     }
  70.  
  71.     for (oc = 0; arg < argc && oc < 10; arg++,oc++) {
  72.         outcol[oc] = atoi(argv[arg]);
  73.     }
  74.     noc = oc;
  75.     if (abut) {
  76.         while (fgets(inline,BUFSIZ,abut) != NULL) {
  77.         len = strlen(inline);
  78.         inline[len-1] = '\0';
  79.         fprintf(out,"%s ",inline);
  80.         if (fgets(inline,BUFSIZ,in) != NULL) {
  81.           for (oc = 0; oc < noc; oc++) {
  82.            if (nextcol(outcol[oc]) == NULL) {
  83.             fprintf(stderr,"Not enough columns.\n");
  84.             exit(1);
  85.            }
  86.            fprintf(out,"%s ",outstr);
  87.           }
  88.         }
  89.             else {
  90.           for(oc = 0; oc < noc; oc++) {
  91.            fprintf(out,"* ");
  92.           }
  93.         }
  94.         fprintf(out,"\n");
  95.         }
  96.     }
  97.     else {
  98.         while (fgets(inline,BUFSIZ,in) != NULL) {
  99.         for (oc = 0; oc < noc; oc++) {
  100.            if (nextcol(outcol[oc]) == NULL) {
  101.             fprintf(stderr,"Not enough columns.\n");
  102.             exit(1);
  103.            }
  104.            fprintf(out,"%s ",outstr);
  105.         }
  106.         fprintf(out,"\n");
  107.         }
  108.     }
  109. }
  110.