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.
5 #ifndef CHROMEOS_SYSTEM_NAME_VALUE_PAIRS_PARSER_H_
6 #define CHROMEOS_SYSTEM_NAME_VALUE_PAIRS_PARSER_H_
11 #include "base/basictypes.h"
12 #include "chromeos/chromeos_export.h"
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
{
25 typedef std::map
<std::string
, std::string
> NameValueMap
;
27 // The obtained info will be written into the given map.
28 explicit NameValuePairsParser(NameValueMap
* map
);
30 void AddNameValuePair(const std::string
& key
, const std::string
& value
);
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
);
39 // Parses name-value pairs from the file.
40 // Returns false if there was any error in the file. Valid pairs will still be
42 bool GetNameValuePairsFromFile(const base::FilePath
& file_path
,
43 const std::string
& eq
,
44 const std::string
& delim
);
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
);
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
);
65 // Same as ParseNameValuePairsWithComments(), but uses the output of the given
66 // tool as the input to parse.
67 bool ParseNameValuePairsFromTool(
70 const std::string
& eq
,
71 const std::string
& delim
,
72 const std::string
& comment_delim
);
77 DISALLOW_COPY_AND_ASSIGN(NameValuePairsParser
);
81 } // namespace chromeos
83 #endif // CHROMEOS_SYSTEM_NAME_VALUE_PAIRS_PARSER_H_