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

Unified 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 side-by-side diff with in-line comments
Download patch
Index: chrome/browser/common/ini_parser.cc
diff --git a/chrome/browser/common/ini_parser.cc b/chrome/browser/common/ini_parser.cc
new file mode 100644
index 0000000000000000000000000000000000000000..d169e599a5636906d63a35732bc69393f12d1f64
--- /dev/null
+++ b/chrome/browser/common/ini_parser.cc
@@ -0,0 +1,64 @@
+// Copyright 2013 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "ini_parser.h"
+
+#include "base/strings/string_tokenizer.h"
+
+INIParser::INIParser() {}
+
+INIParser::~INIParser() {
+}
+
+void INIParser::Parse(const std::string& content) {
+ // Reads the whole INI file.
+ base::StringTokenizer tokenizer(content, "\r\n");
+
+ // Parses the file.
+ std::string current_section;
+ while (tokenizer.GetNext()) {
+ std::string line = tokenizer.token();
+ if (line.empty()) {
+ // Skips the empty line.
+ continue;
+ }
+ if (line[0] == '#' || line[0] == ';') {
+ // This line is a comment.
+ continue;
+ }
+ if (line[0] == '[') {
+ // It is a section header.
+ current_section = line.substr(1);
+ size_t end = current_section.rfind(']');
+ if (end != std::string::npos)
+ current_section.erase(end);
+ } else {
+ std::string key, value;
+ size_t equal = line.find('=');
+ if (equal != std::string::npos) {
+ key = line.substr(0, equal);
+ value = line.substr(equal + 1);
+ HandlePair(current_section, key, value);
+ }
+ }
+ }
+}
+
+DictionaryValueINIParser::DictionaryValueINIParser() {
+}
+
+DictionaryValueINIParser::~DictionaryValueINIParser() {
+}
+
+void DictionaryValueINIParser::HandlePair(const std::string& section,
+ const std::string& key,
+ const std::string& value) {
+
+ // Checks whether the section and key contain a '.' character.
+ // Those sections and keys break DictionaryValue's path format,
+ // so we discard them.
+ if (section.find('.') == std::string::npos &&
+ key.find('.') == std::string::npos)
+ root_.SetString(section + "." + key, value);
+}

Powered by Google App Engine
This is Rietveld 408576698