00001 /** 00002 \file Robots/LoBot/util/LoString.H 00003 \brief String functions. 00004 */ 00005 00006 // //////////////////////////////////////////////////////////////////// // 00007 // The iLab Neuromorphic Vision C++ Toolkit - Copyright (C) 2000-2005 // 00008 // by the University of Southern California (USC) and the iLab at USC. // 00009 // See http://iLab.usc.edu for information about this project. // 00010 // //////////////////////////////////////////////////////////////////// // 00011 // Major portions of the iLab Neuromorphic Vision Toolkit are protected // 00012 // under the U.S. patent ``Computation of Intrinsic Perceptual Saliency // 00013 // in Visual Environments, and Applications'' by Christof Koch and // 00014 // Laurent Itti, California Institute of Technology, 2001 (patent // 00015 // pending; application number 09/912,225 filed July 23, 2001; see // 00016 // http://pair.uspto.gov/cgi-bin/final/home.pl for current status). // 00017 // //////////////////////////////////////////////////////////////////// // 00018 // This file is part of the iLab Neuromorphic Vision C++ Toolkit. // 00019 // // 00020 // The iLab Neuromorphic Vision C++ Toolkit is free software; you can // 00021 // redistribute it and/or modify it under the terms of the GNU General // 00022 // Public License as published by the Free Software Foundation; either // 00023 // version 2 of the License, or (at your option) any later version. // 00024 // // 00025 // The iLab Neuromorphic Vision C++ Toolkit is distributed in the hope // 00026 // that it will be useful, but WITHOUT ANY WARRANTY; without even the // 00027 // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR // 00028 // PURPOSE. See the GNU General Public License for more details. // 00029 // // 00030 // You should have received a copy of the GNU General Public License // 00031 // along with the iLab Neuromorphic Vision C++ Toolkit; if not, write // 00032 // to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, // 00033 // Boston, MA 02111-1307 USA. // 00034 // //////////////////////////////////////////////////////////////////// // 00035 // 00036 // Primary maintainer for this file: mviswana usc edu 00037 // $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/Robots/LoBot/util/LoString.H $ 00038 // $Id: LoString.H 13037 2010-03-23 01:00:53Z mviswana $ 00039 // 00040 00041 #ifndef LOBOT_STRING_UTILITIES_DOT_H 00042 #define LOBOT_STRING_UTILITIES_DOT_H 00043 00044 //------------------------------ HEADERS -------------------------------- 00045 00046 // lobot headers 00047 #include "Robots/LoBot/util/range.hh" 00048 00049 // Standard C++ headers 00050 #include <sstream> 00051 #include <string> 00052 #include <list> 00053 #include <deque> 00054 #include <vector> 00055 #include <iterator> 00056 00057 //----------------------------- NAMESPACE ------------------------------- 00058 00059 namespace lobot { 00060 00061 //--------------------- BASIC STRING CONVERSIONS ------------------------ 00062 00063 /// Convenient (but perhaps not the most efficient) helper to convert 00064 /// various data types to strings. 00065 /// 00066 /// DEVNOTE: Works as long as type T defines an operator << that writes 00067 /// to an ostream. 00068 template<typename T> 00069 std::string to_string(const T& t) 00070 { 00071 std::ostringstream str ; 00072 str << t ; 00073 return str.str() ; 00074 } 00075 00076 /// to_string() specialization for converting ranges to strings. 00077 template<typename T> 00078 std::string to_string(const range<T>& R) 00079 { 00080 std::ostringstream str ; 00081 str << R.min() << ' ' << R.max() ; 00082 return str.str() ; 00083 } 00084 00085 /// Read from string. As above, works as long as type T defines an 00086 /// operator >> that reads from an istream. 00087 template<typename T> 00088 T from_string(const std::string& s, const T& defval = T()) 00089 { 00090 T t(defval) ; 00091 std::istringstream str(s) ; 00092 str >> t ; 00093 return t ; 00094 } 00095 00096 /// from_string() specialization for strings. If the client wants a 00097 /// string from the input string, we just return the input string. If we 00098 /// were to apply the default version of this template function, we would 00099 /// end up parsing the input string as a whitespace separated string 00100 /// stream and only return the first string from this stream. 00101 template<> 00102 std::string from_string(const std::string& s, const std::string&) 00103 { 00104 return s ; 00105 } 00106 00107 //-------------------------- STRING PARSING ----------------------------- 00108 00109 /// Converts a whitespace separated string of T into a vector of T 00110 template<typename T> 00111 std::vector<T> string_to_vector(const std::string& s) 00112 { 00113 std::istringstream str(s) ; 00114 return std::vector<T>(std::istream_iterator<T>(str), 00115 std::istream_iterator<T>()) ; 00116 } 00117 00118 /// Converts a whitespace separated string of T into a deque of T 00119 template<typename T> 00120 std::deque<T> string_to_deque(const std::string& s) 00121 { 00122 std::istringstream str(s) ; 00123 return std::deque<T>(std::istream_iterator<T>(str), 00124 std::istream_iterator<T>()) ; 00125 } 00126 00127 /// Converts a whitespace separated string of T into a list of T 00128 template<typename T> 00129 std::list<T> string_to_list(const std::string& s) 00130 { 00131 std::istringstream str(s) ; 00132 return std::list<T>(std::istream_iterator<T>(str), 00133 std::istream_iterator<T>()) ; 00134 } 00135 00136 //--------------------------- MISCELLANEOUS ----------------------------- 00137 00138 /// String case conversion 00139 //@{ 00140 std::string upstring(std::string) ; 00141 std::string downstring(std::string) ; 00142 //@} 00143 00144 /// Duplicate the given string n times and return the result in a new 00145 /// string. 00146 std::string dup_string(const std::string& s, int n) ; 00147 00148 //----------------------------------------------------------------------- 00149 00150 } // end of namespace encapsulating this file's definitions 00151 00152 #endif 00153 00154 /* So things look consistent in everyone's emacs... */ 00155 /* Local Variables: */ 00156 /* indent-tabs-mode: nil */ 00157 /* End: */