MOOS 0.2375
/home/toby/moos-ivp/MOOS-2375-Oct0611/Core/MOOSGenLib/MOOSGenLibGlobalHelper.h
Go to the documentation of this file.
00001 
00002 //
00003 //   MOOS - Mission Oriented Operating Suite
00004 //
00005 //   A suit of Applications and Libraries for Mobile Robotics Research
00006 //   Copyright (C) 2001-2005 Massachusetts Institute of Technology and
00007 //   Oxford University.
00008 //
00009 //   This software was written by Paul Newman at MIT 2001-2002 and Oxford
00010 //   University 2003-2005. email: pnewman@robots.ox.ac.uk.
00011 //
00012 //   This file is part of a  MOOS Core Component.
00013 //
00014 //   This program is free software; you can redistribute it and/or
00015 //   modify it under the terms of the GNU General Public License as
00016 //   published by the Free Software Foundation; either version 2 of the
00017 //   License, or (at your option) any later version.
00018 //
00019 //   This program is distributed in the hope that it will be useful,
00020 //   but WITHOUT ANY WARRANTY; without even the implied warranty of
00021 //   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
00022 //   General Public License for more details.
00023 //
00024 //   You should have received a copy of the GNU General Public License
00025 //   along with this program; if not, write to the Free Software
00026 //   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
00027 //   02111-1307, USA.
00028 //
00030 
00033 #ifndef MOOSGenLibGlobalHelperh
00034 #define MOOSGenLibGlobalHelperh
00035 
00036 
00037 #ifndef PI
00038 #define PI 3.141592653589
00039 #endif
00040 
00041 #include <string>
00042 #include <list>
00043 #include <vector>
00044 #include <sstream>
00045 #include <algorithm>
00046 
00047 
00048 
00049 
00051 
00052 typedef std::list<std::string> STRING_LIST ;
00053 
00054 //find the location of sToken in a string sSource with or without case sensitivity
00055 size_t  MOOSStrFind( const std::string &sSource , const std::string & sToken,bool bInsensitive=false);
00056 
00057 
00058 //following function finds token = value in a list of such strings
00059 bool MOOSGetValueFromToken(STRING_LIST & sParams,const std::string & sToken,std::string & sVal);
00060 
00061 //following functions extract a value from a string containing comma seperated pairs of Token = Val....
00062 //by default case sensitivity on the Token is off. Thus  given two strings S1="X=AbCd" and S2="x=AbCd",
00063 //invoking  MOOSValFromString(s,S1,"x",true) and MOOSValFromString(s,S2,"x",true) with both insert "AbCd" into s
00064 bool MOOSValFromString(std::string & sVal,const std::string & sStr,const std::string & sTk,bool bInsensitive=false);
00065 bool MOOSValFromString(double & dfVal,const std::string & sStr,const std::string & sTk,bool bInsensitive=false);
00066 bool MOOSValFromString(float  &  fVal,const std::string & sStr,const std::string & sTk,bool bInsensitive=false);
00067 bool MOOSValFromString(long   &  nVal,const std::string & sStr,const std::string & sTk,bool bInsensitive=false);
00068 bool MOOSValFromString(int    &  nVal,const std::string & sStr,const std::string & sTk,bool bInsensitive=false);
00069 bool MOOSValFromString(bool   &  bVal,const std::string & sStr,const std::string & sTk,bool bInsensitive=false);
00070 bool MOOSValFromString(unsigned int &  nVal,const std::string & sStr,const std::string & sTk,bool bInsensitive=false);
00071 bool MOOSValFromString(std::vector<double> &dfValVec,int &nRows,int &nCols, const std::string & sStr, const std::string & sToken,bool bInsensitive=false);
00072 bool MOOSValFromString(std::vector<unsigned int> &nValVec, int &nRows, int &nCols, const std::string & sStr, const std::string & sToken,bool bInsensitive=false);
00073 bool MOOSValFromString(long long & nVal, const std::string & sStr,const std::string & sTk,bool bInsensitive=false);
00074 
00075 //the following simply parse a MOOSFormated vector [nxm]{a,b,c...}
00076 bool MOOSVectorFromString(const std::string & sStr,std::vector<double> & dfVecVal,int & nRows,int & nCols);
00077 bool MOOSVectorFromString(const std::string & sStr,std::vector<float> & fValVec,int & nRows, int & nCols);
00078 bool MOOSVectorFromString(const std::string & sStr,std::vector<unsigned int> & dfVecVal,int & nRows,int & nCols);
00079 
00080 //the following a sim[lpe line extractor from a file possessing an alog format - see Logger documentation
00081 bool GetNextAlogLineByMessageName(std::istream & Input,
00082                                                                   const std::string & sMessageName,
00083                                                                   double & dfTime,
00084                                                                   std::string & sSource,
00085                                                                   std::string & sPayload);
00086 
00087 
00089 std::string DoubleVector2String(const std::vector<double> & V);
00090 
00092 std::stringstream & Write (std::stringstream & os,const std::vector<double> & Vec);
00093 
00095 std::stringstream & Write (std::stringstream & os,const std::vector<int> & Vec);
00096 
00097 //the ubiquitous chomp function
00098 std::string MOOSChomp(std::string &sStr, const std::string &sTk=",",bool bInsensitive=false);
00099 
00101 void MOOSRemoveChars(std::string & sStr,const std::string & sTok);
00102 
00104 void MOOSToUpper(std::string &str);
00105 
00107 void MOOSTrimWhiteSpace(std::string & str);
00108 
00110 bool MOOSIsNumeric(std::string  str);
00111 
00113 bool MOOSStrCmp(std::string s1,std::string s2);
00114 
00116 bool MOOSWildCmp(const std::string & sPattern, const std::string & sString ) ;
00117 
00118 
00120 
00122 double GetMOOSSkew();
00123 void SetMOOSSkew(double dfSkew);
00124 
00126 double GetMOOSSkew();
00127 
00129 bool SetMOOSTimeWarp(double dfWarp);
00130 
00132 double GetMOOSTimeWarp();
00133 
00135 void MOOSPause(int nMS,bool bApplyTimeWarping = true);
00136 
00141 double MOOSTime(bool bApplyTimeWarping=true);
00142 
00145 bool SetWin32HighPrecisionTiming(bool bEnable);
00146 
00148 double HPMOOSTime(bool bApplyTimeWarping = true);
00149 
00153 double MOOSLocalTime(bool bApplyTimeWarping=true);
00154 
00155 
00157 int     MOOSGetch();
00158 
00159 
00161 //formatted printing
00163 void MOOSTrace(std::string  Str);
00164 
00166 void MOOSTrace(const char *FmtStr,...);
00167 
00169 std::string MOOSFormat(const char * FmtStr,...);
00170 
00172 void InhibitMOOSTraceInThisThread(bool bInhibit = true);
00173 
00175 bool MOOSFail(const char * FmtStr,...);
00176 
00178 std::string MOOSGetTimeStampString();
00179 
00181 std::string MOOSGetDate();
00182 
00184 #define  MOOSHERE  MOOSFormat("File %s Line %d", __FILE__,__LINE__).c_str()
00185 
00187 void Progress(double dfPC);
00188 
00189 
00190 //these are used to let people format string used to control
00191 //actuation (via a Thirdparty task) - one has to question why they are here though..
00192 std::string MOOSThirdPartyActuationString(double * pdfRudder,double * pdfElevator,double * pdfThrust);
00193 std::string MOOSThirdPartyStatusString(std::string sStatusCommand);
00194 
00195 
00197 
00199 double MOOS_ANGLE_WRAP(double dfAng);
00200 
00202 double MOOSDeg2Rad(double dfDeg);
00203 
00205 double MOOSRad2Deg(double dfRad);
00206 
00208 bool MOOSAbsLimit(double & dfVal,double dfLimit);
00209 
00211 double MOOSWhiteNoise(double Sigma);
00212 
00214 double MOOSNormalInv(double dfArea);
00215 
00217 int MOOSDiscreteUniform(int nMin, int nMax);
00218 
00220 double MOOSUniformRandom(double dfMin, double dfMax);
00221 
00223 template <class T>
00224 const T& MOOSClamp(const T &val, const T &min, const T &max)
00225 {
00226     if (val < min) return min;
00227     if (max < val) return max;
00228     else return val;
00229 }
00230 
00231 
00233 
00237 bool GetDirectoryContents(const std::string & sPath,std::list<std::string> &sContents,bool bFiles= true);
00238 
00240 bool MOOSCreateDirectory(const std::string & sDirectory);
00241 
00243 bool MOOSFileParts(std::string sFullPath, std::string & sPath,std::string &sFile,std::string & sExtension);
00244 
00245 
00246 
00247 
00249 
00250 
00252 template <class  T > T SwapByteOrder(const T &v)
00253 {
00254     T r = v;
00255     char * aR = (char*)&r;
00256     std::reverse(aR,aR+sizeof(T));
00257     return r;
00258 }
00259 
00260 
00262 bool IsLittleEndian();
00263 
00264 
00268 template<class D> struct static_caster
00269 {
00270     template<class S> D operator()(S s) const { return static_cast<D> (s); }
00271 };
00272 
00273 
00277 template<class D> struct dynamic_caster
00278 {
00279     template<class S> D operator()(S s) const { return dynamic_cast<D> (s); }
00280 };
00281 
00282 
00283 //adds a token/value pair to end of the supplied string sIn
00284 template <class T>
00285 std::string & MOOSAddValToString(std::string & sIn, const std::string & sTok,const T & Val)
00286 {
00287     std::stringstream s;
00288     
00289     if(!sIn.empty())
00290                 s<<",";
00291     
00292     s<<sTok<<"="<<Val;
00293     
00294     sIn+=s.str();
00295     
00296     return sIn;
00297 }
00298 
00299 
00301 #ifdef _WIN32
00302 #define UNUSED_PARAMETER(a) a
00303 #else
00304 #define UNUSED_PARAMETER(a)
00305 #endif
00306 
00307 
00308 
00309 #endif
00310 
00311 
00312 
00313 
00314 
00315 
00316 
00317 
00318 
00319 
00320 
00321 
00322 
00323 
00324 
00325 
00326 
00327 
00328 
00329 
00330 
00331 
00332 
00333 
00334 
00335 
00336 
00337 
00338 
00339 
00340 
00341 
00342 
00343 
00344 
00345 
00346 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines