Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(59)

Side by Side Diff: base/ini_parser.cc

Issue 16982004: Move Firefox importer's INI parser to c/browser/common. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Add multiple calls to Parse unit test. Created 7 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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
OLDNEW
« base/ini_parser.h ('K') | « base/ini_parser.h ('k') | base/ini_parser_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698