www.pudn.com > mputil.rar > configuration.cpp
/* * configuration.cpp * * Copyright (c) 2003 Machine Perception Laboratory * University of California San Diego. * Please read the disclaimer and notes about redistribution * at the end of this file. * * Authors: Josh Susskind */ #ifdef WIN32 #pragma warning(disable:4786) #endif #include "configuration.h" #include "../common.h" #include#include map *MPConfiguration::m_varsPtr; // ================================================================ MPConfiguration::~MPConfiguration() { if (!m_infile.is_open()) m_infile.close(); } // ================================================================ void MPConfiguration::record_int (const string &key, const string &val) { m_vars.insert ( make_pair ( key, val ) ); m_varsPtr = &m_vars; } // ================================================================ void MPConfiguration::record_double (const string &key, const string &val) { m_vars.insert ( make_pair ( key, val ) ); m_varsPtr = &m_vars; } // ================================================================ void MPConfiguration::record_string (const string &key, const string &val) { m_vars.insert( make_pair ( key, val ) ); m_varsPtr = &m_vars; } // ================================================================ void MPConfiguration::record (const string &key, int &addr) { char buffer[255]; sprintf(buffer,"%i",addr); record_int(key, buffer); m_intvars.insert( make_pair ( key, &addr ) ); } // ================================================================ void MPConfiguration::record (const string &key, double &addr) { char buffer[50]; gcvt( addr, 7, buffer ); string s = buffer; record_double(key, s); m_doublevars.insert( make_pair ( key, &addr ) ); } // ================================================================ void MPConfiguration::record (const string &key, string &addr) { record_string(key, addr); m_stringvars.insert( make_pair ( key, &addr ) ); } // ================================================================ int MPConfiguration::load(const char *file) { m_infile.open(file); if (!m_infile.is_open()) return -1; while(!m_infile.eof()) { char buf[255]; string line; string key; string value; string::size_type index; m_infile.getline(buf, 255); if (buf[0] == '#') continue; line = buf; index = line.find("="); if ((int)index == -1) continue; key = line.substr(0, index); value = line.substr(index+1); remove_whites(key); remove_comments(value); remove_whites(value); if (key == "" || value == "") continue; record_int (key, value); m_vars[key] = value; if (find(key.c_str())) (*m_varsPtr)[key] = value; if (m_intvars.find(key) != m_intvars.end()) { int *v = m_intvars[key]; *v = atoi(value.c_str()); } if (m_doublevars.find(key) != m_doublevars.end()) { double *v = m_doublevars[key]; *v = atof(value.c_str()); } if (m_stringvars.find(key) != m_stringvars.end()) { string *v = m_stringvars[key]; *v = value; } } m_infile.close(); return 1; } // ================================================================ int MPConfiguration::find (const char *key) { map ::const_iterator mi; mi = (*m_varsPtr).find(key); return (mi != (*m_varsPtr).end()); } // ================================================================ int MPConfiguration::intval (const char *key) { return(atoi((*m_varsPtr)[key].c_str())); } // ================================================================ double MPConfiguration::doubleval (const char *key) { return atof((*m_varsPtr)[key].c_str()); } // ================================================================ string MPConfiguration::stringval (const char *key) { return (*m_varsPtr)[key]; } // ================================================================ void MPConfiguration::print_values (std::ostream &ostr) { for (map ::iterator it = (*m_varsPtr).begin(); it != (*m_varsPtr).end(); ++it) ostr << it->first << "=" << it->second << endl; } // ================================================================ void MPConfiguration::remove_comments (string &str) { if (str.empty()) return; int pos = str.find('#', 0); if (pos > 0) str.erase(pos, str.length()-pos); } // ================================================================ void MPConfiguration::remove_headwhite (string &str) { if (str.empty()) return; for (string::iterator s = str.begin(); s != str.end(); ++s) { if (!isspace(*s)) break; str = s+1; } } // ================================================================ void MPConfiguration::remove_tailwhite (string &str) { if (str.empty()) return; for (string::iterator s = str.end()-1; s != str.begin(); --s) { if (!isspace(*s)) break; str = str.substr(0, str.length()-1); } } // ================================================================ void MPConfiguration::remove_whites (string &str) { remove_tailwhite(str); remove_headwhite(str); } // ================================================================ /* * * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * 3. The name of the author may not be used to endorse or promote products derived from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * */