diff options
Diffstat (limited to 'ext/dsent/libutil/Config.cc')
-rw-r--r-- | ext/dsent/libutil/Config.cc | 94 |
1 files changed, 34 insertions, 60 deletions
diff --git a/ext/dsent/libutil/Config.cc b/ext/dsent/libutil/Config.cc index f858c6926..c0b4f5bda 100644 --- a/ext/dsent/libutil/Config.cc +++ b/ext/dsent/libutil/Config.cc @@ -1,69 +1,44 @@ -#include "Config.h" +/* Copyright (c) 2012 Massachusetts Institute of Technology + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ #include <fstream> #include "Assert.h" +#include "Config.h" + +using namespace std; namespace LibUtil { - Config::Config(const String& delimiter_, const String& comment_, const String& sentry_) - : mDelimiter(delimiter_), mComment(comment_), mSentry(sentry_) - {} - - Config::Config(const Config& config_) - : StringMap(config_) - { - mDelimiter = config_.mDelimiter; - mComment = config_.mComment; - mSentry = config_.mSentry; - } - - Config::~Config() - {} - - Config* Config::clone() const - { - return new Config(*this); - } - - void Config::readFile(const String& filename_) + void readFile(const char *filename_, map<String, String> &config) { - std::ifstream fin(filename_.c_str()); + std::ifstream ist_(filename_); - ASSERT(fin, "File not found: " + filename_); - fin >> (*this); - return; - } - - void Config::readString(const String& str_) - { - String newString = str_; - newString.substitute(";", "\n"); - std::istringstream iss(newString, std::istringstream::in); - - iss >> (*this); - } - - std::ostream& operator<<(std::ostream& ost_, const Config& config_) - { - Config::ConstIterator it; - for(it = config_.begin(); it != config_.end(); it++) - { - ost_ << it->first << " " << config_.mDelimiter << " "; - ost_ << it->second << std::endl; - } - return ost_; - } - - std::istream& operator>>(std::istream& ist_, Config& config_) - { // Set a Config from ist_ // Read in keys and values, keeping internal whitespace typedef String::size_type pos; - const String& delim = config_.mDelimiter; // separator - const String& comm = config_.mComment; // comment - const String& sentry = config_.mSentry; // end of file sentry - const pos skip = delim.length(); // length of separator + const String& delimiter = "="; + const String& comment = "#"; + const String& sentry = "End"; + const pos skip = delimiter.length(); // length of separator String nextline = ""; // might need to read ahead to see where value ends @@ -83,17 +58,17 @@ namespace LibUtil } // Ignore comments and the spaces on both ends - line = line.substr(0, line.find(comm)); + line = line.substr(0, line.find(comment)); line.trim(); // Check for end of file sentry - if((sentry != "") && (line.find(sentry) != String::npos)) return ist_; + if((sentry != "") && (line.find(sentry) != String::npos)) return; if(line.length() == 0) continue; // Parse the line if it contains a delimiter - pos delimPos = line.find(delim); + pos delimPos = line.find(delimiter); ASSERT((delimPos < String::npos), "Invalid config line: '" + line + "'"); // Extract the key @@ -119,7 +94,7 @@ namespace LibUtil nlcopy.trim(); if(nlcopy == "") continue; - nextline = nextline.substr(0, nextline.find(comm)); + nextline = nextline.substr(0, nextline.find(comment)); //if(nextline.find(delim) != String::npos) // continue; if((sentry != "") && (nextline.find(sentry) != String::npos)) @@ -136,9 +111,8 @@ namespace LibUtil // Store key and value key.trim(); line.trim(); - config_.set(key, line); // overwrites if key is repeated + config[key] = line; // overwrites if key is repeated } - return ist_; } } |