configurable.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (C) 2005 by Robot Group Leipzig                             *
00003  *    martius@informatik.uni-leipzig.de                                    *
00004  *    fhesse@informatik.uni-leipzig.de                                     *
00005  *    der@informatik.uni-leipzig.de                                        *
00006  *                                                                         *
00007  *   This program is free software; you can redistribute it and/or modify  *
00008  *   it under the terms of the GNU General Public License as published by  *
00009  *   the Free Software Foundation; either version 2 of the License, or     *
00010  *   (at your option) any later version.                                   *
00011  *                                                                         *
00012  *   This program is distributed in the hope that it will be useful,       *
00013  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00014  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00015  *   GNU General Public License for more details.                          *
00016  *                                                                         *
00017  *   You should have received a copy of the GNU General Public License     *
00018  *   along with this program; if not, write to the                         *
00019  *   Free Software Foundation, Inc.,                                       *
00020  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
00021  ***************************************************************************
00022  *                                                                         *
00023  *  DESCRIPTION                                                            *
00024  *                                                                         *
00025  *   $Log: configurable.cpp,v $
00026  *   Revision 1.1.2.6  2006/02/08 16:17:40  martius
00027  *   no namespace using
00028  *
00029  *   Revision 1.1.2.5  2006/01/18 16:48:10  martius
00030  *   configurables can be stored and reloaded
00031  *
00032  *   Revision 1.1.2.4  2006/01/18 10:45:32  martius
00033  *   *** empty log message ***
00034  *
00035  *   Revision 1.1.2.3  2006/01/18 10:44:49  martius
00036  *   (re)storeCfg
00037  *
00038  *   Revision 1.1.2.2  2006/01/16 17:40:13  martius
00039  *   parsing works
00040  *
00041  *   Revision 1.1.2.1  2006/01/16 17:27:17  martius
00042  *   *** empty log message ***
00043  *
00044  *                                                                 *
00045  ***************************************************************************/
00046 
00047 #include "configurable.h"
00048 
00049 using namespace std;
00050 
00051 void Configurable::insertCVSInfo(paramkey& str, const char* file, const char* revision){
00052   string f(file);
00053   string rev(revision);
00054   int colon = f.find(':');
00055   int end   = f.rfind('$');
00056   if(colon >= 0 && end >= 0 && colon+1 < end){
00057     str += f.substr(colon+1, end-colon-1);
00058   }
00059   str += "-";
00060   colon = rev.find(':');
00061   end   = rev.rfind('$');
00062   if(colon >= 0 && end >= 0 && colon+1 < end){
00063     str += rev.substr(colon+1, end-colon-1);
00064   }
00065 }
00066 
00067 #ifndef AVR
00068 
00069 bool Configurable::storeCfg(const char* filenamestem){
00070   char name[256];
00071   FILE* f;
00072   sprintf(name, "%s.cfg",filenamestem);
00073   if(!(f = fopen(name, "w"))) return false;
00074   print(f,""); // save config values to file
00075   fclose(f);
00076   return true;
00077 }
00078 
00079 bool Configurable::restoreCfg(const char* filenamestem){
00080   char name[256];
00081   FILE* f;
00082   sprintf(name, "%s.cfg",filenamestem);
00083   if(!(f=fopen(name, "r")))
00084     return false;
00085   parse(f);
00086   fclose(f);
00087   return true;
00088 }
00089 
00090 void Configurable::print(FILE* f, const char* prefix) const {
00091   const char* pre = prefix==0 ? "": prefix;    
00092   const unsigned short spacelength=20;
00093   char spacer[spacelength];
00094   memset(spacer, ' ', spacelength);  spacer[spacelength-1]=0;
00095     
00096   paramlist list = getParamList();
00097   fprintf(f, "%s[%s][%i]\n", pre, getName().c_str(), getId());
00098   for(paramlist::iterator i = list.begin(); i!= list.end() ; i++) {
00099     const string& k = (*i).first;
00100     fprintf(f, "%s %s=%s%f\n", pre, k.c_str(), 
00101             spacer+(k.length() > spacelength  ? spacelength : k.length()), (*i).second);
00102   }
00103 }
00104 
00105 void Configurable::parse(FILE* f) {
00106   char* buffer = 0;    
00107   size_t len=0;    
00108   ssize_t read=0;    
00109   while ((read = getline(&buffer, &len, f)) != -1) {
00110     char* p = strchr(buffer,'=');
00111     if(p!=0){
00112       *p=0; // terminate string (key) at = sign   
00113       char* s = buffer;
00114       while (*s==' ') s++; // skip leading spaces
00115       // cout << "Set: " << s << " Val:" << atof(p+1) << endl;    
00116       setParam(s, atof(p+1)); 
00117     }    
00118   }
00119   if(buffer) free(buffer);
00120 }
00121 
00122 #endif

Generated on Tue Apr 4 19:05:03 2006 for Robotsystem from Robot Group Leipzig by  doxygen 1.4.5