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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
|
// Copyright (c) 2012 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 "chromeos/system/name_value_pairs_parser.h"
#include "base/command_line.h"
#include "base/file_util.h"
#include "base/files/file_path.h"
#include "base/logging.h"
#include "base/process/launch.h"
#include "base/stl_util.h"
#include "base/strings/string_tokenizer.h"
#include "base/strings/string_util.h"
namespace chromeos { // NOLINT
namespace system {
namespace {
const char kQuoteChars[] = "\"";
const char kTrimChars[] = "\" ";
bool GetToolOutput(int argc, const char* argv[], std::string& output) {
DCHECK_GE(argc, 1);
if (!base::PathExists(base::FilePath(argv[0]))) {
LOG(WARNING) << "Tool for statistics not found: " << argv[0];
return false;
}
std::vector<std::string> args;
for (int argn = 0; argn < argc; ++argn)
args.push_back(argv[argn]);
if (!base::GetAppOutput(args, &output)) {
LOG(WARNING) << "Error executing " << argv[0];
return false;
}
return true;
}
} // namespace
NameValuePairsParser::NameValuePairsParser(NameValueMap* map)
: map_(map) {
}
void NameValuePairsParser::AddNameValuePair(const std::string& key,
const std::string& value) {
if (!ContainsKey(*map_, key)) {
(*map_)[key] = value;
VLOG(1) << "name: " << key << ", value: " << value;
} else {
LOG(WARNING) << "Key " << key << " already has value " << (*map_)[key]
<< ", ignoring new value: " << value;
}
}
bool NameValuePairsParser::ParseNameValuePairs(const std::string& in_string,
const std::string& eq,
const std::string& delim) {
return ParseNameValuePairsWithComments(in_string, eq, delim, "");
}
bool NameValuePairsParser::ParseNameValuePairsWithComments(
const std::string& in_string,
const std::string& eq,
const std::string& delim,
const std::string& comment_delim) {
bool all_valid = true;
// Set up the pair tokenizer.
base::StringTokenizer pair_toks(in_string, delim);
pair_toks.set_quote_chars(kQuoteChars);
// Process token pairs.
while (pair_toks.GetNext()) {
std::string pair(pair_toks.token());
// Anything before the first |eq| is the key, anything after is the value.
// |eq| must exist.
size_t eq_pos = pair.find(eq);
if (eq_pos != std::string::npos) {
// First |comment_delim| after |eq_pos| starts the comment.
// A value of |std::string::npos| means that the value spans to the end
// of |pair|.
size_t value_size = std::string::npos;
if (!comment_delim.empty()) {
size_t comment_pos = pair.find(comment_delim, eq_pos + 1);
if (comment_pos != std::string::npos)
value_size = comment_pos - eq_pos - 1;
}
std::string key;
std::string value;
base::TrimString(pair.substr(0, eq_pos), kTrimChars, &key);
base::TrimString(pair.substr(eq_pos + 1, value_size), kTrimChars, &value);
if (!key.empty()) {
AddNameValuePair(key, value);
continue;
}
}
LOG(WARNING) << "Invalid token pair: " << pair << ". Ignoring.";
all_valid = false;
}
return all_valid;
}
bool NameValuePairsParser::GetSingleValueFromTool(int argc,
const char* argv[],
const std::string& key) {
std::string output_string;
if (!GetToolOutput(argc, argv, output_string))
return false;
TrimWhitespaceASCII(output_string, TRIM_ALL, &output_string);
AddNameValuePair(key, output_string);
return true;
}
bool NameValuePairsParser::GetNameValuePairsFromFile(
const base::FilePath& file_path,
const std::string& eq,
const std::string& delim) {
std::string contents;
if (base::ReadFileToString(file_path, &contents)) {
return ParseNameValuePairs(contents, eq, delim);
} else {
LOG(WARNING) << "Unable to read statistics file: " << file_path.value();
return false;
}
}
bool NameValuePairsParser::ParseNameValuePairsFromTool(
int argc,
const char* argv[],
const std::string& eq,
const std::string& delim,
const std::string& comment_delim) {
std::string output_string;
if (!GetToolOutput(argc, argv, output_string))
return false;
return ParseNameValuePairsWithComments(
output_string, eq, delim, comment_delim);
}
} // namespace system
} // namespace chromeos
|