이 INI 구문 분석 코드를 개선하는 데 도움이됩니다
문제
이것은 내가 생각해 낸 단순한 것입니다 이 질문. 나는 그것에 완전히 만족하지 않으며 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 파일에서 일부 구성 문자열을 파싱하는 간단한 방법을 원합니다.
해결책
// 이름 = 값에 공백이 없다면 어떻게해야합니까?
// 값이 따옴표로 둘러싸인 경우 어떻게합니까?
나는 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으로 대체하여 정적 숯 배열을 제거합니다.
다른 팁
이것:
for (size_t i=1; i<line.length(); i++)
{
if (line[i]!=L']')
header.push_back(line[i]);
else
break;
}
어떤 플랫폼에 있는지에 따라 WSTRCHR, WCSCHR, WSTRCHR 또는 다른 것에 대한 호출로 단순화해야합니다.
// 한 번에 줄에 줄을 넣는 방법?
(비 회원) 사용 getline 표준 문자열 헤더의 기능.
제휴하지 않습니다 StackOverflow