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#include "chrome/common/ini_parser.h" 6 7#include "base/logging.h" 8#include "base/strings/string_tokenizer.h" 9 10INIParser::INIParser() : used_(false) {} 11 12INIParser::~INIParser() {} 13 14void INIParser::Parse(const std::string& content) { 15 DCHECK(!used_); 16 used_ = true; 17 base::StringTokenizer tokenizer(content, "\r\n"); 18 19 std::string current_section; 20 while (tokenizer.GetNext()) { 21 std::string line = tokenizer.token(); 22 if (line.empty()) { 23 // Skips the empty line. 24 continue; 25 } 26 if (line[0] == '#' || line[0] == ';') { 27 // This line is a comment. 28 continue; 29 } 30 if (line[0] == '[') { 31 // It is a section header. 32 current_section = line.substr(1); 33 size_t end = current_section.rfind(']'); 34 if (end != std::string::npos) 35 current_section.erase(end); 36 } else { 37 std::string key, value; 38 size_t equal = line.find('='); 39 if (equal != std::string::npos) { 40 key = line.substr(0, equal); 41 value = line.substr(equal + 1); 42 HandleTriplet(current_section, key, value); 43 } 44 } 45 } 46} 47 48DictionaryValueINIParser::DictionaryValueINIParser() {} 49 50DictionaryValueINIParser::~DictionaryValueINIParser() {} 51 52void DictionaryValueINIParser::HandleTriplet(const std::string& section, 53 const std::string& key, 54 const std::string& value) { 55 56 // Checks whether the section and key contain a '.' character. 57 // Those sections and keys break DictionaryValue's path format when not 58 // using the *WithoutPathExpansion methods. 59 if (section.find('.') == std::string::npos && 60 key.find('.') == std::string::npos) 61 root_.SetString(section + "." + key, value); 62} 63