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() {} | |
Mark Mentovai
2013/06/17 18:52:37
Why {} here but on separate lines for the destruct
tommycli
2013/06/17 19:49:24
Done.
| |
12 | |
13 INIParser::~INIParser() { | |
14 } | |
15 | |
16 void INIParser::Parse(const std::string& content) { | |
17 // Reads the whole INI file. | |
18 base::StringTokenizer tokenizer(content, "\r\n"); | |
19 | |
20 // Parses the file. | |
21 std::string current_section; | |
22 while (tokenizer.GetNext()) { | |
23 std::string line = tokenizer.token(); | |
24 if (line.empty()) { | |
25 // Skips the empty line. | |
26 continue; | |
27 } | |
28 if (line[0] == '#' || line[0] == ';') { | |
29 // This line is a comment. | |
30 continue; | |
31 } | |
32 if (line[0] == '[') { | |
33 // It is a section header. | |
34 current_section = line.substr(1); | |
35 size_t end = current_section.rfind(']'); | |
36 if (end != std::string::npos) | |
37 current_section.erase(end); | |
Mark Mentovai
2013/06/17 18:52:37
This just silently throws away any data after the
tommycli
2013/06/17 19:49:24
Yes. Noted in comment in header.
| |
38 } else { | |
39 std::string key, value; | |
40 size_t equal = line.find('='); | |
41 if (equal != std::string::npos) { | |
42 key = line.substr(0, equal); | |
43 value = line.substr(equal + 1); | |
44 HandlePair(current_section, key, value); | |
45 } | |
46 } | |
47 } | |
48 } | |
49 | |
50 DictionaryValueINIParser::DictionaryValueINIParser() { | |
51 } | |
52 | |
53 DictionaryValueINIParser::~DictionaryValueINIParser() { | |
54 } | |
55 | |
56 void DictionaryValueINIParser::HandlePair(const std::string& section, | |
57 const std::string& key, | |
58 const std::string& value) { | |
59 | |
60 // Checks whether the section and key contain a '.' character. | |
61 // Those sections and keys break DictionaryValue's path format, | |
Mark Mentovai
2013/06/17 18:52:37
I don’t believe this comment. There’s a whole suit
tommycli
2013/06/17 19:49:24
I'm not certain why it was implemented this way in
| |
62 // so we discard them. | |
63 if (section.find('.') == std::string::npos && | |
64 key.find('.') == std::string::npos) | |
65 root_.SetString(section + "." + key, value); | |
66 } | |
67 | |
68 } // namespace base | |
OLD | NEW |