The Machine Perception Toolbox

[Introduction]- [News]- [Download]- [Screenshots]- [Manual (pdf)]- [Forums]- [API Reference]- [Repository ]

 

Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

configuration.cpp

Go to the documentation of this file.
00001 /*
00002  * configuration.cpp
00003  *
00004  * Copyright (c) 2003 Machine Perception Laboratory
00005  * University of California San Diego.
00006  * Please read the disclaimer and notes about redistribution
00007  * at the end of this file.
00008  *
00009  * Authors: Josh Susskind
00010  */
00011 //#ifdef WIN32
00012 #pragma warning(disable:4786)
00013 //#endif
00014 
00015 #include "configuration.h"
00016 #include "../common.h"
00017 #include <iostream>
00018 
00019 map<string, string> *MPConfiguration::m_varsPtr;
00020 
00021 // ================================================================
00022 
00023 MPConfiguration::~MPConfiguration() {
00024         if (!m_infile.is_open()) 
00025                 m_infile.close();
00026 }
00027 
00028 // ================================================================
00029 
00030 void MPConfiguration::record_int (const string &key, const string &val) {
00031         m_vars.insert ( make_pair ( key, val ) );
00032         m_varsPtr = &m_vars;
00033 }
00034 
00035 // ================================================================
00036 
00037 void MPConfiguration::record_double (const string &key, const string &val) {
00038         m_vars.insert ( make_pair ( key, val ) );
00039         m_varsPtr = &m_vars;
00040 }
00041 
00042 // ================================================================
00043 
00044 void MPConfiguration::record_string (const string &key, const string &val) {
00045         m_vars.insert( make_pair ( key, val ) );
00046         m_varsPtr = &m_vars;
00047 }
00048 
00049 // ================================================================
00050 
00051 void MPConfiguration::record (const string &key, int &addr) {
00052         char buffer[255];
00053         itoa (addr, buffer, 10);
00054         record_int(key, buffer);
00055         m_intvars.insert( make_pair ( key, &addr ) );
00056 }
00057 
00058 // ================================================================
00059 
00060 void MPConfiguration::record (const string &key, double &addr) {
00061 
00062         char buffer[50];
00063         gcvt( addr, 7, buffer );
00064         string s = buffer;
00065         record_double(key, s);
00066         m_doublevars.insert( make_pair ( key, &addr ) );
00067 }
00068 
00069 // ================================================================
00070 
00071 void MPConfiguration::record (const string &key, string &addr) {
00072         record_string(key, addr);
00073         m_stringvars.insert( make_pair ( key, &addr ) );
00074 }
00075 
00076 // ================================================================
00077 
00078 void MPConfiguration::load(const char *file) {
00079         m_infile.open(file);
00080         if (!m_infile.is_open()) return;
00081         while(!m_infile.eof()) {
00082                 char buf[255];
00083                 string line;
00084                 string key;
00085                 string value;
00086                 string::size_type index;
00087 
00088                 m_infile.getline(buf, 255);
00089                 if (buf[0] == '#') continue;
00090                 line = buf;
00091                 
00092                 index = line.find("=");
00093                 if (index == -1) continue;
00094 
00095                 key = line.substr(0, index);
00096                 value = line.substr(index+1);
00097 
00098                 remove_whites(key);
00099                 remove_comments(value);
00100                 remove_whites(value);
00101 
00102                 if (key == "" || value == "") continue;
00103 
00104                 if (find(key.c_str()))
00105                         (*m_varsPtr)[key] = value;
00106 
00107                 if (m_intvars.find(key) != m_intvars.end()) {
00108                         int *v = m_intvars[key];
00109                         *v = atoi(value.c_str());
00110                 }
00111                 
00112                 if (m_doublevars.find(key) != m_doublevars.end()) {
00113                         double *v = m_doublevars[key];
00114                         *v = atof(value.c_str());
00115                 }
00116 
00117                 if (m_stringvars.find(key) != m_stringvars.end()) {
00118                         string *v = m_stringvars[key];
00119                         *v = value;
00120                 }
00121         }
00122 
00123         m_infile.close();
00124 }
00125 
00126 // ================================================================
00127 
00128 int MPConfiguration::find (const char *key) {
00129         map<string, string>::const_iterator mi;
00130         mi = (*m_varsPtr).find(key);
00131         return (mi != (*m_varsPtr).end());
00132 }
00133 
00134 // ================================================================
00135 
00136 int MPConfiguration::intval (const char *key) {
00137         return(atoi((*m_varsPtr)[key].c_str()));
00138 }
00139 
00140 // ================================================================
00141 
00142 double MPConfiguration::doubleval (const char *key) {
00143         return atof((*m_varsPtr)[key].c_str());
00144 }
00145 
00146 // ================================================================
00147 
00148 string MPConfiguration::stringval (const char *key) {
00149         return (*m_varsPtr)[key];
00150 }
00151 
00152 // ================================================================
00153 
00154 void MPConfiguration::print_values (std::ostream &ostr) {
00155 
00156         for (map<string, string>::iterator it = (*m_varsPtr).begin(); 
00157                 it != (*m_varsPtr).end(); ++it)
00158                 ostr << it->first << "=" << it->second << endl;
00159 
00160 }
00161 
00162 // ================================================================
00163 
00164 void MPConfiguration::remove_comments (string &str) {
00165         if (str.empty()) return;
00166         int pos = str.find('#', 0);
00167         if (pos > 0)
00168                 str.erase(pos, str.length()-pos);
00169 }
00170 
00171 // ================================================================
00172 
00173 void MPConfiguration::remove_headwhite (string &str) {
00174         if (str.empty()) return;
00175         for (string::iterator s = str.begin(); s != str.end(); ++s) {
00176                 if (!isspace(*s)) break;
00177                 str = s+1;
00178         }
00179 }
00180 
00181 // ================================================================
00182 
00183 void MPConfiguration::remove_tailwhite (string &str) {
00184         if (str.empty()) return;
00185         for (string::iterator s = str.end()-1; s != str.begin(); --s) {
00186                 if (!isspace(*s)) break;
00187                 str = str.substr(0, str.length()-1);
00188         }
00189 }
00190 
00191 // ================================================================
00192 
00193 void MPConfiguration::remove_whites (string &str) {
00194         remove_tailwhite(str);
00195         remove_headwhite(str);
00196 }
00197 
00198 // ================================================================
00199 
00200 /*
00201  * 
00202  * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
00203  * 
00204  *    1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
00205  *    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.
00206  *    3. The name of the author may not be used to endorse or promote products derived from this software without specific prior written permission.
00207  * 
00208  * 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.
00209  * 
00210  */

Generated on Mon Nov 8 17:07:32 2004 for MPT by  doxygen 1.3.9.1