Home | Doxygen Documentation | Tutorials | Developer Tools (restricted)

toolbox/stringFunc.hh
Go to the documentation of this file.
00001 
00004 #ifndef StringFuns_hh
00005 #define StringFuns_hh
00006 
00007 #include <string>
00008 #include <vector>
00009 #include <iomanip>
00010 #include "basics/outputOperator.hh"
00011 #include "basics/typedefs.hh"
00012 #include "toolbox/sequence.hh"
00013 
00014 namespace concepts {
00015 
00016   // *************************************************************** tolower **
00017 
00018   char tolower(const char ch);
00019 
00020   // ********************************************************* stringtolower **
00021 
00022   std::string stringtolower(const std::string s);
00023 
00024   // ******************************************************** removeAllWhite **
00025 
00027   std::string removeAllWhite(const std::string str);
00028 
00029   // *********************************************************** getFilename **
00030 
00032   std::string getFilename(const std::string str);
00033 
00034   // ***************************************************** getFilenamePrefix **
00035 
00039   std::string getFilenamePrefix(const std::string str);
00040 
00041   // ********************************************************** getDirectory **
00042 
00044   std::string getDirectory(const std::string str);
00045 
00046   // *********************************************************** splitString **
00047 
00051   std::vector<std::string> splitString(const std::string text,
00052                                        const std::string separators);
00053 
00054 
00055   // ************************************************* splitStringNameParams **
00056 
00059   std::vector<std::string> splitStringNameParams(const std::string text);
00060 
00061   // **************************************************** splitStringByComma **
00062 
00069   std::vector<std::string> splitStringByComma(const std::string text);
00070 
00071   // ************************************************************ stringSubs **
00072 
00077   template<class F>
00078   std::string stringSubs(const std::string str,
00079                          const std::string var, F value) {
00080     std::stringstream s;
00081     s << std::setprecision(16) << value;
00082     std::string sNew = str;
00083     
00084     std::string::size_type i;
00085     while((i = sNew.find(var)) != std::string::npos)
00086       sNew.replace(i, var.size(), s.str());
00087     return sNew;
00088   }
00089 
00090 
00091   // ************************************************* ParseObjectFromString **
00092 
00098   template<class F>
00099   class ParseObjectFromString : public OutputOperator {
00100   public:
00102     ParseObjectFromString(const char* name = "",
00103                           const Sequence<F> data = Sequence<F>())
00104       : name_(name), data_ (data) {}
00106     ParseObjectFromString(const char* name, const F data)
00107       : name_(name), data_(1) { data_[0] = data; }
00109     ParseObjectFromString(const char* name, const F data1, const F data2)
00110       : name_(name), data_(2) { data_[0] = data1; data_[1] = data2; }
00112     ParseObjectFromString(const char* name,
00113                           const F data1, const F data2, const F data3)
00114       : name_(name), data_(3) { 
00115       data_[0] = data1;
00116       data_[1] = data2;
00117       data_[2] = data3;
00118     }
00119 
00121     std::string& name() { return name_; }
00123     Sequence<F>& data() { return data_; }
00124 
00126     bool parse(const std::string s);
00127   protected:
00128     std::ostream& info(std::ostream& os) const;
00129   private:
00131     std::string name_;
00133     Sequence<F> data_;
00134   };
00135 
00136   template<class F>
00137   bool ParseObjectFromString<F>::parse(const std::string s) {
00138     Sequence<std::string> seq = splitStringNameParams(s);
00139     if (seq.size() == 0) return false;
00140 
00141     // remove white spaces
00142     for(Sequence<std::string>::iterator i = seq.begin(); i != seq.end(); ++i) {
00143       std::string t = removeAllWhite(*i); *i = t;
00144     }
00145     std::stringstream st; st << seq[0];
00146     st >> name_;
00147     data_.resize(seq.size()-1);
00148     for(uint i = 0; i < data_.size(); ++i) {
00149       std::stringstream st; st << seq[i+1];
00150       st >> data_[i];
00151     }
00152 
00153     return true;
00154   }
00155 
00156   template<class F>
00157   std::ostream& ParseObjectFromString<F>::info(std::ostream& os) const {
00158     os << name_ << '(';
00159     for(typename Sequence<F>::const_iterator i = data_.begin(); 
00160         i != data_.end();) {
00161       os << *i++;
00162       if (i != data_.end())
00163         os << ", ";
00164     }
00165     return os << ')';
00166   }
00167 
00168   // ******************************************** realSeqFromStringWithPower **
00169 
00178   Sequence<Real> realSeqFromStringWithPower(const std::string s);
00179 
00180 } // namespace concepts
00181 
00182 #endif // StringFuns_hh

Home | Doxygen Documentation | Tutorials | Developer Tools (restricted)