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 "chrome/browser/chromeos/system/name_value_pairs_parser.h" | 5 #include "chrome/browser/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_path.h" | 8 #include "base/file_path.h" |
9 #include "base/file_util.h" | 9 #include "base/file_util.h" |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
11 #include "base/process_util.h" | 11 #include "base/process_util.h" |
12 #include "base/string_tokenizer.h" | 12 #include "base/string_tokenizer.h" |
13 #include "base/string_util.h" | 13 #include "base/string_util.h" |
14 | 14 |
15 namespace chromeos { // NOLINT | 15 namespace chromeos { // NOLINT |
16 namespace system { | 16 namespace system { |
17 | 17 |
18 namespace { | 18 namespace { |
19 | 19 |
20 const char kQuoteChars[] = "\""; | 20 const char kQuoteChars[] = "\""; |
21 const char kTrimChars[] = "\" "; | 21 const char kTrimChars[] = "\" \t\r"; |
Mattias Nissler (ping if slow)
2012/04/19 13:51:39
Are you sure this change is save? I.e. did you mak
Joao da Silva
2012/04/19 16:01:30
Better leave it out for now :-) Everything seemed
| |
22 | 22 |
23 bool GetToolOutput(int argc, const char* argv[], std::string& output) { | 23 bool GetToolOutput(int argc, const char* argv[], std::string& output) { |
24 DCHECK_GE(argc, 1); | 24 DCHECK_GE(argc, 1); |
25 | 25 |
26 if (!file_util::PathExists(FilePath(argv[0]))) { | 26 if (!file_util::PathExists(FilePath(argv[0]))) { |
27 LOG(WARNING) << "Tool for statistics not found: " << argv[0]; | 27 LOG(WARNING) << "Tool for statistics not found: " << argv[0]; |
28 return false; | 28 return false; |
29 } | 29 } |
30 | 30 |
31 CommandLine command_line(argc, argv); | 31 CommandLine command_line(argc, argv); |
(...skipping 21 matching lines...) Expand all Loading... | |
53 const std::string& eq, | 53 const std::string& eq, |
54 const std::string& delim) { | 54 const std::string& delim) { |
55 return ParseNameValuePairsWithComments(in_string, eq, delim, ""); | 55 return ParseNameValuePairsWithComments(in_string, eq, delim, ""); |
56 } | 56 } |
57 | 57 |
58 bool NameValuePairsParser::ParseNameValuePairsWithComments( | 58 bool NameValuePairsParser::ParseNameValuePairsWithComments( |
59 const std::string& in_string, | 59 const std::string& in_string, |
60 const std::string& eq, | 60 const std::string& eq, |
61 const std::string& delim, | 61 const std::string& delim, |
62 const std::string& comment_delim) { | 62 const std::string& comment_delim) { |
63 bool all_valid = true; | |
63 // Set up the pair tokenizer. | 64 // Set up the pair tokenizer. |
64 StringTokenizer pair_toks(in_string, delim); | 65 StringTokenizer pair_toks(in_string, delim); |
65 pair_toks.set_quote_chars(kQuoteChars); | 66 pair_toks.set_quote_chars(kQuoteChars); |
66 // Process token pairs. | 67 // Process token pairs. |
67 while (pair_toks.GetNext()) { | 68 while (pair_toks.GetNext()) { |
68 std::string pair(pair_toks.token()); | 69 std::string pair(pair_toks.token()); |
69 if (pair.find(eq) == 0) { | 70 // Anything before the first |eq| is the key, anything after is the value. |
70 LOG(WARNING) << "Empty key: '" << pair << "'. Aborting."; | 71 // |eq| must exist. |
71 return false; | 72 size_t eq_pos = pair.find(eq); |
72 } | 73 if (eq_pos != std::string::npos) { |
73 StringTokenizer keyvalue(pair, eq); | 74 // First |comment_delim| after |eq_pos| starts the comment. |
74 std::string key; | 75 // A value of |std::string::npos| means that the value spans to the end |
75 std::string value; | 76 // of |pair|. |
76 if (keyvalue.GetNext()) { | 77 size_t value_size = std::string::npos; |
77 TrimString(keyvalue.token(), kTrimChars, &key); | 78 if (!comment_delim.empty()) { |
78 if (keyvalue.GetNext()) { | 79 size_t comment_pos = pair.find(comment_delim, eq_pos + 1); |
79 value = keyvalue.token(); | 80 if (comment_pos != std::string::npos) |
80 if (keyvalue.GetNext()) { | 81 value_size = comment_pos - eq_pos - 1; |
81 LOG(WARNING) << "Multiple key tokens: '" << pair << "'. Aborting."; | 82 } |
82 return false; | |
83 } | |
84 // If value ends with a comment, throw away everything after | |
85 // comment_delim is encountered. | |
86 if (!comment_delim.empty()) { | |
87 StringTokenizer value_with_comment(value, comment_delim); | |
88 value_with_comment.GetNext(); | |
89 value = value_with_comment.token(); | |
90 } | |
91 | 83 |
92 TrimString(value, kTrimChars, &value); | 84 std::string key; |
85 std::string value; | |
86 TrimString(pair.substr(0, eq_pos), kTrimChars, &key); | |
87 TrimString(pair.substr(eq_pos + 1, value_size), kTrimChars, &value); | |
88 | |
89 if (!key.empty()) { | |
90 AddNameValuePair(key, value); | |
91 continue; | |
93 } | 92 } |
94 } | 93 } |
95 if (key.empty()) { | 94 |
96 LOG(WARNING) << "Invalid token pair: '" << pair << "'. Aborting."; | 95 LOG(WARNING) << "Invalid token pair: " << pair << ". Ignoring."; |
97 return false; | 96 all_valid = false; |
98 } | |
99 AddNameValuePair(key, value); | |
100 } | 97 } |
101 return true; | 98 return all_valid; |
102 } | 99 } |
103 | 100 |
104 bool NameValuePairsParser::GetSingleValueFromTool(int argc, | 101 bool NameValuePairsParser::GetSingleValueFromTool(int argc, |
105 const char* argv[], | 102 const char* argv[], |
106 const std::string& key) { | 103 const std::string& key) { |
107 std::string output_string; | 104 std::string output_string; |
108 if (!GetToolOutput(argc, argv, output_string)) | 105 if (!GetToolOutput(argc, argv, output_string)) |
109 return false; | 106 return false; |
110 | 107 |
111 TrimWhitespaceASCII(output_string, TRIM_ALL, &output_string); | 108 TrimWhitespaceASCII(output_string, TRIM_ALL, &output_string); |
112 AddNameValuePair(key, output_string); | 109 AddNameValuePair(key, output_string); |
113 return true; | 110 return true; |
114 } | 111 } |
115 | 112 |
116 void NameValuePairsParser::GetNameValuePairsFromFile(const FilePath& file_path, | 113 bool NameValuePairsParser::GetNameValuePairsFromFile(const FilePath& file_path, |
117 const std::string& eq, | 114 const std::string& eq, |
118 const std::string& delim) { | 115 const std::string& delim) { |
119 std::string contents; | 116 std::string contents; |
120 if (file_util::ReadFileToString(file_path, &contents)) { | 117 if (file_util::ReadFileToString(file_path, &contents)) { |
121 ParseNameValuePairs(contents, eq, delim); | 118 return ParseNameValuePairs(contents, eq, delim); |
122 } else { | 119 } else { |
123 LOG(WARNING) << "Unable to read statistics file: " << file_path.value(); | 120 LOG(WARNING) << "Unable to read statistics file: " << file_path.value(); |
121 return false; | |
124 } | 122 } |
125 } | 123 } |
126 | 124 |
127 bool NameValuePairsParser::ParseNameValuePairsFromTool( | 125 bool NameValuePairsParser::ParseNameValuePairsFromTool( |
128 int argc, | 126 int argc, |
129 const char* argv[], | 127 const char* argv[], |
130 const std::string& eq, | 128 const std::string& eq, |
131 const std::string& delim, | 129 const std::string& delim, |
132 const std::string& comment_delim) { | 130 const std::string& comment_delim) { |
133 std::string output_string; | 131 std::string output_string; |
134 if (!GetToolOutput(argc, argv, output_string)) | 132 if (!GetToolOutput(argc, argv, output_string)) |
135 return false; | 133 return false; |
136 | 134 |
137 return ParseNameValuePairsWithComments( | 135 return ParseNameValuePairsWithComments( |
138 output_string, eq, delim, comment_delim); | 136 output_string, eq, delim, comment_delim); |
139 } | 137 } |
140 | 138 |
141 } // namespace system | 139 } // namespace system |
142 } // namespace chromeos | 140 } // namespace chromeos |
OLD | NEW |