/* SAMPLE: Routine to generate sample listings of ASCII datasets Format: % sample [-r count | -e count] [] This routine generates an output listing with the file name, number of records, maximum and minimum record lengths, and sample lines with column numbers. By default the first and last 25 records are displayed. The user may specify a different number of records using the "-r" option, or can indicate that every N records should be selected by using the "-e" option. 23 Mar 1994, acr: creation */ #include #include #include #include #define MAX_RECORD_LENGTH 10000 #define DEFAULT_LINES 25 /*=========================================================================*/ main (argc,argv) int argc; char *argv[]; { char inptline[MAX_RECORD_LENGTH]; /* input record */ char *string; /* for error checking */ int i,j,k; /* loop/subscript */ int count; /* record count */ int headtail; /* true if first/last records are shown */ FILE *ifp, *ofp; /* file pointers */ char filename[1000]; /* input file name */ int length; /* record length */ /* Display totals: */ int maxlength, minlength; /* extrema */ int records; /* record count */ /*-----------------------------------------------------------------*/ /* Arguments */ if (argc < 2 || argc > 5) { fprintf (stderr, "Usage: sample [-r count | -e count] []\n"); exit(0); } i = 1; ifp = NULL; ofp = stdout; count = 0; headtail = 1; while (i < argc) { if (!strcmp(argv[i],"-r")) { ++i; if ((count=atoi(argv[i])) < 1) { fprintf (stderr,"sample: Invalid record count (%s)\n",argv[i]); exit(11); } ++i; } else if (!strcmp(argv[i],"-e")) { ++i; if ((count=atoi(argv[i])) < 1) { fprintf (stderr,"sample: Invalid record count (%s)\n",argv[i]); exit(11); } ++i; headtail = 0; } else if (ifp == NULL) { if ((ifp=fopen(argv[i],"r")) == NULL) { fprintf (stderr,"sample: Unable to open \"%s\" for reading.\n", argv[i]); exit(12); } strcpy(filename,argv[i]); /* save filename for display */ ++i; } else { if ((ofp=fopen(argv[i],"w")) == NULL) { fprintf (stderr,"sample: Unable to open \"%s\" for writing.\n", argv[i]); exit(13); } ++i; } } if (count==0) count = DEFAULT_LINES; /*------------------------------------------------------------------*/ /* First pass through the file to determine extreme record lengths and total record count: */ maxlength = 0; minlength = MAX_RECORD_LENGTH; records = 0; while ((string = fgets(inptline,MAX_RECORD_LENGTH,ifp)) != NULL) { length = strlen(inptline); maxlength = (length > maxlength) ? length : maxlength; minlength = (length < minlength) ? length : minlength; ++records; } /* Check record length: */ if (maxlength == MAX_RECORD_LENGTH) { fprintf (stderr,"sample: Maximum record length (%d) exceeded.\n", MAX_RECORD_LENGTH - 1); exit(21); } /*-------------------------------------------------------------------*/ /* Second pass; rewind file and print header: */ rewind(ifp); fprintf(ofp," File: %s\n",filename); fprintf(ofp," Number of records: %d\n",records); fprintf(ofp," Maximum record length: %d Minimum record length %d\n", maxlength, minlength); fprintf(ofp,"\n"); /* Print separator and byte count headers, based on maximum record length: */ for (i=0; i 9999) { for (i=1; i<=maxlength; ++i) fprintf(ofp,"%1d",(i/10000)%10); fprintf(ofp,"\n"); } if (maxlength > 999) { for (i=1; i<=maxlength; ++i) fprintf(ofp,"%1d",(i/1000)%10); fprintf(ofp,"\n"); } if (maxlength > 99) { for (i=1; i<=maxlength; ++i) fprintf(ofp,"%1d",(i/100)%10); fprintf(ofp,"\n"); } if (maxlength > 9) { for (i=1; i<=maxlength; ++i) fprintf(ofp,"%1d",(i/10)%10); fprintf(ofp,"\n"); } for (i=1; i<=maxlength; ++i) fprintf(ofp,"%1d",i%10); fprintf(ofp,"\n"); /*-------------------------------------*/ /* Loop through records again, printing those requested: */ /* If we're printing the first and last records: */ if (headtail) { /* First, make sure there are more records in the file than we expect to print out. If not, just dump the whole file: */ if (records <= 2*count) { while (fgets(inptline,MAX_RECORD_LENGTH,ifp) != NULL) fputs(inptline,ofp); } else { for (i=0; i