diff options
author | monster <monster@ydb.tech> | 2022-07-07 14:41:37 +0300 |
---|---|---|
committer | monster <monster@ydb.tech> | 2022-07-07 14:41:37 +0300 |
commit | 06e5c21a835c0e923506c4ff27929f34e00761c2 (patch) | |
tree | 75efcbc6854ef9bd476eb8bf00cc5c900da436a2 /library/cpp/config/ini.cpp | |
parent | 03f024c4412e3aa613bb543cf1660176320ba8f4 (diff) | |
download | ydb-06e5c21a835c0e923506c4ff27929f34e00761c2.tar.gz |
fix ya.make
Diffstat (limited to 'library/cpp/config/ini.cpp')
-rw-r--r-- | library/cpp/config/ini.cpp | 62 |
1 files changed, 62 insertions, 0 deletions
diff --git a/library/cpp/config/ini.cpp b/library/cpp/config/ini.cpp new file mode 100644 index 00000000000..6c1897dd279 --- /dev/null +++ b/library/cpp/config/ini.cpp @@ -0,0 +1,62 @@ +#include "ini.h" + +#include <util/string/strip.h> +#include <util/stream/input.h> + +using namespace NConfig; + +namespace { + inline TStringBuf StripComment(TStringBuf line) { + return line.Before('#').Before(';'); + } +} + +TConfig NConfig::ParseIni(IInputStream& in) { + TConfig ret = ConstructValue(TDict()); + + { + TConfig* cur = &ret; + TString line; + + while (in.ReadLine(line)) { + TStringBuf tmp = StripComment(line); + TStringBuf stmp = StripString(tmp); + + if (stmp.empty()) { + continue; + } + + if (stmp[0] == '[') { + //start section + if (*(stmp.end() - 1) != ']') { + ythrow TConfigParseError() << "malformed section " << stmp; + } + + stmp = TStringBuf(stmp.data() + 1, stmp.end() - 1); + cur = &ret; + + while (!!stmp) { + TStringBuf section; + + stmp.Split('.', section, stmp); + + cur = &cur->GetNonConstant<TDict>()[section]; + if (!cur->IsA<TDict>()) { + *cur = ConstructValue(TDict()); + } + } + } else { + //value + TStringBuf key, value; + + tmp.Split('=', key, value); + + auto& dict = cur->GetNonConstant<TDict>(); + auto strippedValue = TString(StripString(value)); + dict[StripString(key)] = ConstructValue(strippedValue); + } + } + } + + return ret; +} |