1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
|
#ifndef MISC_STRING_H
#define MISC_STRING_H
/*
GLT OpenGL C++ Toolkit (LGPL)
Copyright (C) 2000-2002 Nigel Stewart
Email: nigels@nigels.com
WWW: http://www.nigels.com/glt/
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/*! \file
\brief string and wstring utility routines
\author Nigel Stewart, RMIT (nigels@nigels.com)
\ingroup Misc
\todo Implement utf8encode
\note The std::wstring class is for 16bit unicode text
*/
#include "glt_config.h"
#ifdef GLT_WIN32
#pragma warning(disable: 4786)
#endif
#include <string>
#include <vector>
#include <iosfwd>
#ifdef GLT_UNIX
namespace std
{
typedef basic_string <wchar_t> wstring;
}
#endif
/*!
\brief Test a string for binary bytes
\ingroup Misc
*/
bool isBinary(const std::string &str);
//
// DOS <-> UNIX
//
/*!
\brief Convert text to Unix end-of-line convention
\ingroup Misc
*/
void dos2unix(std::string &dest,const std::string &src);
/*!
\brief Convert wide text to Unix end-of-line convention
\ingroup Misc
*/
void dos2unix(std::wstring &dest,const std::wstring &src);
/*!
\brief Convert text to DOS end-of-line convention
\ingroup Misc
*/
void unix2dos(std::string &dest,const std::string &src);
/*!
\brief Convert wide text to DOS end-of-line convention
\ingroup Misc
*/
void unix2dos(std::wstring &dest,const std::wstring &src);
//
// string <-> stream
//
/*!
\brief Read an input stream into a string, until EOF
\ingroup Misc
*/
void readStream (std::istream &is,std::string &dest);
/*!
\brief Write a string to an output stream
\ingroup Misc
*/
void writeStream(std::ostream &os,const std::string &src);
/*!
\brief Read an input stream into a wide string, until EOF
\ingroup Misc
*/
void readUnicodeStream (std::istream &is,std::wstring &dest);
/*!
\brief Write a wide string to an output stream
\ingroup Misc
*/
void writeUnicodeStream(std::ostream &os,const std::wstring &src);
//
// string <-> wstring
//
/*!
\brief Convert a string to a wide string
\ingroup Misc
*/
void string2wstring(std::wstring &dest, const std::string &src);
/*!
\brief Convert a wide string to a string
\ingroup Misc
*/
void wstring2string(std::string &dest, const std::wstring &src);
/*!
\brief Convert a utf8 encoded string to a wide string
\ingroup Misc
*/
void utf8decode(std::wstring &dest, const std::string &src);
//
// string -> C/C++ source
//
/*!
\brief Output a memory buffer as C/C++ source
\ingroup Misc
*/
void bin2src(std::ostream &os, const unsigned char *buffer, const int n);
/*!
\brief Output a binary string as C/C++ source
\ingroup Misc
*/
void bin2src(std::ostream &os, const std::string &src);
/*!
\brief Encode binary input stream as C/C++ source, until EOF
\ingroup Misc
*/
void bin2src(std::ostream &os, std::istream &is);
//
// string -> gcc assembler
//
/*!
\brief Output a memory buffer as gcc assembler
\ingroup Misc
*/
void bin2asm(std::ostream &os, const unsigned char *buffer, const int n);
/*!
\brief Output a string as gcc assembler
\ingroup Misc
*/
void bin2asm(std::ostream &os, const std::string &src);
/*!
\brief Encode input stream as gcc assembler
\ingroup Misc
*/
void bin2asm(std::ostream &os, std::istream &is);
//
// hex conversion
//
/*!
\brief Convert from ASCII character to binary
\ingroup Misc
*/
unsigned int fromHex4(unsigned char ch);
/*!
\brief Convert from binary to ASCII character
\ingroup Misc
*/
unsigned char toHex4(unsigned int val);
//
// string <-> vector<string>
//
/*!
\brief Split a string, using a delimiter
\ingroup Misc
*/
bool stringSplit(std::vector<std::string> &vec, const std::string &str, const std::string &delim);
/*!
\brief Merge a vector of strings, inserting a delimiter
\ingroup Misc
*/
bool stringMerge(const std::vector<std::string> &vec, std::string &str, const std::string &delim);
//
// Adaptors for ANSI C functions
//
/*!
\brief std::string version of ANSI C atof()
\ingroup Misc
*/
double atof(const std::string &str);
/*!
\brief std::string version of ANSI C atoi()
\ingroup Misc
*/
int atoi(const std::string &str);
/*!
\brief std::string version of ANSI C atol()
\ingroup Misc
*/
long atol(const std::string &str);
/*!
\brief std::string to bool, inspired by ANSI C atoi()
\ingroup Misc
*/
bool atob(const std::string &str);
/*!
\brief Generalised parsing of containers
\param str string to be parsed
\param f string to element parsing function. atof, atoi etc..
\param match characters the form an element. "+-eE.0123456789"
\param begin iterator to first container element
\param end iterator to last+1 container element
\ingroup Misc
*/
template<class T,class I>
const int atoc(const std::string &str,T (*f)(const std::string &),const std::string &match,const I &begin,const I &end)
{
int n = 0;
I i = begin;
size_t j = 0;
while (i!=end)
{
j = str.find_first_of(match,j);
if (j==std::string::npos)
break;
*i = f(str.substr(j));
n++;
i++;
j = str.find_first_not_of(match,j);
if (j==std::string::npos)
break;
}
for (;i!=end;i++)
*i = T();
return n;
}
#endif
|