📄 signal_file.c
字号:
/*..........................................................................*//* *//* L a s t W a v e P a c k a g e 'signal' 2.1 *//* *//* Copyright (C) 1998-2002 Emmanuel Bacry. *//* email : lastwave@cmap.polytechnique.fr *//* *//*..........................................................................*//* *//* This program is a free software, you can redistribute it and/or *//* modify it under the terms of the GNU General Public License as *//* published by the Free Software Foundation; either version 2 of the *//* License, or (at your option) any later version *//* *//* This program is distributed in the hope that it will be useful, *//* but WITHOUT ANY WARRANTY; without even the implied warranty of *//* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *//* GNU General Public License for more details. *//* *//* You should have received a copy of the GNU General Public License *//* along with this program (in a file named COPYRIGHT); *//* if not, write to the Free Software Foundation, Inc., *//* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *//* *//*..........................................................................*//****************************************************************************//* *//* signal_file.c Deals with the files *//* *//****************************************************************************/#include "lastwave.h"#include "signals.h"/********************************************************* Write a signal in a file *******************************************************//* * Function to write the y-values of a signal using raw format * signal : the signal to write * stream : the file stream * binaryCoding : either BinaryLittleEndian or BinaryBigEndian or 0 (in that latter case the current cpu mode is used) */void WriteSigRawStream(SIGNAL signal,STREAM s, char binaryCoding,int nsize){ FILE *stream; int flagBL; void *array; int i; /* Some basic tests on the stream */ if (s->mode != StreamWrite) Errorf("WriteSigRawStream() : The stream should be an output stream and not an input stream"); if (s->stream == NULL) Errorf("WriteSigRawStream() : You cannot write a signal to standard output"); 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); /* Set the array the data must be read in */ if (sizeof(LWFLOAT) == nsize) array = signal->Y; else { array = Malloc(nsize*signal->size); for (i=0;i<signal->size;i++) { switch(nsize) { case sizeof(float) : ((float *) array)[i] = (float) signal->Y[i]; break; case sizeof(double) : ((double *) array)[i] = (double) signal->Y[i]; break; } } } /* Big/Little conversions */ if (binaryCoding == 0) { if (IsCPULittleEndian) binaryCoding = BinaryLittleEndian; else binaryCoding = BinaryBigEndian; } if (((binaryCoding == BinaryLittleEndian) && IsCPUBigEndian) || ((binaryCoding == BinaryBigEndian) && IsCPULittleEndian)) { flagBL = YES; BigLittleValues(array,signal->size,nsize); } else flagBL = NO; /* We then write the values */ fwrite(array,nsize,signal->size,stream); /* Go back if necessary */ if (flagBL && sizeof(LWFLOAT) == nsize) { BigLittleValues(array,signal->size,nsize); } /* Free the array */ if (sizeof(LWFLOAT) != nsize) Free(array);}/* * Same as above but with a filename instead of a stream */void WriteSigRawFile(SIGNAL signal,char *filename, char binaryCoding,int nsize){ STREAM stream; /* Open file */ stream = OpenFileStream(filename,"w"); if (stream == NULL) Errorf("WriteSigRawFile() : Error while opening the file %s",filename); WriteSigRawStream(signal,stream,binaryCoding,nsize); CloseStream(stream);}/* * Function to write a signal using only ascii format * signal : the signal to write * stream : the file stream * flagBinary : If YES then the values are coded using binary codes (and not ascii) * mode == "xy" ==> write X Y (two columns in ascii mode and X-values followed by Y-values if binary mode) * "yx" ==> write Y X (two columns in ascii mode and Y-values followed by X-values if binary mode) * "x" ==> write X (just X is stored) * "y" ==> write Y (just Y is stored) * "" ==> the function chooses the best mode. * flagHeader : Do we want a header to be written ? (if flagBinary==YES then one should have flagHeader==YES) * format1,format2 : ascii printf type of format (start with %) */void WriteSigStream(SIGNAL signal,STREAM s, char flagBinary, char *mode,int flagHeader,char *format1,char *format2){ int i; FILE *stream; char *type1,*type2; char *format; char theFormat[1000]; /* Some basic tests on the stream */ if (s->mode != StreamWrite) Errorf("WriteSigStream() : The stream should be an output stream and not an input stream"); if (s->stream == NULL) Errorf("WriteSigStream() : You cannot write a signal to standard output"); stream = s->stream; if ((*format1 != '\0' || *format2 != '\0') && flagBinary) Errorf("WriteSigStream() : Cannot specify a format in binary mode"); /* Test the format1 */ if (*format1 == '\0') type1 = NULL; else { format = format1; if (strlen(format)<2 && format[0] != '%') Errorf("WriteSigStream() : Bad format '%s'",format1); format++; while(*format != '\0' && *format != '%' && !isalpha(*format)) format++; if (*format == '\0' || *format == '%') Errorf("WriteSigStream() : Bad format '%s'",format1); /* Case of an int */ if (*format == 'd' || *format == 'i' || *format == 'o' || *format == 'x' || *format == 'X' || *format == 'u') type1 = intType; /* Case of an double */ else if (*format == 'f' || *format == 'e' || *format == 'E' || *format == 'g' || *format == 'G') type1 = floatType; else Errorf("WriteSigStream() : Bad format '%s", format1); } /* Test the X-format */ if (*format2 == '\0') type2 = NULL; else { format = format2; if (strlen(format)<2 && format[0] != '%') Errorf("WriteSigStream() : Bad format '%s'",format2); format++; while(*format != '\0' && *format != '%' && !isalpha(*format)) format++; if (*format == '\0' || *format == '%') Errorf("WriteSigStream() : Bad format '%s'",format2); /* Case of an int */ if (*format == 'd' || *format == 'i' || *format == 'o' || *format == 'x' || *format == 'X' || *format == 'u') type2 = intType; /* Case of an double */ else if (*format == 'f' || *format == 'e' || *format == 'E' || *format == 'g' || *format == 'G') type2 = floatType; else Errorf("WriteSigStream() : Bad format '%s", format2); } /* Test the mode */ if (!strcmp(mode,"")) { if (signal->type == YSIG) mode = "y"; else mode = "xy"; } if (strcmp(mode,"xy") && strcmp(mode,"yx") && strcmp(mode,"x") &&strcmp(mode,"y")) Errorf("WriteSigStream() : Mode '%s' not allowed",mode); /* Some basic checkings */ if (flagBinary==YES) { if (flagHeader ==NO) Errorf("WriteSigStream() : Cannot write binary values without header"); } /* Write header if asked */ if (flagHeader == YES) { fprintf(stream,"LastWave Header\n"); fprintf(stream,"size %d\n",signal->size); if (signal->name == NULL || signal->name[0] == '\0') fprintf(stream,"name none\n"); else fprintf(stream,"name %s\n",signal->name); fprintf(stream,"firstp %d\n",signal->firstp); fprintf(stream,"lastp %d\n",signal->lastp); if (flagBinary == NO) fprintf(stream,"binary no\n"); else { if (IsCPULittleEndian) fprintf(stream,"binary little %ld\n",sizeof(LWFLOAT)); else fprintf(stream,"binary big %ld\n",sizeof(LWFLOAT)); } /* 2 types of signal XYSIG or YSIG */ if (signal->type == YSIG && !strcmp(mode,"y")) {#ifdef NUMDOUBLE fprintf(stream,"dx %.16g\n",signal->dx); fprintf(stream,"x0 %.16g\n",signal->x0);#else fprintf(stream,"dx %.8g\n",signal->dx); fprintf(stream,"x0 %.8g\n",signal->x0);#endif } if (signal->type == XYSIG && strcmp(mode,"xy") && strcmp(mode,"yx")) {#ifdef NUMDOUBLE fprintf(stream,"dx %.16g\n",1.0); fprintf(stream,"x0 %.16g\n",0.0);#else fprintf(stream,"dx %.8g\n",1.0); fprintf(stream,"x0 %.8g\n",0.0);#endif } fprintf(stream,"End of Header\n"); } if (flagBinary == NO) { if (type1 == NULL) { type1 = floatType;#ifdef NUMDOUBLE format1 = "%.16g";#else format1 = "%.8g";#endif } if (type2 == NULL) { type2 = floatType;#ifdef NUMDOUBLE format2 = "%.16g";#else format2 = "%.8g";#endif } /* We then write in the file */ /* according to the mode */ if (!strcmp(mode,"y")) { sprintf(theFormat,"%s\n",format1); if (type1 == floatType) { for (i=0;i<signal->size;i++) { fprintf(stream,theFormat,signal->Y[i]); } } else { for (i=0;i<signal->size;i++) { fprintf(stream,theFormat,(int) signal->Y[i]); } } } if (!strcmp(mode,"x")) { sprintf(theFormat,"%s\n",format1); if (type1 == floatType) { for (i=0;i<signal->size;i++) { fprintf(stream,theFormat,XSig(signal,i)); } } else { for (i=0;i<signal->size;i++) { fprintf(stream,theFormat,XSig(signal,i)); } } } if (!strcmp(mode,"xy")) { sprintf(theFormat,"%s %s\n",format1,format2); if (type1 == floatType) { if (type2 == floatType) { for (i=0;i<signal->size;i++) { fprintf(stream,theFormat,XSig(signal,i),signal->Y[i]); } } else { for (i=0;i<signal->size;i++) { fprintf(stream,theFormat,XSig(signal,i),(int) signal->Y[i]); } } } else { if (type2 == floatType) { for (i=0;i<signal->size;i++) { fprintf(stream,theFormat,(int) XSig(signal,i),signal->Y[i]); } } else { for (i=0;i<signal->size;i++) { fprintf(stream,theFormat,(int) XSig(signal,i),(int) signal->Y[i]); } } } } if (!strcmp(mode,"yx")) { sprintf(theFormat,"%s %s\n",format1,format2); if (type1 == floatType) { if (type2 == floatType) { for (i=0;i<signal->size;i++) { fprintf(stream,theFormat,signal->Y[i],XSig(signal,i)); } } else { for (i=0;i<signal->size;i++) { fprintf(stream,theFormat,signal->Y[i],(int) XSig(signal,i)); } } } else { if (type2 == floatType) { for (i=0;i<signal->size;i++) { fprintf(stream,theFormat,(int) signal->Y[i],XSig(signal,i)); } } else { for (i=0;i<signal->size;i++) { fprintf(stream,theFormat,(int) signal->Y[i],(int) XSig(signal,i)); } } } } } else { /* We then write the values */ if (!strcmp(mode,"xy")) { fwrite(signal->X,sizeof(LWFLOAT),signal->size,stream); fwrite(signal->Y,sizeof(LWFLOAT),signal->size,stream); } else if (!strcmp(mode,"yx")) { fwrite(signal->Y,sizeof(LWFLOAT),signal->size,stream); fwrite(signal->X,sizeof(LWFLOAT),signal->size,stream); } if (!strcmp(mode,"x")) { fwrite(signal->X,sizeof(LWFLOAT),signal->size,stream); } if (!strcmp(mode,"y")) { fwrite(signal->Y,sizeof(LWFLOAT),signal->size,stream); } }}/* * Same as above but with a filename instead of a stream */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -