views:

1497

answers:

2

I can't use the Get*Profile functions because I'm using an older version of the windows CE platform SDK which doesn't have those. It doesn't have to be too general.

[section]
  name = some string

I just need to open the file, check for the existence of "section", and the the value associated with "name". Standard C++ preferred.

A: 

What I came up with:

std::wifstream file(L"\\Windows\\myini.ini");
if (file)
{
  bool section=false;
  while (!file.eof())
  {
    WCHAR _line[256];
    file.getline(_line, ELEMENTS(_line));
    std::wstringstream lineStm(_line);
    std::wstring &line=lineStm.str();
    if (line.empty()) continue;

    switch (line[0])
    {
      // new header
      case L'[':
      {
        std::wstring header;
        for (size_t i=1; i<line.length(); i++)
        {
          if (line[i]!=L']')
            header.push_back(line[i]);
          else
            break;
        }
        if (header==L"Section")
          section=true;
        else
          section=false;
      }
  break;
      // comments
      case ';':
      case ' ':
      case '#':
      break;
      // var=value
      default:
      {
        if (!section) continue;

        std::wstring name, dummy, value;
        lineStm >> name >> dummy;
        ws(lineStm);
        WCHAR _value[256];
        lineStm.getline(_value, ELEMENTS(_value));
        value=_value;
      }
    }
  }
}
1800 INFORMATION
+2  A: 

You should have a look at Boost.Program_options. It has a parse_config_file function that fills a map of variables. Just what you need !

Benoît