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

Unified 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: 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 side-by-side diff with in-line comments
Download patch
Index: chrome/browser/chromeos/system/name_value_pairs_parser.cc
diff --git a/chrome/browser/chromeos/system/name_value_pairs_parser.cc b/chrome/browser/chromeos/system/name_value_pairs_parser.cc
index 45ea536f7f8216cba3f732f1be70cedaccbba3b0..cef796bb62e151e610904727ce76da9736d8e0e9 100644
--- a/chrome/browser/chromeos/system/name_value_pairs_parser.cc
+++ b/chrome/browser/chromeos/system/name_value_pairs_parser.cc
@@ -18,7 +18,7 @@ namespace system {
namespace {
const char kQuoteChars[] = "\"";
-const char kTrimChars[] = "\" ";
+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
bool GetToolOutput(int argc, const char* argv[], std::string& output) {
DCHECK_GE(argc, 1);
@@ -60,45 +60,42 @@ bool NameValuePairsParser::ParseNameValuePairsWithComments(
const std::string& eq,
const std::string& delim,
const std::string& comment_delim) {
+ bool all_valid = true;
// Set up the pair tokenizer.
StringTokenizer pair_toks(in_string, delim);
pair_toks.set_quote_chars(kQuoteChars);
// Process token pairs.
while (pair_toks.GetNext()) {
std::string pair(pair_toks.token());
- if (pair.find(eq) == 0) {
- LOG(WARNING) << "Empty key: '" << pair << "'. Aborting.";
- return false;
- }
- StringTokenizer keyvalue(pair, eq);
- std::string key;
- std::string value;
- if (keyvalue.GetNext()) {
- TrimString(keyvalue.token(), kTrimChars, &key);
- if (keyvalue.GetNext()) {
- value = keyvalue.token();
- if (keyvalue.GetNext()) {
- LOG(WARNING) << "Multiple key tokens: '" << pair << "'. Aborting.";
- return false;
- }
- // If value ends with a comment, throw away everything after
- // comment_delim is encountered.
- if (!comment_delim.empty()) {
- StringTokenizer value_with_comment(value, comment_delim);
- value_with_comment.GetNext();
- value = value_with_comment.token();
- }
-
- TrimString(value, kTrimChars, &value);
+ // Anything before the first |eq| is the key, anything after is the value.
+ // |eq| must exist.
+ size_t eq_pos = pair.find(eq);
+ if (eq_pos != std::string::npos) {
+ // First |comment_delim| after |eq_pos| starts the comment.
+ // A value of |std::string::npos| means that the value spans to the end
+ // of |pair|.
+ size_t value_size = std::string::npos;
+ if (!comment_delim.empty()) {
+ size_t comment_pos = pair.find(comment_delim, eq_pos + 1);
+ if (comment_pos != std::string::npos)
+ value_size = comment_pos - eq_pos - 1;
+ }
+
+ std::string key;
+ std::string value;
+ TrimString(pair.substr(0, eq_pos), kTrimChars, &key);
+ TrimString(pair.substr(eq_pos + 1, value_size), kTrimChars, &value);
+
+ if (!key.empty()) {
+ AddNameValuePair(key, value);
+ continue;
}
}
- if (key.empty()) {
- LOG(WARNING) << "Invalid token pair: '" << pair << "'. Aborting.";
- return false;
- }
- AddNameValuePair(key, value);
+
+ LOG(WARNING) << "Invalid token pair: " << pair << ". Ignoring.";
+ all_valid = false;
}
- return true;
+ return all_valid;
}
bool NameValuePairsParser::GetSingleValueFromTool(int argc,
@@ -113,14 +110,15 @@ bool NameValuePairsParser::GetSingleValueFromTool(int argc,
return true;
}
-void NameValuePairsParser::GetNameValuePairsFromFile(const FilePath& file_path,
+bool NameValuePairsParser::GetNameValuePairsFromFile(const FilePath& file_path,
const std::string& eq,
const std::string& delim) {
std::string contents;
if (file_util::ReadFileToString(file_path, &contents)) {
- ParseNameValuePairs(contents, eq, delim);
+ return ParseNameValuePairs(contents, eq, delim);
} else {
LOG(WARNING) << "Unable to read statistics file: " << file_path.value();
+ return false;
}
}

Powered by Google App Engine
This is Rietveld 408576698