Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
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
00032 #include <ros/console.h>
00033 #include <ros/assert.h>
00034
00035 namespace TELEKYB_NAMESPACE
00036 {
00037
00038
00039
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
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
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
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
00097 bool RawOptionsContainer::updateOption(const std::string& key, const std::string& value)
00098 {
00099 bool ret = false;
00100
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
00115 bool RawOptionsContainer::addOption(const std::string& key, const std::string& value)
00116 {
00117 bool ret = false;
00118
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
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
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
00147 ros::shutdown();
00148 return false;
00149 }
00150
00151
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
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
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
00193 for(int i = 0; i < nWords-1; i++)
00194 {
00195
00196 addUpdateDashOption(words[i],words[i+1], overwrite, false);
00197 }
00198
00199 return ret;
00200 }
00201
00202
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 }
00231
00232