Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

::GetPrivateProfileString read whole section of INI file

I'm modifying existing C++ application and moving out some values that are currently hard coded.

I'm doing this with one class that will "manage" this whole thing and hold map<CString, CString> of the values from the INI file.

Right now I have to read each value separately using ::GetPrivateProfileString function - can I somehow read whole section instead of single value?

Prefer not to have to read the file manually, but if there's any reasonable (i.e. efficient + simple to use) existing way I'm open for suggestions.

Edit: just now had to use it "for real" and the solution was indeed passing NULL as the lpKeyName value. Complete code including parsing the return value:

char buffer[MAX_STRING_SIZE];
int charsCount = ::GetPrivateProfileString("MySection", NULL, NULL, buffer, MAX_STRING_SIZE, m_strIniPath);
CString curValue;
curValue.Empty();
char curChar = '\0';
for (int i = 0; i < charsCount; i++)
{
    curChar = buffer[i];
    if (curChar == '\0')
    {
        if (curValue.GetLength() > 0)
            HandleValue(curValue);
        curValue.Empty();
    }
    else
    {
        curValue.AppendFormat("%c", curChar);
    }
}
if (curValue.GetLength() > 0)
    HandleValue(curValue);

It's not trivial as it returns the keys separated by zero character (EOS?) so I had to extract them using loop such as the above - share it here for the sake of everyone who might need it. :-)

like image 610
Shadow Wizard Hates Omicron Avatar asked Feb 26 '23 12:02

Shadow Wizard Hates Omicron


1 Answers

You don't need to read the file manually but it helps to read the manual for GetPrivateProfileString:

lpKeyName [in] : The name of the key whose associated string is to be retrieved. If this parameter is NULL, all key names in the section specified by the lpAppName parameter are copied to the buffer specified by the lpReturnedString parameter.

like image 119
Eugen Constantin Dinca Avatar answered Mar 07 '23 06:03

Eugen Constantin Dinca