Chromium Code Reviews| Index: chrome/browser/chromeos/name_value_pairs_parser.cc |
| diff --git a/chrome/browser/chromeos/name_value_pairs_parser.cc b/chrome/browser/chromeos/name_value_pairs_parser.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..33519e7c5e041a3df45f45b7ef09ae777f37362e |
| --- /dev/null |
| +++ b/chrome/browser/chromeos/name_value_pairs_parser.cc |
| @@ -0,0 +1,103 @@ |
| +// Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "chrome/browser/chromeos/name_value_pairs_parser.h" |
| + |
| +#include "base/command_line.h" |
| +#include "base/logging.h" |
| +#include "base/process_util.h" |
| +#include "base/string_tokenizer.h" |
| +#include "base/string_util.h" |
| +#include "base/threading/thread_restrictions.h" |
| + |
| +namespace chromeos { // NOLINT |
| + |
| +namespace { |
| + |
| +const char kQuotaChars[] = "\""; |
|
stevenjb
2011/04/25 17:07:33
s/kQuotaChars/kQuoteChars
Dmitry Polukhin
2011/04/25 18:50:00
Done.
|
| + |
| +} // namespace |
| + |
| +NameValuePairsParser::NameValuePairsParser(MachineInfo* machine_info) |
| + : machine_info_(machine_info) { |
| +} |
| + |
| +void NameValuePairsParser::AddNameValuePair(const std::string& key, |
| + const std::string& value) { |
| + (*machine_info_)[key] = value; |
| + VLOG(1) << "name: " << key << ", value: " << value; |
| +} |
| + |
| +bool NameValuePairsParser::ParseNameValuePairs(const std::string& in_string, |
| + const std::string& eq, |
| + const std::string& delim) { |
| + // Set up the pair tokenizer. |
| + StringTokenizer pair_toks(in_string, delim); |
| + pair_toks.set_quote_chars(kQuotaChars); |
| + // 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,value; |
| + if (keyvalue.GetNext()) { |
| + TrimString(keyvalue.token(), kQuotaChars, &key); |
| + if (keyvalue.GetNext()) { |
| + TrimString(keyvalue.token(), kQuotaChars, &value); |
| + if (keyvalue.GetNext()) { |
| + LOG(WARNING) << "Multiple key tokens: '" << pair << "'. Aborting."; |
| + return false; |
| + } |
| + } |
| + } |
| + if (key.empty()) { |
| + LOG(WARNING) << "Invalid token pair: '" << pair << "'. Aborting."; |
| + return false; |
| + } |
| + AddNameValuePair(key, value); |
| + } |
| + return true; |
| +} |
| + |
| +bool NameValuePairsParser::GetSingleValueFromTool(int argc, |
| + const char* argv[], |
| + const std::string& key) { |
| + CommandLine command_line(argc, argv); |
| + std::string output_string; |
| + // TODO(stevenjb,satorux): Make this non blocking: crosbug.com/5603. |
| + base::ThreadRestrictions::ScopedAllowIO allow_io_for_thread_join; |
| + if (argc < 1 || !base::GetAppOutput(command_line, &output_string)) { |
| + LOG(WARNING) << "Error excuting: " << command_line.command_line_string(); |
| + return false; |
| + } |
| + TrimWhitespaceASCII(output_string, TRIM_ALL, &output_string); |
| + AddNameValuePair(key, output_string); |
| + return true; |
| +} |
| + |
| +bool NameValuePairsParser::ParseNameValuePairsFromTool( |
| + int argc, |
| + const char* argv[], |
| + const std::string& eq, |
| + const std::string& delim) { |
| + CommandLine command_line(argc, argv); |
| + std::string output_string; |
| + // TODO(stevenjb,satorux): Make this non blocking: crosbug.com/5603. |
| + base::ThreadRestrictions::ScopedAllowIO allow_io_for_thread_join; |
| + if (argc < 1 || !base::GetAppOutput(command_line, &output_string)) { |
| + LOG(WARNING) << "Error excuting: " << command_line.command_line_string(); |
| + return false; |
| + } |
| + if (!ParseNameValuePairs(output_string, eq, delim)) { |
| + LOG(WARNING) << "Error parsing values while excuting: " |
| + << command_line.command_line_string(); |
| + return false; |
| + } |
| + return true; |
| +} |
| + |
| +} // namespace chromeos |