fintp_utils
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
StringUtil.h
Go to the documentation of this file.
1 /*
2 * FinTP - Financial Transactions Processing Application
3 * Copyright (C) 2013 Business Information Systems (Allevo) S.R.L.
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>
17 * or contact Allevo at : 031281 Bucuresti, 23C Calea Vitan, Romania,
18 * phone +40212554577, office@allevo.ro <mailto:office@allevo.ro>, www.allevo.ro.
19 */
20 
21 #ifndef STRINGUTIL_H
22 #define STRINGUTIL_H
23 
24 #include "Collections.h"
25 #include <string>
26 #include <limits>
27 #include <boost/lexical_cast.hpp>
28 #include <boost/algorithm/string.hpp>
29 
30 using namespace std;
31 
32 namespace FinTP
33 {
35  {
36  private :
37 
38  string m_Separator;
39  string m_Data;
40 
41  string::size_type m_SeparatorIndex;
42 
43  inline static bool nocase_compare( char c1, char c2 );
44  //inline static char to_upper( char c1 );
45 
46  public:
47 
48  explicit StringUtil( const string& data = "" ) : m_Separator( " " ), m_Data( data ), m_SeparatorIndex( 0 ) {};
49 
50  void Split( const string& separator );
51  string NextToken();
52  bool MoreTokens() const;
53 
54 
55  string Trim() const;
56  static string Trim( const string& source );
57 
58  // conversion functions
59  static inline long ParseLong( const string& source ) { return strtol( source.c_str(), NULL, 10 ); }
60  static inline unsigned long ParseULong( const string& source )
61  {
62  unsigned long x = strtoul( source.c_str(), NULL, 10 );
63  if ( source[ 0 ] == '-' )
64  throw boost::bad_lexical_cast( typeid( source ), typeid( x ) );
65  return x;
66  }
67 
68  static inline int ParseInt( const string& source ) { return atoi( source.c_str() ); }
69  static inline unsigned int ParseUInt( const string& source )
70  {
71  unsigned int x = ( unsigned int )strtoul( source.c_str(), NULL, 10 );
72  if ( source[ 0 ] == '-' )
73  throw boost::bad_lexical_cast( typeid( source ), typeid( x ) );
74  return x;
75  }
76 
77  static inline short ParseShort( const string& source )
78  {
79  int x = atoi( source.c_str() );
80  if ( ( x < SHRT_MIN ) || ( x > SHRT_MAX ) )
81  throw boost::bad_lexical_cast( typeid( source ), typeid( x ) );
82  return ( short )x;
83  }
84  static inline float ParseFloat( const string& source ) { return ( float )atof( source.c_str() ); }
85  static inline double ParseDouble( const string& source ){ return strtod( source.c_str(), NULL ); }
86 
87  template < class T >
88  static inline string ToString( const T value ) { return boost::lexical_cast< string >( value ); }
89 
90  static string FindBetween( const string& value, const string& first, const string& last );
91  static string Pad( const string& value, const string& padleft, const string& padright );
92  static string Replace( const string& value, const string& what, const string& with );
93  static bool StartsWith( const string& value, const string& with );
94 
95  static string::size_type CaseInsensitiveFind( const string& first, const string& second );
96  static string ToUpper( const string& input );
97  static string ToLower( const string& input );
98 
99  static bool IsAlphaNumeric( const string& input );
100 
101  static string DeserializeFromFile( const string& filename );
102  static void SerializeToFile( const string& filename, const string& content );
103  };
104 }
105 
106 #endif // STRINGUTIL_H