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/stl_util.h" |
12 #include "base/string_tokenizer.h" | 13 #include "base/string_tokenizer.h" |
13 #include "base/string_util.h" | 14 #include "base/string_util.h" |
14 | 15 |
15 namespace chromeos { // NOLINT | 16 namespace chromeos { // NOLINT |
16 namespace system { | 17 namespace system { |
17 | 18 |
18 namespace { | 19 namespace { |
19 | 20 |
20 const char kQuoteChars[] = "\""; | 21 const char kQuoteChars[] = "\""; |
21 const char kTrimChars[] = "\" "; | 22 const char kTrimChars[] = "\" "; |
(...skipping 16 matching lines...) Expand all Loading... |
38 } | 39 } |
39 | 40 |
40 } // namespace | 41 } // namespace |
41 | 42 |
42 NameValuePairsParser::NameValuePairsParser(NameValueMap* map) | 43 NameValuePairsParser::NameValuePairsParser(NameValueMap* map) |
43 : map_(map) { | 44 : map_(map) { |
44 } | 45 } |
45 | 46 |
46 void NameValuePairsParser::AddNameValuePair(const std::string& key, | 47 void NameValuePairsParser::AddNameValuePair(const std::string& key, |
47 const std::string& value) { | 48 const std::string& value) { |
48 (*map_)[key] = value; | 49 if (!ContainsKey(*map_, key)) { |
49 VLOG(1) << "name: " << key << ", value: " << value; | 50 (*map_)[key] = value; |
| 51 VLOG(1) << "name: " << key << ", value: " << value; |
| 52 } else { |
| 53 LOG(WARNING) << "Key " << key << " already has value " << (*map_)[key] |
| 54 << ", ignoring new value: " << value; |
| 55 } |
50 } | 56 } |
51 | 57 |
52 bool NameValuePairsParser::ParseNameValuePairs(const std::string& in_string, | 58 bool NameValuePairsParser::ParseNameValuePairs(const std::string& in_string, |
53 const std::string& eq, | 59 const std::string& eq, |
54 const std::string& delim) { | 60 const std::string& delim) { |
55 return ParseNameValuePairsWithComments(in_string, eq, delim, ""); | 61 return ParseNameValuePairsWithComments(in_string, eq, delim, ""); |
56 } | 62 } |
57 | 63 |
58 bool NameValuePairsParser::ParseNameValuePairsWithComments( | 64 bool NameValuePairsParser::ParseNameValuePairsWithComments( |
59 const std::string& in_string, | 65 const std::string& in_string, |
60 const std::string& eq, | 66 const std::string& eq, |
61 const std::string& delim, | 67 const std::string& delim, |
62 const std::string& comment_delim) { | 68 const std::string& comment_delim) { |
| 69 bool all_valid = true; |
63 // Set up the pair tokenizer. | 70 // Set up the pair tokenizer. |
64 StringTokenizer pair_toks(in_string, delim); | 71 StringTokenizer pair_toks(in_string, delim); |
65 pair_toks.set_quote_chars(kQuoteChars); | 72 pair_toks.set_quote_chars(kQuoteChars); |
66 // Process token pairs. | 73 // Process token pairs. |
67 while (pair_toks.GetNext()) { | 74 while (pair_toks.GetNext()) { |
68 std::string pair(pair_toks.token()); | 75 std::string pair(pair_toks.token()); |
69 if (pair.find(eq) == 0) { | 76 // Anything before the first |eq| is the key, anything after is the value. |
70 LOG(WARNING) << "Empty key: '" << pair << "'. Aborting."; | 77 // |eq| must exist. |
71 return false; | 78 size_t eq_pos = pair.find(eq); |
72 } | 79 if (eq_pos != std::string::npos) { |
73 StringTokenizer keyvalue(pair, eq); | 80 // First |comment_delim| after |eq_pos| starts the comment. |
74 std::string key; | 81 // A value of |std::string::npos| means that the value spans to the end |
75 std::string value; | 82 // of |pair|. |
76 if (keyvalue.GetNext()) { | 83 size_t value_size = std::string::npos; |
77 TrimString(keyvalue.token(), kTrimChars, &key); | 84 if (!comment_delim.empty()) { |
78 if (keyvalue.GetNext()) { | 85 size_t comment_pos = pair.find(comment_delim, eq_pos + 1); |
79 value = keyvalue.token(); | 86 if (comment_pos != std::string::npos) |
80 if (keyvalue.GetNext()) { | 87 value_size = comment_pos - eq_pos - 1; |
81 LOG(WARNING) << "Multiple key tokens: '" << pair << "'. Aborting."; | 88 } |
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 | 89 |
92 TrimString(value, kTrimChars, &value); | 90 std::string key; |
| 91 std::string value; |
| 92 TrimString(pair.substr(0, eq_pos), kTrimChars, &key); |
| 93 TrimString(pair.substr(eq_pos + 1, value_size), kTrimChars, &value); |
| 94 |
| 95 if (!key.empty()) { |
| 96 AddNameValuePair(key, value); |
| 97 continue; |
93 } | 98 } |
94 } | 99 } |
95 if (key.empty()) { | 100 |
96 LOG(WARNING) << "Invalid token pair: '" << pair << "'. Aborting."; | 101 LOG(WARNING) << "Invalid token pair: " << pair << ". Ignoring."; |
97 return false; | 102 all_valid = false; |
98 } | |
99 AddNameValuePair(key, value); | |
100 } | 103 } |
101 return true; | 104 return all_valid; |
102 } | 105 } |
103 | 106 |
104 bool NameValuePairsParser::GetSingleValueFromTool(int argc, | 107 bool NameValuePairsParser::GetSingleValueFromTool(int argc, |
105 const char* argv[], | 108 const char* argv[], |
106 const std::string& key) { | 109 const std::string& key) { |
107 std::string output_string; | 110 std::string output_string; |
108 if (!GetToolOutput(argc, argv, output_string)) | 111 if (!GetToolOutput(argc, argv, output_string)) |
109 return false; | 112 return false; |
110 | 113 |
111 TrimWhitespaceASCII(output_string, TRIM_ALL, &output_string); | 114 TrimWhitespaceASCII(output_string, TRIM_ALL, &output_string); |
112 AddNameValuePair(key, output_string); | 115 AddNameValuePair(key, output_string); |
113 return true; | 116 return true; |
114 } | 117 } |
115 | 118 |
116 void NameValuePairsParser::GetNameValuePairsFromFile(const FilePath& file_path, | 119 bool NameValuePairsParser::GetNameValuePairsFromFile(const FilePath& file_path, |
117 const std::string& eq, | 120 const std::string& eq, |
118 const std::string& delim) { | 121 const std::string& delim) { |
119 std::string contents; | 122 std::string contents; |
120 if (file_util::ReadFileToString(file_path, &contents)) { | 123 if (file_util::ReadFileToString(file_path, &contents)) { |
121 ParseNameValuePairs(contents, eq, delim); | 124 return ParseNameValuePairs(contents, eq, delim); |
122 } else { | 125 } else { |
123 LOG(WARNING) << "Unable to read statistics file: " << file_path.value(); | 126 LOG(WARNING) << "Unable to read statistics file: " << file_path.value(); |
| 127 return false; |
124 } | 128 } |
125 } | 129 } |
126 | 130 |
127 bool NameValuePairsParser::ParseNameValuePairsFromTool( | 131 bool NameValuePairsParser::ParseNameValuePairsFromTool( |
128 int argc, | 132 int argc, |
129 const char* argv[], | 133 const char* argv[], |
130 const std::string& eq, | 134 const std::string& eq, |
131 const std::string& delim, | 135 const std::string& delim, |
132 const std::string& comment_delim) { | 136 const std::string& comment_delim) { |
133 std::string output_string; | 137 std::string output_string; |
134 if (!GetToolOutput(argc, argv, output_string)) | 138 if (!GetToolOutput(argc, argv, output_string)) |
135 return false; | 139 return false; |
136 | 140 |
137 return ParseNameValuePairsWithComments( | 141 return ParseNameValuePairsWithComments( |
138 output_string, eq, delim, comment_delim); | 142 output_string, eq, delim, comment_delim); |
139 } | 143 } |
140 | 144 |
141 } // namespace system | 145 } // namespace system |
142 } // namespace chromeos | 146 } // namespace chromeos |
OLD | NEW |