summaryrefslogtreecommitdiff
path: root/libs/pbd/string_convert.cc
blob: a543c96ff1d464f4b9a1333a8e8378e034067bd4 (plain)
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
/*
    Copyright (C) 2015 Tim Mayberry

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program 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 General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

*/

#include "pbd/string_convert.h"

#ifndef __STDC_FORMAT_MACROS
#define __STDC_FORMAT_MACROS
#endif
#include <inttypes.h>
#include <cerrno>
#include <cstdio>
#include <limits>

#include <glib.h>
#include <glib/gprintf.h>

#include "pbd/compose.h"
#include "pbd/debug.h"
#include "pbd/i18n.h"

#define DEBUG_SCONVERT(msg) DEBUG_TRACE (PBD::DEBUG::StringConvert, string_compose ("%1: %2\n", __LINE__, msg));

#define CONVERT_BUF_SIZE 32

namespace PBD {

bool string_to_bool (const std::string& str, bool& val)
{
	if (str.empty ()) {
		return false;

	} else if (str == X_("1")) {
		val = true;
		return true;

	} else if (str == X_("0")) {
		val = false;
		return true;

	} else if (str == X_("y")) {
		val = true;
		return true;

	} else if (str == X_("n")) {
		val = false;
		return true;

	} else if (g_ascii_strncasecmp (str.c_str(), X_("yes"), str.length()) == 0) {
		val = true;
		return true;

	} else if (g_ascii_strncasecmp (str.c_str(), X_("no"), str.length()) == 0) {
		val = false;
		return true;

	} else if (g_ascii_strncasecmp (str.c_str(), X_("true"), str.length()) == 0) {
		val = true;
		return true;

	} else if (g_ascii_strncasecmp (str.c_str(), X_("false"), str.length()) == 0) {
		val = false;
		return true;
	}

	DEBUG_SCONVERT (
	    string_compose ("string_to_bool conversion failed for %1", str));

	return false;
}

bool string_to_int16 (const std::string& str, int16_t& val)
{
	if (sscanf (str.c_str (), "%" SCNi16, &val) != 1) {
		DEBUG_SCONVERT (
		    string_compose ("string_to_int16 conversion failed for %1", str));
		return false;
	}
	return true;
}

bool string_to_uint16 (const std::string& str, uint16_t& val)
{
	if (sscanf (str.c_str (), "%" SCNu16, &val) != 1) {
		DEBUG_SCONVERT (
		    string_compose ("string_to_uint16 conversion failed for %1", str));
		return false;
	}
	return true;
}

bool string_to_int32 (const std::string& str, int32_t& val)
{
	if (sscanf (str.c_str (), "%" SCNi32, &val) != 1) {
		DEBUG_SCONVERT (
		    string_compose ("string_to_int32 conversion failed for %1", str));
		return false;
	}
	return true;
}

bool string_to_uint32 (const std::string& str, uint32_t& val)
{
	if (sscanf (str.c_str (), "%" SCNu32, &val) != 1) {
		DEBUG_SCONVERT (
		    string_compose ("string_to_uint32 conversion failed for %1", str));
		return false;
	}
	return true;
}

bool string_to_int64 (const std::string& str, int64_t& val)
{
	if (sscanf (str.c_str (), "%" SCNi64, &val) != 1) {
		DEBUG_SCONVERT (
		    string_compose ("string_to_int64 conversion failed for %1", str));
		return false;
	}
	return true;
}

bool string_to_uint64 (const std::string& str, uint64_t& val)
{
	if (sscanf (str.c_str (), "%" SCNu64, &val) != 1) {
		DEBUG_SCONVERT (
		    string_compose ("string_to_uint64 conversion failed for %1", str));
		return false;
	}
	return true;
}

template <class FloatType>
static bool
_string_to_infinity (const std::string& str, FloatType& val)
{
	if (!g_ascii_strncasecmp (str.c_str (), X_ ("inf"), str.length ()) ||
	    !g_ascii_strncasecmp (str.c_str (), X_ ("+inf"), str.length ()) ||
	    !g_ascii_strncasecmp (str.c_str (), X_ ("INFINITY"), str.length ()) ||
	    !g_ascii_strncasecmp (str.c_str (), X_ ("+INFINITY"), str.length ())) {
		val = std::numeric_limits<FloatType>::infinity ();
		return true;
	} else if (!g_ascii_strncasecmp (str.c_str (), X_ ("-inf"), str.length ()) ||
	           !g_ascii_strncasecmp (str.c_str (), X_ ("-INFINITY"), str.length ())) {
		val = -std::numeric_limits<FloatType>::infinity ();
		return true;
	}
	return false;
}

bool
_string_to_double (const std::string& str, double& val)
{
	val = g_ascii_strtod (str.c_str (), NULL);

	// It is possible that the conversion was successful and another thread
	// has set errno meanwhile but as most conversions are currently not
	// checked for error conditions this is better than nothing.
	if (errno == ERANGE) {
		DEBUG_SCONVERT (string_compose ("string_to_double possible conversion failure for %1", str));
		// There should not be any conversion failures as we control the string
		// contents so returning false here should not have any impact...
		return false;
	}
	return true;
}

bool string_to_float (const std::string& str, float& val)
{
	double tmp;
	if (_string_to_double (str, tmp)) {
		val = (float)tmp;
		return true;
	}

	if (_string_to_infinity (str, val)) {
		return true;
	}

	return false;
}

bool string_to_double (const std::string& str, double& val)
{
	if (_string_to_double (str, val)) {
		return true;
	}

	if (_string_to_infinity (str, val)) {
		return true;
	}

	return false;
}

bool bool_to_string (bool val, std::string& str)
{
	if (val) {
		str = X_("1");
	} else {
		str = X_("0");
	}
	return true;
}

bool int16_to_string (int16_t val, std::string& str)
{
	char buffer[CONVERT_BUF_SIZE];

	int retval = g_snprintf (buffer, sizeof(buffer), "%" PRIi16, val);

	if (retval <= 0 || retval >= (int)sizeof(buffer)) {
		DEBUG_SCONVERT (
		    string_compose ("int16_to_string conversion failure for %1", val));
		return false;
	}
	str = buffer;
	return true;
}

bool uint16_to_string (uint16_t val, std::string& str)
{
	char buffer[CONVERT_BUF_SIZE];

	int retval = g_snprintf (buffer, sizeof(buffer), "%" PRIu16, val);

	if (retval <= 0 || retval >= (int)sizeof(buffer)) {
		DEBUG_SCONVERT (
		    string_compose ("uint16_to_string conversion failure for %1", val));
		return false;
	}
	str = buffer;
	return true;
}

bool int32_to_string (int32_t val, std::string& str)
{
	char buffer[CONVERT_BUF_SIZE];

	int retval = g_snprintf (buffer, sizeof(buffer), "%" PRIi32, val);

	if (retval <= 0 || retval >= (int)sizeof(buffer)) {
		DEBUG_SCONVERT (
		    string_compose ("int32_to_string conversion failure for %1", val));
		return false;
	}
	str = buffer;
	return true;
}

bool uint32_to_string (uint32_t val, std::string& str)
{
	char buffer[CONVERT_BUF_SIZE];

	int retval = g_snprintf (buffer, sizeof(buffer), "%" PRIu32, val);

	if (retval <= 0 || retval >= (int)sizeof(buffer)) {
		DEBUG_SCONVERT (
		    string_compose ("uint32_to_string conversion failure for %1", val));
		return false;
	}
	str = buffer;
	return true;
}

bool int64_to_string (int64_t val, std::string& str)
{
	char buffer[CONVERT_BUF_SIZE];

	int retval = g_snprintf (buffer, sizeof(buffer), "%" PRIi64, val);

	if (retval <= 0 || retval >= (int)sizeof(buffer)) {
		DEBUG_SCONVERT (
		    string_compose ("int64_to_string conversion failure for %1", val));
		return false;
	}
	str = buffer;
	return true;
}

bool uint64_to_string (uint64_t val, std::string& str)
{
	char buffer[CONVERT_BUF_SIZE];

	int retval = g_snprintf (buffer, sizeof(buffer), "%" PRIu64, val);

	if (retval <= 0 || retval >= (int)sizeof(buffer)) {
		DEBUG_SCONVERT (
		    string_compose ("uint64_to_string conversion failure for %1", val));
		return false;
	}
	str = buffer;
	return true;
}

template <class FloatType>
static bool
_infinity_to_string (FloatType val, std::string& str)
{
	if (val == std::numeric_limits<FloatType>::infinity ()) {
		str = "inf";
		return true;
	} else if (val == -std::numeric_limits<FloatType>::infinity ()) {
		str = "-inf";
		return true;
	}
	return false;
}

static bool
_double_to_string (double val, std::string& str)
{
	char buffer[G_ASCII_DTOSTR_BUF_SIZE];

	char* d_cstr = g_ascii_dtostr (buffer, sizeof(buffer), val);

	if (d_cstr == NULL) {
		return false;
	}
	str = d_cstr;
	return true;
}

bool float_to_string (float val, std::string& str)
{
	if (_infinity_to_string (val, str)) {
		return true;
	}

	if (_double_to_string (val, str)) {
		return true;
	}

	DEBUG_SCONVERT (string_compose ("float_to_string conversion failure for %1", val));
	return false;
}

bool double_to_string (double val, std::string& str)
{
	if (_infinity_to_string (val, str)) {
		return true;
	}

	if (_double_to_string (val, str)) {
		return true;
	}

	DEBUG_SCONVERT (string_compose ("double_to_string conversion failure for %1", val));
	return false;
}

} // namespace PBD