blob: 820b26d36eb990c4a52b6dc87f4efba39830cc69 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
|
// Copyright 2014 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 "chrome/common/ini_parser.h"
#include <stddef.h>
#include "base/check.h"
#include "base/strings/strcat.h"
#include "base/strings/string_tokenizer.h"
INIParser::INIParser() : used_(false) {}
INIParser::~INIParser() {}
void INIParser::Parse(const std::string& content) {
DCHECK(!used_);
used_ = true;
base::StringTokenizer tokenizer(content, "\r\n");
base::StringPiece current_section;
while (tokenizer.GetNext()) {
base::StringPiece line = tokenizer.token_piece();
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 = current_section.substr(0, end);
} else {
base::StringPiece 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(base::StringPiece section,
base::StringPiece key,
base::StringPiece 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(base::StrCat({section, ".", key}), value);
}
|