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

Side by Side Diff: chrome/browser/common/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: 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 "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;
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 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698