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