| 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" | 8 #include "base/file_util.h" |
| 9 #include "base/files/file_path.h" | 9 #include "base/files/file_path.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 const char kQuoteChars[] = "\""; | |
| 23 const char kTrimChars[] = "\" "; | |
| 24 | |
| 25 bool GetToolOutput(int argc, const char* argv[], std::string& output) { | 22 bool GetToolOutput(int argc, const char* argv[], std::string& output) { |
| 26 DCHECK_GE(argc, 1); | 23 DCHECK_GE(argc, 1); |
| 27 | 24 |
| 28 if (!base::PathExists(base::FilePath(argv[0]))) { | 25 if (!base::PathExists(base::FilePath(argv[0]))) { |
| 29 LOG(WARNING) << "Tool for statistics not found: " << argv[0]; | 26 LOG(WARNING) << "Tool for statistics not found: " << argv[0]; |
| 30 return false; | 27 return false; |
| 31 } | 28 } |
| 32 | 29 |
| 33 std::vector<std::string> args; | 30 std::vector<std::string> args; |
| 34 for (int argn = 0; argn < argc; ++argn) | 31 for (int argn = 0; argn < argc; ++argn) |
| (...skipping 30 matching lines...) Expand all Loading... |
| 65 } | 62 } |
| 66 | 63 |
| 67 bool NameValuePairsParser::ParseNameValuePairsWithComments( | 64 bool NameValuePairsParser::ParseNameValuePairsWithComments( |
| 68 const std::string& in_string, | 65 const std::string& in_string, |
| 69 const std::string& eq, | 66 const std::string& eq, |
| 70 const std::string& delim, | 67 const std::string& delim, |
| 71 const std::string& comment_delim) { | 68 const std::string& comment_delim) { |
| 72 bool all_valid = true; | 69 bool all_valid = true; |
| 73 // Set up the pair tokenizer. | 70 // Set up the pair tokenizer. |
| 74 base::StringTokenizer pair_toks(in_string, delim); | 71 base::StringTokenizer pair_toks(in_string, delim); |
| 75 pair_toks.set_quote_chars(kQuoteChars); | 72 pair_toks.set_quote_chars("\""); |
| 76 // Process token pairs. | 73 // Process token pairs. |
| 77 while (pair_toks.GetNext()) { | 74 while (pair_toks.GetNext()) { |
| 78 std::string pair(pair_toks.token()); | 75 std::string pair(pair_toks.token()); |
| 79 // Anything before the first |eq| is the key, anything after is the value. | 76 // Anything before the first |eq| is the key, anything after is the value. |
| 80 // |eq| must exist. | 77 // |eq| must exist. |
| 81 size_t eq_pos = pair.find(eq); | 78 size_t eq_pos = pair.find(eq); |
| 82 if (eq_pos != std::string::npos) { | 79 if (eq_pos != std::string::npos) { |
| 83 // First |comment_delim| after |eq_pos| starts the comment. | 80 // First |comment_delim| after |eq_pos| starts the comment. |
| 84 // A value of |std::string::npos| means that the value spans to the end | 81 // A value of |std::string::npos| means that the value spans to the end |
| 85 // of |pair|. | 82 // of |pair|. |
| 86 size_t value_size = std::string::npos; | 83 size_t value_size = std::string::npos; |
| 87 if (!comment_delim.empty()) { | 84 if (!comment_delim.empty()) { |
| 88 size_t comment_pos = pair.find(comment_delim, eq_pos + 1); | 85 size_t comment_pos = pair.find(comment_delim, eq_pos + 1); |
| 89 if (comment_pos != std::string::npos) | 86 if (comment_pos != std::string::npos) |
| 90 value_size = comment_pos - eq_pos - 1; | 87 value_size = comment_pos - eq_pos - 1; |
| 91 } | 88 } |
| 92 | 89 |
| 90 static const char kTrimChars[] = "\" "; |
| 93 std::string key; | 91 std::string key; |
| 94 std::string value; | 92 std::string value; |
| 95 base::TrimString(pair.substr(0, eq_pos), kTrimChars, &key); | 93 base::TrimString(pair.substr(0, eq_pos), kTrimChars, &key); |
| 96 base::TrimString(pair.substr(eq_pos + 1, value_size), kTrimChars, &value); | 94 base::TrimString(pair.substr(eq_pos + 1, value_size), kTrimChars, &value); |
| 97 | 95 |
| 98 if (!key.empty()) { | 96 if (!key.empty()) { |
| 99 AddNameValuePair(key, value); | 97 AddNameValuePair(key, value); |
| 100 continue; | 98 continue; |
| 101 } | 99 } |
| 102 } | 100 } |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 142 std::string output_string; | 140 std::string output_string; |
| 143 if (!GetToolOutput(argc, argv, output_string)) | 141 if (!GetToolOutput(argc, argv, output_string)) |
| 144 return false; | 142 return false; |
| 145 | 143 |
| 146 return ParseNameValuePairsWithComments( | 144 return ParseNameValuePairsWithComments( |
| 147 output_string, eq, delim, comment_delim); | 145 output_string, eq, delim, comment_delim); |
| 148 } | 146 } |
| 149 | 147 |
| 150 } // namespace system | 148 } // namespace system |
| 151 } // namespace chromeos | 149 } // namespace chromeos |
| OLD | NEW |