OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 BASE_INI_PARSER_H_ | 5 #ifndef CHROME_COMMON_INI_PARSER_H_ |
6 #define BASE_INI_PARSER_H_ | 6 #define CHROME_COMMON_INI_PARSER_H_ |
7 | 7 |
8 #include <string> | 8 #include <string> |
9 | 9 |
10 #include "base/base_export.h" | |
11 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
12 #include "base/values.h" | 11 #include "base/values.h" |
13 | 12 |
14 namespace base { | |
15 | |
16 // Parses INI files in a string. Users should in inherit from this class. | 13 // Parses INI files in a string. Users should in inherit from this class. |
17 // This is a very basic INI parser with these characteristics: | 14 // This is a very basic INI parser with these characteristics: |
18 // - Ignores blank lines. | 15 // - Ignores blank lines. |
19 // - Ignores comment lines beginning with '#' or ';'. | 16 // - Ignores comment lines beginning with '#' or ';'. |
20 // - Duplicate key names in the same section will simply cause repeated calls | 17 // - Duplicate key names in the same section will simply cause repeated calls |
21 // to HandleTriplet with the same |section| and |key| parameters. | 18 // to HandleTriplet with the same |section| and |key| parameters. |
22 // - No escape characters supported. | 19 // - No escape characters supported. |
23 // - Global properties result in calls to HandleTriplet with an empty string in | 20 // - Global properties result in calls to HandleTriplet with an empty string in |
24 // the |section| argument. | 21 // the |section| argument. |
25 // - Section headers begin with a '[' character. It is recommended, but | 22 // - Section headers begin with a '[' character. It is recommended, but |
26 // not required to close the header bracket with a ']' character. All | 23 // not required to close the header bracket with a ']' character. All |
27 // characters after a closing ']' character is ignored. | 24 // characters after a closing ']' character is ignored. |
28 // - Key value pairs are indicated with an '=' character. Whitespace is not | 25 // - Key value pairs are indicated with an '=' character. Whitespace is not |
29 // ignored. Quoting is not supported. Everything before the first '=' | 26 // ignored. Quoting is not supported. Everything before the first '=' |
30 // is considered the |key|, and everything after is the |value|. | 27 // is considered the |key|, and everything after is the |value|. |
31 class BASE_EXPORT INIParser { | 28 class INIParser { |
32 public: | 29 public: |
33 INIParser(); | 30 INIParser(); |
34 virtual ~INIParser(); | 31 virtual ~INIParser(); |
35 | 32 |
36 // May only be called once per instance. | 33 // May only be called once per instance. |
37 void Parse(const std::string& content); | 34 void Parse(const std::string& content); |
38 | 35 |
39 private: | 36 private: |
40 virtual void HandleTriplet(const std::string& section, | 37 virtual void HandleTriplet(const std::string& section, |
41 const std::string& key, | 38 const std::string& key, |
42 const std::string& value) = 0; | 39 const std::string& value) = 0; |
43 | 40 |
44 bool used_; | 41 bool used_; |
45 }; | 42 }; |
46 | 43 |
47 // Parsed values are stored as strings at the "section.key" path. Triplets with | 44 // Parsed values are stored as strings at the "section.key" path. Triplets with |
48 // |section| or |key| parameters containing '.' are ignored. | 45 // |section| or |key| parameters containing '.' are ignored. |
49 class BASE_EXPORT DictionaryValueINIParser : public INIParser { | 46 class DictionaryValueINIParser : public INIParser { |
50 public: | 47 public: |
51 DictionaryValueINIParser(); | 48 DictionaryValueINIParser(); |
52 virtual ~DictionaryValueINIParser(); | 49 virtual ~DictionaryValueINIParser(); |
53 | 50 |
54 const DictionaryValue& root() const { return root_; } | 51 const base::DictionaryValue& root() const { return root_; } |
55 | 52 |
56 private: | 53 private: |
57 // INIParser implementation. | 54 // INIParser implementation. |
58 virtual void HandleTriplet(const std::string& section, | 55 virtual void HandleTriplet(const std::string& section, |
59 const std::string& key, | 56 const std::string& key, |
60 const std::string& value) OVERRIDE; | 57 const std::string& value) OVERRIDE; |
61 | 58 |
62 DictionaryValue root_; | 59 base::DictionaryValue root_; |
63 | 60 |
64 DISALLOW_COPY_AND_ASSIGN(DictionaryValueINIParser); | 61 DISALLOW_COPY_AND_ASSIGN(DictionaryValueINIParser); |
65 }; | 62 }; |
66 | 63 |
67 } // namespace base | 64 #endif // CHROME_COMMON_INI_PARSER_H_ |
68 | |
69 #endif // BASE_INI_PARSER_H_ | |
OLD | NEW |