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