帮助改进这个INI解析代码

发布于 2024-07-06 03:37:17 字数 1296 浏览 4 评论 0原文

这是我针对这个问题< /a>. 我对此并不完全满意,我认为这是一个帮助改进我对 STL 和基于流的编程的使用的机会。

std::wifstream file(L"\\Windows\\myini.ini");
if (file)
{
  bool section=false;
  while (!file.eof())
  {
    std::wstring line;
    std::getline(file, line);
    if (line.empty()) continue;

    switch (line[0])
    {
      // new header
      case L'[':
      {
        std::wstring header;
        size_t pos=line.find(L']');
        if (pos!=std::wstring::npos)
        {
          header=line.substr(1, pos);
          if (header==L"Section")
            section=true;
          else
            section=false;
        }
      }
  break;
      // comments
      case ';':
      case ' ':
      case '#':
      break;
      // var=value
      default:
      {
        if (!section) continue;

// what if the name = value does not have white space?
// what if the value is enclosed in quotes?
        std::wstring name, dummy, value;
        lineStm >> name >> dummy;
        ws(lineStm);
        WCHAR _value[256];
        lineStm.getline(_value, ELEMENTS(_value));
        value=_value;
      }
    }
  }
}

您将如何改进这一点? 请不要推荐替代库 - 我只是想要一个简单的方法来从 INI 文件中解析一些配置字符串。

This is something simple I came up with for this question. I'm not entirely happy with it and I saw it as a chance to help improve my use of STL and streams based programming.

std::wifstream file(L"\\Windows\\myini.ini");
if (file)
{
  bool section=false;
  while (!file.eof())
  {
    std::wstring line;
    std::getline(file, line);
    if (line.empty()) continue;

    switch (line[0])
    {
      // new header
      case L'[':
      {
        std::wstring header;
        size_t pos=line.find(L']');
        if (pos!=std::wstring::npos)
        {
          header=line.substr(1, pos);
          if (header==L"Section")
            section=true;
          else
            section=false;
        }
      }
  break;
      // comments
      case ';':
      case ' ':
      case '#':
      break;
      // var=value
      default:
      {
        if (!section) continue;

// what if the name = value does not have white space?
// what if the value is enclosed in quotes?
        std::wstring name, dummy, value;
        lineStm >> name >> dummy;
        ws(lineStm);
        WCHAR _value[256];
        lineStm.getline(_value, ELEMENTS(_value));
        value=_value;
      }
    }
  }
}

How would you improve this? Please do not recommend alternative libraries - I just want a simple method for parsing out some config strings from an INI file.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

孤千羽 2024-07-13 03:37:17

// 如果 name = value 没有空格怎么办?
// 如果该值用引号括起来怎么办?

我会使用 boost::regex 来匹配每种不同类型的元素,例如:

boost::smatch matches;
boost::regex name_value("(\S+)\s*=\s*(\S+)");
if(boost::regex_match(line, matches, name_value))
{
    name = matches[1];
    value = matches[2];
}

正则表达式可能需要一些调整。

我还将 de stream.getline 替换为 std::getline,从而摆脱静态 char 数组。

// what if the name = value does not have white space?
// what if the value is enclosed in quotes?

I would use boost::regex to match for every different type of element, something like:

boost::smatch matches;
boost::regex name_value("(\S+)\s*=\s*(\S+)");
if(boost::regex_match(line, matches, name_value))
{
    name = matches[1];
    value = matches[2];
}

the regular expressions might need some tweaking.

I would also replace de stream.getline with std::getline, getting rid of the static char array.

混浊又暗下来 2024-07-13 03:37:17

这:

for (size_t i=1; i<line.length(); i++)
        {
          if (line[i]!=L']')
            header.push_back(line[i]);
          else
            break;
        }

应该通过调用 wstrchr、wcschr、WSTRCHR 或其他东西来简化,具体取决于您所在的平台。

This:

for (size_t i=1; i<line.length(); i++)
        {
          if (line[i]!=L']')
            header.push_back(line[i]);
          else
            break;
        }

should be simplified by a call to wstrchr, wcschr, WSTRCHR, or something else, depending on what platform you are on.

带刺的爱情 2024-07-13 03:37:17

// 如何一次性将一行变成字符串?

使用标准字符串标头中的(非成员)getline 函数。

// how to get a line into a string in one go?

Use the (nonmember) getline function from the standard string header.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文