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

📄 clsconfigfile.cpp

📁 set of c++ classes for read/write access to ascii files.
💻 CPP
📖 第 1 页 / 共 3 页
字号:
    read(m_iConfFile , cFileBuffer , lSize);    close (m_iConfFile);    // sad ono sta je u memoriji ponovo zapisemo u file    // ponovo otvaramo file i preko trunc otkinemo nepotreban dio    if ( cNewFile )    {       if ( ( m_iConfFile = open ( cNewFile , O_RDWR|O_CREAT|O_TRUNC , S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH ) ) == -1 )       {            delete [] cFileBuffer;            return false;       }    }    else    {       if ( ( m_iConfFile = open ( m_sFileName.c_str() , O_RDWR|O_CREAT|O_TRUNC , S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH ) ) == -1 )       {            delete [] cFileBuffer;            return false;       }    }    lseek ( m_iConfFile , 0 , SEEK_SET );		    // definiramo iLineStart za pocetak zapisivanja    // postavljamo enter kao zadnji znak u bufferu    int iLineStart = 0;    std::string sSeqName = "";    cFileBuffer[lSize]=0x0a;    for (int i=0; i<lSize; i++)    {    	        if ( cFileBuffer[i] == 0x0A )        {            // predaj funkciji WriteLine sadrzaj do entera            cFileBuffer[i] = 0x00;            WriteLine( sSeqName, cFileBuffer + iLineStart , i - iLineStart );            iLineStart = i+1;        }   	 	    }    // nakon sto smo predali liniju znakova    // zelimo zapisati sekciju i ime sekcije    int iWriteSeqName = true;    for( oIter = m_lsConfigSeq.begin() ; oIter != m_lsConfigSeq.end() ; ++oIter )    {        // iWriteSeqName kaze jesu li varijable vec upisane        (*oIter).WriteSeq( iWriteSeqName, m_iConfFile);    }    delete [] cFileBuffer;    return true;}//#############################################################################int  	clsConfigFile::WriteLine  		(std::string& sSeqName, const char* cStart ,long lSize ){    // zelimo upisati liniju znakova predanih iz funkcije WriteFile    long lCommentSize = 0 ;    int iHashInSeq = false;    int iHashInQuote = false;    const char* pcHash = 0;    // sad ispitujemo da # (hash) nije unutar sekcije ili navodnika     for (int i=0; i<lSize; i++)     {         if (cStart[i] == '[')         {             iHashInSeq = true;             continue;         }         else if (cStart[i] == ']')         {             iHashInSeq = false;             continue;         }         else if (cStart[i] == '"' )         {             iHashInQuote = ! iHashInQuote;             continue;         }        if ( cStart[i] == '#' && !iHashInSeq && !iHashInQuote )        {            pcHash = &cStart[i];            break;        }     }     // nakon sto smo odredili poziciju validnog hasha (nije u sekciji ili navodnicima)     // odrezemo taj dio iz buffera i dalje radimo sa linijom umanjenom za komentar    if (pcHash)    {        lCommentSize = lSize - (pcHash - cStart);        lSize = pcHash - cStart ;    }    eStateType iState = eStartLine;    std::string sVarName;    std::string sVarIndex;    std::string sVarString;    int iEndLine = false;    int iWriteSeqName;     // sad preko switcha i stanja zapisujemo sadrzaj linije bez komentara, znak po znak    for (int i=0; i<lSize; i++)    {    	        switch ( iState )        {            case eStartLine:   	            if (cStart[i] == ' ' || cStart[i] == 0x0A)   	            {   	                continue;   	            }   	            else if (cStart[i] == '"' )   	            {   	                sVarName = "";   	                iState = eNameRead;   	                continue;   	            }   	   	            // kad naidjemo na bracket zelimo zapisati PRIJASNJU sekciju   	            // jer ne znamo je li u medjuvremenu dodana koja varijabla unutar nje   	            // pa se sekcija zapise tek kad naidjemo na pocetak nove   	   	            else if (cStart[i] == '[' )   	            {   	                // ispisi sekciju (ne zelimo i ime sekcije jer smo ga zapisali prije)   	                iWriteSeqName = false;   	                WriteSeq ( iWriteSeqName, sSeqName);   	                sSeqName = "";   	                iState = eSeqNameRead;   	                continue;   	            }   	   	   	        sVarName = cStart[i];   	            iState = eNameRead;   	            continue;   	        case eSeqNameRead:   	            if (cStart[i] == 0x0A )   	            {   	                iState = eStartLine;   	                continue;   	            }   	            else if (cStart[i] == ']' )   	            {   	                 // upakiramo ime sekcije u std::string i dodamo komentar ako ga ima   	   	                 std::string sOutString = "[";                     sOutString +=  sSeqName;                     sOutString += "]";                     if (pcHash)                     {                         sOutString += " ";                         sOutString += pcHash ;                         pcHash = 0;                     }                     sOutString += "\n";                     // zapisujemo taj std::string ako sekcija postoji                     if (SeqExists(sSeqName.c_str()))                     {                        write(m_iConfFile, sOutString.c_str(), sOutString.size());   	                 }   	   	                iState = eStartLine;   	                continue;   	            }   	   	            sSeqName += cStart[i];   	            continue;            case eNameRead:   	            if (cStart[i] == ' ' || cStart[i] == '"' )   	            {   	                iEndLine = true;   	                break;   	            }   	             	   	            sVarName += cStart[i];   	            break;   	   	        default:   	            break;   	   	    }   	    if (iEndLine) break;    }    clsConfigVar* pVar = GetVar (sSeqName.c_str(), sVarName.c_str());   	    // unutar pointera pVar upisujemo sadrzaj varijable    // ako ima komentara, dodaj ga na kraj    // postavi flag da je zapisana i zapisi je u file    if (pVar && pVar->m_iWritten == 0 )    {        sVarString = pVar->GetVarString ();        if (pcHash)        {            sVarString += " ";            sVarString += pcHash ;            pcHash = 0;        }        sVarString += "\n";        pVar->m_iWritten = 1;        write( m_iConfFile, sVarString.c_str(), sVarString.size());    }    else if (pcHash)    {        sVarString = pcHash ;        sVarString += "\n";        pcHash = 0;        write( m_iConfFile, sVarString.c_str(), sVarString.size());    }    return true;}//#############################################################################int 	clsConfigFile::Count	  		(const char* cFileName){    return m_lsConfigSeq.size();    if ( cFileName );//DUMMY}//#############################################################################int clsConfigFile::CountVar       (const char* cSeqName ){    std::list<clsConfigSeq>::iterator oIter;    for( oIter = m_lsConfigSeq.begin() ; oIter != m_lsConfigSeq.end() ; ++oIter )    {        if ( (*oIter).m_sSeqName == cSeqName)        {            return (*oIter).Count();        }	    }    return false;}//#############################################################################int 	clsConfigFile::CountIndex		(const char* cSeqName,const char* cVarname ){    std::list<clsConfigSeq>::iterator oIter;    for( oIter = m_lsConfigSeq.begin() ; oIter != m_lsConfigSeq.end() ; ++oIter )    {        if ( (*oIter).m_sSeqName == cSeqName)        {            return (*oIter).CountIndex (cVarname);        }	    }    return false;}//#############################################################################const char* 	clsConfigFile::GetSeqName		(long lIndex){    std::list<clsConfigSeq>::iterator oIter;    int i = 0;    for( oIter = m_lsConfigSeq.begin() ; oIter != m_lsConfigSeq.end() ; ++oIter )    {        if ( i == lIndex )        {            return (*oIter).m_sSeqName.c_str ( );        }        ++i;    }    return false;}//############################################################################int		clsConfigFile::AddSeq			(const char* cSeqName){//     clsConfigSeq *pTemp = 0;//     pTemp = new clsConfigSeq(cSeqName);    clsConfigSeq oSeq ( cSeqName );    // if ( pTemp == 0 ) return false;//     m_lsConfigSeq.push_back( *pTemp );    m_lsConfigSeq.push_back ( oSeq );     return true;}//############################################################################int		clsConfigFile::AddVar			(const char* cSeqName, const char* cNewVarName){    std::list<clsConfigSeq>::iterator oIter;    for( oIter = m_lsConfigSeq.begin() ; oIter != m_lsConfigSeq.end() ; ++oIter )    {        if ( (*oIter).m_sSeqName ==  cSeqName)        {            return (*oIter).AddVar (cNewVarName);        }	    }    return false;}//#############################################################################int     clsConfigFile::SetVar          (const char* cSeqName, const char* cVarname, const long lNewValue, long lIndex ){    std::string sNewValue;    char cLong[16];    sprintf ( cLong , "%ld" , lNewValue );    sNewValue = cLong;    return SetVar ( cSeqName, cVarname, sNewValue, lIndex );}//#############################################################################int     clsConfigFile::SetVar          (const char* cSeqName, const char* cVarname, const char* cNewValue, long lIndex ){    std::string sNewValue =  cNewValue;    return SetVar ( cSeqName, cVarname, sNewValue, lIndex );}//#############################################################################int     clsConfigFile::SetVar          (const char* cSeqName, const char* cVarname, const float fNewValue, long lIndex ){    std::string sNewValue;    char cFloat[28];    sprintf ( cFloat , "%f" , fNewValue );    sNewValue = cFloat;    return SetVar (cSeqName, cVarname, sNewValue, lIndex );}//#############################################################################int     clsConfigFile::SetVar          (const char* cSeqName, const char* cVarname, const std::string& sNewValue, long lIndex ){    std::list<clsConfigSeq>::iterator oIter;    for( oIter = m_lsConfigSeq.begin() ; oIter != m_lsConfigSeq.end() ; ++oIter )    {        if ( (*oIter).m_sSeqName ==  cSeqName)        {           return (*oIter).SetVar (cVarname,sNewValue,lIndex) ;        }	    }    return false;}//#############################################################################int     clsConfigFile::GetVarValue     (const char* cSeqName, const char* cVarname, long& lNewValue, long lIndex ){    std::string sNewValue;    if ( ! GetVarValue (cSeqName, cVarname , sNewValue , lIndex )) return false;    const char* cTmp = sNewValue.c_str();    if ( ( ! strncmp ( cTmp , "0x" , 2 ) ) || ( ! strncmp ( cTmp , "0X" , 2 ) ) )        lNewValue = HexToNum ( cTmp );    else        lNewValue = atol ( cTmp );    return true;}//#############################################################################int     clsConfigFile::GetVarValue     (const char* cSeqName, const char* cVarname, unsigned long& lNewValue, long lIndex ){    std::string sNewValue;    if ( ! GetVarValue (cSeqName, cVarname , sNewValue , lIndex )) return false;    const char* cTmp = sNewValue.c_str();    if ( ( ! strncmp ( cTmp , "0x" , 2 ) ) || ( ! strncmp ( cTmp , "0X" , 2 ) ) )        lNewValue = HexToNum ( cTmp );    else        lNewValue = atol ( cTmp );    return true;}//#############################################################################int     clsConfigFile::GetVarValue     (const char* cSeqName, const char* cVarname, char** cNewValue, long lIndex ){    std::string sNewValue;    if ( ! GetVarValue (cSeqName, cVarname , sNewValue , lIndex )) return false;    *cNewValue = (char*)sNewValue.c_str () ;    return true;}//#############################################################################int     clsConfigFile::GetVarValue     (const char* cSeqName, const char* cVarname, float& fNewValue, long lIndex ){    std::string sNewValue;    if ( ! GetVarValue (cSeqName, cVarname , sNewValue , lIndex )) return false;    fNewValue = atof (sNewValue.c_str ()) ;    return true;}//#############################################################################int     clsConfigFile::GetVarValue     (const char* cSeqName, const char* cVarname, std::string& sNewValue, long lIndex ){    std::list<clsConfigSeq>::iterator oIter;    for( oIter = m_lsConfigSeq.begin() ; oIter != m_lsConfigSeq.end() ; ++oIter )    {        if ( (*oIter).m_sSeqName ==  cSeqName)        {            return (*oIter).GetVarIndex ( cVarname ,sNewValue ,lIndex );        }	    }    return false;}//#############################################################################int     clsConfigFile::GetVarValue     (const char* cSeqName, long lVarIndex , std::string& sNewValue, long lIndex ){    std::list<clsConfigSeq>::iterator oIter;    for( oIter = m_lsConfigSeq.begin() ; oIter != m_lsConfigSeq.end() ; ++oIter )    {        if ( (*oIter).m_sSeqName ==  cSeqName)        {            return (*oIter).GetVarIndex ( lVarIndex , sNewValue , lIndex );        }	    }

⌨️ 快捷键说明

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