1 // Copyright (c) 2012 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 "chromeos/system/name_value_pairs_parser.h"
7 #include "base/command_line.h"
8 #include "base/file_util.h"
9 #include "base/files/file_path.h"
10 #include "base/logging.h"
11 #include "base/process/launch.h"
12 #include "base/stl_util.h"
13 #include "base/strings/string_tokenizer.h"
14 #include "base/strings/string_util.h"
16 namespace chromeos
{ // NOLINT
21 const char kQuoteChars
[] = "\"";
22 const char kTrimChars
[] = "\" ";
24 bool GetToolOutput(int argc
, const char* argv
[], std::string
& output
) {
27 if (!base::PathExists(base::FilePath(argv
[0]))) {
28 LOG(WARNING
) << "Tool for statistics not found: " << argv
[0];
32 std::vector
<std::string
> args
;
33 for (int argn
= 0; argn
< argc
; ++argn
)
34 args
.push_back(argv
[argn
]);
35 if (!base::GetAppOutput(args
, &output
)) {
36 LOG(WARNING
) << "Error executing " << argv
[0];
45 NameValuePairsParser::NameValuePairsParser(NameValueMap
* map
)
49 void NameValuePairsParser::AddNameValuePair(const std::string
& key
,
50 const std::string
& value
) {
51 if (!ContainsKey(*map_
, key
)) {
53 VLOG(1) << "name: " << key
<< ", value: " << value
;
55 LOG(WARNING
) << "Key " << key
<< " already has value " << (*map_
)[key
]
56 << ", ignoring new value: " << value
;
60 bool NameValuePairsParser::ParseNameValuePairs(const std::string
& in_string
,
61 const std::string
& eq
,
62 const std::string
& delim
) {
63 return ParseNameValuePairsWithComments(in_string
, eq
, delim
, "");
66 bool NameValuePairsParser::ParseNameValuePairsWithComments(
67 const std::string
& in_string
,
68 const std::string
& eq
,
69 const std::string
& delim
,
70 const std::string
& comment_delim
) {
71 bool all_valid
= true;
72 // Set up the pair tokenizer.
73 base::StringTokenizer
pair_toks(in_string
, delim
);
74 pair_toks
.set_quote_chars(kQuoteChars
);
75 // Process token pairs.
76 while (pair_toks
.GetNext()) {
77 std::string
pair(pair_toks
.token());
78 // Anything before the first |eq| is the key, anything after is the value.
80 size_t eq_pos
= pair
.find(eq
);
81 if (eq_pos
!= std::string::npos
) {
82 // First |comment_delim| after |eq_pos| starts the comment.
83 // A value of |std::string::npos| means that the value spans to the end
85 size_t value_size
= std::string::npos
;
86 if (!comment_delim
.empty()) {
87 size_t comment_pos
= pair
.find(comment_delim
, eq_pos
+ 1);
88 if (comment_pos
!= std::string::npos
)
89 value_size
= comment_pos
- eq_pos
- 1;
94 base::TrimString(pair
.substr(0, eq_pos
), kTrimChars
, &key
);
95 base::TrimString(pair
.substr(eq_pos
+ 1, value_size
), kTrimChars
, &value
);
98 AddNameValuePair(key
, value
);
103 LOG(WARNING
) << "Invalid token pair: " << pair
<< ". Ignoring.";
109 bool NameValuePairsParser::GetSingleValueFromTool(int argc
,
111 const std::string
& key
) {
112 std::string output_string
;
113 if (!GetToolOutput(argc
, argv
, output_string
))
116 TrimWhitespaceASCII(output_string
, TRIM_ALL
, &output_string
);
117 AddNameValuePair(key
, output_string
);
121 bool NameValuePairsParser::GetNameValuePairsFromFile(
122 const base::FilePath
& file_path
,
123 const std::string
& eq
,
124 const std::string
& delim
) {
125 std::string contents
;
126 if (base::ReadFileToString(file_path
, &contents
)) {
127 return ParseNameValuePairs(contents
, eq
, delim
);
129 LOG(WARNING
) << "Unable to read statistics file: " << file_path
.value();
134 bool NameValuePairsParser::ParseNameValuePairsFromTool(
137 const std::string
& eq
,
138 const std::string
& delim
,
139 const std::string
& comment_delim
) {
140 std::string output_string
;
141 if (!GetToolOutput(argc
, argv
, output_string
))
144 return ParseNameValuePairsWithComments(
145 output_string
, eq
, delim
, comment_delim
);
148 } // namespace system
149 } // namespace chromeos