📄 signal_file.c
字号:
void WriteSigFile(SIGNAL signal,char *filename, char flagBinary, char *mode,int flagHeader, char *formatX, char *formatY){ STREAM stream; /* Open file */ stream = OpenFileStream(filename,"w"); if (stream == NULL) Errorf("WriteSigFile() : Error while opening the file %s",filename); WriteSigStream(signal,stream,flagBinary,mode,flagHeader,formatX,formatY); CloseStream(stream);}/* * The corresponding command */void C_Write(char **argv){ SIGNAL signal; STREAM stream; char *filename,*mode,*bmode,*format1,*format2; int flagHeader,flagBinary,flagRaw,nsize; char opt,binaryCoding; argv = ParseArgv(argv,tSIGNALI,&signal,-1); argv = ParseArgv(argv,tSTREAM_,NULL,&stream,-1); if (stream == NULL) argv = ParseArgv(argv,tSTR,&filename,-1); else filename = NULL; argv = ParseArgv(argv,tSTR_,"",&mode,-1); /* options */ flagHeader = YES; flagBinary = YES; flagRaw = NO; format1 = format2 = ""; while(opt = ParseOption(&argv)) { switch(opt) { case 'h': flagHeader = NO; break; case 'f': argv = ParseArgv(argv,tSTR,&format1,-1); format2 = format1; if (*argv != NULL && **argv != '-') argv = ParseArgv(argv,tSTR,&format2,-1); break; case 'a': flagBinary = NO; break; case 'r': if (*argv != NULL && '0'<=**argv && **argv<='9') { Printf("hein\n"); argv = ParseArgv(argv,tINT_,0,&nsize,-1); bmode = ""; } else argv = ParseArgv(argv,tSTR_,"",&bmode,tINT_,0,&nsize,-1); if (!strcmp(mode,"little")) binaryCoding = BinaryLittleEndian; else if (!strcmp(mode,"big")) binaryCoding = BinaryBigEndian; else if (!strcmp(mode,"")) binaryCoding = 0; else Errorf("Bad binary mode '%s'",mode); flagRaw = YES; if (nsize != 0 && nsize != sizeof(float) && nsize != sizeof(double)) Errorf("Bad number of bytes (%d) for float (must be either %d or %d)",nsize,sizeof(float),sizeof(double)); break; default: ErrorOption(opt); } } NoMoreArgs(argv); if (flagRaw == YES) { if (*format1 != '\0') Errorf("Cannot specify a format in raw mode"); if (stream == NULL) WriteSigRawFile(signal,filename,binaryCoding,nsize); else WriteSigRawStream(signal,stream,binaryCoding,nsize); } else { if (*format1 != '\0' && flagBinary) Errorf("Cannot specify a format in binary mode"); if (stream == NULL) WriteSigFile(signal,filename,flagBinary,mode,flagHeader,format1,format2); else WriteSigStream(signal,stream,flagBinary,mode,flagHeader,format1,format2); }}/********************************************************* Read a signal in a file *******************************************************//* * Function to read from a stream a signal coded in raw format, i.e., no header and binary (LWFLOAT) values * * 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) * * binaryCoding : either BinaryLittleEndian or BinaryBigEndian or 0 (in that latter case the current cpu mode is used) * nsize : in case of binary coding, corresponds to number of bytes to use foreach float (should be either sizeof(float), sizeof(double) or 0) */void ReadSigRawStream(SIGNAL signal,STREAM s, int firstIndex,int sizeToRead, char binaryCoding,int nsize){ long int pos,pos1; int size; FILE *stream; void *array; int i; /* * Some basic tests on the stream */ if (s->mode != StreamRead) Errorf("ReadSigRawStream() : The stream should be an input stream and not an output stream"); if (s->stream == NULL) Errorf("ReadSigRawStream() : You cannot read a signal to standard input"); stream = s->stream; /* test on nsize */ if (nsize != 0 && nsize != sizeof(float) && nsize != sizeof(double)) Errorf("ReadSigRawStream() : Bad number of bytes (%d) for float (must be either %d or %d or 0)",nsize,sizeof(float),sizeof(double)); if (nsize == 0) nsize = sizeof(LWFLOAT); /* Compute the size */ pos = ftell(stream); fseek(stream,0,SEEK_END); pos1 = ftell(stream); fseek(stream,pos-pos1,SEEK_CUR); size = (pos1-pos+1)/nsize; /* Some checkings */ if (firstIndex >= size) Errorf("ReadSigRawStream() : 'firstIndex' (%d) is too large",firstIndex); if (sizeToRead == -1) sizeToRead = size-firstIndex; if (sizeToRead+firstIndex > size) Errorf("ReadSigRawStream() : 'sizeToRead' (%d) is too large",sizeToRead); /* Allocation */ SizeSignal(signal,sizeToRead,YSIG); /* Position the stream */ if (fseek(stream,firstIndex*nsize,SEEK_CUR) != 0) Errorf("ReadSigRawStream() : firstIndex seems to be too large for this file"); /* Set the array the data must be read in */ if (sizeof(LWFLOAT) == nsize) array = signal->Y; else array = Malloc(nsize*sizeToRead); /* Read the data */ if (fread(array,nsize,sizeToRead,stream) != sizeToRead) Errorf("ReadSigRawStream() : sizeToRead seems to be too large for this file"); /* Big/Little conversions */ if (binaryCoding == 0) { if (IsCPULittleEndian) binaryCoding = BinaryLittleEndian; else binaryCoding = BinaryBigEndian; } if (((binaryCoding == BinaryLittleEndian) && IsCPUBigEndian) || ((binaryCoding == BinaryBigEndian) && IsCPULittleEndian)) { BigLittleValues(array,sizeToRead,nsize); } /* Conversion of the array if necessary */ if (sizeof(LWFLOAT) != nsize) { for (i=0;i<sizeToRead;i++) { if (sizeof(float) == nsize) signal->Y[i] = ((float *) array)[i]; else signal->Y[i] = ((double *) array)[i]; } Free(array); } /* Some settings */ SetNameSignal(signal,NULL);}/* Same as above but with a filename instead of a stream */void ReadSigRawFile(SIGNAL signal,char *filename, int firstIndex,int sizeToRead, char binaryCoding, int nsize){ STREAM stream; /* Open file */ stream = OpenFileStream(filename,"r"); if (stream == NULL) Errorf("ReadSigRawFile() : Error while opening the file %s",filename); ReadSigRawStream(signal,stream,firstIndex,sizeToRead,binaryCoding,nsize); CloseStream(stream); if (!strcmp(signal->name,"")) SetNameSignal(signal,filename);}/* * Function to get information about a signal stored in a stream without reading the signal itself. * * The parameters are : * * stream : the file stream * * The following parameters will be filled by te function * * siginfo : a signal structure where all the fields are filled up except the Y or X * (contains, size, firstp, lastp, XYSIG, YSIG, dx, x0 info) * WARNING : NO ALLOCATION IN 'siginfo' IS MADE * header : YES if there is a header * flagBinary : YES or NO * binaryCoding : BinaryLittleEndian or BinaryBigEndian (if flagBinary == YES only) * nCols : the number of columns (if flagBinary == NO only) * nSize : in case of binary coding the number of bytes used to code a float * * It returns YES if the format is a LastWave format otherwise it returns NO * * This function does not change the position of the stream. */char ReadInfoSigStream(STREAM s, SIGNAL siginfo, char *header, char *flagBinary, char *binaryCoding, int *nColumns, int *nsize){ int flagHeader; int flagXY; int firstp,lastp,size; LWFLOAT dx,x0,f,f1; char name[255],str[1000],*str1,c; int n,i; int nCol; FILE *stream; fpos_t begining; char flagBinary1; char flagLittleEndian; char flagNewFormat; /* * Some basic tests on the stream */ if (s->mode != StreamRead) Errorf("ReadInfoSigStream() :The stream should be an input stream and not an output stream"); if (s->stream == NULL) Errorf("ReadInfoSigStream() : You cannot read a signal to standard input"); stream = s->stream; /* * Let's try to read the header */ flagHeader = NO; flagBinary1 = NO; flagXY = YES; flagNewFormat = NO; fgetpos(stream,&begining); 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"); /* Extended header (Signal Package, version 1.5) */ n += fscanf(stream,"binary %[^\n\r] ",&str); if (n == 5) { n--; flagNewFormat = YES; if (!strcmp(str, "no")) flagBinary1 = NO; else { flagBinary1 = YES; if (!strncmp(str, "little",6)) { flagLittleEndian = YES; str1 = str+6; } else if (!strncmp(str, "big",3)) { flagLittleEndian = NO; str1 = str+3; } else Errorf("ReadSigInfoStream() : Bad field value '%s' of field 'binary'",str); *nsize = sizeof(float); if (*str1 == ' ') { str1++; sscanf(str1,"%d",nsize); } } } else flagBinary1 = 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"); } }fsetpos(stream,&begining);/* * Case there is a header */if (flagHeader) { *header = YES; siginfo->size = size; siginfo->lastp = lastp; siginfo->firstp = firstp; if (flagXY == NO) { siginfo->dx = dx; siginfo->x0 = x0; siginfo->type = YSIG; *nColumns = 1; } else { siginfo->type = XYSIG; *nColumns = 2; } if (flagBinary1) { *flagBinary = YES; if (flagLittleEndian) *binaryCoding = BinaryLittleEndian; else *binaryCoding = BinaryBigEndian; } return(YES);}/* * Case there is no header *//* How many columns on the first line ? */#ifdef NUMDOUBLEif (fscanf(stream,"%lf",&f) != 1) return(NO);#elseif (fscanf(stream,"%f",&f) != 1) return(NO);#endiffsetpos(stream,&begining); 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++;}fsetpos(stream,&begining);if (nCol <= 0) return(NO);/* Let's read !! */size = 0;for(i= 0 ;;i++) { if (nCol == 1) {#ifdef NUMDOUBLE
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -