2 * Copyright (C) 2015-2017 Tim Mayberry <mojofunk@gmail.com>
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; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 #include "pbd/string_convert.h"
21 #ifndef __STDC_FORMAT_MACROS
22 #define __STDC_FORMAT_MACROS
30 #include <glib/gprintf.h>
32 #include "pbd/compose.h"
33 #include "pbd/debug.h"
36 #define DEBUG_SCONVERT(msg) DEBUG_TRACE (PBD::DEBUG::StringConvert, string_compose ("%1: %2\n", __LINE__, msg));
38 #define CONVERT_BUF_SIZE 32
42 bool string_to_bool (const std::string& str, bool& val)
47 } else if (str == X_("1")) {
51 } else if (str == X_("0")) {
55 } else if (str == X_("y")) {
59 } else if (str == X_("n")) {
63 } else if (g_ascii_strncasecmp (str.c_str(), X_("yes"), str.length()) == 0) {
67 } else if (g_ascii_strncasecmp (str.c_str(), X_("no"), str.length()) == 0) {
71 } else if (g_ascii_strncasecmp (str.c_str(), X_("true"), str.length()) == 0) {
75 } else if (g_ascii_strncasecmp (str.c_str(), X_("false"), str.length()) == 0) {
81 string_compose ("string_to_bool conversion failed for %1", str));
86 bool string_to_int16 (const std::string& str, int16_t& val)
88 if (sscanf (str.c_str (), "%" SCNi16, &val) != 1) {
90 string_compose ("string_to_int16 conversion failed for %1", str));
96 bool string_to_uint16 (const std::string& str, uint16_t& val)
98 if (sscanf (str.c_str (), "%" SCNu16, &val) != 1) {
100 string_compose ("string_to_uint16 conversion failed for %1", str));
106 bool string_to_int32 (const std::string& str, int32_t& val)
108 if (sscanf (str.c_str (), "%" SCNi32, &val) != 1) {
110 string_compose ("string_to_int32 conversion failed for %1", str));
116 bool string_to_uint32 (const std::string& str, uint32_t& val)
118 if (sscanf (str.c_str (), "%" SCNu32, &val) != 1) {
120 string_compose ("string_to_uint32 conversion failed for %1", str));
126 bool string_to_int64 (const std::string& str, int64_t& val)
128 if (sscanf (str.c_str (), "%" SCNi64, &val) != 1) {
130 string_compose ("string_to_int64 conversion failed for %1", str));
136 bool string_to_uint64 (const std::string& str, uint64_t& val)
138 if (sscanf (str.c_str (), "%" SCNu64, &val) != 1) {
140 string_compose ("string_to_uint64 conversion failed for %1", str));
146 template <class FloatType>
148 _string_to_infinity (const std::string& str, FloatType& val)
150 if (!g_ascii_strncasecmp (str.c_str (), X_ ("inf"), str.length ()) ||
151 !g_ascii_strncasecmp (str.c_str (), X_ ("+inf"), str.length ()) ||
152 !g_ascii_strncasecmp (str.c_str (), X_ ("INFINITY"), str.length ()) ||
153 !g_ascii_strncasecmp (str.c_str (), X_ ("+INFINITY"), str.length ())) {
154 val = std::numeric_limits<FloatType>::infinity ();
156 } else if (!g_ascii_strncasecmp (str.c_str (), X_ ("-inf"), str.length ()) ||
157 !g_ascii_strncasecmp (str.c_str (), X_ ("-INFINITY"), str.length ())) {
158 val = -std::numeric_limits<FloatType>::infinity ();
165 _string_to_double (const std::string& str, double& val)
167 val = g_ascii_strtod (str.c_str (), NULL);
169 // It is possible that the conversion was successful and another thread
170 // has set errno meanwhile but as most conversions are currently not
171 // checked for error conditions this is better than nothing.
172 if (errno == ERANGE) {
173 DEBUG_SCONVERT (string_compose ("string_to_double possible conversion failure for %1", str));
174 // There should not be any conversion failures as we control the string
175 // contents so returning false here should not have any impact...
181 bool string_to_float (const std::string& str, float& val)
184 if (_string_to_double (str, tmp)) {
189 if (_string_to_infinity (str, val)) {
196 bool string_to_double (const std::string& str, double& val)
198 if (_string_to_double (str, val)) {
202 if (_string_to_infinity (str, val)) {
209 bool bool_to_string (bool val, std::string& str)
219 bool int16_to_string (int16_t val, std::string& str)
221 char buffer[CONVERT_BUF_SIZE];
223 int retval = g_snprintf (buffer, sizeof(buffer), "%" PRIi16, val);
225 if (retval <= 0 || retval >= (int)sizeof(buffer)) {
227 string_compose ("int16_to_string conversion failure for %1", val));
234 bool uint16_to_string (uint16_t val, std::string& str)
236 char buffer[CONVERT_BUF_SIZE];
238 int retval = g_snprintf (buffer, sizeof(buffer), "%" PRIu16, val);
240 if (retval <= 0 || retval >= (int)sizeof(buffer)) {
242 string_compose ("uint16_to_string conversion failure for %1", val));
249 bool int32_to_string (int32_t val, std::string& str)
251 char buffer[CONVERT_BUF_SIZE];
253 int retval = g_snprintf (buffer, sizeof(buffer), "%" PRIi32, val);
255 if (retval <= 0 || retval >= (int)sizeof(buffer)) {
257 string_compose ("int32_to_string conversion failure for %1", val));
264 bool uint32_to_string (uint32_t val, std::string& str)
266 char buffer[CONVERT_BUF_SIZE];
268 int retval = g_snprintf (buffer, sizeof(buffer), "%" PRIu32, val);
270 if (retval <= 0 || retval >= (int)sizeof(buffer)) {
272 string_compose ("uint32_to_string conversion failure for %1", val));
279 bool int64_to_string (int64_t val, std::string& str)
281 char buffer[CONVERT_BUF_SIZE];
283 int retval = g_snprintf (buffer, sizeof(buffer), "%" PRIi64, val);
285 if (retval <= 0 || retval >= (int)sizeof(buffer)) {
287 string_compose ("int64_to_string conversion failure for %1", val));
294 bool uint64_to_string (uint64_t val, std::string& str)
296 char buffer[CONVERT_BUF_SIZE];
298 int retval = g_snprintf (buffer, sizeof(buffer), "%" PRIu64, val);
300 if (retval <= 0 || retval >= (int)sizeof(buffer)) {
302 string_compose ("uint64_to_string conversion failure for %1", val));
309 template <class FloatType>
311 _infinity_to_string (FloatType val, std::string& str)
313 if (val == std::numeric_limits<FloatType>::infinity ()) {
316 } else if (val == -std::numeric_limits<FloatType>::infinity ()) {
324 _double_to_string (double val, std::string& str)
326 char buffer[G_ASCII_DTOSTR_BUF_SIZE];
328 char* d_cstr = g_ascii_dtostr (buffer, sizeof(buffer), val);
330 if (d_cstr == NULL) {
337 bool float_to_string (float val, std::string& str)
339 if (_infinity_to_string (val, str)) {
343 if (_double_to_string (val, str)) {
347 DEBUG_SCONVERT (string_compose ("float_to_string conversion failure for %1", val));
351 bool double_to_string (double val, std::string& str)
353 if (_infinity_to_string (val, str)) {
357 if (_double_to_string (val, str)) {
361 DEBUG_SCONVERT (string_compose ("double_to_string conversion failure for %1", val));