2
Copyright (c) 2010, 2012, Oracle and/or its affiliates. All rights reserved.
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.
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.
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
23
#ifndef string_helpers_hpp
24
#define string_helpers_hpp
34
//using namespace std;
41
using std::wistringstream;
44
/************************************************************
45
* String Helper Functions
46
************************************************************/
49
* Returns the boolean value of a wide character string.
51
* Returns true if the argument is equal, ignoring case, to the string
52
* "true"; otherwise, false.
55
toBool(const wstring& ws, bool vdefault)
57
// can't get manipulators to compile
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);
66
// this works but isn't worth the overhead:
69
//std::transform(ws.begin(), ws.end(), t.begin(),
70
// static_cast< int (*)(int) >(std::tolower));
73
if (ws.length() == 0) {
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'));
87
* Parses a wide character string as a (signed) decimal integer.
89
* Returns the integral value of a wide character string, the default value
90
* if the string is empty, or the error value if the conversion has failed.
92
template< typename I >
94
toI(const wstring& ws, I vdefault, I verror)
97
if (ws.length() == 0) {
100
wistringstream wiss(ws);
102
if (wiss.fail() || !wiss.eof()) {
110
toInt(const wstring& ws, int vdefault, int verror)
112
return toI< int >(ws, vdefault, verror);
116
* Returns the character representation of an int.
121
// JNI crashes with gcc & operator<<(ostream &, long/int)
122
//std::ostringstream o;
126
snprintf(s, 256, "%d", i);
131
* Returns a multi-byte representation of a wide-character string.
133
* XXX document semantics since this conversion ignores any
134
* character set encodings...
135
* This function is not very efficient in that it involves multiple
136
* copying operations.
139
toString(const wstring& ws)
141
//sprintf(charptr,"%ls",wsdtring.c_str());
142
string s(ws.begin(), ws.end());
147
* Returns a string representation of all elements in the set.
149
* This function is not very efficient in that it involves multiple
150
* copying operations.
153
toString(const set< string >& s)
157
set< string >::iterator i = s.begin();
162
while (++i != s.end()) {
174
#endif // string_helpers_hpp