⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 signal_file.c

📁 LastWave
💻 C
📖 第 1 页 / 共 4 页
字号:
   */  if (flagBinary == NO) {    if (IsCPULittleEndian) {      flagLittleEndian = YES;      Warningf("ReadSigStream() : (old Signal Package 1.1.2 header) You should update the format (writing the file again using 'write'). I am assuming the values are binary coded (little endian).");    }    else {      flagLittleEndian = NO;      Warningf("ReadSigStream() : (old Signal Package 1.1.2 header) You should update the format (writing the file again using 'write'). I am assuming the values are binary coded (big endian).");    }    flagBinary = YES;    fsetpos(stream,&beginning);  }      /* CASE OF A BINARY FILE  (there should be a header, otherwise you should use the ReadSigRawStream function) */    if (flagHeader == NO) Errorf("ReadSigStream() : It looks like the stream contains binary coded values with no header. I can't read that");    /*   * Check firstIndex and sizeToRead    */  if (firstIndex < 0) firstIndex = 0;  if (sizeToRead == -1) sizeToRead = size - firstIndex;  if (firstIndex+sizeToRead > size) Errorf("ReadSigStream() : Either firstIndex or sizeToRead is too big");    /* Case XY */    if (flagXY) {        /* Set signal size */    SizeSignal(signal,sizeToRead,XYSIG);    if (fseek(stream,firstIndex*nsize,SEEK_CUR) != 0)           Errorf("ReadSigStream() : firstIndex seems to be too large for this file");        /* Set the array the data must be read in */    if (sizeof(LWFLOAT) == nsize) array = signal->X;    else array = Malloc(nsize*sizeToRead);          /* Read the data */    if (fread(array,sizeof(LWFLOAT),sizeToRead,stream) != sizeToRead)      Errorf("ReadSigStream() : sizeToRead seems to be too large for this file");        /* Big/Little conversions */    if ((flagLittleEndian && IsCPUBigEndian) || (!flagLittleEndian && 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->X[i] = ((float *) array)[i];        else signal->X[i] = ((double *) array)[i];      }      Free(array);    }        /* Move to the Y data */      fseek(stream,size-sizeToRead-firstIndex,SEEK_CUR);    fseek(stream,firstIndex*nsize,SEEK_CUR);        /* 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,sizeof(LWFLOAT),sizeToRead,stream) != sizeToRead)      Errorf("ReadSigStream() : sizeToRead seems to be too large for this file");        /* Big/Little conversions */    if ((flagLittleEndian && IsCPUBigEndian) || (!flagLittleEndian && 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);    }        /* Go to the end of the signal */    fseek(stream,size-sizeToRead-firstIndex,SEEK_CUR);  }    /* Case Y */    else {        /* Set signal size */    SizeSignal(signal,sizeToRead,YSIG);    if (fseek(stream,firstIndex*nsize,SEEK_CUR) != 0)           Errorf("ReadSigStream() : 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("ReadSigStream() : sizeToRead seems to be too large for this file");        /* Big/Little conversions */    if ((flagLittleEndian && IsCPUBigEndian) || (!flagLittleEndian && 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);    }        /* Go to the end of the signal */    fseek(stream,size-sizeToRead-firstIndex,SEEK_CUR);  }    /* Some settings */                if (strcmp(name,"none")) SetNameSignal(signal,name);  else SetNameSignal(signal,NULL);  if (flagXY == NO) {    signal->dx = dx;    signal->x0 = x0;  }  signal->firstp = firstp;  signal->lastp = lastp;}/* Same as above but with a filename instead of a stream */void ReadSigFile(SIGNAL signal,char *filename,  int firstIndex,int sizeToRead, int xcol,int ycol){  STREAM stream;    /* Open file */  stream = OpenFileStream(filename,"r");  if (stream == NULL) Errorf("ReadSigFile() : Error while opening the file %s",filename);    ReadSigStream(signal,stream,firstIndex,sizeToRead, xcol,ycol);	  CloseStream(stream);	    if (!strcmp(signal->name,"")) SetNameSignal(signal,filename);}/* * The corresponding commands  */void C_Read(char **argv){	SIGNAL signal;	char *filename,*mode;  int xcol,ycol,nsize;	char flagHeader,flagRaw,binaryCoding;	int firstIndex,sizeToRead;  char opt;  STREAM stream;  	argv = ParseArgv(argv,tSIGNAL,&signal,-1);	  argv = ParseArgv(argv,tSTREAM_,NULL,&stream,-1);    if (stream == NULL) argv = ParseArgv(argv,tSTR,&filename,-1);  else filename = NULL;    argv = ParseArgv(argv,tINT_,-2,&xcol,tINT_,-2,&ycol,-1);		if (xcol == -2 && ycol == -2) xcol = ycol = 0;	else if (ycol == -2) {	  ycol = xcol;	  xcol = -1;	}	else if (xcol < 0 || ycol < 0) Errorf("Bad <xcol> or <ycol> value");		firstIndex = 0;	sizeToRead = -1;	  /* options */  flagHeader = YES;  flagRaw = NO;  while(opt = ParseOption(&argv)) {     switch(opt) {      case 'f':        argv = ParseArgv(argv,tINT,&firstIndex,-1);        if (firstIndex < 0) ErrorUsage1();           break;      case 's':         argv = ParseArgv(argv,tINT,&sizeToRead,-1);        if (sizeToRead <= 0) ErrorUsage1();           break;      case 'r':        if (*argv != NULL && '0'<=**argv && **argv<='9') {          argv = ParseArgv(argv,tINT_,0,&nsize,-1);          mode = "";        }        else argv = ParseArgv(argv,tSTR_,"",&mode,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) {    if (stream == NULL) ReadSigRawFile(signal,filename,firstIndex,sizeToRead,binaryCoding,nsize);    else ReadSigRawStream(signal,stream,firstIndex,sizeToRead,binaryCoding,nsize);    return;  }    if (stream == NULL) ReadSigFile(signal,filename,firstIndex,sizeToRead,xcol,ycol);  else ReadSigStream(signal,stream,firstIndex,sizeToRead,xcol,ycol);}void C_ReadInfo(char **argv){	struct signal siginfo;	char *filename,header,flagBinary,binaryCoding,*type;	int nCols,nsize;	char flagPrint;  char opt;  STREAM stream;  LISTV lv,lv1;    argv = ParseArgv(argv,tSTREAM_,NULL,&stream,-1);    if (stream == NULL) argv = ParseArgv(argv,tSTR,&filename,-1);  else filename = NULL;      /* options */  flagPrint = NO;  while(opt = ParseOption(&argv)) {     switch(opt) {      case 'p':        flagPrint = YES;        break;      default: ErrorOption(opt);    }  }      NoMoreArgs(argv);    if (filename) {    if (ReadInfoSigFile(filename, &siginfo, &header,&flagBinary, &binaryCoding, &nCols,&nsize)==NO) {      if (flagPrint) Printf("Filename '%s' cannot be read using the 'read' command\n",filename);      return;    }  }  else {    if (ReadInfoSigStream(stream, &siginfo, &header,&flagBinary, &binaryCoding, &nCols,&nsize)==NO) {      if (flagPrint) Printf("Filename '%s' cannot be read using the 'read' command\n",filename);      return;    }  }      if (flagPrint) {    if (filename) {      if (header) Printf("Filename '%s' has a LastWave header\n",filename);      else Printf("Filename '%s' has no header\n",filename);    }    else {      if (header) Printf("Stream has a LastWave header\n");      else Printf("Stream has no header\n");    }    if (header) {      if (siginfo.type == YSIG) {        if (flagBinary == YES) {          if (binaryCoding == BinaryLittleEndian) Printf("YSIG (Binary coded, little endian)\n");          else Printf("YSIG (Binary coded, big endian)\n");          if (nsize == sizeof(float)) type = "float";          else if (nsize == sizeof(double)) type = "double";          else type = "???";          Printf("Float size is %d bytes (C-type is '%s')\n",nsize,type);         }        else  Printf("YSIG (ascii coded)\n");        Printf("size   : %d\n",siginfo.size);#ifdef NUMDOUBLE                 Printf("x0     : %.16g\n",siginfo.x0);        Printf("dx     : %.16g\n",siginfo.dx);#else        Printf("x0     : %.8g\n",siginfo.x0);        Printf("dx     : %.8g\n",siginfo.dx);#endif        Printf("firstp : %d\n",siginfo.firstp);        Printf("lastp  : %d\n",siginfo.lastp);      }      else {        if (flagBinary == YES) {          if (binaryCoding == BinaryLittleEndian) Printf("XYSIG (Binary coded, little endian)\n");          else Printf("XYSIG (Binary coded, big endian)\n");          if (nsize == sizeof(float)) type = "float";          else if (nsize == sizeof(double)) type = "double";          else type = "???";          Printf("Float size is %d bytes (C-type is '%s')\n",nsize,type);         }        else  Printf("XYSIG (ascii coded)\n");        Printf("size   : %d\n",siginfo.size);        Printf("firstp : %d\n",siginfo.firstp);        Printf("lastp  : %d\n",siginfo.lastp);      }    }    else {      Printf("%d Columns (ascii coded)\n",nCols);      Printf("size   : %d\n",siginfo.size);    }  }    else {    lv = TNewListv();    SetResultValue(lv);    if (header) {      if (siginfo.type == YSIG) {        AppendInt2Listv(lv,1);        if (flagBinary == YES) {          if (binaryCoding==BinaryLittleEndian) AppendStr2Listvf(lv,"binary little %d",nsize);          else AppendStr2Listvf(lv,"binary big %d",nsize);        }        else {          AppendStr2Listv(lv,"ascii");        }        AppendInt2Listv(lv,siginfo.size);        lv1 = TNewListv();        AppendValue2Listv(lv,(VALUE) lv1);        AppendStr2Listv(lv1,"y");        AppendFloat2Listv(lv1,siginfo.x0);        AppendFloat2Listv(lv1,siginfo.dx);        lv1 = TNewListv();        AppendValue2Listv(lv,(VALUE) lv1);        AppendFloat2Listv(lv1,siginfo.firstp);        AppendFloat2Listv(lv1,siginfo.lastp);      }      else {        if (flagBinary == YES) {          AppendInt2Listv(lv,1);          if (binaryCoding==BinaryLittleEndian) AppendStr2Listvf(lv,"binary little %d",nsize);          else AppendStr2Listvf(lv,"binary big %d",nsize);        }        else {          AppendStr2Listv(lv,"ascii");        }        AppendInt2Listv(lv,siginfo.size);        AppendStr2Listv(lv,"xy");        lv1 = TNewListv();        AppendValue2Listv(lv,(VALUE) lv1);        AppendFloat2Listv(lv1,siginfo.firstp);        AppendFloat2Listv(lv1,siginfo.lastp);      }    }    else {            AppendInt2Listv(lv,0);      AppendInt2Listv(lv,siginfo.size);    }  } }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -