OpenWalnut  1.2.5
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
WStringUtils.h
1 //---------------------------------------------------------------------------
2 //
3 // Project: OpenWalnut ( http://www.openwalnut.org )
4 //
5 // Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
6 // For more information see http://www.openwalnut.org/copying
7 //
8 // This file is part of OpenWalnut.
9 //
10 // OpenWalnut is free software: you can redistribute it and/or modify
11 // it under the terms of the GNU Lesser General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // OpenWalnut is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public License
21 // along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
22 //
23 //---------------------------------------------------------------------------
24 
25 #ifndef WSTRINGUTILS_H
26 #define WSTRINGUTILS_H
27 
28 #include <algorithm>
29 #include <iostream>
30 #include <iomanip>
31 #include <iterator>
32 #include <list>
33 #include <set>
34 #include <sstream>
35 #include <string>
36 #include <vector>
37 
38 #include <boost/lexical_cast.hpp>
39 
40 #include "WExportCommon.h"
41 
42 /**
43  * Some utilities for string manipulation and output operations. Please note
44  * that the overloaded ostream output operators aren't in a separate namespace
45  * but the string manipulation functions. This is because of short use of e.g.
46  * the <tt><<</tt> operator instead of <tt>string_utils::operator( cout,
47  * myVector)</tt>.
48  *
49  * The reason for not using the Boost trimming functions is, that Boost
50  * providing just Whitespace trimming depending on the current locale, but we
51  * might want to trim other character sets too.
52  *
53  * The reason for not using the Boost case switching functions is that we want
54  * those functions to return a <tt>std::string</tt> copy which is modified to
55  * make some call chains ala: <tt>foo( rTrim( toLower( str ), "bar" ) );</tt>.
56  *
57  * The reason for not using Boosts Tokenizer is, that this tokenizer, is much
58  * most simplest to use :).
59  */
60 namespace string_utils
61 {
62  /** We consider the following characters as whitespace:
63  * - <tt>\\r</tt> carriage return
64  * - <tt>\\n</tt> newline
65  * - <tt>\\t</tt> tab
66  * - <tt>' '</tt> space
67  */
68  extern OWCOMMON_EXPORT const std::string WHITESPACE;
69 
70  /**
71  * Trims any occurence of each character given in parameter t from the end
72  * (or right side) of the given string.
73  *
74  * \param source String to trim
75  * \param t String representing a set containg all trimmable characters
76  * \return A copy of the trimmed string
77  */
78 
79  std::string OWCOMMON_EXPORT rTrim( const std::string& source, const std::string& t = WHITESPACE );
80 
81  /**
82  * Trims any occurence of each character given in parameter t from the
83  * start (or left side) of the given string.
84  *
85  * \param source String to trim
86  * \param t String representing a set containg all trimmable characters
87  * \return A copy of the trimmed string
88  */
89  std::string OWCOMMON_EXPORT lTrim( const std::string& source, const std::string& t =
90  WHITESPACE );
91 
92  /**
93  * Trims any occurence of each character given in parameter t from both
94  * ends (right and left side) of the given string.
95  *
96  * \param source String to trim
97  * \param t String representing a set containg all trimmable characters
98  * \return A copy of the trimmed string
99  */
100  std::string OWCOMMON_EXPORT trim( const std::string& source, const std::string& t = WHITESPACE );
101 
102  /**
103  * Transforms all characters in the given string into upper case
104  * characters.
105  *
106  * \param source String to transpose.
107  * \return A copy of the upper case only string
108  */
109  std::string OWCOMMON_EXPORT toUpper( const std::string& source );
110 
111  /**
112  * Transforms all characters in the given string into lower case
113  * characters.
114  *
115  * \param source String to transpose.
116  * \return A copy of the lower case only string
117  */
118  std::string OWCOMMON_EXPORT toLower( const std::string& source );
119 
120  /**
121  * Splits the given string into a vector of strings (so called tokens).
122  *
123  * \param source String to tokenize
124  * \param compress If true, charactes matching between two tokens are
125  * collapsed and handled as just one character.
126  * \param delim String representing a set containg all characters considered
127  * as whitespace.
128  * \return A vector of strings containing the tokens.
129  */
130  std::vector< std::string > OWCOMMON_EXPORT tokenize( const std::string& source,
131  const std::string& delim = WHITESPACE,
132  bool compress = true );
133 
134  /**
135  * Writes every vector to an output stream such as cout, if its elements
136  * have an output operator defined.
137  *
138  * \param os The output stream where the elements are written to
139  * \param v Vector containing the elements
140  * \return The output stream again.
141  */
142  template< class T > std::ostream& operator<<( std::ostream& os, const std::vector< T >& v )
143  {
144  std::stringstream result;
145  result << "[" << std::scientific << std::setprecision( 16 );
146  std::copy( v.begin(), v.end(), std::ostream_iterator< T >( result, ", " ) );
147  os << rTrim( result.str(), ", " ) << "]";
148  return os;
149  }
150 
151  /**
152  * Write an input stream into the given vector. The delimiter is implicitly set to ", ".
153  * Also wrapping brackets '[' ']' are expected. In general this is the opposite of the
154  * output operator above.
155  * \warning The inputstream is first written into a string then the convertion into T
156  * via boost::lexical_cast takes place.
157  * \warning The delimiter should not be in an elements string representation since then
158  * the tokenizer may gets confused
159  *
160  * \param in Input stream
161  * \param v Vector where to store the elements.
162  *
163  * \return The input stream again
164  */
165  template< class T > std::istream& operator>>( std::istream& in, std::vector< T >& v )
166  {
167  std::string str;
168  in >> str;
169  trim( str, "[]" ); // remove preceeding and trailing brackets '[', ']' if any
170  std::vector< std::string > tokens = tokenize( str, ", " );
171  v.resize( 0 ); // clear would deallocate
172  v.reserve( tokens.size() );
173  for( size_t i = 0; i < tokens.size(); ++i )
174  {
175  v.push_back( boost::lexical_cast< T >( tokens[i] ) );
176  }
177  return in;
178  }
179 
180  /**
181  * Writes every list to an output stream such as cout, if its elements have
182  * an output operator defined.
183  *
184  * \param os The output stream where the elements are written to
185  * \param l List containing the elements
186  * \return The output stream again.
187  */
188  template< class T > std::ostream& operator<<( std::ostream& os, const std::list< T >& l )
189  {
190  std::stringstream result;
191  result << "<" << std::scientific;
192  std::copy( l.begin(), l.end(), std::ostream_iterator< T >( result, ", " ) );
193  os << rTrim( result.str(), ", " ) << ">";
194  return os;
195  }
196 
197  /**
198  * Writes every set to an output stream such as cout, if its elements have
199  * an output operator defined.
200  *
201  * \param os The output stream where the elements are written to
202  * \param s set containing the elements
203  * \return The output stream again.
204  */
205  template< class T > std::ostream& operator<<( std::ostream& os, const std::set< T >& s )
206  {
207  std::stringstream result;
208  result << "{" << std::scientific;
209  std::copy( s.begin(), s.end(), std::ostream_iterator< T >( result, ", " ) );
210  os << rTrim( result.str(), ", " ) << "}";
211  return os;
212  }
213 } // end of namespace
214 
215 #endif // WSTRINGUTILS_H