Home | History | Annotate | Download | only in system
      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
      3 // found in the LICENSE file.
      4 
      5 #ifndef CHROMEOS_SYSTEM_NAME_VALUE_PAIRS_PARSER_H_
      6 #define CHROMEOS_SYSTEM_NAME_VALUE_PAIRS_PARSER_H_
      7 
      8 #include <map>
      9 #include <string>
     10 
     11 #include "base/basictypes.h"
     12 #include "chromeos/chromeos_export.h"
     13 
     14 namespace base {
     15 class FilePath;
     16 }
     17 
     18 namespace chromeos {
     19 namespace system {
     20 
     21 // The parser is used to get machine info as name-value pairs. Defined
     22 // here to be accessible by tests.
     23 class CHROMEOS_EXPORT NameValuePairsParser {
     24  public:
     25   typedef std::map<std::string, std::string> NameValueMap;
     26 
     27   // The obtained info will be written into the given map.
     28   explicit NameValuePairsParser(NameValueMap* map);
     29 
     30   void AddNameValuePair(const std::string& key, const std::string& value);
     31 
     32   // Executes tool and inserts (key, <output>) into map_.
     33   // The program name (argv[0]) should be an absolute path. The function
     34   // checks if the program exists before executing it as some programs
     35   // don't exist on Linux desktop; returns false in that case.
     36   bool GetSingleValueFromTool(int argc, const char* argv[],
     37                               const std::string& key);
     38 
     39   // Parses name-value pairs from the file.
     40   // Returns false if there was any error in the file. Valid pairs will still be
     41   // added to the map.
     42   bool GetNameValuePairsFromFile(const base::FilePath& file_path,
     43                                  const std::string& eq,
     44                                  const std::string& delim);
     45 
     46   // These will parse strings with output in the format:
     47   // <key><EQ><value><DELIM>[<key><EQ><value>][...]
     48   // e.g. ParseNameValuePairs("key1=value1 key2=value2", "=", " ")
     49   // Returns false if there was any error in in_string. Valid pairs will still
     50   // be added to the map.
     51   bool ParseNameValuePairs(const std::string& in_string,
     52                            const std::string& eq,
     53                            const std::string& delim);
     54 
     55   // This version allows for values which end with a comment
     56   // beginning with comment_delim.
     57   // e.g. "key2=value2 # Explanation of value\n"
     58   // Returns false if there was any error in in_string. Valid pairs will still
     59   // be added to the map.
     60   bool ParseNameValuePairsWithComments(const std::string& in_string,
     61                                        const std::string& eq,
     62                                        const std::string& delim,
     63                                        const std::string& comment_delim);
     64 
     65   // Same as ParseNameValuePairsWithComments(), but uses the output of the given
     66   // tool as the input to parse.
     67   bool ParseNameValuePairsFromTool(
     68       int argc,
     69       const char* argv[],
     70       const std::string& eq,
     71       const std::string& delim,
     72       const std::string& comment_delim);
     73 
     74  private:
     75   NameValueMap* map_;
     76 
     77   DISALLOW_COPY_AND_ASSIGN(NameValuePairsParser);
     78 };
     79 
     80 }  // namespace system
     81 }  // namespace chromeos
     82 
     83 #endif  // CHROMEOS_SYSTEM_NAME_VALUE_PAIRS_PARSER_H_
     84