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

📄 int_listv.c

📁 LastWave
💻 C
📖 第 1 页 / 共 4 页
字号:
        lv->values[i] = NULL;      }      else lv->values[i] = vcarray[i];    }    return(lv);  }        if (lv->values[0] == NULL) {    farray = TMalloc(sizeof(LWFLOAT)*lv->length);    for (i=0;i<lv->length;i++) {      if (lv->values[i] != NULL) break;      farray[i] = lv->f[i];    }    if (i<lv->length) Errorf("Don't know how to sort");    qsort(farray,lv->length,sizeof(LWFLOAT),&qsortcmpnum);    for (i=0;i<lv->length;i++) {      lv->f[i] = farray[i];    }    return(lv);  }    if (GetTypeValue(lv->values[0]) == strType) {    vcarray = TMalloc(sizeof(VALUE)*lv->length);    for (i=0;i<lv->length;i++) {      if (lv->values[i] == NULL) break;      if (GetTypeValue(lv->values[i]) != strType) break;      vcarray[i] = lv->values[i];    }    if (i<lv->length) Errorf("Don't know how to sort");    qsort(vcarray,lv->length,sizeof(VALUE),&qsortcmpstr);    for (i=0;i<lv->length;i++) {      lv->values[i] = vcarray[i];    }    return(lv);  }  SetErrorf("SortListv() : Don't know how to sort this listv");  return(NULL);}void PrintListvColumn(LISTV lv,int colSize){  int i,l;  char *str;  static NUMVALUE nc = NULL;    if (lv->length == 0) return;    if (nc == NULL) nc = NewNumValue();    /* Computing the size of the columns if 'colSize' is not specified */  if (colSize <= 0) {    l = 0;    for (i=0;i<lv->length;i++) {      if (lv->values[i] != NULL) {        if (GetTypeValue(lv->values[i]) == strType) str = ToStrValue(lv->values[i],NO);        else str = ToStrValue(lv->values[i],YES);      }      else {        nc->f = lv->f[i];        str = ToStrValue(nc,YES);      }      if (*str == '\'' && str[strlen(str)-1] == '\'') {        str[strlen(str)-1] = '\0';        str++;      }      l = MAX(l,strlen(str));    }    colSize = l+2;  }    /* Then just print !! */  PrintStrColumn(NULL,colSize);    for (i=0;i<lv->length;i++) {    if (lv->values[i] != NULL) {      if (GetTypeValue(lv->values[i]) == strType) str = ToStrValue(lv->values[i],NO);      else str = ToStrValue(lv->values[i],YES);    }    else {      nc->f = lv->f[i];      str = ToStrValue(nc,YES);    }    if (*str == '\'' && str[strlen(str)-1] == '\'') {      str[strlen(str)-1] = '\0';      str++;    }    PrintStrColumn(str,0);  }    PrintStrColumn(NULL,-1);}/* * Read a Listv made of strings or numbers ONLY from a column file */typedef enum {  skipCol = 0,  strCol,  numCol,  signalCol,  bestCol} colType;void CReadListvStream(LISTV lv,STREAM s, int nCols, colType *ct, int nSkipLines, int firstIndex,int sizeToRead, char commentChar, char *separators1){  FILE *stream;  LISTV lv1;  int i,j,n;  LWFLOAT f;  char str[10000],*endstr,c,flagSkipLine,*str1;  int line;  SIGNAL ss;  char formatAll[1000];  char formatSkip[1000];  char separatorsAll[1000];  char ostr[1000];  char *separators;    // Dealing with separators and formats  separatorsAll[0] = '\n';  separatorsAll[1] = '\r';  separators = separatorsAll+2;  strcpy(separators,separators1);    strcpy(formatAll,"%[^");  separators1 = separatorsAll;  while (*separators1 != '\0') {    switch(*separators1)     {      case '%' : strcat(formatAll,"%%"); break;      default : strncat(formatAll,separators1,1); break;    }    separators1++;  }  strcat(formatAll,"]");    strcpy(formatSkip,"%*[");  separators1 = separators;  while (*separators1 != '\0') {    switch(*separators1)     {      case '%' : strcat(formatSkip,"%%"); break;      default : strncat(formatSkip,separators1,1); break;    }    separators1++;  }  strcat(formatSkip,"]");    /*    * Some basic tests on the stream    */  if (s->mode != StreamRead) Errorf("ReadListvStream() : The stream should be an input stream and not an output stream");   if (s->stream == NULL) Errorf("ReadListvStream() : You cannot read a listv from standard input");   stream = s->stream;       // Skip the first lines  line = 1;  while (!feof(stream) && nSkipLines != 0) {    fscanf(stream,"%*[^\n\r] ");    nSkipLines--;    line++;  }  if (nSkipLines != 0) Errorf("ReadListvStream() : too few lines in file");      // Allocation of the listv  for (i = 0; i< nCols;i++) {    if (ct[i] == skipCol) continue;    if (ct[i] != signalCol) {      lv1 = NewListv();      AppendValue2Listv(lv,(VALUE) lv1);          DeleteValue(lv1);    }    else {      ss = NewSignal();      AppendValue2Listv(lv,(VALUE) ss);          DeleteSignal(ss);    }  }    // The main loop  if (firstIndex < 0) firstIndex = 0;  while (!feof(stream) && sizeToRead != 0) {        // Skip the line until firstIndex    while(firstIndex > 0) {      fscanf(stream,"%*[^\n\r] ");      firstIndex--;      line++;    }        if (sizeToRead >0) sizeToRead--;        flagSkipLine = NO;        // Loop on the column    for (j = 0, i = 0; i<nCols;i++) {      if (feof(stream)) break;            if (flagSkipLine) {        fscanf(stream,"%*[^\n\r] ");        i = nCols-1;        continue;      }                  str[0] = '\0';      // We read everything up to the next separator (any separator) nor included !      fscanf(stream,formatAll,str);      str1 = str;            // We take out the leading spaces      while (str1[0] == ' ') str1++;            // If we are in the first column and if we get a commentChar ==> we must skip the line      if (i==0 && commentChar != 0 && str1[0] == commentChar) {        flagSkipLine = YES;         continue;      }      switch(ct[i]) {                    //***************************          // CASE OF A 'x' CHARACTER          //***************************        case skipCol :                               j-=1;                    break;                    //*****************************          // CASE OF A 's' CHARACTER          //*****************************          case strCol :                               // We must store the string          lv1 = (LISTV) lv->values[j];          AppendStr2Listv(lv1,str);                    break;                    //*****************************          // CASE OF A 'f' CHARACTER          //*****************************          case numCol :                               // We must read a number from this string#ifdef NUMDOUBLE          n = sscanf(str1,"%lf%s",&f,ostr);#else          n = sscanf(str1,"%f%s",&f,ostr);#endif                    // If we read 0 argument ==> error          if (n == 0) Errorf("ReadListvStream() : line %d : column %d is not a number",line,i+1);                    // If we read 2 arguments ==> error          if (n == 2) Errorf("ReadListvStream() : line %d : column %d is a number followed by a string",line,i+1);                    // Oterwise, we must store the number          lv1 = (LISTV) lv->values[j];          AppendFloat2Listv(lv1,f);                    break;                              //*****************************          // CASE OF A 'F' CHARACTER          //*****************************          case signalCol :                     #ifdef NUMDOUBLE          n = sscanf(str1,"%lf%s",&f,ostr);#else          n = sscanf(str1,"%f%s",&f,ostr);#endif                    // If we read 0 argument ==> error          if (n == 0) Errorf("ReadListvStream() : line %d : column %d is not a number",line,i+1);                    // If we read 2 arguments ==> error          if (n == 2) Errorf("ReadListvStream() : line %d : column %d is a number followed by a string",line,i+1);                    // Oterwise, we must store the string          ss = (SIGNAL) lv->values[j];          AppendFloat2Signal(ss,f);                    break;                    //*****************************          // CASE OF A '?' CHARACTER          //*****************************          case bestCol :                               // We try to read a number from this string#ifdef NUMDOUBLE          n = sscanf(str1,"%lf%s",&f,ostr);#else          n = sscanf(str1,"%f%s",&f,ostr);#endif                    // If we read 0 or 2, we do not have a number, we have a string          lv1 = (LISTV) lv->values[j];          if (n == 0 || n==2) AppendStr2Listv(lv1,str);          // Otherwise, we have a number          else AppendFloat2Listv(lv1,f);                              break;      }            // Increment the listv index      j++;            // Take out the separator      c = fgetc(stream);      fscanf(stream,"%*[ \t]");            // Case the separator is a regular separator ==> we must check we do not expect the line to be over      if (strrchr(separators,c) != NULL) {        if (nCols-1 == i) Errorf("ReadListvStream() : line %d : more than %d columns",line,nCols);      }            // Case the separator is a \n or \r ==> we must check we expect the line to be over      else {        if (nCols-1 != i) Errorf("ReadListvStream() : line %d : less than %d columns",line,nCols);        fscanf(stream,"%*[\n\r]"); // we read extra new lines        line+=1;      }    }  }  }/* * Write a Listv made of listv, strings or numbers/signals/images into a file (each element is stored one after the other) */void WriteListvStream(LISTV lv,STREAM s, char flagBinary){  int i;  VALUE val;  LWFLOAT f;  SIGNAL sig;  IMAGE im;  LISTV lv1;  char *type;  char *str;  FILE *stream;    /*    * Some basic tests on the stream    */  if (s->mode != StreamWrite) Errorf("WriteListvStream() : The stream should be an output stream and not an input stream");   stream = s->stream;       /*   * Writing a header   */  fprintf(stream,"LastWave 2.1 Listv\n");   fprintf(stream,"length = %d\n",lv->length);  if (flagBinary == NO) fprintf(stream,"binary no\n");  else {    if (IsCPULittleEndian) fprintf(stream,"binary little %d\n",sizeof(LWFLOAT));    else fprintf(stream,"binary big %d\n",sizeof(LWFLOAT));  }    /* If length 0 --> that's it! */   if (lv->length == 0) return;    /* Otherwise we loop on the elements */  for (i = 0;i<lv->length;i++) {        /* Get the type of the element */    type = GetListvNth(lv,i,&val,&f);    fprintf(stream,"\n#>> Start Element #%d\n",i);    fprintf(stream,"type = %s\n",type);        /* Case of a string */    if (type == strType) {      str = GetListvNthStr(lv,i);      fprintf(stream,"size = %d\n",strlen(str));      fprintf(stream,"|%s|\n",str);    }        /* Case of a number */    else if (type == numType) {      if (!flagBinary) {        f = GetListvNthFloat(lv,i);#ifdef NUMDOUBLE	        fprintf(stream,"%.16g\n",f);#else        fprintf(stream,"%.8g\n",f);#endif      }      else {        fwrite(&f,sizeof(LWFLOAT),1,stream);              fprintf(stream,"\n");      }    }        /* Case of a signal */    else if (type == signalType || type == signaliType) {      sig = (SIGNAL) val;      WriteSigStream(sig,s,flagBinary,"",YES,"","");      fprintf(stream,"\n");    }        /* Case of an image */    else if (type == imageType || type == imageiType) {      im = (IMAGE) val;      WriteImageStream(im,s,NO,YES,flagBinary,"","",NO,0,0);      fprintf(stream,"\n");    }        /* Case of an listv */    else if (type == listvType) {      lv1 = (LISTV) val;      WriteListvStream(lv1,s,flagBinary);    }        /* Other types --> error */    else {      Errorf("WriteListvStream() : Invalid element type '%s' : %s",type);        return;    }        fprintf(stream,"##<< Stop Element #%d\n",i);  }}   /* * Read a Listv made of listv, strings or numbers/signals/images from a file (each element is stored one after the other) */static void SkipCharsStream(FILE *stream, char flagNewLine);static void Skip1LineStream(FILE *stream){  fscanf(stream,"%*[^\n\r]");  SkipCharsStream(stream,NO);}static char *Read1LineStream(FILE *stream){  static char str[10000];    fscanf(stream,"%[^\n\r]",&str);  SkipCharsStream(stream,NO);    return(str);}static void SkipCharsStream(FILE *stream, char flagNewLine){  char c;    while (1) {    c = (char) fgetc(stream);    if (c == ' ') {      flagNewLine = NO;      continue;    }    if  (c == '\n' || c == '\r') {      flagNewLine = YES;      continue;    }    if (c == '#' && flagNewLine) {      Skip1LineStream(stream);      flagNewLine = NO;      continue;    }    ungetc(c,stream);    break;  }}void ReadListvStream(LISTV lv,STREAM s){  char flagBinary;  int i,n,size;  LWFLOAT g;  float f;  double d;  SIGNAL sig;  IMAGE im;  LISTV lv1;  char type[100],*str;  FILE *stream;  int endmode,floatSize;  extern void PrintSignal(SIGNAL signal);    /*    * Some basic tests on the stream    */  if (s->mode != StreamRead) Errorf("ReadListvStream() : The stream should be an input stream and not an output stream");   stream = s->stream;       /*   * Reading the header   */    /* Skip the first line */   Skip1LineStream(stream);    if (fscanf(stream,"length = %d",&n) != 1) Errorf("ReadListvStream() : Expect header line starting by the word 'length'");  SkipCharsStream(stream,NO);  SetLengthListv(lv,0);    /* If length 0 --> that's it! */   if (n == 0) return;    str = Read1LineStream(stream);  if (strncmp("binary ",str,7)) Errorf("ReadListvStream() : Expect header line starting by the word 'binary'");  str += 7;  if (!strcmp(str,"no")) flagBinary = NO;  else {    flagBinary = YES;    if (!strncmp("little ",str,7)) {      endmode = BinaryLittleEndian;      str+=7;    }    else if (!strncmp("big ",str,4)) {      endmode = BinaryBigEndian;      str+=4;    }    else Errorf("ReadListvStream() : Error in header 'binary' line");    if (sscanf(str,"%d",&floatSize) != 1) Errorf("ReadListvStream() : Error in header 'binary' line");

⌨️ 快捷键说明

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