Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(305)

Side by Side Diff: chrome/browser/chromeos/system/name_value_pairs_parser.cc

Issue 10127009: Make NameValuePairsParser support values that contain the 'equal' separator. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix typo Created 8 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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"
(...skipping 27 matching lines...) Expand all
38 } 38 }
39 39
40 } // namespace 40 } // namespace
41 41
42 NameValuePairsParser::NameValuePairsParser(NameValueMap* map) 42 NameValuePairsParser::NameValuePairsParser(NameValueMap* map)
43 : map_(map) { 43 : map_(map) {
44 } 44 }
45 45
46 void NameValuePairsParser::AddNameValuePair(const std::string& key, 46 void NameValuePairsParser::AddNameValuePair(const std::string& key,
47 const std::string& value) { 47 const std::string& value) {
48 (*map_)[key] = value; 48 if (map_->find(key) == map_->end()) {
petkov 2012/04/23 09:34:06 you could use ContainsKey from base.
Joao da Silva 2012/04/23 09:50:06 Done.
49 VLOG(1) << "name: " << key << ", value: " << value; 49 (*map_)[key] = value;
50 VLOG(1) << "name: " << key << ", value: " << value;
51 } else {
52 LOG(WARNING) << "Key " << key << " already has value " << (*map_)[key]
53 << ", ignoring new value: " << value;
54 }
50 } 55 }
51 56
52 bool NameValuePairsParser::ParseNameValuePairs(const std::string& in_string, 57 bool NameValuePairsParser::ParseNameValuePairs(const std::string& in_string,
53 const std::string& eq, 58 const std::string& eq,
54 const std::string& delim) { 59 const std::string& delim) {
55 return ParseNameValuePairsWithComments(in_string, eq, delim, ""); 60 return ParseNameValuePairsWithComments(in_string, eq, delim, "");
56 } 61 }
57 62
58 bool NameValuePairsParser::ParseNameValuePairsWithComments( 63 bool NameValuePairsParser::ParseNameValuePairsWithComments(
59 const std::string& in_string, 64 const std::string& in_string,
60 const std::string& eq, 65 const std::string& eq,
61 const std::string& delim, 66 const std::string& delim,
62 const std::string& comment_delim) { 67 const std::string& comment_delim) {
68 bool all_valid = true;
63 // Set up the pair tokenizer. 69 // Set up the pair tokenizer.
64 StringTokenizer pair_toks(in_string, delim); 70 StringTokenizer pair_toks(in_string, delim);
65 pair_toks.set_quote_chars(kQuoteChars); 71 pair_toks.set_quote_chars(kQuoteChars);
66 // Process token pairs. 72 // Process token pairs.
67 while (pair_toks.GetNext()) { 73 while (pair_toks.GetNext()) {
68 std::string pair(pair_toks.token()); 74 std::string pair(pair_toks.token());
69 if (pair.find(eq) == 0) { 75 // Anything before the first |eq| is the key, anything after is the value.
70 LOG(WARNING) << "Empty key: '" << pair << "'. Aborting."; 76 // |eq| must exist.
71 return false; 77 size_t eq_pos = pair.find(eq);
72 } 78 if (eq_pos != std::string::npos) {
petkov 2012/04/23 09:34:06 We don't handle (i.e., ignore without setting is_v
Joao da Silva 2012/04/23 09:50:06 The StringTokenizer takes care of that; it doesn't
73 StringTokenizer keyvalue(pair, eq); 79 // First |comment_delim| after |eq_pos| starts the comment.
74 std::string key; 80 // A value of |std::string::npos| means that the value spans to the end
75 std::string value; 81 // of |pair|.
76 if (keyvalue.GetNext()) { 82 size_t value_size = std::string::npos;
77 TrimString(keyvalue.token(), kTrimChars, &key); 83 if (!comment_delim.empty()) {
78 if (keyvalue.GetNext()) { 84 size_t comment_pos = pair.find(comment_delim, eq_pos + 1);
79 value = keyvalue.token(); 85 if (comment_pos != std::string::npos)
80 if (keyvalue.GetNext()) { 86 value_size = comment_pos - eq_pos - 1;
81 LOG(WARNING) << "Multiple key tokens: '" << pair << "'. Aborting."; 87 }
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 88
92 TrimString(value, kTrimChars, &value); 89 std::string key;
90 std::string value;
91 TrimString(pair.substr(0, eq_pos), kTrimChars, &key);
92 TrimString(pair.substr(eq_pos + 1, value_size), kTrimChars, &value);
93
94 if (!key.empty()) {
95 AddNameValuePair(key, value);
96 continue;
93 } 97 }
94 } 98 }
95 if (key.empty()) { 99
96 LOG(WARNING) << "Invalid token pair: '" << pair << "'. Aborting."; 100 LOG(WARNING) << "Invalid token pair: " << pair << ". Ignoring.";
97 return false; 101 all_valid = false;
98 }
99 AddNameValuePair(key, value);
100 } 102 }
101 return true; 103 return all_valid;
102 } 104 }
103 105
104 bool NameValuePairsParser::GetSingleValueFromTool(int argc, 106 bool NameValuePairsParser::GetSingleValueFromTool(int argc,
105 const char* argv[], 107 const char* argv[],
106 const std::string& key) { 108 const std::string& key) {
107 std::string output_string; 109 std::string output_string;
108 if (!GetToolOutput(argc, argv, output_string)) 110 if (!GetToolOutput(argc, argv, output_string))
109 return false; 111 return false;
110 112
111 TrimWhitespaceASCII(output_string, TRIM_ALL, &output_string); 113 TrimWhitespaceASCII(output_string, TRIM_ALL, &output_string);
112 AddNameValuePair(key, output_string); 114 AddNameValuePair(key, output_string);
113 return true; 115 return true;
114 } 116 }
115 117
116 void NameValuePairsParser::GetNameValuePairsFromFile(const FilePath& file_path, 118 bool NameValuePairsParser::GetNameValuePairsFromFile(const FilePath& file_path,
117 const std::string& eq, 119 const std::string& eq,
118 const std::string& delim) { 120 const std::string& delim) {
119 std::string contents; 121 std::string contents;
120 if (file_util::ReadFileToString(file_path, &contents)) { 122 if (file_util::ReadFileToString(file_path, &contents)) {
121 ParseNameValuePairs(contents, eq, delim); 123 return ParseNameValuePairs(contents, eq, delim);
122 } else { 124 } else {
123 LOG(WARNING) << "Unable to read statistics file: " << file_path.value(); 125 LOG(WARNING) << "Unable to read statistics file: " << file_path.value();
126 return false;
124 } 127 }
125 } 128 }
126 129
127 bool NameValuePairsParser::ParseNameValuePairsFromTool( 130 bool NameValuePairsParser::ParseNameValuePairsFromTool(
128 int argc, 131 int argc,
129 const char* argv[], 132 const char* argv[],
130 const std::string& eq, 133 const std::string& eq,
131 const std::string& delim, 134 const std::string& delim,
132 const std::string& comment_delim) { 135 const std::string& comment_delim) {
133 std::string output_string; 136 std::string output_string;
134 if (!GetToolOutput(argc, argv, output_string)) 137 if (!GetToolOutput(argc, argv, output_string))
135 return false; 138 return false;
136 139
137 return ParseNameValuePairsWithComments( 140 return ParseNameValuePairsWithComments(
138 output_string, eq, delim, comment_delim); 141 output_string, eq, delim, comment_delim);
139 } 142 }
140 143
141 } // namespace system 144 } // namespace system
142 } // namespace chromeos 145 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698