jspace/jspace/strutil.hpp

Go to the documentation of this file.
00001 /* 
00002  * Copyright (C) 2006 Roland Philippsen <roland dot philippsen at gmx dot net>
00003  *
00004  * BSD license:
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions
00007  * are met:
00008  * 1. Redistributions of source code must retain the above copyright
00009  *    notice, this list of conditions and the following disclaimer.
00010  * 2. Redistributions in binary form must reproduce the above copyright
00011  *    notice, this list of conditions and the following disclaimer in the
00012  *    documentation and/or other materials provided with the distribution.
00013  * 3. Neither the name of the copyright holder nor the names of
00014  *    contributors to this software may be used to endorse or promote
00015  *    products derived from this software without specific prior written
00016  *    permission.
00017  *
00018  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS''
00019  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
00020  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
00021  * PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT
00022  * HOLDER OR THE CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY
00023  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00024  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
00025  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00026  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
00027  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00028  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00029  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00030  */
00031 
00038 #ifndef SFL_STRUTIL_HPP
00039 #define SFL_STRUTIL_HPP
00040 
00041 
00042 #include <string>
00043 #include <sstream>
00044 
00045 
00046 namespace wbcnet {
00047   
00048   template<typename displayable_t, typename prefix_t>
00049   std::string displayString(displayable_t const & displayable, prefix_t const & prefix)
00050   {
00051     std::ostringstream result;
00052     displayable.display(result, prefix);
00053     return result.str();
00054   }
00055   
00056 }
00057 
00058 
00059 namespace sfl {
00060   
00062   template<typename Foo>
00063   std::string to_string(const Foo & foo) {
00064     std::ostringstream os;
00065     os << foo;
00066     return os.str();
00067   }
00068   
00070   template<>
00071   std::string to_string<bool>(const bool & flag);
00072   
00074   template<typename Foo>
00075   bool string_to(const std::string & str, Foo & foo) {
00076     Foo bar;
00077     std::istringstream is(str);
00078     if( ! (is >> bar))
00079       return false;
00080     foo = bar;
00081     return true;
00082   }
00083   
00089   template<>
00090   bool string_to<bool>(const std::string & str, bool & foo);
00091   
00096   template<typename Foo>
00097   bool string_to_bool(const std::string & str, Foo & foo) {
00098     bool bar;
00099     if( ! string_to(str, bar))
00100       return false;
00101     foo = bar ? 1 : 0;
00102     return true;
00103   }
00104 
00105   
00130   bool splitstring(std::string const & input, char separator,
00131        std::string & head, std::string & tail);
00132   
00133   
00138   template<typename tokenlist_t>
00139   size_t tokenize(std::string const & input, char separator, tokenlist_t & output) {
00140     std::string head;
00141     std::string tail(input);
00142     while (splitstring(tail, separator, head, tail))
00143       output.push_back(head);
00144     output.push_back(head);
00145     return output.size();
00146   }
00147   
00148   
00153   template<typename tokenlist_t, typename value_t>
00154   bool token_to(tokenlist_t const & tokenlist, size_t index, value_t & value) {
00155     if (index >= tokenlist.size())
00156       return false;
00157     return string_to(tokenlist[index], value);
00158   }
00159   
00160 }
00161 
00162 #endif // SFL_STRUTIL_HPP

Generated on Fri Aug 26 01:31:16 2011 for Stanford Whole-Body Control Framework by  doxygen 1.5.4