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

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: Addressed comments 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()) {
49 VLOG(1) << "name: " << key << ", value: " << value; 49 (*map_)[key] = value;
50 VLOG(1) << "name: " << key << ", value: " << value;
51 }
Dmitry Polukhin 2012/04/23 08:34:48 Should we report warning if given key already exis
Joao da Silva 2012/04/23 09:13:18 Yes, done. This is not expected to happen, but we
50 } 52 }
51 53
52 bool NameValuePairsParser::ParseNameValuePairs(const std::string& in_string, 54 bool NameValuePairsParser::ParseNameValuePairs(const std::string& in_string,
53 const std::string& eq, 55 const std::string& eq,
54 const std::string& delim) { 56 const std::string& delim) {
55 return ParseNameValuePairsWithComments(in_string, eq, delim, ""); 57 return ParseNameValuePairsWithComments(in_string, eq, delim, "");
56 } 58 }
57 59
58 bool NameValuePairsParser::ParseNameValuePairsWithComments( 60 bool NameValuePairsParser::ParseNameValuePairsWithComments(
59 const std::string& in_string, 61 const std::string& in_string,
60 const std::string& eq, 62 const std::string& eq,
61 const std::string& delim, 63 const std::string& delim,
62 const std::string& comment_delim) { 64 const std::string& comment_delim) {
65 bool all_valid = true;
63 // Set up the pair tokenizer. 66 // Set up the pair tokenizer.
64 StringTokenizer pair_toks(in_string, delim); 67 StringTokenizer pair_toks(in_string, delim);
65 pair_toks.set_quote_chars(kQuoteChars); 68 pair_toks.set_quote_chars(kQuoteChars);
66 // Process token pairs. 69 // Process token pairs.
67 while (pair_toks.GetNext()) { 70 while (pair_toks.GetNext()) {
68 std::string pair(pair_toks.token()); 71 std::string pair(pair_toks.token());
69 if (pair.find(eq) == 0) { 72 // Anything before the first |eq| is the key, anything after is the value.
70 LOG(WARNING) << "Empty key: '" << pair << "'. Aborting."; 73 // |eq| must exist.
71 return false; 74 size_t eq_pos = pair.find(eq);
72 } 75 if (eq_pos != std::string::npos) {
73 StringTokenizer keyvalue(pair, eq); 76 // First |comment_delim| after |eq_pos| starts the comment.
74 std::string key; 77 // A value of |std::string::npos| means that the value spans to the end
75 std::string value; 78 // of |pair|.
76 if (keyvalue.GetNext()) { 79 size_t value_size = std::string::npos;
77 TrimString(keyvalue.token(), kTrimChars, &key); 80 if (!comment_delim.empty()) {
78 if (keyvalue.GetNext()) { 81 size_t comment_pos = pair.find(comment_delim, eq_pos + 1);
79 value = keyvalue.token(); 82 if (comment_pos != std::string::npos)
80 if (keyvalue.GetNext()) { 83 value_size = comment_pos - eq_pos - 1;
81 LOG(WARNING) << "Multiple key tokens: '" << pair << "'. Aborting."; 84 }
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 85
92 TrimString(value, kTrimChars, &value); 86 std::string key;
87 std::string value;
88 TrimString(pair.substr(0, eq_pos), kTrimChars, &key);
89 TrimString(pair.substr(eq_pos + 1, value_size), kTrimChars, &value);
90
91 if (!key.empty()) {
92 AddNameValuePair(key, value);
93 continue;
93 } 94 }
94 } 95 }
95 if (key.empty()) { 96
96 LOG(WARNING) << "Invalid token pair: '" << pair << "'. Aborting."; 97 LOG(WARNING) << "Invalid token pair: " << pair << ". Ignoring.";
97 return false; 98 all_valid = false;
98 }
99 AddNameValuePair(key, value);
100 } 99 }
101 return true; 100 return all_valid;
102 } 101 }
103 102
104 bool NameValuePairsParser::GetSingleValueFromTool(int argc, 103 bool NameValuePairsParser::GetSingleValueFromTool(int argc,
105 const char* argv[], 104 const char* argv[],
106 const std::string& key) { 105 const std::string& key) {
107 std::string output_string; 106 std::string output_string;
108 if (!GetToolOutput(argc, argv, output_string)) 107 if (!GetToolOutput(argc, argv, output_string))
109 return false; 108 return false;
110 109
111 TrimWhitespaceASCII(output_string, TRIM_ALL, &output_string); 110 TrimWhitespaceASCII(output_string, TRIM_ALL, &output_string);
112 AddNameValuePair(key, output_string); 111 AddNameValuePair(key, output_string);
113 return true; 112 return true;
114 } 113 }
115 114
116 void NameValuePairsParser::GetNameValuePairsFromFile(const FilePath& file_path, 115 bool NameValuePairsParser::GetNameValuePairsFromFile(const FilePath& file_path,
117 const std::string& eq, 116 const std::string& eq,
118 const std::string& delim) { 117 const std::string& delim) {
119 std::string contents; 118 std::string contents;
120 if (file_util::ReadFileToString(file_path, &contents)) { 119 if (file_util::ReadFileToString(file_path, &contents)) {
121 ParseNameValuePairs(contents, eq, delim); 120 return ParseNameValuePairs(contents, eq, delim);
122 } else { 121 } else {
123 LOG(WARNING) << "Unable to read statistics file: " << file_path.value(); 122 LOG(WARNING) << "Unable to read statistics file: " << file_path.value();
123 return false;
124 } 124 }
125 } 125 }
126 126
127 bool NameValuePairsParser::ParseNameValuePairsFromTool( 127 bool NameValuePairsParser::ParseNameValuePairsFromTool(
128 int argc, 128 int argc,
129 const char* argv[], 129 const char* argv[],
130 const std::string& eq, 130 const std::string& eq,
131 const std::string& delim, 131 const std::string& delim,
132 const std::string& comment_delim) { 132 const std::string& comment_delim) {
133 std::string output_string; 133 std::string output_string;
134 if (!GetToolOutput(argc, argv, output_string)) 134 if (!GetToolOutput(argc, argv, output_string))
135 return false; 135 return false;
136 136
137 return ParseNameValuePairsWithComments( 137 return ParseNameValuePairsWithComments(
138 output_string, eq, delim, comment_delim); 138 output_string, eq, delim, comment_delim);
139 } 139 }
140 140
141 } // namespace system 141 } // namespace system
142 } // namespace chromeos 142 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698