Chromium Code Reviews| Index: base/ini_parser.cc |
| diff --git a/base/ini_parser.cc b/base/ini_parser.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..c4d71c7a7f8daceeebd88af3a60e4a2ed3c89456 |
| --- /dev/null |
| +++ b/base/ini_parser.cc |
| @@ -0,0 +1,65 @@ |
| +// Copyright 2013 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "base/ini_parser.h" |
| + |
| +#include "base/strings/string_tokenizer.h" |
| + |
| +namespace base { |
| + |
| +INIParser::INIParser() {} |
| + |
| +INIParser::~INIParser() {} |
| + |
| +void INIParser::Parse(const std::string& content) { |
| + // Reads the whole INI file. |
| + base::StringTokenizer tokenizer(content, "\r\n"); |
| + |
| + // Parses the file. |
| + std::string current_section; |
| + while (tokenizer.GetNext()) { |
| + std::string line = tokenizer.token(); |
| + if (line.empty()) { |
| + // Skips the empty line. |
| + continue; |
| + } |
| + if (line[0] == '#' || line[0] == ';') { |
| + // This line is a comment. |
| + continue; |
| + } |
| + if (line[0] == '[') { |
| + // It is a section header. |
| + current_section = line.substr(1); |
| + size_t end = current_section.rfind(']'); |
| + if (end != std::string::npos) |
| + current_section.erase(end); |
| + } else { |
| + std::string key, value; |
| + size_t equal = line.find('='); |
| + if (equal != std::string::npos) { |
| + key = line.substr(0, equal); |
| + value = line.substr(equal + 1); |
| + HandleTriplet(current_section, key, value); |
| + } |
| + } |
| + } |
| +} |
| + |
| +DictionaryValueINIParser::DictionaryValueINIParser() {} |
| + |
| +DictionaryValueINIParser::~DictionaryValueINIParser() {} |
| + |
| +void DictionaryValueINIParser::HandleTriplet(const std::string& section, |
| + const std::string& key, |
| + const std::string& value) { |
| + |
| + // Checks whether the section and key contain a '.' character. |
|
Mark Mentovai
2013/06/17 19:59:48
This comment is not entirely correct—say that it b
tommycli
2013/06/17 21:23:25
Done.
|
| + // Those sections and keys break DictionaryValue's path format, |
| + // so we discard them. |
| + if (section.find('.') == std::string::npos && |
| + key.find('.') == std::string::npos) |
| + root_.SetString(section + "." + key, value); |
| +} |
| + |
| +} // namespace base |