📄 signal_file.c
字号:
n = fscanf(stream,"%lf ",&f);#else n = fscanf(stream,"%f ",&f);#endif if (n == EOF) break; if (n != 1) return(NO); size++; } else if (nCol == 2) {#ifdef NUMDOUBLE n = fscanf(stream,"%lf %lf ",&f,&f1);#else n = fscanf(stream,"%f %f ",&f,&f1);#endif if (n == EOF) break; if (n != 2) return(NO); size++; } else {#ifdef NUMDOUBLE n = fscanf(stream,"%lf %lf%*[^\n\r] ",&f,&f1);#else n = fscanf(stream,"%f %f%*[^\n\r] ",&f,&f1);#endif if (n == EOF) break; if (n != 2) return(NO); size++; } }fsetpos(stream,&begining);/* Fill up the results */*header = NO;siginfo->size = size;siginfo->lastp = size-1;siginfo->firstp = 0;siginfo->dx = 1;siginfo->x0 = 0;if (nCol == 2) siginfo->type = XYSIG;else siginfo->type = YSIG;*nColumns = nCol;*flagBinary = NO;return(YES);}/* Same as above but with a filename instead of a stream */char ReadInfoSigFile(char *filename, SIGNAL siginfo, char *header, char *flagBinary, char *binaryCoding, int *nColumns, int *nsize){ STREAM stream; char ans; /* Open file */ stream = OpenFileStream(filename,"r"); if (stream == NULL) Errorf("ReadInfoSigFile() : Error while opening the file %s",filename); ans = ReadInfoSigStream(stream,siginfo,header,flagBinary,binaryCoding,nColumns,nsize); CloseStream(stream); return(ans);}/* * Function to read from a stream a signal. * It can be coded using * - (Optional LastWave Header), ascii coded floats * - Header, binary coded floats * * The parameters are : * * signal : the signal to read * stream : the file stream * * firstIndex : first index in the file to be read (starting from 0) * sizeToRead : the number of points to be read (if -1 everything is read) * * The two next fields are used only for ascii streams (othewise they should be set to 0,0) * * xcol : The column number corresponding to the x-values (starting from 1) * ycol : The column number corresponding to the y-values (starting from 1) * If xcol or ycol is 0 then the function make some natural assumptions..... * If xcol == -1 then only the Y field should be read */void ReadSigStream(SIGNAL signal,STREAM s, int firstIndex,int sizeToRead, int xcol,int ycol){ int flagHeader; int flagXY; int firstp,lastp,size; LWFLOAT dx,x0,f; char name[255],str[1000],*str1,c; int n,i,j; int nCol; FILE *stream; fpos_t beginning; char flagBinary; char flagLittleEndian; char flagNewFormat; void *array; size_t nsize; /* * Some basic tests on the stream */ if (s->mode != StreamRead) Errorf("ReadSigStream() :The stream should be an input stream and not an output stream"); if (s->stream == NULL) Errorf("ReadSigStream() : You cannot read a signal to standard input"); stream = s->stream; /* * Let's try to read the header */ flagHeader = NO; flagBinary = NO; flagXY = YES; flagNewFormat = NO; fgetpos(stream,&beginning); str1 = "LastWave Header"; while (*str1 != '\0') { c = fgetc(stream); if (c != *str1) break; str1++; } if (*str1 == '\0') { c = fgetc(stream); c = fgetc(stream); if (c != '\n') ungetc(c,stream); flagHeader = YES; n = 0; n += fscanf(stream,"size %d ",&size); n += fscanf(stream,"name %[^\n\r] ",name); n += fscanf(stream,"firstp %d ",&firstp); n += fscanf(stream,"lastp %d ",&lastp); if (n != 4) Errorf("ReadSigStream() : Error in the header of the file"); n += fscanf(stream,"binary %[^\n\r] ",&str); if (n == 5) { n--; flagNewFormat = YES; if (!strcmp(str, "no")) flagBinary = NO; else { flagBinary = YES; if (!strncmp(str, "little",6)) { flagLittleEndian = YES; str1 = str+6; } else if (!strncmp(str, "big",3)) { flagLittleEndian = NO; str1 = str+3; } else Errorf("ReadSigStream() : Bad field value '%s' of field 'binary'",str); nsize = sizeof(float); if (*str1 == ' ') { str1++; sscanf(str1,"%ld",&nsize); } if (nsize != sizeof(float) && nsize != sizeof(double)) Errorf("ReadSigStream() : Can't read binary files using %d bytes to store numbers (that size is neither float or double)",nsize); } } else flagBinary = NO; /* End of extended header */ fscanf(stream,"%[^\n\r] ",str); if (strcmp(str,"End of Header")) { /* it is a YSIG */ flagXY = NO;#ifdef NUMDOUBLE n += sscanf(str,"dx %lf ",&dx); n += fscanf(stream,"x0 %lf ",&x0);#else n += sscanf(str,"dx %f ",&dx); n += fscanf(stream,"x0 %f ",&x0);#endif if (n != 6) Errorf("ReadSigStream() : Error in the header of the file"); fscanf(stream,"%[^\n\r] ",str); if (strcmp(str,"End of Header")) Errorf("ReadSigStream() : Error in the header of the file"); }fgetpos(stream,&beginning); }fsetpos(stream,&beginning);/* CASE OF AN ASCII FILE */if (flagBinary == NO) { while (1) { /* * How many columns on the first line ? */#ifdef NUMDOUBLE if (fscanf(stream,"%lf",&f) != 1) Errorf("ReadSigStream() : Bad format");#else if (fscanf(stream,"%f",&f) != 1) Errorf("ReadSigStream() : Bad format");#endif fsetpos(stream,&beginning); fscanf(stream,"%[^\n\r] ",str); nCol = 0; str1 = str; while (*str1 != '\0' && (*str1 == ' ' || *str1 == '\t')) str1++; while (*str1 != '\0') { nCol++; while (*str1 != '\0' && *str1 != ' ' && *str1 != '\t') str1++; while (*str1 != '\0' && (*str1 == ' ' || *str1 == '\t')) str1++; } if (nCol == 0) { if (flagNewFormat) Errorf("ReadSigStream() : Cannot figure out the number of colums"); break; } /* * Possible errors and set xcol and ycol */ if (xcol > nCol) Errorf("ReadSigStream() : Only %d columns in file ==> x column number %d is incorrect",nCol,xcol); if (ycol > nCol) Errorf("ReadSigStream() : Only %d columns in file ==> y column number %d is incorrect",nCol,ycol); if (xcol > 0 && ycol <= 0) Errorf("ReadSigStream() : Sorry you cannot read just X"); if (xcol <= 0) { if (xcol == -1) xcol = 0; else if (flagHeader) { if (flagXY) xcol = 1; else xcol = 0; } else if (nCol == 2) xcol = 1; else xcol = 0; } if (ycol <= 0) { if (flagHeader) { if (flagXY) ycol = 2; else ycol = 1; } else if (nCol == 2) ycol = 2; else ycol = 1; } /* * Compute the size of the signal if no header and size to read is not specified */ if (flagHeader == NO && sizeToRead == -1) { fsetpos(stream,&beginning); size = 0; while(fscanf(stream,"%[^\n\r] ",str) != EOF) size++; } /* * Check firstIndex and sizeToRead */ if (firstIndex < 0) firstIndex = 0; if (sizeToRead == -1) size = size - firstIndex; else if (flagHeader == YES) { if (firstIndex+sizeToRead > size) Errorf("ReadSigStream() : Either firstIndex or sizeToRead is too big"); size = sizeToRead; } else size = sizeToRead; /* * Allocation */ if (xcol != 0) SizeSignal(signal,size,XYSIG); else SizeSignal(signal,size,YSIG); /* * We skip everything up to firstIndex */ fsetpos(stream,&beginning); for (i = 0;i<firstIndex;i++) { n = fscanf(stream,"%[^\n\r] ",str); if (n!=1) { if (flagNewFormat) Errorf("ReadSigStream() : Signal is too short while reading file"); break; } } /* * Let's read !! */ for(i= 0 ;i<size;i++) { /* Case both X and Y must be read */ if (xcol != 0) { for (j = 1; j<MIN(xcol,ycol);j++) fscanf(stream,"%*s"); n = 0;#ifdef NUMDOUBLE if (xcol < ycol) n = fscanf(stream,"%lf",&(signal->X[i])); else if (xcol > ycol) n = fscanf(stream,"%lf",&(signal->Y[i])); else { n = fscanf(stream,"%lf%*[^\n\r] ",&(signal->Y[i]));#else if (xcol < ycol) n = fscanf(stream,"%f",&(signal->X[i])); else if (xcol > ycol) n = fscanf(stream,"%f",&(signal->Y[i])); else { n = fscanf(stream,"%f%*[^\n\r] ",&(signal->Y[i]));#endif signal->X[i] = signal->Y[i]; n+=1; } if (xcol != ycol) { j++; for (; j<MAX(xcol,ycol);j++) fscanf(stream,"%*s");#ifdef NUMDOUBLE if (xcol < ycol) n += fscanf(stream,"%lf%*[^\n\r] ",&(signal->Y[i])); else n += fscanf(stream,"%lf%*[^\n\r] ",&(signal->X[i]));#else if (xcol < ycol) n += fscanf(stream,"%f%*[^\n\r] ",&(signal->Y[i])); else n += fscanf(stream,"%f%*[^\n\r] ",&(signal->X[i]));#endif } if (n != 2) { if (flagNewFormat) Errorf("ReadSigStream() : Error while reading file"); break; } } /* Case just Y must be read */ else { for (j = 1; j<ycol;j++) fscanf(stream,"%*s");#ifdef NUMDOUBLE n = fscanf(stream,"%lf%*[^\n\r] ",&(signal->Y[i]));#else n = fscanf(stream,"%f%*[^\n\r] ",&(signal->Y[i]));#endif if (n != 1) { if (flagNewFormat) Errorf("ReadSigStream() : Error while reading file"); break; } } } if (i != size) break; if (flagNewFormat == NO && flagHeader) Warningf("ReadSigStream() : (old Signal Package 1.1.2 header) You should update the format (writing the file again using 'write')"); /* Some initialization */ signal->size = size; if (flagHeader) { if (sizeToRead == -1){ signal->lastp = lastp; signal->firstp = firstp; } if (flagXY == NO) { signal->dx = dx; signal->x0 = x0 + dx*firstIndex; } if (strcmp(name,"none")) SetNameSignal(signal,name); else SetNameSignal(signal,NULL); } else { signal->x0 = firstIndex; } /* We might need to sort the signal */ if (xcol != 0) SortSig(signal); return; } } /* * If there was an error assuming data is ascii coded maybe it is because the header is a Signal Package 1.1.2 header * and we are currently dealing with binary coded values without knowing it !
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -