linux/windows 讀寫ini配置檔案

查志強發表於2016-07-06
DWORD MyClass::GetPrivateProfileString(
          LPCTSTR lpAppName,        // section name
          LPCTSTR lpKeyName,        // key name
          LPCTSTR lpDefault,        // default string
          LPTSTR lpReturnedString,  // destination buffer
          DWORD nSize,              // size of destination buffer
          LPCTSTR lpFileName        // initialization file name
           )
{
    int iRet = -1;
    //WINDOWS編譯環境
#ifdef WIN32
    return ::GetPrivateProfileString(lpAppName, lpKeyName, lpDefault, lpReturnedString, nSize, lpFileName);
//linux編譯環境
#elif defined LINUX
    short bfindsection = false;
    char szsection[100] = {0};
    char szentry[100] = {0};
    char sztmp[MAXLINELENGTH] = {0};
    char sztmpb[MAXLINELENGTH] = {0};
    char szresult[MAXLINELENGTH] = {0};
    memset(lpReturnedString, 0, strlen(lpReturnedString));
    //去右空格
    //TrimRight(lpAppName);
    //TrimRight(lpKeyName);
    //TrimRight(lpFileName);
    if(false == CAdapter::PathFileExists(lpFileName))
    {
        strcpy(lpReturnedString,lpDefault);
        return strlen(lpDefault);
    }

    sprintf(szsection , "[%s]" , lpAppName);
    sprintf(szentry , "%s=" , lpKeyName);

    FILE* stream;
    stream = fopen( lpFileName, "r" );
    if( stream != NULL )
    {
        //找到pcsection
        while(!feof(stream) && !bfindsection)
        {
            memset(sztmp,0x00,MAXLINELENGTH);
            fscanf(stream , "%s" , sztmp) ;
            bfindsection = !strcmp(sztmp , szsection) ;
        }

        memset(sztmp,0x00,MAXLINELENGTH);
        while (!feof(stream) && bfindsection && sztmp[0] != '[')
        {
            memset(sztmp,0x00,sizeof(sztmp));
            ReadString(sztmp , MAXLINELENGTH, stream);
            //RemoveChar(sztmp,' ');
            //printf("%s\n", sztmp);
            RemoveSpace(sztmp);
            //printf("%s\n\n", sztmp);
            strncpy(sztmpb, sztmp, sizeof(sztmpb));
            if (strncmp(sztmp,szentry,strlen(szentry)) == 0)
            {
                strncpy(szresult , &sztmpb[strlen(szentry)], sizeof(szresult)-1);
                //RemoveChar(szresult,'=');
                RemoveChar(szresult,'\n');
                RemoveChar(szresult,'\r');
                if(nSize < strlen(szresult) && lpReturnedString != NULL)
                {
                    strncpy(lpReturnedString, szresult, nSize-1);
                    lpReturnedString[nSize-1] = '\0';
                    iRet = nSize-1;
                }
                else if(lpReturnedString != NULL)
                {
                    strncpy(lpReturnedString, szresult, strlen(szresult));
                    iRet = strlen(lpReturnedString);
                }

                break;
            }
        }
    }
    //取預設值的情況:
    //1.開啟檔案失敗
    //2.AppName未找到
    //3.KeyName未找到
    if(((NULL == stream) || !bfindsection || (sztmp[0] == '[') || (!feof(stream) && iRet==-1)) && (lpDefault != NULL))
    {
        strncpy(lpReturnedString, lpDefault, nSize);
        iRet = strlen(lpDefault);
    }
    if(stream != NULL)
    {
        fclose( stream );
        stream = NULL;
    }
#endif
    return iRet;
}
//按行讀取檔案,去除行中的'\r'和'\n'
char* MyClass::ReadFileLine(char* szConn, FILE* stream)
{
char* pc = szConn;
char conn[2] = {0};
while (fread(conn, 1, 1, stream) > 0)
{
if (conn[0] == '\r')
continue;
if (conn[0] == '\n')
break;
memcpy(pc, conn, 1);
++pc;
}
*pc = '\0';
return szConn;
}

bool MyClass::WritePrivateProfileString( LPCSTR lpAppName, LPCSTR lpKeyName, LPCSTR lpString, LPCSTR lpFileName )
{
#ifdef WIN32
return ::WritePrivateProfileString(lpAppName, lpKeyName, lpString, lpFileName);
#elif defined LINUX
char szsection[100] = {0};
char szentry[100] = {0};
char sztmp[MAXLINELENGTH] = {0};
sprintf(szsection, "[%s]", lpAppName);
sprintf(szentry , "%s=" , lpKeyName);
//read file conn
if (!PathFileExists(lpFileName))
{
//file not exist
FILE* pfile = fopen(lpFileName, "w");
if (!pfile)
{
return false;
}
sprintf(sztmp, "%s\n%s%s\n", szsection, szentry, lpString);
fwrite(sztmp, sizeof(char), strlen(sztmp), pfile);
fclose(pfile);
return true;
}
std::string strConn = "";
std::string strRow = "";
size_t nAppPos = std::string::npos;
size_t nKeyPos = 0;
bool bfindapp = true;
bool bfindkey = false;
FILE* pfile = NULL;
pfile = fopen(lpFileName, "r");
while (!feof(pfile))
{
memset(sztmp, 0, sizeof(sztmp));
fscanf(pfile, "%s", sztmp); //行讀取 確保每行無空格以\n結尾,否則需自定義函式
strRow = sztmp;
if (nAppPos == std::string::npos && (nAppPos = strRow.find(szsection)) == 0)
{
strConn += szsection;
strConn += "\n";
nAppPos = strConn.length();
nKeyPos = std::string::npos;
}
else if (nKeyPos == std::string::npos && (nKeyPos = strRow.find(szentry)) == 0)
{
strConn += szentry;
strConn += lpString;
strConn += "\n";
}
else
{
strConn += strRow;
strConn += "\n";
}
}
if (nAppPos == std::string::npos && nKeyPos == 0)
{
memset(sztmp, 0, sizeof(sztmp));
sprintf(sztmp, "%s\n%s%s\n", szsection, szentry, lpString);
strConn += sztmp;
}
else if (nAppPos != std::string::npos && nKeyPos == std::string::npos)
{
std::string strBack = strConn.substr(nAppPos);
strConn = strConn.substr(0, nAppPos);
strConn += szentry;
strConn += lpString;
strConn += "\n" + strBack;
}
fclose(pfile);
pfile = fopen(lpFileName, "w");
fwrite(strConn.c_str(), sizeof(char), strConn.length(), pfile);
fclose(pfile);
return true;
#endif
}

相關文章