MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
string_helpers.hpp
1 /*
2  Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
3 
4  This program is free software; you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation; version 2 of the License.
7 
8  This program is distributed in the hope that it will be useful,
9  but WITHOUT ANY WARRANTY; without even the implied warranty of
10  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  GNU General Public License for more details.
12 
13  You should have received a copy of the GNU General Public License
14  along with this program; if not, write to the Free Software
15  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
16 */
17 
18 /*
19  * string_helpers.hpp
20  *
21  */
22 
23 #ifndef string_helpers_hpp
24 #define string_helpers_hpp
25 
26 //#include <cstdio>
27 #include <iostream>
28 #include <sstream>
29 #include <string>
30 #include <set>
31 
32 namespace utils {
33 
34 //using namespace std;
35 //using std::cout;
36 //using std::endl;
37 //using std::flush;
38 using std::string;
39 using std::wstring;
40 using std::ostream;
41 using std::wistringstream;
42 using std::set;
43 
44 /************************************************************
45  * String Helper Functions
46  ************************************************************/
47 
54 inline bool
55 toBool(const wstring& ws, bool vdefault)
56 {
57  // can't get manipulators to compile
58  //bool r;
59  //wistringstream(ws) >> ios_base::boolalpha >> r;
60  //wistringstream(ws) >> setiosflags(ios_base::boolalpha) >> r;
61  // but even if compiling, this seems to return true for empty strings:
62  //wistringstream wss(ws);
63  //wss.flags(ios_base::boolalpha);
64  //wss >> r;
65 
66  // this works but isn't worth the overhead:
67  //#include <cctype>
68  //wstring t;
69  //std::transform(ws.begin(), ws.end(), t.begin(),
70  // static_cast< int (*)(int) >(std::tolower));
71 
72  bool val;
73  if (ws.length() == 0) {
74  val = vdefault;
75  } else {
76  // short & simple
77  val = ((ws.length() == 4)
78  && (ws[0] == L'T' || ws[0] == L't')
79  && (ws[1] == L'R' || ws[1] == L'r')
80  && (ws[2] == L'U' || ws[2] == L'u')
81  && (ws[3] == L'E' || ws[3] == L'e'));
82  }
83  return val;
84 }
85 
92 template< typename I >
93 inline I
94 toI(const wstring& ws, I vdefault, I verror)
95 {
96  I val;
97  if (ws.length() == 0) {
98  val = vdefault;
99  } else {
100  wistringstream wiss(ws);
101  wiss >> val;
102  if (wiss.fail() || !wiss.eof()) {
103  val = verror;
104  }
105  }
106  return val;
107 }
108 
109 inline int
110 toInt(const wstring& ws, int vdefault, int verror)
111 {
112  return toI< int >(ws, vdefault, verror);
113 }
114 
118 inline string
119 toString(int i)
120 {
121  // JNI crashes with gcc & operator<<(ostream &, long/int)
122  //std::ostringstream o;
123  //o << i;
124  //return o.str();
125  char s[256];
126  snprintf(s, 256, "%d", i);
127  return string(s);
128 }
129 
138 inline string
139 toString(const wstring& ws)
140 {
141  //sprintf(charptr,"%ls",wsdtring.c_str());
142  string s(ws.begin(), ws.end());
143  return s;
144 }
145 
152 inline string
153 toString(const set< string >& s)
154 {
155  string r;
156  r += "[";
157  set< string >::iterator i = s.begin();
158  if (i != s.end()) {
159  r += "\"";
160  r += *i;
161  r += "\"";
162  while (++i != s.end()) {
163  r += ", \"";
164  r += *i;
165  r += "\"";
166  }
167  }
168  r += "]";
169  return r;
170 }
171 
172 } // utils
173 
174 #endif // string_helpers_hpp