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 // Reads the whole INI file. | |
17 base::StringTokenizer tokenizer(content, "\r\n"); | |
18 | |
19 // Parses the file. | |
20 std::string current_section; | |
21 while (tokenizer.GetNext()) { | |
22 std::string line = tokenizer.token(); | |
23 if (line.empty()) { | |
24 // Skips the empty line. | |
25 continue; | |
26 } | |
27 if (line[0] == '#' || line[0] == ';') { | |
28 // This line is a comment. | |
29 continue; | |
30 } | |
31 if (line[0] == '[') { | |
32 // It is a section header. | |
33 current_section = line.substr(1); | |
34 size_t end = current_section.rfind(']'); | |
35 if (end != std::string::npos) | |
36 current_section.erase(end); | |
37 } else { | |
38 std::string key, value; | |
39 size_t equal = line.find('='); | |
40 if (equal != std::string::npos) { | |
41 key = line.substr(0, equal); | |
42 value = line.substr(equal + 1); | |
43 HandleTriplet(current_section, key, value); | |
44 } | |
45 } | |
46 } | |
47 } | |
48 | |
49 DictionaryValueINIParser::DictionaryValueINIParser() {} | |
50 | |
51 DictionaryValueINIParser::~DictionaryValueINIParser() {} | |
52 | |
53 void DictionaryValueINIParser::HandleTriplet(const std::string& section, | |
54 const std::string& key, | |
55 const std::string& value) { | |
56 | |
57 // 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.
| |
58 // Those sections and keys break DictionaryValue's path format, | |
59 // so we discard them. | |
60 if (section.find('.') == std::string::npos && | |
61 key.find('.') == std::string::npos) | |
62 root_.SetString(section + "." + key, value); | |
63 } | |
64 | |
65 } // namespace base | |
OLD | NEW |