diff options
author | tommycli@chromium.org <tommycli@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-06-18 23:31:12 +0000 |
---|---|---|
committer | tommycli@chromium.org <tommycli@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-06-18 23:31:12 +0000 |
commit | e288af7e53c20efd31db7bc1c58bababb9e36c50 (patch) | |
tree | 52daa3114c64bc0057fbf0099a76d6dbf223b96f | |
parent | 0d6a1888af2a9a6c98e12eca23fb7e252858dc72 (diff) | |
download | chromium_src-e288af7e53c20efd31db7bc1c58bababb9e36c50.zip chromium_src-e288af7e53c20efd31db7bc1c58bababb9e36c50.tar.gz chromium_src-e288af7e53c20efd31db7bc1c58bababb9e36c50.tar.bz2 |
Move Firefox importer's INI parser to c/browser/common.
Media Galleries API Picasa Import will require reading some INI files. Rather than re-invent the wheel and duplicate code, we are moving the INI parser used by Firefox import into chrome/browser/common.
BUG=151701
Review URL: https://chromiumcodereview.appspot.com/16982004
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@207111 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r-- | base/base.gyp | 1 | ||||
-rw-r--r-- | base/base.gypi | 2 | ||||
-rw-r--r-- | base/ini_parser.cc | 66 | ||||
-rw-r--r-- | base/ini_parser.h | 69 | ||||
-rw-r--r-- | base/ini_parser_unittest.cc | 131 | ||||
-rw-r--r-- | chrome/browser/importer/firefox_importer_utils.cc | 53 |
6 files changed, 275 insertions, 47 deletions
diff --git a/base/base.gyp b/base/base.gyp index aaead7d..e35e6b1 100644 --- a/base/base.gyp +++ b/base/base.gyp @@ -494,6 +494,7 @@ 'i18n/rtl_unittest.cc', 'i18n/string_search_unittest.cc', 'i18n/time_formatting_unittest.cc', + 'ini_parser_unittest.cc', 'ios/device_util_unittest.mm', 'json/json_parser_unittest.cc', 'json/json_reader_unittest.cc', diff --git a/base/base.gypi b/base/base.gypi index 7a5879b..2ccb2bb 100644 --- a/base/base.gypi +++ b/base/base.gypi @@ -201,6 +201,8 @@ 'hi_res_timer_manager_win.cc', 'hi_res_timer_manager.h', 'id_map.h', + 'ini_parser.cc', + 'ini_parser.h', 'ios/device_util.h', 'ios/device_util.mm', 'ios/ios_util.h', diff --git a/base/ini_parser.cc b/base/ini_parser.cc new file mode 100644 index 0000000..7a2bd1c --- /dev/null +++ b/base/ini_parser.cc @@ -0,0 +1,66 @@ +// 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 "base/ini_parser.h" + +#include "base/logging.h" +#include "base/strings/string_tokenizer.h" + +namespace base { + +INIParser::INIParser() : used_(false) {} + +INIParser::~INIParser() {} + +void INIParser::Parse(const std::string& content) { + DCHECK(!used_); + used_ = true; + base::StringTokenizer tokenizer(content, "\r\n"); + + 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); + HandleTriplet(current_section, key, value); + } + } + } +} + +DictionaryValueINIParser::DictionaryValueINIParser() {} + +DictionaryValueINIParser::~DictionaryValueINIParser() {} + +void DictionaryValueINIParser::HandleTriplet(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 when not + // using the *WithoutPathExpansion methods. + if (section.find('.') == std::string::npos && + key.find('.') == std::string::npos) + root_.SetString(section + "." + key, value); +} + +} // namespace base diff --git a/base/ini_parser.h b/base/ini_parser.h new file mode 100644 index 0000000..0aa7754 --- /dev/null +++ b/base/ini_parser.h @@ -0,0 +1,69 @@ +// 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. + +#ifndef BASE_INI_PARSER_H_ +#define BASE_INI_PARSER_H_ + +#include <string> + +#include "base/base_export.h" +#include "base/basictypes.h" +#include "base/values.h" + +namespace base { + +// Parses INI files in a string. Users should in inherit from this class. +// This is a very basic INI parser with these characteristics: +// - Ignores blank lines. +// - Ignores comment lines beginning with '#' or ';'. +// - Duplicate key names in the same section will simply cause repeated calls +// to HandleTriplet with the same |section| and |key| parameters. +// - No escape characters supported. +// - Global properties result in calls to HandleTriplet with an empty string in +// the |section| argument. +// - Section headers begin with a '[' character. It is recommended, but +// not required to close the header bracket with a ']' character. All +// characters after a closing ']' character is ignored. +// - Key value pairs are indicated with an '=' character. Whitespace is not +// ignored. Quoting is not supported. Everything before the first '=' +// is considered the |key|, and everything after is the |value|. +class BASE_EXPORT INIParser { + public: + INIParser(); + virtual ~INIParser(); + + // May only be called once per instance. + void Parse(const std::string& content); + + private: + virtual void HandleTriplet(const std::string& section, + const std::string& key, + const std::string& value) = 0; + + bool used_; +}; + +// Parsed values are stored as strings at the "section.key" path. Triplets with +// |section| or |key| parameters containing '.' are ignored. +class BASE_EXPORT DictionaryValueINIParser : public INIParser { + public: + DictionaryValueINIParser(); + virtual ~DictionaryValueINIParser(); + + const DictionaryValue& root() const { return root_; } + + private: + // INIParser implementation. + virtual void HandleTriplet(const std::string& section, + const std::string& key, + const std::string& value) OVERRIDE; + + DictionaryValue root_; + + DISALLOW_COPY_AND_ASSIGN(DictionaryValueINIParser); +}; + +} // namespace base + +#endif // BASE_INI_PARSER_H_ diff --git a/base/ini_parser_unittest.cc b/base/ini_parser_unittest.cc new file mode 100644 index 0000000..ff90f6c --- /dev/null +++ b/base/ini_parser_unittest.cc @@ -0,0 +1,131 @@ +// 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 <string> +#include <vector> + +#include "base/ini_parser.h" +#include "testing/gtest/include/gtest/gtest.h" + +namespace base { + +namespace { + +struct TestTriplet { + TestTriplet(const std::string& section, + const std::string& key, + const std::string& value) + : section(section), + key(key), + value(value) { + } + + std::string section; + std::string key; + std::string value; +}; + +class TestINIParser : public INIParser { + public: + explicit TestINIParser( + const std::vector<TestTriplet>& expected_triplets) + : expected_triplets_(expected_triplets), + pair_i_(0) { + } + virtual ~TestINIParser() {} + + size_t pair_i() { + return pair_i_; + } + + private: + virtual void HandleTriplet(const std::string& section, const std::string& key, + const std::string& value) OVERRIDE { + EXPECT_EQ(expected_triplets_[pair_i_].section, section); + EXPECT_EQ(expected_triplets_[pair_i_].key, key); + EXPECT_EQ(expected_triplets_[pair_i_].value, value); + ++pair_i_; + } + + std::vector<TestTriplet> expected_triplets_; + size_t pair_i_; +}; + +TEST(INIParserTest, BasicValid) { + std::vector<TestTriplet> expected_triplets; + expected_triplets.push_back(TestTriplet("section1", "key1", "value1")); + expected_triplets.push_back(TestTriplet("section1", "key2", "value2")); + expected_triplets.push_back(TestTriplet("section1", "key3", "value3")); + expected_triplets.push_back(TestTriplet("section2", "key4", "value4")); + expected_triplets.push_back(TestTriplet("section2", "key5", + "value=with=equals")); + expected_triplets.push_back(TestTriplet("section2", "key6", "value6")); + TestINIParser test_parser(expected_triplets); + + test_parser.Parse( + "[section1]\n" + "key1=value1\n" + "key2=value2\r\n" // Testing DOS "\r\n" line endings. + "key3=value3\n" + "[section2\n" // Testing omitted closing bracket. + "key4=value4\r" // Testing "\r" line endings. + "key5=value=with=equals\n" + "key6=value6"); // Testing omitted final line ending. +} + +TEST(INIParserTest, IgnoreBlankLinesAndComments) { + std::vector<TestTriplet> expected_triplets; + expected_triplets.push_back(TestTriplet("section1", "key1", "value1")); + expected_triplets.push_back(TestTriplet("section1", "key2", "value2")); + expected_triplets.push_back(TestTriplet("section1", "key3", "value3")); + expected_triplets.push_back(TestTriplet("section2", "key4", "value4")); + expected_triplets.push_back(TestTriplet("section2", "key5", "value5")); + expected_triplets.push_back(TestTriplet("section2", "key6", "value6")); + TestINIParser test_parser(expected_triplets); + + test_parser.Parse( + "\n" + "[section1]\n" + "key1=value1\n" + "\n" + "\n" + "key2=value2\n" + "key3=value3\n" + "\n" + ";Comment1" + "\n" + "[section2]\n" + "key4=value4\n" + "#Comment2\n" + "key5=value5\n" + "\n" + "key6=value6\n"); +} + +TEST(INIParserTest, DictionaryValueINIParser) { + DictionaryValueINIParser test_parser; + + test_parser.Parse( + "[section1]\n" + "key1=value1\n" + "key.2=value2\n" + "key3=va.lue3\n" + "[se.ction2]\n" + "key.4=value4\n" + "key5=value5\n"); + + const DictionaryValue& root = test_parser.root(); + std::string value; + EXPECT_TRUE(root.GetString("section1.key1", &value)); + EXPECT_EQ("value1", value); + EXPECT_FALSE(root.GetString("section1.key.2", &value)); + EXPECT_TRUE(root.GetString("section1.key3", &value)); + EXPECT_EQ("va.lue3", value); + EXPECT_FALSE(root.GetString("se.ction2.key.4", &value)); + EXPECT_FALSE(root.GetString("se.ction2.key5", &value)); +} + +} // namespace + +} // namespace base diff --git a/chrome/browser/importer/firefox_importer_utils.cc b/chrome/browser/importer/firefox_importer_utils.cc index 70c11f1..4553c11 100644 --- a/chrome/browser/importer/firefox_importer_utils.cc +++ b/chrome/browser/importer/firefox_importer_utils.cc @@ -9,10 +9,10 @@ #include <string> #include "base/file_util.h" +#include "base/ini_parser.h" #include "base/logging.h" #include "base/strings/string_number_conversions.h" #include "base/strings/string_split.h" -#include "base/strings/string_util.h" #include "base/strings/stringprintf.h" #include "base/strings/utf_string_conversions.h" #include "base/values.h" @@ -50,9 +50,12 @@ class FirefoxURLParameterFilter : public TemplateURLParser::ParameterFilter { } // namespace base::FilePath GetFirefoxProfilePath() { - DictionaryValue root; base::FilePath ini_file = GetProfilesINI(); - ParseProfileINI(ini_file, &root); + std::string content; + file_util::ReadFileToString(ini_file, &content); + base::DictionaryValueINIParser ini_parser; + ini_parser.Parse(content); + const DictionaryValue& root = ini_parser.root(); base::FilePath source_path; for (int i = 0; ; ++i) { @@ -130,50 +133,6 @@ bool GetFirefoxVersionAndPathFromProfile(const base::FilePath& profile_path, return ret; } -void ParseProfileINI(const base::FilePath& file, DictionaryValue* root) { - // Reads the whole INI file. - std::string content; - file_util::ReadFileToString(file, &content); - ReplaceSubstringsAfterOffset(&content, 0, "\r\n", "\n"); - std::vector<std::string> lines; - base::SplitString(content, '\n', &lines); - - // Parses the file. - root->Clear(); - std::string current_section; - for (size_t i = 0; i < lines.size(); ++i) { - std::string line = lines[i]; - 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); - // Checks whether the section and key contain a '.' character. - // Those sections and keys break DictionaryValue's path format, - // so we discard them. - if (current_section.find('.') == std::string::npos && - key.find('.') == std::string::npos) - root->SetString(current_section + "." + key, value); - } - } - } -} - bool CanImportURL(const GURL& url) { const char* kInvalidSchemes[] = {"wyciwyg", "place", "about", "chrome"}; |