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

📄 clsconfigfile.cpp

📁 set of c++ classes for read/write access to ascii files.
💻 CPP
📖 第 1 页 / 共 3 页
字号:
#include "clsconfigfile.h"#include <string.h>#include <sys/types.h>#include <sys/stat.h>#include <fcntl.h>#include <unistd.h>#include <stdio.h>//#############################################################################clsConfigVar::clsConfigVar (const char* cNewName){    m_sVarName = cNewName;}//#############################################################################clsConfigVar::~clsConfigVar (){}//#############################################################################int clsConfigVar::SetIndex ( const std::string& sNewValue, long lIndex ){    // za zadanu varijablu, nadji u listi zadani index i popuni ga ako postoji    // u slucaju da je broj indexa veci od duljine liste,    // preostale indexe dinamicki popuni praznim stringom    if ( (unsigned)lIndex >= m_vcVarIndex.size())    {        for ( long iCounter = m_vcVarIndex.size() ; iCounter <= lIndex ; iCounter++ )        {            std::string sString ( "" );//            std::string* pString = new std::string("");//            m_vcVarIndex.push_back( *pString );            m_vcVarIndex.push_back ( sString );        }    }    std::string pString = sNewValue;    m_vcVarIndex[lIndex] = pString;    return true;}//#############################################################################int clsConfigVar::GetIndex ( std::string& sBackValue, long lIndex ){    // za zadanu varijablu, nadji u listi zadani index vrati njegovu vrijednost    if ((unsigned)lIndex >= m_vcVarIndex.size())    {        return false;    }    sBackValue = m_vcVarIndex[lIndex];    return true;}//#############################################################################int clsConfigVar::DelIndex ( long lIndex ){    // za zadanu varijablu, nadji u listi zadani index i obrisi ga    if ((unsigned)lIndex >= m_vcVarIndex.size())    {        return false;    }    // ako je na kraju liste, izbrisi ga s liste, inace ga popuni praznim stringom    if ( (unsigned)lIndex == m_vcVarIndex.size() - 1 )    {        m_vcVarIndex.pop_back();        return true;    }    m_vcVarIndex[lIndex] = "";    return true;}//#############################################################################int clsConfigVar::Count    ( ){    return m_vcVarIndex.size();}//#############################################################################std::string& clsConfigVar::GetVarString    ( ){    // vrati sadrzaj varijable wrappirane navodnicima    static std::string sVarString;    sVarString = '"' + m_sVarName + "\" =";    for (unsigned int i = 0 ; i < m_vcVarIndex.size() ; i++)    {        sVarString += " \"" + m_vcVarIndex[i] + "\"" ;    }    sVarString += " ";    return sVarString;}//#############################################################################//#############################################################################//#############################################################################//#############################################################################//#############################################################################clsConfigFile::clsConfigFile (const char* cFileName){    m_sFileName = cFileName ? cFileName : "";    m_iConfFile = -1;}//#############################################################################clsConfigFile::~clsConfigFile (){    Clear();}//#############################################################################	int     clsConfigFile::ReadFile  		(const char* cFileName){    // pokusaj otvoriti file za citanje    if ( cFileName )    {       if ( ( m_iConfFile = open ( cFileName , O_RDONLY ) ) == -1 )       {           return false;       }    }    else    {       if ( ( m_iConfFile = open ( m_sFileName.c_str() , O_RDONLY ) ) == -1 )       {            return false;       }    }    //pogledati koliki je file preko funkcije lseek    //dinamicki stvoriti buffer te velicine i osloboditi toliki mem. prostor    //iskopirati file u buffer preko funkcije read  (procitati file)    //zatvoriti file    long lSize = lseek ( m_iConfFile , 0 , SEEK_END );    lseek ( m_iConfFile , 0 , SEEK_SET );    char* cFileBuffer = new char [lSize];    if ( ! cFileBuffer )    {        close (m_iConfFile);        return false;    }    memset ( cFileBuffer , 0x00 ,   lSize  );	int iRezultat = read(m_iConfFile , cFileBuffer , lSize);	close (m_iConfFile);		if (iRezultat <= 0)	{	    delete [] cFileBuffer;    	return false;	}		// sad citaj sta je u bufferu i postavljaj varijable s pripadajucim indexima dok ne naidjes na enter	// unutar switcha promatramo stanja citanja    AddSeq ( CONFIG_FILE_GLOBAL_SECTION ); //For global variables		eStateType iState = eStartLine;	std::string sSeqName;    std::string sVarName;    std::string sVarIndex;    int iCountIndex = 0;    int iQuote = false;    for (int i=0; i<lSize; i++)    {    	        switch ( iState )        {            case eStartLine:   	            if (cFileBuffer[i] == ' ' || cFileBuffer[i] == 0x0A )   	            {   	                continue;   	            }   	            else if (cFileBuffer[i] == '#' )   	            {   	                iState = eComment;   	                continue;   	            }   	            else if (cFileBuffer[i] == '"' )   	            {   	                sVarName = "";   	                iCountIndex = 0;   	                iQuote = true;       // sad smo unutar navodnika   	                iState = eNameRead;   	                continue;   	            }   	            else if (cFileBuffer[i] == '[' )   	            {   	                sSeqName = "";   	                iCountIndex = 0;   	                iState = eSeqNameRead;   	                continue;   	            }   	            else if (i == lSize-1)   	            {   	                iState = eNameRead;   	                continue;   	            }   	            sVarName = cFileBuffer[i];   	            iCountIndex = 0;   	            iState = eNameRead;   	            continue;   	   	   	        case eSeqNameRead:   	            if (cFileBuffer[i] == 0x0A )   	            {   	                iState = eStartLine;   	                AddSeq (sSeqName.c_str());   	                continue;   	            }   	   	   	            else if (cFileBuffer[i] == ']' )   	            {   	                AddSeq (sSeqName.c_str());   	                iState = eStartLine;   	                continue;   	   	            }   	            else if (i == lSize-1)   	            {   	                sSeqName += cFileBuffer[i];   	                AddSeq (sSeqName.c_str());   	                iState = eStartLine;   	                continue;   	            }   	   	            sSeqName += cFileBuffer[i];   	            continue;   	            case eNameRead:   	            if (cFileBuffer[i] == 0x0A )   	            {   	                iState = eStartLine;   	                AddVar (sSeqName.c_str(), sVarName.c_str());   	                continue;   	            }   	            else if (cFileBuffer[i] == '#' )   	            {   	                if (! iQuote)   	                {    	                iState = eComment;    	                AddVar (sSeqName.c_str(), sVarName.c_str());    	                continue;   	                }   	            }   	            else if (cFileBuffer[i] == ' ' || cFileBuffer[i] == '=' )   	            {   	                if (! iQuote)   	                {   	                    iState = ePreIndex;   	                    AddVar (sSeqName.c_str(), sVarName.c_str());   	                    continue;   	                }   	            }   	            else if (cFileBuffer[i] == '"' )   	            {   	                iQuote = !iQuote;   	   	   	                AddVar (sSeqName.c_str(), sVarName.c_str());   	                iState = ePreIndex;   	                continue;   	   	            }   	            else if (i == lSize-1)   	            {   	                sVarName += cFileBuffer[i];   	                AddVar (sSeqName.c_str(), sVarName.c_str());   	                iState = ePreIndex;   	                continue;   	            }   	   	            sVarName += cFileBuffer[i];   	            continue;            case ePreIndex:    	        if (cFileBuffer[i] == 0x0A )   	            {   	                iState = eStartLine;   	                continue;   	            }   	            else if (cFileBuffer[i] == '#' )   	            {   	                iState = eComment;   	                continue;   	            }   	            else if (cFileBuffer[i] == ' ' || cFileBuffer[i] == '=' )   	            {   	                continue;   	            }   	            else if (cFileBuffer[i] == '"' )   	            {   	                iQuote = true;   	                iState = eIndexRead;   	                sVarIndex = "";   	                continue;   	   	            }   	            else if (i == lSize-1)   	            {   	                iState = eIndexRead;   	                continue;   	            }   	   	            sVarIndex = cFileBuffer[i];                iState = eIndexRead;   	            continue;            case eIndexRead:    	        if (cFileBuffer[i] == 0x0A )   	            {   	                SetVar ( sSeqName.c_str(), sVarName.c_str(), sVarIndex.c_str(), iCountIndex++);   	                iState = eStartLine;   	                continue;   	            }   	            else if (cFileBuffer[i] == '#' )   	            {   	                if (! iQuote)   	                {   	                    SetVar ( sSeqName.c_str(), sVarName.c_str(), sVarIndex.c_str(), iCountIndex++);   	                    iState = eComment;   	                    continue;   	                }   	            }   	            else if (cFileBuffer[i] == ' ' )   	            {   	                if (! iQuote )   	                {   	                    SetVar ( sSeqName.c_str(), sVarName.c_str(), sVarIndex.c_str(), iCountIndex++);   	                    iState =  ePreIndex;   	                    continue;   	                }   	            }   	            else if (cFileBuffer[i] == '"' )   	            {   	                if (iQuote)   	                {   	                    iQuote = false; // izlazimo iz stanja navodnika   	                    SetVar ( sSeqName.c_str(), sVarName.c_str(), sVarIndex.c_str(), iCountIndex++);   	                    iState = ePreIndex;   	                    continue;   	                }   	                else if (! iQuote )   	                {   	                    iQuote = true;   	                    SetVar ( sSeqName.c_str(), sVarName.c_str(), sVarIndex.c_str(), iCountIndex++);   	                    sVarIndex = "";   	                    continue;   	                }   	   	            }                else if (i == lSize-1)   	            {   	                sVarIndex += cFileBuffer[i];   	                SetVar ( sSeqName.c_str(), sVarName.c_str(), sVarIndex.c_str(), iCountIndex++);                    continue;   	            }   	   	            sVarIndex += cFileBuffer[i];   	            continue;            case eComment:                if (cFileBuffer[i] == 0x0A )   	            {   	                iState = eStartLine;   	                continue;   	            }   	            else if (i == lSize-1)   	            {   	                iState = eStartLine;   	                continue;   	            }   	        }    }    delete [] cFileBuffer;    return true;}//#############################################################################int  	clsConfigFile::WriteFile  		(const char* cNewFile){    // otvaramo file za citanje i pisanje    if ( cNewFile )    {       if ( ( m_iConfFile = open ( cNewFile , O_RDWR|O_CREAT , S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH ) ) == -1 )       {           return false;       }    }    else    {       if ( ( m_iConfFile = open ( m_sFileName.c_str() , O_RDWR|O_CREAT , S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH ) ) == -1 )       {            return false;       }    }    // unutar svih sekcija i varijabli postavi flag m_iWritten na nulu.    // time deklariramo da niti jedna sekcija i niti jedna njena varijabla    // nije vec procitana    std::list<clsConfigSeq>::iterator oIter;    for( oIter = m_lsConfigSeq.begin() ; oIter != m_lsConfigSeq.end() ; ++oIter )    {         (*oIter).ClearWritten();    }    // odredimo duljinu filea i dinamicki stvorimo buffer velicine za 1 byte veceg    // od velicine filea [lSize+1], jer kasnije zelimo upisati enter kao zadnji znak    // oslobodimo mem. prostor    // iskopiramo file u buffer preko funkcije read  (procitati file)    // zatvorimo file i dalje radimo s bufferom    long lSize = lseek ( m_iConfFile , 0 , SEEK_END );    lseek ( m_iConfFile , 0 , SEEK_SET );    char* cFileBuffer = new char [lSize+1];    if ( ! cFileBuffer )    {        close (m_iConfFile);        return false;    }    memset ( cFileBuffer , 0x00 ,   lSize  );

⌨️ 快捷键说明

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