Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members

stringutils.h

Go to the documentation of this file.
00001 // -*- C++ -*- 00002 00003 // PLearn (A C++ Machine Learning Library) 00004 // Copyright (C) 1998 Pascal Vincent 00005 // Copyright (C) 1999-2002 Pascal Vincent, Yoshua Bengio and University of Montreal 00006 // 00007 00008 // Redistribution and use in source and binary forms, with or without 00009 // modification, are permitted provided that the following conditions are met: 00010 // 00011 // 1. Redistributions of source code must retain the above copyright 00012 // notice, this list of conditions and the following disclaimer. 00013 // 00014 // 2. Redistributions in binary form must reproduce the above copyright 00015 // notice, this list of conditions and the following disclaimer in the 00016 // documentation and/or other materials provided with the distribution. 00017 // 00018 // 3. The name of the authors may not be used to endorse or promote 00019 // products derived from this software without specific prior written 00020 // permission. 00021 // 00022 // THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR 00023 // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 00024 // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN 00025 // NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 00026 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 00027 // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 00028 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 00029 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 00030 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 00031 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00032 // 00033 // This file is part of the PLearn library. For more information on the PLearn 00034 // library, go to the PLearn Web site at www.plearn.org 00035 00036 00037 00038 00039 /* ******************************************************* 00040 * $Id: stringutils.h,v 1.26 2004/07/21 16:30:51 chrish42 Exp $ 00041 * AUTHORS: Pascal Vincent 00042 * This file is part of the PLearn library. 00043 ******************************************************* */ 00044 00045 // This file contains useful functions for string manipulation 00046 // that are used in the PLearn Library 00047 00048 00051 #ifndef stringutils_INC 00052 #define stringutils_INC 00053 00054 #include <string> 00055 #include <vector> 00056 #include <iostream> 00057 #include <iomanip> 00058 #include <sstream> 00059 #include <plearn/io/PStream.h> 00060 // #include "TMat.h" 00061 00062 namespace PLearn { 00063 using namespace std; 00064 00065 // THIS slash CHAR HAS TO GO AWAY!!!! 00066 #if defined(_MINGW_) 00067 #define slash "\\" 00068 #define slash_char '\\' 00069 #else 00070 #define slash "/" 00071 #define slash_char '/' 00072 #endif 00073 00075 template<class T> string tostring(const T& x); 00076 00078 inline string tostring(const char* s) { return string(s); } 00079 00081 string left(const string& s, size_t width, char padding=' '); 00082 string right(const string& s, size_t width, char padding=' '); 00083 string center(const string& s, size_t width, char padding=' '); 00084 00085 // this function handle numbers with exponents (such as 10.2E09) 00086 // as well as Nans. String can have trailing whitespaces on both sides 00087 bool pl_isnumber(const string& s,double* dbl=NULL); 00088 bool pl_isnumber(const string& s,float* dbl); 00089 00091 long tolong(const string& s, int base=10); 00092 double todouble(const string& s); 00093 bool tobool(const string& s); 00094 inline int toint(const string& s, int base=10) { return int(tolong(s,base)); } 00095 inline float tofloat(const string& s) { return float(todouble(s)); } 00096 #ifdef USEFLOAT 00097 inline float toreal(const string& s) { return tofloat(s); } 00098 #endif 00099 #ifdef USEDOUBLE 00100 inline double toreal(const string& s) { return todouble(s); } 00101 #endif 00102 00103 00104 string removeblanks(const string& s); 00105 00107 string removeallblanks(const string& s); 00108 00110 string removenewline(const string& s); 00111 00113 string lowerstring(const string& s); 00114 00116 string upperstring(const string& s); 00117 00120 string pgetline(istream& in=cin); 00121 00123 bool isBlank(const string& s); 00124 00126 bool isParagraphBlank(const string& s); 00127 00128 00130 string space_to_underscore(string str); 00131 00133 string underscore_to_space(string str); 00134 00136 string backslash_to_slash(string str); 00137 00140 int search_replace(string& text, const string& searchstr, const string& replacestr); 00141 00143 vector<string> split(const string& s, char delimiter); 00144 00150 vector<string> split(const string& s, const string& delimiters=" \t\n\r", bool keepdelimiters=false); 00151 00157 void split_on_first(const string& s, const string& delimiters, string& left, string& right); 00158 00164 pair<string,string> split_on_first(const string& s, 00165 const string& delimiters=" \t\n\r"); 00166 00168 string join(const vector<string>& s, const string& separator=" "); 00169 00171 vector<string> addprepostfix(const string& prefix, const vector<string>& names, const string& postfix); 00172 00174 inline vector<string> addprefix(const string& prefix, const vector<string>& names) 00175 { return addprepostfix(prefix, names, ""); } 00176 00178 inline vector<string> addpostfix(const vector<string>& names, const string& postfix) 00179 { return addprepostfix("", names, postfix); } 00180 00183 string addprepostfix(const string& prefix, const string& text, const string& postfix); 00184 00187 inline string addprefix(const string& prefix, const string& text) 00188 { return addprepostfix(prefix, text, ""); } 00189 00192 inline string addpostfix(const string& text, const string& postfix) 00193 { return addprepostfix("", text, postfix); } 00194 00197 vector<string> stringvector(int argc, char** argv); 00198 00206 string get_option(const vector<string> &command_line, 00207 const string& option, const string& default_value); 00208 00215 bool find(const vector<string> &command_line, const string& option); 00216 00218 int findpos(const vector<string> &v, string element); 00219 00221 vector<string> remove(const vector<string> &v, string element); 00222 00224 00226 string extract_filename(const string& filepath); 00227 00229 string extract_directory(const string& filepath); 00230 00233 string extract_extension(const string& filepath); 00234 00236 //'.' and everything after it 00237 string remove_extension(const string& filename); 00238 00240 string remove_trailing_slash(const string& path); 00241 00243 string append_slash(const string& path); 00244 00246 string extract_filename_without_extension(const string& filepath); 00247 00249 void remove_comments(string& text, const string& commentstart="#"); 00250 00252 vector<string> getNonBlankLines(const string & in); 00253 00256 string* data_filename_2_filenames(const string& filename, int& nb_files); 00257 00259 ostream& operator<<(ostream& out, const vector<string>& vs); 00260 00262 template <class U, class V> 00263 ostream& operator<<(ostream& out, const pair<U,V>& p) 00264 { 00265 return out << p.first << ':' << p.second; 00266 } 00267 00269 00270 00276 template<class T> string tostring(const T& x) 00277 { 00278 ostringstream out; 00279 // Commented out because this adds a trailing space. 00280 // PStream pout(&out); 00281 // pout << x; 00282 out << x; 00283 return out.str(); 00284 00285 /* Old strstream code 00286 ostrstream out; 00287 out << setprecision(8) << x; 00288 char* buf = out.str(); 00289 int n = out.pcount(); 00290 string s(buf,n); 00291 out.freeze(false); // return ownership to the stream, so that it may free it... 00292 return s; 00293 */ 00294 } 00295 00296 string tostring(const double& x); 00297 00298 string tostring(const float& x); 00299 00300 } // end of namespace PLearn 00301 00302 #endif 00303 00304 00305 00306

Generated on Tue Aug 17 16:07:31 2004 for PLearn by doxygen 1.3.7