|
| 1 | +// Copyright (c) 2016 Zeex |
| 2 | +// All rights reserved. |
| 3 | +// |
| 4 | +// Redistribution and use in source and binary forms, with or without |
| 5 | +// modification, are permitted provided that the following conditions are met: |
| 6 | +// |
| 7 | +// 1. Redistributions of source code must retain the above copyright notice, |
| 8 | +// this list of conditions and the following disclaimer. |
| 9 | +// 2. Redistributions in binary form must reproduce the above copyright notice, |
| 10 | +// this list of conditions and the following disclaimer in the documentation |
| 11 | +// and/or other materials provided with the distribution. |
| 12 | +// |
| 13 | +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 14 | +// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 15 | +// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 16 | +// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
| 17 | +// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 18 | +// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 19 | +// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 20 | +// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 21 | +// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 22 | +// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 23 | +// POSSIBILITY OF SUCH DAMAGE. |
| 24 | + |
| 25 | +#include <algorithm> |
| 26 | +#include <cctype> |
| 27 | +#include <cstdlib> |
| 28 | +#include <fstream> |
| 29 | +#include <functional> |
| 30 | +#include <iostream> |
| 31 | +#include <sstream> |
| 32 | +#include <string> |
| 33 | + |
| 34 | +#include "configreader.h" |
| 35 | + |
| 36 | +namespace { |
| 37 | + |
| 38 | +struct is_space : public std::unary_function<char, bool> { |
| 39 | + bool operator()(char c) const { |
| 40 | + return std::isspace(c) != 0; |
| 41 | + } |
| 42 | +}; |
| 43 | + |
| 44 | +inline std::string &TrimStringLeft(std::string &s) { |
| 45 | + s.erase(s.begin(), |
| 46 | + std::find_if(s.begin(), s.end(), std::not1(is_space()))); |
| 47 | + return s; |
| 48 | +} |
| 49 | + |
| 50 | +inline std::string &TrimStringRight(std::string &s) { |
| 51 | + s.erase(std::find_if(s.rbegin(), s.rend(), std::not1(is_space())).base(), |
| 52 | + s.end()); |
| 53 | + return s; |
| 54 | +} |
| 55 | + |
| 56 | +inline std::string &TrimString(std::string &s) { |
| 57 | + return TrimStringLeft(TrimStringRight(s)); |
| 58 | +} |
| 59 | + |
| 60 | +} // anonymous namespace |
| 61 | + |
| 62 | +ConfigReader::ConfigReader() { |
| 63 | +} |
| 64 | + |
| 65 | +ConfigReader::ConfigReader(const std::string &filename) { |
| 66 | + ReadFromFile(filename); |
| 67 | +} |
| 68 | + |
| 69 | +ConfigReader::ConfigReader(std::istream &stream) { |
| 70 | + ReadFromStream(stream); |
| 71 | +} |
| 72 | + |
| 73 | +bool ConfigReader::ReadFromFile(const std::string &filename) { |
| 74 | + std::ifstream stream(filename.c_str()); |
| 75 | + |
| 76 | + if (!stream.is_open()) { |
| 77 | + return false; |
| 78 | + } |
| 79 | + |
| 80 | + ReadFromStream(stream); |
| 81 | + return true; |
| 82 | +} |
| 83 | + |
| 84 | +void ConfigReader::ReadFromString(const std::string &config) { |
| 85 | + std::istringstream stream(config); |
| 86 | + ReadFromStream(stream); |
| 87 | +} |
| 88 | + |
| 89 | +void ConfigReader::ReadFromStream(std::istream &stream) { |
| 90 | + std::string line; |
| 91 | + |
| 92 | + while (std::getline(stream, line)) { |
| 93 | + TrimString(line); |
| 94 | + |
| 95 | + std::string::iterator delimIter = |
| 96 | + std::find_if(line.begin(), line.end(), is_space()); |
| 97 | + if (delimIter == line.end()) { |
| 98 | + continue; |
| 99 | + } |
| 100 | + |
| 101 | + std::string::iterator valueIter = |
| 102 | + std::find_if(delimIter, line.end(), std::not1(is_space())); |
| 103 | + if (valueIter == line.end()) { |
| 104 | + continue; |
| 105 | + } |
| 106 | + |
| 107 | + std::string name = std::string(line.begin(), delimIter); |
| 108 | + std::string value = std::string(valueIter, line.end()); |
| 109 | + |
| 110 | + options_.insert(std::make_pair(name, value)); |
| 111 | + } |
| 112 | +} |
| 113 | + |
| 114 | +void ConfigReader::GetValue(const std::string &name, |
| 115 | + std::string &value) const { |
| 116 | + value = GetValueWithDefault(name, value); |
| 117 | +} |
| 118 | + |
| 119 | +std::string ConfigReader::GetValueWithDefault( |
| 120 | + const std::string &name, |
| 121 | + const std::string &defaultValue) const |
| 122 | +{ |
| 123 | + option_map::const_iterator iterator = options_.find(name); |
| 124 | + if (iterator != options_.end()) { |
| 125 | + return iterator->second; |
| 126 | + } |
| 127 | + return defaultValue; |
| 128 | +} |
0 commit comments