blob: b4b00b8d2de2922b47ae24f1ec9c48b5d029db18 [file] [log] [blame]
Jeongik Cha1199caa2021-07-20 12:32:58 +09001/* ----------------------------------------------------------------------------
2 libconfig - A library for processing structured configuration files
3 Copyright (C) 2005-2018 Mark A Lindner
4
5 This file is part of libconfig.
6
7 This library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public License
9 as published by the Free Software Foundation; either version 2.1 of
10 the License, or (at your option) any later version.
11
12 This library is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Library General Public
18 License along with this library; if not, see
19 <http://www.gnu.org/licenses/>.
20 ----------------------------------------------------------------------------
21*/
22
23#include "util.h"
24#include "wincompat.h"
25
26#include <errno.h>
27#include <stdio.h>
28#include <stdlib.h>
29#include <string.h>
30
31/* ------------------------------------------------------------------------- */
32
33long long libconfig_parse_integer(const char *s, int *ok)
34{
35 long long llval;
36 char *endptr;
37 int errsave = errno;
38 errno = 0;
39 llval = strtoll(s, &endptr, 0); /* base 10 or base 8 */
40 if(*endptr || errno)
41 {
42 errno = 0;
43 *ok = 0;
44 return(0); /* parse error */
45 }
46 errno = errsave;
47
48 *ok = 1;
49 return(llval);
50}
51
52/* ------------------------------------------------------------------------- */
53
54unsigned long long libconfig_parse_hex64(const char *s)
55{
56#ifdef __MINGW32__
57
58 /* MinGW's strtoull() seems to be broken; it only returns the lower
59 * 32 bits...
60 */
61
62 const char *p = s;
63 unsigned long long val = 0;
64
65 if(*p != '0')
66 return(0);
67
68 ++p;
69
70 if(*p != 'x' && *p != 'X')
71 return(0);
72
73 for(++p; isxdigit(*p); ++p)
74 {
75 val <<= 4;
76 val |= ((*p < 'A') ? (*p & 0xF) : (9 + (*p & 0x7)));
77 }
78
79 return(val);
80
81#else /* ! __MINGW32__ */
82
83 return(strtoull(s, NULL, 16));
84
85#endif /* __MINGW32__ */
86}
87
88/* ------------------------------------------------------------------------- */
89
90void libconfig_format_double(double val, int precision, int sci_ok, char *buf,
91 size_t buflen)
92{
93 const char *fmt = sci_ok ? "%.*g" : "%.*f";
94 char *p, *q;
95
96 snprintf(buf, buflen - 3, fmt, precision, val);
97
98 /* Check for exponent. */
99 p = strchr(buf, 'e');
100 if(p) return;
101
102 /* Check for decimal point. */
103 p = strchr(buf, '.');
104 if(!p)
105 {
106 /* No decimal point. Add trailing ".0". */
107 strcat(buf, ".0");
108 }
109 else
110 {
111 /* Remove any excess trailing 0's after decimal point. */
112 for(q = buf + strlen(buf) - 1; q > p + 1; --q)
113 {
114 if(*q == '0')
115 *q = '\0';
116 else
117 break;
118 }
119 }
120}
121
122/* ------------------------------------------------------------------------- */