1 // Copyright 2014 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.
5 #include "chrome/common/ini_parser.h"
7 #include "base/logging.h"
8 #include "base/strings/string_tokenizer.h"
10 INIParser::INIParser() : used_(false) {}
12 INIParser::~INIParser() {}
14 void INIParser::Parse(const std::string
& content
) {
17 base::StringTokenizer
tokenizer(content
, "\r\n");
19 std::string current_section
;
20 while (tokenizer
.GetNext()) {
21 std::string line
= tokenizer
.token();
23 // Skips the empty line.
26 if (line
[0] == '#' || line
[0] == ';') {
27 // This line is a comment.
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
);
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 HandleTriplet(current_section
, key
, value
);
48 DictionaryValueINIParser::DictionaryValueINIParser() {}
50 DictionaryValueINIParser::~DictionaryValueINIParser() {}
52 void DictionaryValueINIParser::HandleTriplet(const std::string
& section
,
53 const std::string
& key
,
54 const std::string
& value
) {
56 // Checks whether the section and key contain a '.' character.
57 // Those sections and keys break DictionaryValue's path format when not
58 // using the *WithoutPathExpansion methods.
59 if (section
.find('.') == std::string::npos
&&
60 key
.find('.') == std::string::npos
)
61 root_
.SetString(section
+ "." + key
, value
);