RawOptionsContainer.cpp
Go to the documentation of this file.
00001 // ----------------------------------------------------------------------------
00002 //
00003 // $Id$
00004 //
00005 // Copyright 2008, 2009, 2010, 2011  Antonio Franchi and Paolo Stegagno    
00006 //
00007 // This file is part of MIP.
00008 //
00009 // MIP is free software: you can redistribute it and/or modify
00010 // it under the terms of the GNU General Public License as published by
00011 // the Free Software Foundation, either version 3 of the License, or
00012 // (at your option) any later version.
00013 //
00014 // MIP is distributed in the hope that it will be useful,
00015 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00016 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017 // GNU General Public License for more details.
00018 //
00019 // You should have received a copy of the GNU General Public License
00020 // along with MIP. If not, see <http://www.gnu.org/licenses/>.
00021 //
00022 // Contact info: antonio.franchi@tuebingen.mpg.de stegagno@dis.uniroma1.it
00023 //
00024 // ----------------------------------------------------------------------------
00025 
00026 #include <telekyb_base/Options/RawOptionsContainer.hpp>
00027 
00028 #include <telekyb_base/File/File.hpp>
00029 #include <telekyb_base/Tools/XmlRpcHelper.hpp>
00030 
00031 // ros
00032 #include <ros/console.h>
00033 #include <ros/assert.h>
00034 
00035 namespace TELEKYB_NAMESPACE
00036 {
00037 
00038 // private
00039 // definition
00040 StringMap RawOptionsContainer::rawOptionsMap;
00041 
00042 bool RawOptionsContainer::addUpdateDashOption(std::string dashkey, const std::string& value, bool overwrite, bool showDashError)
00043 {
00044         if (dashkey.length() < 2 || !(dashkey[0] == '-' && dashkey[1] == '-'))
00045         {
00046                 if (showDashError)
00047                 {
00048                         ROS_WARN_STREAM("Syntax error:" << dashkey << " must be preceded by a dash '--'.");
00049                 }
00050                 return false;
00051         }
00052 
00053         // Syntax ok
00054         dashkey.erase(0,2);
00055         bool ret = true;
00056 
00057         if (overwrite)
00058                 addUpdateOption(dashkey,value);
00059         else
00060                 ret = addOption(dashkey,value);
00061 
00062         return ret;
00063 }
00064 
00065 
00066 // public
00067 bool RawOptionsContainer::hasOption(const std::string& key)
00068 {
00069         StringMap::iterator it;
00070         it = rawOptionsMap.find(key);
00071 
00072         return (it != rawOptionsMap.end());
00073 }
00074 
00075 bool RawOptionsContainer::removeOption(const std::string& key)
00076 {
00077         // erase return 1 if Object was erased.
00078         return (rawOptionsMap.erase(key) == 1);
00079 }
00080 
00081 bool RawOptionsContainer::getOption(const std::string& key, std::string& value)
00082 {
00083         bool ret = false;
00084 
00085         StringMap::iterator it;
00086         it = rawOptionsMap.find(key);
00087 
00088         if (it != rawOptionsMap.end())
00089         {
00090                 value = it->second;
00091                 ret = true;
00092         }
00093 
00094         return ret;
00095 }
00096 // Option must exist
00097 bool RawOptionsContainer::updateOption(const std::string& key, const std::string& value)
00098 {
00099         bool ret = false;
00100         // would be more efficient with iterator. Doesn't matter.
00101         if (hasOption(key))
00102         {
00103                 rawOptionsMap[key] = value;
00104                 ret = true;
00105 
00106                 ROS_DEBUG_STREAM("Updated: " << key << " with value " <<  value);
00107         }
00108         else
00109         {
00110                 ROS_WARN_STREAM("Saw: " << key << " but did NOT update, because option was not found!");
00111         }
00112         return ret;
00113 }
00114 // Option must NOT exist
00115 bool RawOptionsContainer::addOption(const std::string& key, const std::string& value)
00116 {
00117         bool ret = false;
00118         // would be more efficient with iterator. Doesn't matter.
00119         if (!hasOption(key))
00120         {
00121                 rawOptionsMap[key] = value;
00122                 ret = true;
00123 
00124                 ROS_DEBUG_STREAM("Added: " << key << " with value " << value);
00125         }
00126         else
00127         {
00128                 ROS_WARN_STREAM("Saw: " << key << " but did NOT add, because it is already set.!");
00129         }
00130         return ret;
00131 }
00132 // Value gets set whether option exists or not
00133 void RawOptionsContainer::addUpdateOption(const std::string& key, const std::string& value)
00134 {
00135         rawOptionsMap[key] = value;
00136 
00137         ROS_DEBUG_STREAM("Added/Updated: " << key << " with value " << value);
00138 }
00139 
00140 // parse CommandLine Arguments.
00141 bool RawOptionsContainer::parseCommandLine(int argc, char* const argv[], bool overwrite )
00142 {
00143         if (argc % 2 == 0)
00144         {
00145                 ROS_FATAL_STREAM("Non-odd options, " << TELEKYB_BASENAME << " launching aborted.");
00146                 //ROS_BREAK();
00147                 ros::shutdown();
00148                 return false;
00149         }
00150 
00151         // ok to continue
00152         bool ret = true;
00153         for (int i = 1; i < argc; i += 2)
00154         {
00155                 std::string name(argv[i]);
00156                 std::string value(argv[i + 1]);
00157 
00158                 ret = addUpdateDashOption(name,value,overwrite,true);
00159         }
00160 
00161         return ret;
00162 }
00163 
00164 bool RawOptionsContainer::parseCommandLine(const std::vector<std::string>& commandLineArgs, bool overwrite )
00165 {
00166         if (commandLineArgs.size() % 2 == 0)
00167         {
00168                 ROS_FATAL_STREAM("Non-odd options, " << TELEKYB_BASENAME << " launching aborted.");
00169                 //ROS_BREAK();
00170                 ros::shutdown();
00171                 return false;
00172         }
00173 
00174         bool ret = true;
00175         for(unsigned int i = 0; i < commandLineArgs.size(); i+=2)
00176         {
00177                 // Function checks for correct syntax // last argument false: report no errors!
00178                 ret = addUpdateDashOption(commandLineArgs.at(i),commandLineArgs.at(i+1), overwrite, true);
00179         }
00180         return ret;
00181 }
00182 
00183 bool RawOptionsContainer::parseFile(const std::string& fileName, bool overwrite)
00184 {
00185         bool ret = true;
00186 
00187         File file(fileName);
00188         std::vector<std::string> words;
00189         ret = file.getWords(words);
00190 
00191         int nWords = words.size();
00192         // only walk to the second to last, because the last field must always be an option.
00193         for(int i = 0; i < nWords-1; i++)
00194         {
00195                 // Function checks for correct syntax // last argument false: report no errors!
00196                 addUpdateDashOption(words[i],words[i+1], overwrite, false);
00197         }
00198 
00199         return ret;
00200 }
00201 
00202 // be careful with this.
00203 void RawOptionsContainer::clearOptions()
00204 {
00205         rawOptionsMap.clear();
00206 }
00207 
00208 void RawOptionsContainer::print()
00209 {
00210         std::cout << toString();
00211 }
00212 std::string RawOptionsContainer::toString()
00213 {
00214         std::stringstream ss;
00215         StringMap::iterator it;
00216         for (it = rawOptionsMap.begin(); it != rawOptionsMap.end(); it++)
00217         {
00218                 ss << "-" << it->first << " " << it->second;
00219                 ss << "\n";
00220         }
00221 
00222         return ss.str();
00223 }
00224 
00225 StringMap& RawOptionsContainer::getMap()
00226 {
00227         return rawOptionsMap;
00228 }
00229 
00230 } // namespace
00231 
00232 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Friends Defines


telekyb_base
Author(s): Dr. Antonio Franchi and Martin Riedel
autogenerated on Mon Nov 11 2013 11:12:34