~ubuntu-branches/ubuntu/trusty/mysql-5.6/trusty

« back to all changes in this revision

Viewing changes to storage/ndb/test/crund/martins_little_helpers/src/utils/string_helpers.hpp

  • Committer: Package Import Robot
  • Author(s): James Page
  • Date: 2014-02-12 11:54:27 UTC
  • Revision ID: package-import@ubuntu.com-20140212115427-oq6tfsqxl1wuwehi
Tags: upstream-5.6.15
ImportĀ upstreamĀ versionĀ 5.6.15

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
   Copyright (c) 2010, 2012, 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
 
 
48
/**
 
49
 * Returns the boolean value of a wide character string.
 
50
 *
 
51
 * Returns true if the argument is equal, ignoring case, to the string
 
52
 * "true"; otherwise, false.
 
53
 */
 
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
 
 
86
/**
 
87
 * Parses a wide character string as a (signed) decimal integer.
 
88
 *
 
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.
 
91
 */
 
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
 
 
115
/**
 
116
 * Returns the character representation of an int.
 
117
 */
 
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
 
 
130
/**
 
131
 * Returns a multi-byte representation of a wide-character string.
 
132
 * 
 
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.
 
137
 */
 
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
 
 
146
/**
 
147
 * Returns a string representation of all elements in the set.
 
148
 * 
 
149
 * This function is not very efficient in that it involves multiple
 
150
 * copying operations.
 
151
 */
 
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