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 "chrome/browser/common/ini_parser.h" | |
6 | |
7 #include "base/strings/string_tokenizer.h" | |
8 | |
9 INIParser::INIParser() {} | |
10 | |
11 INIParser::~INIParser() { | |
12 } | |
13 | |
14 void INIParser::Parse(const std::string& content) { | |
15 // Reads the whole INI file. | |
16 base::StringTokenizer tokenizer(content, "\r\n"); | |
17 | |
18 // Parses the file. | |
19 std::string current_section; | |
erikwright (departed)
2013/06/14 19:26:46
This code probably predates StringPiece support in
tommycli
2013/06/17 18:01:56
That's a good suggestion. I will try to get this c
| |
20 while (tokenizer.GetNext()) { | |
21 std::string line = tokenizer.token(); | |
22 if (line.empty()) { | |
23 // Skips the empty line. | |
24 continue; | |
25 } | |
26 if (line[0] == '#' || line[0] == ';') { | |
27 // This line is a comment. | |
28 continue; | |
29 } | |
30 if (line[0] == '[') { | |
31 // It is a section header. | |
32 current_section = line.substr(1); | |
33 size_t end = current_section.rfind(']'); | |
34 if (end != std::string::npos) | |
35 current_section.erase(end); | |
36 } else { | |
37 std::string key, value; | |
38 size_t equal = line.find('='); | |
39 if (equal != std::string::npos) { | |
40 key = line.substr(0, equal); | |
41 value = line.substr(equal + 1); | |
42 HandlePair(current_section, key, value); | |
43 } | |
44 } | |
45 } | |
46 } | |
47 | |
48 DictionaryValueINIParser::DictionaryValueINIParser() { | |
49 } | |
50 | |
51 DictionaryValueINIParser::~DictionaryValueINIParser() { | |
52 } | |
53 | |
54 void DictionaryValueINIParser::HandlePair(const std::string& section, | |
55 const std::string& key, | |
56 const std::string& value) { | |
57 | |
58 // Checks whether the section and key contain a '.' character. | |
59 // Those sections and keys break DictionaryValue's path format, | |
60 // so we discard them. | |
61 if (section.find('.') == std::string::npos && | |
62 key.find('.') == std::string::npos) | |
63 root_.SetString(section + "." + key, value); | |
64 } | |
OLD | NEW |