/* File: autoconv.cpp Purpose: To automatically generate HTML files to display the probestation data using the ProbeGraphApplet applet. Author: David Fiske, Ohio State University */ //Include Files: #include #include #include #include //Macros and Constants: #define H1 "c:\\autoconv\\htmltemp1.p2h+" #define H2 "c:\\autoconv\\htmltemp2.p2h+" #define H3 "c:\\autoconv\\htmltemp3.p2h+" #define H4 "c:\\autoconv\\htmltemp4.p2h+" #define H5 "c:\\autoconv\\htmltemp5.p2h+" #define H6 "c:\\autoconv\\htmltemp6.p2h " #define T1 "c:\\autoconv\\titletemp1.p2h+" #define T2 "c:\\autoconv\\titletemp2.p2h+" #define F1 "c:\\autoconv\\filetemp1.p2h+" #define F2 "c:\\autoconv\\filetemp2.p2h+" #define F3 "c:\\autoconv\\filetemp3.p2h+" #define CMD "copy " #define NAMELENGTH 30 //Inline Functions double abs(double x) {return (x < 0.0) ? -x : ((x == 0.0) ? 0.01 : x);} double pow(double x, int p) {return (p == 0) ? 1 : x*pow(x,p-1);} //Prototypes: void clear_nwsp(FILE*); short prompt_base(char[]); void make_names(const char[],char[],char[],char[],char[],char[]); void suffix_name(const char[],const char[],char[],short); void append_name(const char[],const char[4],char[]); short equal(const char[],const char[],short); double stof(const char[]); void convert1(FILE*,FILE*,const char[],short); void print_help(void); short another(void); void makehtml(const char[]); //Function Definitions: void clear_nwsp(FILE *f) { int temp; short cont; do { temp = fgetc(f); if ( cont = ((temp > 33) && (temp < 127)) ) { // not a white char ungetc(temp,f); // Put it back. } } while (cont); } short prompt_base(char base[]) { short repeat; do { printf("Enter the .prb file base name (leave off .prb),\n"); printf("enter '-h' for help, or enter '-q' to quit: "); scanf("%s",base); repeat = ( (base[0] == '-') && (base[1] == 'h')); if (repeat) { print_help(); } } while (repeat); return ( (base[0] == '-') && (base[1] == 'q') ) ? 0 : 1; } void make_names(const char base[], char in[], char an[], char c1[], char c2[], char htm[]) { suffix_name(base,".prb",in,4); append_name(base,"and_",an); append_name(base,"caN_",c1); append_name(base,"caP_",c2); suffix_name(base,".html",htm,5); } void suffix_name(const char base[], const char suffix[], char out[], short l) { short i = 0; short j; do { out[i] = base[i]; } while (base[++i] != 0); for(j = 0; j < l; j++) { out[i++] = suffix[j]; } out[i] = 0; } void append_name(const char base[], const char prefix[4], char out[]) { short i; for(i = 0; i < 4; i++) { out[i] = prefix[i]; } i = 0; do { out[i+4] = base[i]; } while (base[++i] != 0); i += 4; out[i++] = '.'; out[i++] = 't'; out[i++] = 'x'; out[i++] = 't'; out[i] = 0; } short equal(const char st1[5], const char st2[5], short length) { short i; short result = 1; for(i = 0; i < length; i++) { result *= (st1[i] == st2[i]); } return result; } double stof(const char st[]) { double base; short sgn; int ex; short j; if (st[0] == '-') { sgn = 1; } else { sgn = 0; } base = st[0+sgn] - '0'; for(j = 2+sgn; j < 7+sgn; j++) { base += ((double)(st[j+sgn] - '0'))/pow(10.0,j-(1+sgn)); } ex = (100*(st[9+sgn]-'0') + 10*(st[10+sgn]-'0') + st[11+sgn]-'0'); if (st[8+sgn] == '-') { base /= pow(10.0,ex); } else { base *= pow(10.0,ex); } return (-2*sgn + 1)*base; } void convert1(FILE *fin, FILE *fout, const char tag[], short num) { char line[100]; int ctemp; double temp; do { fscanf(fin,"%100s",line); } while (! equal(line,tag,num) ); ctemp = fgetc(fin); do { ungetc(ctemp, fin); fscanf(fin,"%lf",&temp); fprintf(fout,"%f\t",temp); fscanf(fin,"%s",line); temp = log10(abs(stof(line))); fprintf(fout,"%f\n",temp); clear_nwsp(fin); } while ( (ctemp = fgetc(fin)) != 58 ); // i.e. quit when the next character is ":". } void print_help(void) { printf("\nThis program takes a file of type '.prb'\n"); printf("output from the probestation DAQ system, and produces\n"); printf("three tab delimited text files containing the data\n"); printf("of the anode and both cathode measurements.\n\n"); printf("It assumes that measurements have been taken for all three\n"); printf("types of measurements, and that only one run through each\n"); printf("type of measurement was taken.\n\n"); printf("The input file must have the extention '.prb'\n"); printf("and the output files will be named and_.txt,\n"); printf("caN_.txt, and caP_.txt, "); printf("where the input is .prb.\n"); printf("An html file appropriate for use with the ProbeGraphApplet\n"); printf("will also be generated automatically with a similar name.\n\n"); printf("From the command line use \n\nprb2txt \n\n"); printf("or, for multiple files, run prb2txt without arguments and follow\n"); printf("the prompts.\n\n"); printf("(c) D. Fiske 1999\nOhio State University.\n\n"); } short another(void) { char answer; printf("\nConversion successful!\n\n"); printf("Convert another file (y/n)? "); scanf("%c",&answer); printf("\n"); if ( (answer == 'y') || (answer == 'Y') ) { return 1; } else { return 0; } } void makehtml(const char out[]) { char str[350] = CMD H1 T1 H2 T2 H3 F1 H4 F2 H5 F3 H6; system(strncat(str,out,NAMELENGTH+1)); } //Main: int main(int argc, char *argv[]) { char basename[NAMELENGTH-8]; char inname[NAMELENGTH]; char anodename[NAMELENGTH]; char cath1name[NAMELENGTH]; char cath2name[NAMELENGTH]; char htmlname[NAMELENGTH+1]; FILE *infile; FILE *anodefile; FILE *cath1file; FILE *cath2file; FILE *t1; FILE *t2; FILE *f1; FILE *f2; FILE *f3; short repeat = 1; short swtch = argc; while (repeat) { switch(swtch) { case 1: if (prompt_base(basename)) { make_names(basename,inname,anodename,cath1name,cath2name,htmlname); } else { return 1; } break; case 2: swtch = 1; // In so we'll get the right case when repeating. if ( (*argv[1] == '-') && (*(argv[1]+1) == 'h') ) { print_help(); return 1; } else { make_names(argv[1],inname,anodename,cath1name,cath2name,htmlname); } break; default: return 0; } infile = fopen(inname, "r"); anodefile = fopen(anodename, "w"); cath1file = fopen(cath1name, "w"); cath2file = fopen(cath2name, "w"); t1 = fopen("c:\\autoconv\\titletemp1.p2h", "w"); t2 = fopen("c:\\autoconv\\titletemp2.p2h", "w"); f1 = fopen("c:\\autoconv\\filetemp1.p2h", "w"); f2 = fopen("c:\\autoconv\\filetemp2.p2h", "w"); f3 = fopen("c:\\autoconv\\filetemp3.p2h", "w"); fprintf(t1,"Graph of %s", (argc == 1) ? basename : argv[1]); fprintf(t2,"%s", (argc == 1) ? basename : argv[1]); fprintf(f1,"%s",anodename); fprintf(f2,"%s",cath1name); fprintf(f3,"%s",cath2name); convert1(infile,anodefile,"ANODE",5); convert1(infile,cath1file,"CathodeN",8); convert1(infile,cath2file,"CathodeP",8); fclose(f3); fclose(f2); fclose(f1); fclose(t2); fclose(t1); fclose(cath2file); fclose(cath1file); fclose(anodefile); fclose(infile); makehtml(htmlname); repeat = another(); } return 1; }