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
|
#include <sys/types.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <limits.h>
#include "int.h"
#include "girstring.h"
#include "casprintf.h"
#include "string_parser.h"
static const char *
strippedSubstring(const char * const string) {
const char * p;
for (p = &string[0]; isspace(*p); ++p);
return p;
}
void
interpretUll(const char * const string,
uint64_t * const ullP,
const char ** const errorP) {
/* strtoull() has the same disappointing weaknesses of strtoul().
See interpretUint().
*/
const char * const strippedString = strippedSubstring(string);
if (strippedString[0] == '\0')
casprintf(errorP, "Null (or all whitespace) string.");
else if (!isdigit(strippedString[0]))
casprintf(errorP, "First non-blank character is '%c', not a digit.",
strippedString[0]);
else {
/* strtoull() does a bizarre thing where if the number is out
of range, it returns a clamped value but tells you about it
by setting errno = ERANGE. If it is not out of range,
strtoull() leaves errno alone.
*/
char * tail;
errno = 0; /* So we can tell if strtoull() overflowed */
*ullP = XMLRPC_STRTOULL(strippedString, &tail, 10);
if (tail[0] != '\0')
casprintf(errorP, "Non-digit stuff in string: %s", tail);
else if (errno == ERANGE)
casprintf(errorP, "Number too large");
else
*errorP = NULL;
}
}
void
interpretLl(const char * const string,
int64_t * const llP,
const char ** const errorP) {
if (string[0] == '\0')
casprintf(errorP, "Null string.");
else {
/* strtoll() does a bizarre thing where if the number is out
of range, it returns a clamped value but tells you about it
by setting errno = ERANGE. If it is not out of range,
strtoll() leaves errno alone.
*/
char * tail;
errno = 0; /* So we can tell if strtoll() overflowed */
*llP = XMLRPC_STRTOLL(string, &tail, 10);
if (tail[0] != '\0')
casprintf(errorP, "Non-digit stuff in string: %s", tail);
else if (errno == ERANGE)
casprintf(errorP, "Number too large");
else
*errorP = NULL;
}
}
void
interpretUint(const char * const string,
unsigned int * const uintP,
const char ** const errorP) {
/* strtoul() does a lousy job of dealing with invalid numbers. A null
string is just zero; a negative number is a large positive one; a
positive (cf unsigned) number is accepted. strtoul is inconsistent
in its treatment of the tail; if there is no valid number at all,
it returns the entire string as the tail, including leading white
space and sign, which are not themselves invalid.
*/
const char * const strippedString = strippedSubstring(string);
if (strippedString[0] == '\0')
casprintf(errorP, "Null (or all whitespace) string.");
else if (!isdigit(strippedString[0]))
casprintf(errorP, "First non-blank character is '%c', not a digit.",
strippedString[0]);
else {
/* strtoul() does a bizarre thing where if the number is out
of range, it returns a clamped value but tells you about it
by setting errno = ERANGE. If it is not out of range,
strtoul() leaves errno alone.
*/
char * tail;
unsigned long ulongValue;
errno = 0; /* So we can tell if strtoul() overflowed */
ulongValue = strtoul(strippedString, &tail, 10);
if (tail[0] != '\0')
casprintf(errorP, "Non-digit stuff in string: %s", tail);
else if (errno == ERANGE)
casprintf(errorP, "Number too large");
else if (ulongValue > UINT_MAX)
casprintf(errorP, "Number too large");
else {
*uintP = ulongValue;
*errorP = NULL;
}
}
}
void
interpretInt(const char * const string,
int * const intP,
const char ** const errorP) {
if (string[0] == '\0')
casprintf(errorP, "Null string.");
else {
/* strtol() does a bizarre thing where if the number is out
of range, it returns a clamped value but tells you about it
by setting errno = ERANGE. If it is not out of range,
strtol() leaves errno alone.
*/
char * tail;
long longValue;
errno = 0; /* So we can tell if strtol() overflowed */
longValue = strtol(string, &tail, 10);
if (tail[0] != '\0')
casprintf(errorP, "Non-digit stuff in string: %s", tail);
else if (errno == ERANGE)
casprintf(errorP, "Number too large");
else if (longValue > INT_MAX)
casprintf(errorP, "Number too large");
else if (longValue < INT_MIN)
casprintf(errorP, "Number too negative");
else {
*intP = longValue;
*errorP = NULL;
}
}
}
void
interpretBinUint(const char * const string,
uint64_t * const valueP,
const char ** const errorP) {
char * tailptr;
long const mantissa_long = strtol(string, &tailptr, 10);
if (errno == ERANGE)
casprintf(errorP,
"Numeric value out of range for computation: '%s'. "
"Try a smaller number with a K, M, G, etc. suffix.",
string);
else {
int64_t const mantissa = mantissa_long;
int64_t argNumber;
*errorP = NULL; /* initial assumption */
if (*tailptr == '\0')
/* There's no suffix. A pure number */
argNumber = mantissa * 1;
else if (stripcaseeq(tailptr, "K"))
argNumber = mantissa * 1024;
else if (stripcaseeq(tailptr, "M"))
argNumber = mantissa * 1024 * 1024;
else if (stripcaseeq(tailptr, "G"))
argNumber = mantissa * 1024 * 1024 * 1024;
else if (stripcaseeq(tailptr, "T"))
argNumber = mantissa * 1024 * 1024 * 1024 * 1024;
else if (stripcaseeq(tailptr, "P"))
argNumber = mantissa * 1024 * 1024 * 1024 * 1024 * 1024;
else {
argNumber = 0; /* quiet compiler warning */
casprintf(errorP, "Garbage suffix '%s' on number", tailptr);
}
if (!*errorP) {
if (argNumber < 0)
casprintf(errorP, "Unsigned numeric value is "
"negative: %" PRId64, argNumber);
else
*valueP = (uint64_t) argNumber;
}
}
}
|