1 // Copyright 2014 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 CHROME_COMMON_INI_PARSER_H_
6 #define CHROME_COMMON_INI_PARSER_H_
10 #include "base/basictypes.h"
11 #include "base/values.h"
13 // Parses INI files in a string. Users should in inherit from this class.
14 // This is a very basic INI parser with these characteristics:
15 // - Ignores blank lines.
16 // - Ignores comment lines beginning with '#' or ';'.
17 // - Duplicate key names in the same section will simply cause repeated calls
18 // to HandleTriplet with the same |section| and |key| parameters.
19 // - No escape characters supported.
20 // - Global properties result in calls to HandleTriplet with an empty string in
21 // the |section| argument.
22 // - Section headers begin with a '[' character. It is recommended, but
23 // not required to close the header bracket with a ']' character. All
24 // characters after a closing ']' character is ignored.
25 // - Key value pairs are indicated with an '=' character. Whitespace is not
26 // ignored. Quoting is not supported. Everything before the first '='
27 // is considered the |key|, and everything after is the |value|.
33 // May only be called once per instance.
34 void Parse(const std::string
& content
);
37 virtual void HandleTriplet(const std::string
& section
,
38 const std::string
& key
,
39 const std::string
& value
) = 0;
44 // Parsed values are stored as strings at the "section.key" path. Triplets with
45 // |section| or |key| parameters containing '.' are ignored.
46 class DictionaryValueINIParser
: public INIParser
{
48 DictionaryValueINIParser();
49 ~DictionaryValueINIParser() override
;
51 const base::DictionaryValue
& root() const { return root_
; }
54 // INIParser implementation.
55 void HandleTriplet(const std::string
& section
,
56 const std::string
& key
,
57 const std::string
& value
) override
;
59 base::DictionaryValue root_
;
61 DISALLOW_COPY_AND_ASSIGN(DictionaryValueINIParser
);
64 #endif // CHROME_COMMON_INI_PARSER_H_