Home | History | Annotate | Download | only in chromeos
      1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #ifndef CHROME_BROWSER_CHROMEOS_NAME_VALUE_PAIRS_PARSER_H_
      6 #define CHROME_BROWSER_CHROMEOS_NAME_VALUE_PAIRS_PARSER_H_
      7 #pragma once
      8 
      9 #include <map>
     10 #include <string>
     11 #include <vector>
     12 
     13 #include "base/basictypes.h"
     14 
     15 namespace chromeos {
     16 
     17 // The parser is used to get machine info as name-value pairs. Defined
     18 // here to be accessable by tests.
     19 class NameValuePairsParser {
     20  public:
     21   typedef std::map<std::string, std::string> NameValueMap;
     22 
     23   // The obtained info will be written into machine_info.
     24   explicit NameValuePairsParser(NameValueMap* map);
     25 
     26   void AddNameValuePair(const std::string& key, const std::string& value);
     27 
     28   // Executes tool and inserts (key, <output>) into map_.
     29   bool GetSingleValueFromTool(int argc, const char* argv[],
     30                               const std::string& key);
     31   // Executes tool, parses the output using ParseNameValuePairs,
     32   // and inserts the results into name_value_pairs_.
     33   bool ParseNameValuePairsFromTool(int argc, const char* argv[],
     34                                    const std::string& eq,
     35                                    const std::string& delim);
     36 
     37  private:
     38   // This will parse strings with output in the format:
     39   // <key><EQ><value><DELIM>[<key><EQ><value>][...]
     40   // e.g. ParseNameValuePairs("key1=value1 key2=value2", "=", " ")
     41   bool ParseNameValuePairs(const std::string& in_string,
     42                            const std::string& eq,
     43                            const std::string& delim);
     44 
     45   NameValueMap* map_;
     46 
     47   DISALLOW_COPY_AND_ASSIGN(NameValuePairsParser);
     48 };
     49 
     50 }  // namespace chromeos
     51 
     52 #endif  // CHROME_BROWSER_CHROMEOS_NAME_VALUE_PAIRS_PARSER_H_
     53