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. 4 5#ifndef CHROME_COMMON_INI_PARSER_H_ 6#define CHROME_COMMON_INI_PARSER_H_ 7 8#include <string> 9 10#include "base/basictypes.h" 11#include "base/values.h" 12 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|. 28class INIParser { 29 public: 30 INIParser(); 31 virtual ~INIParser(); 32 33 // May only be called once per instance. 34 void Parse(const std::string& content); 35 36 private: 37 virtual void HandleTriplet(const std::string& section, 38 const std::string& key, 39 const std::string& value) = 0; 40 41 bool used_; 42}; 43 44// Parsed values are stored as strings at the "section.key" path. Triplets with 45// |section| or |key| parameters containing '.' are ignored. 46class DictionaryValueINIParser : public INIParser { 47 public: 48 DictionaryValueINIParser(); 49 virtual ~DictionaryValueINIParser(); 50 51 const base::DictionaryValue& root() const { return root_; } 52 53 private: 54 // INIParser implementation. 55 virtual void HandleTriplet(const std::string& section, 56 const std::string& key, 57 const std::string& value) OVERRIDE; 58 59 base::DictionaryValue root_; 60 61 DISALLOW_COPY_AND_ASSIGN(DictionaryValueINIParser); 62}; 63 64#endif // CHROME_COMMON_INI_PARSER_H_ 65