/* $Workfile: $ ** ** Title: validIPTbl.c ** ** Author: D Wang Interferometrics Inc ** ** $Revision: 1.10 $ ** ** Checked in $Date: 2007/10/05 19:14:53 $ ** Last Modified $Modtime: $ ** Last Modified By $Author: wang $ ** *********************************************************************** ** ** Purpose: read and validate IP table (imagetbl.img) ** preserve or strip comments ** ** Assumptions and External Effects: ** ** None ** ** Modification History: ** $Log: validIPTbl.c,v $ ** Revision 1.10 2007/10/05 19:14:53 wang ** Check Function count ** ** Revision 1.9 2007/08/10 16:58:39 wang ** Change long line comment format ** ** */ #include #include #include #include "ipcodes.h" #define CMD_LINE_SIZE 80 #define NO_ERR 0 #define PARSEERR -1 char id[FILENAME_MAX]; char inname[FILENAME_MAX]; char outname[FILENAME_MAX]; int verbose = 0, stripComments = 0; void usage(int argc, char *argv[]) { fprintf(stderr, "Purpose: Validate and generate comments for imagetbl.img file"); fprintf(stderr, "Usage: %s options [-v] [-h] [-s] [-t IP] [-i inputfile]\n",argv[0]); fprintf(stderr, "Options:\n -v = verbose\n -h = help\n -s = strip comments\n -t = string prefix used in comments (default = IP)\n -i inputfile\n"); fprintf(stderr, "Example: %s -v -s -t ImgProc -i inputfile | uniq\n", argv[0]); fprintf(stderr, "Example Output:\n; IP 98:05 SSR1 BIAS TRIM ICRA IC08\nX05 29 67 4C 6A 62 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00\n"); fprintf(stderr, "; IP 99:05 SSR1 BIAS TRIM POIS RICE\nX05 29 67 4C 68 07 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00\n"); fprintf(stderr,"Find Problems by using 'grep Error' on output\n"); exit(-1); } int onebyte(char digit,int baseflag) { char numbuff[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; int icount = 0, maxsearch = 10; if(baseflag == 16) maxsearch = 16; if(baseflag == 36) maxsearch = 36; for(icount = 0; icount < maxsearch; icount++) if(numbuff[icount] == digit) return icount; return (PARSEERR); } int twobyte(char digit1,char digit2,int baseflag) { int ten,one; ten = onebyte(digit1,baseflag); one = onebyte(digit2,baseflag); if (ten > PARSEERR && one > PARSEERR) return ((baseflag*ten) + one); return (PARSEERR); } int validateIP( unsigned int linecount, unsigned char *ip ) { unsigned int icount = 0, hi1 = 0, hi2 = 0, nonzero=0; char str[10]; printf("; %s %02u:%02u",id,linecount,ip[0]); /* check for HI SB clear */ for (icount = 1; icount < ip[0] + 1; icount++) { if (ip[icount] == 33) hi1 = 1; if (ip[icount] == 35) hi2 = 1; } for (icount = 1; icount < ip[0] + 1; icount++) { ipcode2str(ip[icount], str); if(ip[icount] != 0) nonzero++; if(icount == 12) printf("\n; %s %02u: ",id,linecount); if ((hi1 || hi2) && icount < 3) printf(" TBL%u",ip[icount]); else printf(" %s",str); } printf("\n"); if( ip[0] <= 1 ) printf("Error: function count min of 1 invalid\n"); if( ip[0] > 20 ) printf("Error: function count max of 20 exceeded\n"); if( nonzero != ip[0]) printf("Error: function count wrong\n"); return(NO_ERR); } int parse_cmd(char *cmdline) { char *ptr; char digit1,digit2; int icount=0,temp = 0; unsigned char bytecount = 0, ip[21]; static unsigned int linecount = 0; cmdline++; /* skip X at beginning of line */ for(icount=0; icount < 21; icount++) { digit1 = *cmdline++; digit2 = *cmdline++; temp = twobyte(digit1,digit2,16); if ( temp == PARSEERR || temp > NUM_IP_CODES) { printf("Error Bad Value %c%c %u\n",digit1, digit2, temp); return(PARSEERR); } ip[icount] = temp; cmdline++; /* skip space */ } bytecount = ip[0]; validateIP( linecount, &ip[0] ); linecount++; return(NO_ERR); } int read_file( char *filename) { FILE *infd; char cmdline[CMD_LINE_SIZE]; char comment, onechar,c; int nread = 0; int linecount = 0, charcount = 0; infd = fopen( filename,"r"); if (infd == NULL) { perror("File Open Error"); return(1); } // parse while((nread = fscanf(infd,"%s",cmdline)) != EOF) { if(cmdline[0] == ';') { if( stripComments == 0) { printf("%s",cmdline); while( (comment = fgetc(infd)) != '\n') printf("%c",comment); printf("\n"); } } else if(cmdline[0] == 'X') { charcount = 0; while( (onechar = fgetc(infd)) != '\n') cmdline[3 + charcount++] = onechar; cmdline[3 + charcount] = '\0'; parse_cmd(cmdline); printf("%s\n",cmdline); fscanf(infd,"\n",cmdline); if(charcount > 60) printf("Error %u chars:Line Too Long\n", charcount); if(charcount < 60) printf("Error %u chars:Line Too Short\n", charcount); } } fclose(infd); return(0); } int read_args(int argc, char *argv[]) { int c; while ((c = getopt(argc,argv,"vhsi:o:t:")) != -1) { switch (c) { case 'v': verbose = 1; if(verbose) printf(";Verbose ON\n"); break; case 'h': usage(argc,argv); break; case 's': /* strip comments */ stripComments = 1; if(verbose) printf(";Strip Comments ON\n"); break; case 'i': strcpy(inname, optarg); if(verbose) printf(";Input file: %s\n",inname); break; case 'o': strcpy(outname, optarg); if(verbose) printf(";Output file: %s\n",outname); break; case 't': strcpy(id, optarg); if(verbose) printf(";ID: %s\n",id); break; default: usage(argc,argv); break; } } } int main(int argc,char *argv[]) { strcpy(id,"IP"); if(argc > 1) read_args(argc,argv); else usage(argc, argv); read_file( inname); }