OpenWalnut 1.2.5
WStringUtils.h
00001 //---------------------------------------------------------------------------
00002 //
00003 // Project: OpenWalnut ( http://www.openwalnut.org )
00004 //
00005 // Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
00006 // For more information see http://www.openwalnut.org/copying
00007 //
00008 // This file is part of OpenWalnut.
00009 //
00010 // OpenWalnut is free software: you can redistribute it and/or modify
00011 // it under the terms of the GNU Lesser General Public License as published by
00012 // the Free Software Foundation, either version 3 of the License, or
00013 // (at your option) any later version.
00014 //
00015 // OpenWalnut is distributed in the hope that it will be useful,
00016 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00017 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018 // GNU Lesser General Public License for more details.
00019 //
00020 // You should have received a copy of the GNU Lesser General Public License
00021 // along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
00022 //
00023 //---------------------------------------------------------------------------
00024 
00025 #ifndef WSTRINGUTILS_H
00026 #define WSTRINGUTILS_H
00027 
00028 #include <algorithm>
00029 #include <iostream>
00030 #include <iomanip>
00031 #include <iterator>
00032 #include <list>
00033 #include <set>
00034 #include <sstream>
00035 #include <string>
00036 #include <vector>
00037 
00038 #include <boost/lexical_cast.hpp>
00039 
00040 #include "WExportCommon.h"
00041 
00042 /**
00043  * Some utilities for string manipulation and output operations. Please note
00044  * that the overloaded ostream output operators aren't in a separate namespace
00045  * but the string manipulation functions. This is because of short use of e.g.
00046  * the <tt><<</tt> operator instead of <tt>string_utils::operator( cout,
00047  * myVector)</tt>.
00048  *
00049  * The reason for not using the Boost trimming functions is, that Boost
00050  * providing just Whitespace trimming depending on the current locale, but we
00051  * might want to trim other character sets too.
00052  *
00053  * The reason for not using the Boost case switching functions is that we want
00054  * those functions to return a <tt>std::string</tt> copy which is modified to
00055  * make some call chains ala: <tt>foo( rTrim( toLower( str ), "bar" ) );</tt>.
00056  *
00057  * The reason for not using Boosts Tokenizer is, that this tokenizer, is much
00058  * most simplest to use :).
00059  */
00060 namespace string_utils
00061 {
00062     /** We consider the following characters as whitespace:
00063      *  - <tt>\\r</tt> carriage return
00064      *  - <tt>\\n</tt> newline
00065      *  - <tt>\\t</tt> tab
00066      *  - <tt>' '</tt> space
00067      */
00068     extern OWCOMMON_EXPORT const std::string WHITESPACE;
00069 
00070     /**
00071      * Trims any occurence of each character given in parameter t from the end
00072      * (or right side) of the given string.
00073      *
00074      * \param source String to trim
00075      * \param t String representing a set containg all trimmable characters
00076      * \return A copy of the trimmed string
00077      */
00078 
00079     std::string OWCOMMON_EXPORT rTrim( const std::string& source, const std::string& t = WHITESPACE );
00080 
00081     /**
00082      * Trims any occurence of each character given in parameter t from the
00083      * start (or left side) of the given string.
00084      *
00085      * \param source String to trim
00086      * \param t String representing a set containg all trimmable characters
00087      * \return A copy of the trimmed string
00088      */
00089     std::string OWCOMMON_EXPORT lTrim( const std::string& source, const std::string& t =
00090             WHITESPACE );
00091 
00092     /**
00093      * Trims any occurence of each character given in parameter t from both
00094      * ends (right and left side) of the given string.
00095      *
00096      * \param source String to trim
00097      * \param t String representing a set containg all trimmable characters
00098      * \return A copy of the trimmed string
00099      */
00100     std::string OWCOMMON_EXPORT trim( const std::string& source, const std::string& t = WHITESPACE );
00101 
00102     /**
00103      * Transforms all characters in the given string into upper case
00104      * characters.
00105      *
00106      * \param source String to transpose.
00107      * \return A copy of the upper case only string
00108      */
00109     std::string OWCOMMON_EXPORT toUpper( const std::string& source );
00110 
00111     /**
00112      * Transforms all characters in the given string into lower case
00113      * characters.
00114      *
00115      * \param source String to transpose.
00116      * \return A copy of the lower case only string
00117      */
00118     std::string OWCOMMON_EXPORT toLower( const std::string& source );
00119 
00120     /**
00121      * Splits the given string into a vector of strings (so called tokens).
00122      *
00123      * \param source String to tokenize
00124      * \param compress If true, charactes matching between two tokens are
00125      * collapsed and handled as just one character.
00126      * \param delim String representing a set containg all characters considered
00127      * as whitespace.
00128      * \return A vector of strings containing the tokens.
00129      */
00130     std::vector< std::string > OWCOMMON_EXPORT tokenize( const std::string& source,
00131                                                        const std::string& delim = WHITESPACE,
00132                                                        bool compress = true );
00133 
00134     /**
00135      * Writes every vector to an output stream such as cout, if its elements
00136      * have an output operator defined.
00137      *
00138      * \param os The output stream where the elements are written to
00139      * \param v Vector containing the elements
00140      * \return The output stream again.
00141      */
00142     template< class T > std::ostream& operator<<( std::ostream& os, const std::vector< T >& v )
00143     {
00144         std::stringstream result;
00145         result << "[" << std::scientific << std::setprecision( 16 );
00146         std::copy( v.begin(), v.end(), std::ostream_iterator< T >( result, ", " ) );
00147         os << rTrim( result.str(), ", " ) << "]";
00148         return os;
00149     }
00150 
00151     /**
00152      * Write an input stream into the given vector. The delimiter is implicitly set to ", ".
00153      * Also wrapping brackets '[' ']' are expected. In general this is the opposite of the
00154      * output operator above.
00155      * \warning The inputstream is first written into a string then the convertion into T
00156      * via boost::lexical_cast takes place.
00157      * \warning The delimiter should not be in an elements string representation since then
00158      * the tokenizer may gets confused
00159      *
00160      * \param in Input stream
00161      * \param v Vector where to store the elements.
00162      *
00163      * \return The input stream again
00164      */
00165     template< class T > std::istream& operator>>( std::istream& in, std::vector< T >& v )
00166     {
00167         std::string str;
00168         in >> str;
00169         trim( str, "[]" ); // remove preceeding and trailing brackets '[', ']' if any
00170         std::vector< std::string > tokens = tokenize( str, ", " );
00171         v.resize( 0 ); // clear would deallocate
00172         v.reserve( tokens.size() );
00173         for( size_t i = 0; i < tokens.size(); ++i )
00174         {
00175             v.push_back( boost::lexical_cast< T >( tokens[i] ) );
00176         }
00177         return in;
00178     }
00179 
00180     /**
00181      * Writes every list to an output stream such as cout, if its elements have
00182      * an output operator defined.
00183      *
00184      * \param os The output stream where the elements are written to
00185      * \param l List containing the elements
00186      * \return The output stream again.
00187      */
00188     template< class T > std::ostream& operator<<( std::ostream& os, const std::list< T >& l )
00189     {
00190         std::stringstream result;
00191         result << "<" << std::scientific;
00192         std::copy( l.begin(), l.end(), std::ostream_iterator< T >( result, ", " ) );
00193         os << rTrim( result.str(), ", " ) << ">";
00194         return os;
00195     }
00196 
00197     /**
00198      * Writes every set to an output stream such as cout, if its elements have
00199      * an output operator defined.
00200      *
00201      * \param os The output stream where the elements are written to
00202      * \param s set containing the elements
00203      * \return The output stream again.
00204      */
00205     template< class T > std::ostream& operator<<( std::ostream& os, const std::set< T >& s )
00206     {
00207         std::stringstream result;
00208         result << "{" << std::scientific;
00209         std::copy( s.begin(), s.end(), std::ostream_iterator< T >( result, ", " ) );
00210         os << rTrim( result.str(), ", " ) << "}";
00211         return os;
00212     }
00213 }  // end of namespace
00214 
00215 #endif  // WSTRINGUTILS_H
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends