OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chromeos/system/name_value_pairs_parser.h" | 5 #include "chromeos/system/name_value_pairs_parser.h" |
6 | 6 |
7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
8 #include "base/file_util.h" | |
9 #include "base/files/file_path.h" | 8 #include "base/files/file_path.h" |
| 9 #include "base/files/file_util.h" |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
11 #include "base/process/launch.h" | 11 #include "base/process/launch.h" |
12 #include "base/stl_util.h" | 12 #include "base/stl_util.h" |
13 #include "base/strings/string_tokenizer.h" | 13 #include "base/strings/string_tokenizer.h" |
14 #include "base/strings/string_util.h" | 14 #include "base/strings/string_util.h" |
15 #include "base/sys_info.h" | 15 #include "base/sys_info.h" |
16 | 16 |
17 namespace chromeos { // NOLINT | 17 namespace chromeos { // NOLINT |
18 namespace system { | 18 namespace system { |
19 | 19 |
20 namespace { | 20 namespace { |
21 | 21 |
22 bool GetToolOutput(int argc, const char* argv[], std::string& output) { | 22 bool GetToolOutput(int argc, const char* argv[], std::string* output) { |
23 DCHECK_GE(argc, 1); | 23 DCHECK_GE(argc, 1); |
24 | 24 |
25 if (!base::PathExists(base::FilePath(argv[0]))) { | 25 if (!base::PathExists(base::FilePath(argv[0]))) { |
26 LOG(WARNING) << "Tool for statistics not found: " << argv[0]; | 26 LOG(WARNING) << "Tool for statistics not found: " << argv[0]; |
27 return false; | 27 return false; |
28 } | 28 } |
29 | 29 |
30 std::vector<std::string> args; | 30 std::vector<std::string> args; |
31 for (int argn = 0; argn < argc; ++argn) | 31 for (int argn = 0; argn < argc; ++argn) |
32 args.push_back(argv[argn]); | 32 args.push_back(argv[argn]); |
33 if (!base::GetAppOutput(args, &output)) { | 33 if (!base::GetAppOutput(args, output)) { |
34 LOG(WARNING) << "Error executing " << argv[0]; | 34 LOG(WARNING) << "Error executing " << argv[0]; |
35 return false; | 35 return false; |
36 } | 36 } |
37 | 37 |
38 return true; | 38 return true; |
39 } | 39 } |
40 | 40 |
41 } // namespace | 41 } // namespace |
42 | 42 |
43 NameValuePairsParser::NameValuePairsParser(NameValueMap* map) | 43 NameValuePairsParser::NameValuePairsParser(NameValueMap* map) |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
102 LOG(WARNING) << "Invalid token pair: " << pair << ". Ignoring."; | 102 LOG(WARNING) << "Invalid token pair: " << pair << ". Ignoring."; |
103 all_valid = false; | 103 all_valid = false; |
104 } | 104 } |
105 return all_valid; | 105 return all_valid; |
106 } | 106 } |
107 | 107 |
108 bool NameValuePairsParser::GetSingleValueFromTool(int argc, | 108 bool NameValuePairsParser::GetSingleValueFromTool(int argc, |
109 const char* argv[], | 109 const char* argv[], |
110 const std::string& key) { | 110 const std::string& key) { |
111 std::string output_string; | 111 std::string output_string; |
112 if (!GetToolOutput(argc, argv, output_string)) | 112 if (!GetToolOutput(argc, argv, &output_string)) |
113 return false; | 113 return false; |
114 | 114 |
115 base::TrimWhitespaceASCII(output_string, base::TRIM_ALL, &output_string); | 115 base::TrimWhitespaceASCII(output_string, base::TRIM_ALL, &output_string); |
116 AddNameValuePair(key, output_string); | 116 AddNameValuePair(key, output_string); |
117 return true; | 117 return true; |
118 } | 118 } |
119 | 119 |
120 bool NameValuePairsParser::GetNameValuePairsFromFile( | 120 bool NameValuePairsParser::GetNameValuePairsFromFile( |
121 const base::FilePath& file_path, | 121 const base::FilePath& file_path, |
122 const std::string& eq, | 122 const std::string& eq, |
123 const std::string& delim) { | 123 const std::string& delim) { |
124 std::string contents; | 124 std::string contents; |
125 if (base::ReadFileToString(file_path, &contents)) { | 125 if (base::ReadFileToString(file_path, &contents)) { |
126 return ParseNameValuePairs(contents, eq, delim); | 126 return ParseNameValuePairs(contents, eq, delim); |
127 } else { | 127 } else { |
128 if (base::SysInfo::IsRunningOnChromeOS()) | 128 if (base::SysInfo::IsRunningOnChromeOS()) |
129 VLOG(1) << "Statistics file not present: " << file_path.value(); | 129 VLOG(1) << "Statistics file not present: " << file_path.value(); |
130 return false; | 130 return false; |
131 } | 131 } |
132 } | 132 } |
133 | 133 |
134 bool NameValuePairsParser::ParseNameValuePairsFromTool( | 134 bool NameValuePairsParser::ParseNameValuePairsFromTool( |
135 int argc, | 135 int argc, |
136 const char* argv[], | 136 const char* argv[], |
137 const std::string& eq, | 137 const std::string& eq, |
138 const std::string& delim, | 138 const std::string& delim, |
139 const std::string& comment_delim) { | 139 const std::string& comment_delim) { |
140 std::string output_string; | 140 std::string output_string; |
141 if (!GetToolOutput(argc, argv, output_string)) | 141 if (!GetToolOutput(argc, argv, &output_string)) |
142 return false; | 142 return false; |
143 | 143 |
144 return ParseNameValuePairsWithComments( | 144 return ParseNameValuePairsWithComments( |
145 output_string, eq, delim, comment_delim); | 145 output_string, eq, delim, comment_delim); |
146 } | 146 } |
147 | 147 |
148 } // namespace system | 148 } // namespace system |
149 } // namespace chromeos | 149 } // namespace chromeos |
OLD | NEW |