/***********************************************************************/ /* Purpose: To transform a data file into database loadable format. */ /* Author: Jeff Yuan */ /* Date: 8/10/1999 */ /* */ /* INPUT: Data file from Apache (Facility monitoring system). */ /* OUTPUT: A file in which the Time, Parameter name, vlaue are listed */ /* in vertical line. */ /***********************************************************************/ #include #include #include #include #include #include main(argc, argv) int argc; char *argv[]; { int np, flag, i, nl; char *line; char **pname; char pdate[11], ptime[9]; double *pvalue; FILE *fpin, *fpout; if(argc != 3) { printf("Usage:\n"); printf("transform.x , \n"); exit(0); } printf("argvs: %s %s\n",argv[1],argv[2]); fpin = fopen(argv[1], "r"); if(fpin == NULL) { printf("file %s does not exist\n",argv[1]); exit(1); } /* first round of screening for number of parameters */ line = (char *)malloc(201*sizeof(char)); /* big enough to hold 200 chars */ if(line==NULL) { printf("something wrong with allocate 1\n"); exit(1); } flag=1; np=0; line=fgets(line,200,fpin); /* read first two lines in the input file */ line=fgets(line,200,fpin); /* to count the number of lines/parameters */ for(;flag&&(fgets(line,200,fpin)!=NULL);) { if(strncmp(line,"Point_",6)==0) np++; else if(np>0) flag=0; /* to stop the loop */ } free(line); close(fpin); fpin = fopen(argv[1], "r"); /* or ues rewind() function to move the cursor to the top */ if(fpin == NULL) { printf("file %s does not exist\n",argv[1]); exit(1); } fpout = fopen(argv[2], "w"); if(fpout == NULL) { printf("can not open file %s\n",argv[2]); exit(1); } /* prepare necessary memory */ nl = 14*np+30; line = (char *)malloc((nl+1)*sizeof(char)); pvalue = (double *)malloc(np*sizeof(double)); pname = (char **)malloc(np*sizeof(char *)); for(i=0;i0) flag=0; } /* start reading data and writing */ for(;fgets(line,nl,fpin)!=NULL;) { if((strlen(line)>17)&&(line[13]==':')&&(line[16]==':')) { flag = read_line(np, line, pdate, ptime, pvalue); if(flag==0) for(i=0;i -1.0e10) fprintf(fpout,"%30s %s %s %12.2lf\n", pname[i], pdate, ptime, pvalue[i]) ; else fprintf(fpout,"%30s %s %s .\n", pname[i], pdate, ptime); } } } /* done! close files and free memory */ close(fpin); close(fpout); free(line); free(pvalue); for(i=0;i