示例#1
0
bool cReadObject::Decode(const char * aLine)
{
   mNumLine++;
   if (aLine[0] == mComC)
      return false;
    if (LineIsBlank(aLine))
      return false;
    const char * aL = aLine;
    const char * aF = mFormat.c_str();
    while (true)
    {
        // const char * aL0 = aL;
        // const char * aF0 = aF;
        std::string aSymb = GetNextStr(aF);
        std::string aVal = GetNextStr(aL);

        if ((aSymb=="") != (aVal==""))
        {
            std::cout << "\n\nAT LINE " << mNumLine << "\n";
            std::cout << "Format =[" << mFormat << "]\n";
            std::cout << "Line =[" <<  aLine << "]\n";
            ELISE_ASSERT(false,"Incoherence between format and line");
        }

        if (aSymb=="")
        {
           return true;
        }

        if (aSymb!=mSymbUnknown)
        {
           std::map<std::string,double *>::iterator iTD = mDoubleLec.find(aSymb);
           std::map<std::string,std::string *>::iterator iTS = mStrLec.find(aSymb);
           if (iTD!=mDoubleLec.end())
           {
                   sscanf(aVal.c_str(),"%lf",iTD->second);
           }
           else if (iTS!=mStrLec.end())
           {
                *(iTS->second) = aVal;
           }
           else
           {
                std::cout << "\n\nAT LINE " << mNumLine << "\n";
                std::cout << "For Symb=[" << aSymb << "]\n";
                ELISE_ASSERT(false,"Symbole is not is not handled");
           }
        }
    }
}
int GetFieldCount(char *fname)
{
    int i;
    FILE *fd;
    char *s;
    char *str;
    char *next_str;
    char line_buff[255];
    int err;
    char *ferr;
    int fcount;

    fd = fopen(fname,"r");
    if(fd == NULL)
    {
        fprintf(stderr,"GetFieldCount could open %s for reading\n",
                fname);
        fflush(stderr);
        return(0);
    }

    while(!feof(fd))
    {
        ferr = fgets(line_buff,sizeof(line_buff),fd);
        if(ferr == NULL)
        {
            break;
        }
        if(line_buff[0] == '#')
        {
            continue;
        }
        if(line_buff[0] == '\n')
        {
            continue;
        }
        fcount = 0;
        str = line_buff;
        while(GetNextStr(str,&next_str))
        {
            fcount++;
            str = next_str;
        }
        break;

    }

    fclose(fd);

    return(fcount);
}
示例#3
0
cReadObject::cReadObject(char aComCar,const std::string & aFormat,const std::string & aSymbUnknown) :
    mComC        (aComCar),
    mFormat      (aFormat),
    mSymbUnknown (aSymbUnknown),
    mNumLine     (0)
{
    const char * aF = aFormat.c_str();

    std::string aSF;
    bool cont = true;
    while (cont)
    {
        aSF = GetNextStr(aF);
        if (aSF!="")
            mSFormat.insert(aSF);
        else
          cont = false;
    }
}
int LoadDataSet(char *fname, void *cookie)
{
    DataSet *ds = (DataSet *)cookie;
    int i;
    FILE *fd;
    char *s;
    char *next_s;
    int count;
    char line_buff[255];
    int err;
    char *ferr;

    if(cookie == NULL)
    {
        return(0);
    }

    fd = fopen(fname,"r");
    if(fd == NULL)
    {
        fprintf(stderr,"LoadDataSet could open %s for reading\n",
                fname);
        fflush(stderr);
        return(0);
    }

    count = 0;
    while(!feof(fd))
    {
        ferr = fgets(line_buff,sizeof(line_buff),fd);
        if(ferr == NULL)
        {
            break;
        }
        if(line_buff[0] == '#')
        {
            continue;
        }
        if(line_buff[0] == '\n')
        {
            continue;
        }
        count++;
    }
    rewind(fd);

    while(count > ds->space_size)
    {
        ExpandData((void *)ds);
    }

    ds->current = 0;
    while(!feof(fd))
    {
        memset(line_buff,0,sizeof(line_buff));
        ferr = fgets(line_buff,sizeof(line_buff),fd);
        if(ferr == NULL)
        {
            break;
        }
        if(line_buff[0] == '#')
        {
            continue;
        }
        if(line_buff[0] == '\n')
        {
            continue;
        }
        count++;
        for(i=0; i < ds->fields; i++)
        {
            ds->data[i][ds->current] = 0.0;
        }
        s = line_buff;
        for(i=0; i < ds->fields; i++)
        {
            s = GetNextStr(s,&next_s);
            if(s == NULL)
            {
                break;
            }
            ds->data[i][ds->current] = strtod(s,NULL);
            free(s);
            s = next_s;
        }
        ds->current++;
        ds->data_c++;
    }

    /*
     * reset the current pointer
     */
    ds->current = 0;
    fclose(fd);

    return(1);
}