Changeset b45f010 in tol


Ignore:
Timestamp:
May 6, 2009, 8:47:13 AM (17 years ago)
Author:
Víctor de Buen <vdebuen@…>
Branches:
1941-docker-image-to-test, devel, master
Children:
9a4c371
Parents:
81c5ba7
Message:

Adding new optional parameters to IncludeBMT in order to allow free formated matrices

git-svn-id: ​https://www.tol-project.org/svn/tolp/trunk@978 be4846ae-f051-483a-831f-b64e7d08527f

File:
1 edited

Legend:

Unmodified
Added
Removed
  • tol/btol/set_type/setgrainc.cpp

    r81c5ba7 rb45f010  
    633633//--------------------------------------------------------------------
    634634DeclareContensClass(BSet, BSetFromFile, BSetIncludeBMT);
    635 DefExtOpr(1, BSetIncludeBMT, "IncludeBMT", 1, 1, "Text",
    636           I2("(Text fileName)", "(Text nombreFichero)"),
     635DefExtOpr(1, BSetIncludeBMT, "IncludeBMT", 1, 4, "Text Text Text Text",
     636          "(Text fileName [, Text cellDelim=\";\", Text rowDelim=\"\\n\", Text skip=\"\"])",
    637637          I2("Returns a set with the real matrix from a Bayes Matrix "
    638638             "Time Table (BMT) file.\n"
    … …  
    668668//--------------------------------------------------------------------
    669669{
    670 
    671     BText fileName=Text(Arg(1));
    672     ifstream fd(fileName.String(), OPENFLAG);
    673     if (fd.fail())
    674     {
    675         Error(I2(BText("Could not open file ")+fileName,
    676                  BText("No se pudo abrir el fichero ")+fileName));
    677         return;
    678     }
    679     char *buffer;
    680     char *aux;
    681     int i,j,k;
    682     int cols=0;
    683     unsigned int rows=0;
     670  BText fileName=Text(Arg(1));
     671  size_t fileSize = GetFileSize(fileName);
     672  BText cellDelim=";";
     673  BText rowDelim="\n";
     674  BText skip="";
     675  if(Arg(2)) { cellDelim = Text(Arg(2)); }
     676  if(Arg(3)) { rowDelim  = Text(Arg(3)); }
     677  if(Arg(4)) { skip      = Text(Arg(4)); }
     678  ifstream fd(fileName.String(), OPENFLAG);
     679  if (fd.fail())
     680  {
     681    Error(I2(BText("Could not open file ")+fileName,
     682             BText("No se pudo abrir el fichero ")+fileName));
     683    return;
     684  }
     685  size_t k=0; 
     686  int i=0,j=0;
     687  int cols=0;
     688  unsigned int rows=0;
    684689   
     690  char read;
     691  char *buffer = new char[MaxLineLength];
     692  BUserMat* uMat = NULL;
     693  if(Arg(2))
     694  {
     695    int pos = 0;
     696    size_t size = 0;
     697    BArray<BDat> x(1024);
     698    while(!fd.eof())
     699    {
     700      k++;
     701      if(pos==MaxLineLength-1)
     702      {
     703        Error(I2("Cell maximum length ","Tamaño máximo de celda ")+MaxLineLength+
     704              I2(" exceeded  at column "," excedido en la columna ")+j+
     705              I2(" iof row "," de la fila ")+i+
     706              I2(" of file "," del fichero ")+fileName.String());
     707        rows = cols = 0; break;
     708      };
     709      fd.get(read);
     710      bool isRowDelim  = strchr(rowDelim .String(), read)!=NULL;
     711      bool isCellDelim = strchr(cellDelim.String(), read)!=NULL;
     712      bool skipedLastCellDelim = isRowDelim&&pos;
     713      if(isCellDelim||skipedLastCellDelim)
     714      {
     715        buffer[pos++] = 0;
     716        if(size>x.MaxSize()-1)
     717        {
     718          int expected_size = Minimum(100*size, Maximum(2*size, ((fileSize*(size+1))/k)));
     719          x.ReallocBuffer(expected_size);
     720        }
     721        x.ReallocBuffer(size+1);
     722        x[size].PutValue(buffer);
     723        size++;
     724        pos = 0;
     725        buffer[0] = 0;
     726        j++;
     727        if(!skipedLastCellDelim) { continue; }
     728      }
     729      if(isRowDelim)
     730      {
     731        if(i==0) { cols = j; }
     732        else if(j&&(cols!=j))
     733        {
     734          Error(I2("The row number", "La fila número")+" "+(i+1)+" "+
     735                I2("has","tiene")+" "+j+" columns "+
     736                I2("but it was expected","pero se esperaban")+" "+cols+" "+
     737                I2(" on file "," en el fichero ")+fileName.String());
     738          rows = cols = 0; break;
     739        }
     740        if(j)
     741        { 
     742          j=0;
     743          i++;
     744        }
     745        continue;
     746      }
     747      if(!strchr(skip.String(), read))
     748      {
     749        buffer[pos++] = read;
     750      }
     751    }
     752    if(pos)
     753    {
     754      buffer[pos++] = 0;
     755      if(size>x.MaxSize()-1)
     756      {
     757        int expected_size = Minimum(100*size, Maximum(2*size, ((fileSize*(size+1))/k)));
     758        x.ReallocBuffer(expected_size);
     759      }
     760      x.ReallocBuffer(size+1);
     761      x[size].PutValue(buffer);
     762      size++;
     763      pos = 0;
     764      buffer[0] = 0;
     765      j++;
     766      if(i==0) { cols = j; }
     767      else if(j&&(cols!=j))
     768      {
     769        Error(I2("The row number", "La fila número")+" "+(i+1)+" "+
     770              I2("has","tiene")+" "+j+" columns "+
     771              I2("but it was expected","pero se esperaban")+" "+cols+" "+
     772              I2(" on file "," en el fichero ")+fileName.String());
     773        rows = cols = 0;
     774      }
     775      if(j)
     776      { 
     777        j=0;
     778        i++;
     779      }
     780    }
     781    if(!i && size) { cols = j; rows = 1; }
     782    else           { rows = i; }
     783    BMatrix<BDat> m(rows,cols,x.GetBuffer());
     784    uMat = new BContensMat("", m, GetFilePrefix(TolPath()));
     785  }
     786  else
     787  {
     788    //VBR: This code is maintained in order to ensure backward compatibility
     789    //but it should be eliminated in next versions
     790
    685791    //As long as modifying matrix row number is not possible,
    686792    //we have no choice but counting row number first and
    687793    //then parse the file. Horrible!
    688    
     794    char *aux;
    689795    //First, count columns and buffer size:
    690     char read;
    691     while(fd.get(read) && (read!='\n'))
    692     {
    693         if (read == ';') cols++;
    694     }
    695     buffer=new char[MaxLineLength];
     796    while(fd.get(read) && (read!=rowDelim[0]))
     797    {
     798      if (read == cellDelim[0]) cols++;
     799    }
    696800   
    697801    //Count rows:
    698     while(fd.ignore(MaxLineLength,'\n')) rows++;
    699 
    700     /* This MACRO is necessary for VC++ compilers. If you use a .NET
    701      * compiler, comment out the next three lines.
    702      * See: http://support.microsoft.com/default.aspx?scid=kb;EN-US;q240015
    703      * for info on this issue. */
    704 # if defined(_MSC_VER) && (_MSC_VER<1400)
     802    while(fd.ignore(MaxLineLength,rowDelim[0])) rows++;
     803
     804    // This MACRO is necessary for VC++ compilers. If you use a .NET
     805    // compiler, comment out the next three lines.
     806    // See: http://support.microsoft.com/default.aspx?scid=kb;EN-US;q240015
     807    // for info on this issue.
     808    # if defined(_MSC_VER) && (_MSC_VER<1400)
    705809    rows++;
    706 # endif
     810    # endif
    707811    BMatrix<BDat> m(rows,cols);
    708812   
    … …  
    713817    for(i=0;i<rows;i++)
    714818    {
    715         fd.getline(buffer,MaxLineLength);
    716         //Parse line:
    717         aux=&(buffer[0]);
    718         for(j=k=0;j<cols;j++)
    719         {
    720     char* ptr = (char*)aux;
    721     for(; k<MaxLineLength-1; k++)
    722     {
    723       if(aux[0] && (aux[0]!=';') && !isspace((unsigned char)aux[0]))
    724       {
    725         ++aux;
     819      fd.getline(buffer,MaxLineLength, rowDelim[0]);
     820      //Parse line:
     821      aux=&(buffer[0]);
     822      for(j=k=0;j<cols;j++)
     823      {
     824        char* ptr = (char*)aux;
     825        for(; k<MaxLineLength-1; k++)
     826        {
     827          if(aux[0] && (aux[0]!=cellDelim[0]) && !isspace((unsigned char)aux[0]))
     828          {
     829            ++aux;
     830          }
     831          else
     832          {
     833            break;
     834          }
     835        }
     836        if(k==MaxLineLength-1)
     837        {
     838          Error(I2("Line maximum length ","Tamaño máximo de línea ")+MaxLineLength+
     839                I2(" exceeded  at column "," excedido en la columna ")+j+
     840                I2(" iof row "," de la fila ")+i+
     841                I2(" of file "," del fichero ")+fileName.String());
     842        };
     843        aux[0]='\0';
     844        aux++;
     845        m(i,j).PutValue(ptr);
    726846      }
    727       else
    728       {
    729         break;
    730       }
    731     }
    732     if(k==MaxLineLength-1)
    733     {
    734       Error(I2("Line maximum length ","Tamaño máximo de línea ")+MaxLineLength+
    735             I2(" exceeded  at column "," excedido en la columna ")+j+
    736             I2(" iof row "," de la fila ")+i+
    737             I2(" of file "," del fichero ")+fileName.String());
    738     };
    739     aux[0]='\0';
    740     aux++;
    741     m(i,j).PutValue(ptr);
    742         }
    743     }
    744     delete(buffer);
    745     fd.close();
    746     BUserMat* uMat = new BContensMat("", m, GetFilePrefix(TolPath()));
    747     contens_.RobStruct(NCons(uMat),NIL,BSet::BMTFile);
    748 }
     847    }
     848    uMat = new BContensMat("", m, GetFilePrefix(TolPath()));
     849  }
     850  contens_.RobStruct(NCons(uMat),NIL,BSet::BMTFile);
     851  delete(buffer);
     852  fd.close();
     853}
     854
    749855
    750856
Note: See TracChangeset for help on using the changeset viewer.