OLD | NEW |
(Empty) | |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "base/ini_parser.h" |
| 6 |
| 7 #include "base/strings/string_tokenizer.h" |
| 8 |
| 9 namespace base { |
| 10 |
| 11 INIParser::INIParser() {} |
| 12 |
| 13 INIParser::~INIParser() {} |
| 14 |
| 15 void INIParser::Parse(const std::string& content) { |
| 16 base::StringTokenizer tokenizer(content, "\r\n"); |
| 17 |
| 18 std::string current_section; |
| 19 while (tokenizer.GetNext()) { |
| 20 std::string line = tokenizer.token(); |
| 21 if (line.empty()) { |
| 22 // Skips the empty line. |
| 23 continue; |
| 24 } |
| 25 if (line[0] == '#' || line[0] == ';') { |
| 26 // This line is a comment. |
| 27 continue; |
| 28 } |
| 29 if (line[0] == '[') { |
| 30 // It is a section header. |
| 31 current_section = line.substr(1); |
| 32 size_t end = current_section.rfind(']'); |
| 33 if (end != std::string::npos) |
| 34 current_section.erase(end); |
| 35 } else { |
| 36 std::string key, value; |
| 37 size_t equal = line.find('='); |
| 38 if (equal != std::string::npos) { |
| 39 key = line.substr(0, equal); |
| 40 value = line.substr(equal + 1); |
| 41 HandleTriplet(current_section, key, value); |
| 42 } |
| 43 } |
| 44 } |
| 45 } |
| 46 |
| 47 DictionaryValueINIParser::DictionaryValueINIParser() {} |
| 48 |
| 49 DictionaryValueINIParser::~DictionaryValueINIParser() {} |
| 50 |
| 51 void DictionaryValueINIParser::HandleTriplet(const std::string& section, |
| 52 const std::string& key, |
| 53 const std::string& value) { |
| 54 |
| 55 // Checks whether the section and key contain a '.' character. |
| 56 // Those sections and keys break DictionaryValue's path format when not |
| 57 // using the *WithoutPathExpansion methods. |
| 58 if (section.find('.') == std::string::npos && |
| 59 key.find('.') == std::string::npos) |
| 60 root_.SetString(section + "." + key, value); |
| 61 } |
| 62 |
| 63 } // namespace base |
OLD | NEW |