| 1 | n/a | /* | 
|---|
| 2 | n/a | * Copyright (c) 2008-2016 Stefan Krah. All rights reserved. | 
|---|
| 3 | n/a | * | 
|---|
| 4 | n/a | * Redistribution and use in source and binary forms, with or without | 
|---|
| 5 | n/a | * modification, are permitted provided that the following conditions | 
|---|
| 6 | n/a | * are met: | 
|---|
| 7 | n/a | * | 
|---|
| 8 | n/a | * 1. Redistributions of source code must retain the above copyright | 
|---|
| 9 | n/a | *    notice, this list of conditions and the following disclaimer. | 
|---|
| 10 | n/a | * | 
|---|
| 11 | n/a | * 2. Redistributions in binary form must reproduce the above copyright | 
|---|
| 12 | n/a | *    notice, this list of conditions and the following disclaimer in the | 
|---|
| 13 | n/a | *    documentation and/or other materials provided with the distribution. | 
|---|
| 14 | n/a | * | 
|---|
| 15 | n/a | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS "AS IS" AND | 
|---|
| 16 | n/a | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | 
|---|
| 17 | n/a | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | 
|---|
| 18 | n/a | * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE | 
|---|
| 19 | n/a | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | 
|---|
| 20 | n/a | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | 
|---|
| 21 | n/a | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | 
|---|
| 22 | n/a | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | 
|---|
| 23 | n/a | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | 
|---|
| 24 | n/a | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | 
|---|
| 25 | n/a | * SUCH DAMAGE. | 
|---|
| 26 | n/a | */ | 
|---|
| 27 | n/a |  | 
|---|
| 28 | n/a |  | 
|---|
| 29 | n/a | #include "mpdecimal.h" | 
|---|
| 30 | n/a | #include <stdio.h> | 
|---|
| 31 | n/a | #include <stdlib.h> | 
|---|
| 32 | n/a | #include <string.h> | 
|---|
| 33 | n/a | #include <ctype.h> | 
|---|
| 34 | n/a | #include <limits.h> | 
|---|
| 35 | n/a | #include <assert.h> | 
|---|
| 36 | n/a | #include <errno.h> | 
|---|
| 37 | n/a | #include <locale.h> | 
|---|
| 38 | n/a | #include "bits.h" | 
|---|
| 39 | n/a | #include "constants.h" | 
|---|
| 40 | n/a | #include "typearith.h" | 
|---|
| 41 | n/a | #include "io.h" | 
|---|
| 42 | n/a |  | 
|---|
| 43 | n/a |  | 
|---|
| 44 | n/a | /* This file contains functions for decimal <-> string conversions, including | 
|---|
| 45 | n/a | PEP-3101 formatting for numeric types. */ | 
|---|
| 46 | n/a |  | 
|---|
| 47 | n/a |  | 
|---|
| 48 | n/a | /* | 
|---|
| 49 | n/a | * Work around the behavior of tolower() and strcasecmp() in certain | 
|---|
| 50 | n/a | * locales. For example, in tr_TR.utf8: | 
|---|
| 51 | n/a | * | 
|---|
| 52 | n/a | * tolower((unsigned char)'I') == 'I' | 
|---|
| 53 | n/a | * | 
|---|
| 54 | n/a | * u is the exact uppercase version of l; n is strlen(l) or strlen(l)+1 | 
|---|
| 55 | n/a | */ | 
|---|
| 56 | n/a | static inline int | 
|---|
| 57 | n/a | _mpd_strneq(const char *s, const char *l, const char *u, size_t n) | 
|---|
| 58 | n/a | { | 
|---|
| 59 | n/a | while (--n != SIZE_MAX) { | 
|---|
| 60 | n/a | if (*s != *l && *s != *u) { | 
|---|
| 61 | n/a | return 0; | 
|---|
| 62 | n/a | } | 
|---|
| 63 | n/a | s++; u++; l++; | 
|---|
| 64 | n/a | } | 
|---|
| 65 | n/a |  | 
|---|
| 66 | n/a | return 1; | 
|---|
| 67 | n/a | } | 
|---|
| 68 | n/a |  | 
|---|
| 69 | n/a | static mpd_ssize_t | 
|---|
| 70 | n/a | strtoexp(const char *s) | 
|---|
| 71 | n/a | { | 
|---|
| 72 | n/a | char *end; | 
|---|
| 73 | n/a | mpd_ssize_t retval; | 
|---|
| 74 | n/a |  | 
|---|
| 75 | n/a | errno = 0; | 
|---|
| 76 | n/a | retval = mpd_strtossize(s, &end, 10); | 
|---|
| 77 | n/a | if (errno == 0 && !(*s != '\0' && *end == '\0')) | 
|---|
| 78 | n/a | errno = EINVAL; | 
|---|
| 79 | n/a |  | 
|---|
| 80 | n/a | return retval; | 
|---|
| 81 | n/a | } | 
|---|
| 82 | n/a |  | 
|---|
| 83 | n/a | /* | 
|---|
| 84 | n/a | * Scan 'len' words. The most significant word contains 'r' digits, | 
|---|
| 85 | n/a | * the remaining words are full words. Skip dpoint. The string 's' must | 
|---|
| 86 | n/a | * consist of digits and an optional single decimal point at 'dpoint'. | 
|---|
| 87 | n/a | */ | 
|---|
| 88 | n/a | static void | 
|---|
| 89 | n/a | string_to_coeff(mpd_uint_t *data, const char *s, const char *dpoint, int r, | 
|---|
| 90 | n/a | size_t len) | 
|---|
| 91 | n/a | { | 
|---|
| 92 | n/a | int j; | 
|---|
| 93 | n/a |  | 
|---|
| 94 | n/a | if (r > 0) { | 
|---|
| 95 | n/a | data[--len] = 0; | 
|---|
| 96 | n/a | for (j = 0; j < r; j++, s++) { | 
|---|
| 97 | n/a | if (s == dpoint) s++; | 
|---|
| 98 | n/a | data[len] = 10 * data[len] + (*s - '0'); | 
|---|
| 99 | n/a | } | 
|---|
| 100 | n/a | } | 
|---|
| 101 | n/a |  | 
|---|
| 102 | n/a | while (--len != SIZE_MAX) { | 
|---|
| 103 | n/a | data[len] = 0; | 
|---|
| 104 | n/a | for (j = 0; j < MPD_RDIGITS; j++, s++) { | 
|---|
| 105 | n/a | if (s == dpoint) s++; | 
|---|
| 106 | n/a | data[len] = 10 * data[len] + (*s - '0'); | 
|---|
| 107 | n/a | } | 
|---|
| 108 | n/a | } | 
|---|
| 109 | n/a | } | 
|---|
| 110 | n/a |  | 
|---|
| 111 | n/a | /* | 
|---|
| 112 | n/a | * Partially verify a numeric string of the form: | 
|---|
| 113 | n/a | * | 
|---|
| 114 | n/a | *     [cdigits][.][cdigits][eE][+-][edigits] | 
|---|
| 115 | n/a | * | 
|---|
| 116 | n/a | * If successful, return a pointer to the location of the first | 
|---|
| 117 | n/a | * relevant coefficient digit. This digit is either non-zero or | 
|---|
| 118 | n/a | * part of one of the following patterns: | 
|---|
| 119 | n/a | * | 
|---|
| 120 | n/a | *     ["0\x00", "0.\x00", "0.E", "0.e", "0E", "0e"] | 
|---|
| 121 | n/a | * | 
|---|
| 122 | n/a | * The locations of a single optional dot or indicator are stored | 
|---|
| 123 | n/a | * in 'dpoint' and 'exp'. | 
|---|
| 124 | n/a | * | 
|---|
| 125 | n/a | * The end of the string is stored in 'end'. If an indicator [eE] | 
|---|
| 126 | n/a | * occurs without trailing [edigits], the condition is caught | 
|---|
| 127 | n/a | * later by strtoexp(). | 
|---|
| 128 | n/a | */ | 
|---|
| 129 | n/a | static const char * | 
|---|
| 130 | n/a | scan_dpoint_exp(const char *s, const char **dpoint, const char **exp, | 
|---|
| 131 | n/a | const char **end) | 
|---|
| 132 | n/a | { | 
|---|
| 133 | n/a | const char *coeff = NULL; | 
|---|
| 134 | n/a |  | 
|---|
| 135 | n/a | *dpoint = NULL; | 
|---|
| 136 | n/a | *exp = NULL; | 
|---|
| 137 | n/a | for (; *s != '\0'; s++) { | 
|---|
| 138 | n/a | switch (*s) { | 
|---|
| 139 | n/a | case '.': | 
|---|
| 140 | n/a | if (*dpoint != NULL || *exp != NULL) | 
|---|
| 141 | n/a | return NULL; | 
|---|
| 142 | n/a | *dpoint = s; | 
|---|
| 143 | n/a | break; | 
|---|
| 144 | n/a | case 'E': case 'e': | 
|---|
| 145 | n/a | if (*exp != NULL) | 
|---|
| 146 | n/a | return NULL; | 
|---|
| 147 | n/a | *exp = s; | 
|---|
| 148 | n/a | if (*(s+1) == '+' || *(s+1) == '-') | 
|---|
| 149 | n/a | s++; | 
|---|
| 150 | n/a | break; | 
|---|
| 151 | n/a | default: | 
|---|
| 152 | n/a | if (!isdigit((uchar)*s)) | 
|---|
| 153 | n/a | return NULL; | 
|---|
| 154 | n/a | if (coeff == NULL && *exp == NULL) { | 
|---|
| 155 | n/a | if (*s == '0') { | 
|---|
| 156 | n/a | if (!isdigit((uchar)*(s+1))) | 
|---|
| 157 | n/a | if (!(*(s+1) == '.' && | 
|---|
| 158 | n/a | isdigit((uchar)*(s+2)))) | 
|---|
| 159 | n/a | coeff = s; | 
|---|
| 160 | n/a | } | 
|---|
| 161 | n/a | else { | 
|---|
| 162 | n/a | coeff = s; | 
|---|
| 163 | n/a | } | 
|---|
| 164 | n/a | } | 
|---|
| 165 | n/a | break; | 
|---|
| 166 | n/a |  | 
|---|
| 167 | n/a | } | 
|---|
| 168 | n/a | } | 
|---|
| 169 | n/a |  | 
|---|
| 170 | n/a | *end = s; | 
|---|
| 171 | n/a | return coeff; | 
|---|
| 172 | n/a | } | 
|---|
| 173 | n/a |  | 
|---|
| 174 | n/a | /* scan the payload of a NaN */ | 
|---|
| 175 | n/a | static const char * | 
|---|
| 176 | n/a | scan_payload(const char *s, const char **end) | 
|---|
| 177 | n/a | { | 
|---|
| 178 | n/a | const char *coeff; | 
|---|
| 179 | n/a |  | 
|---|
| 180 | n/a | while (*s == '0') | 
|---|
| 181 | n/a | s++; | 
|---|
| 182 | n/a | coeff = s; | 
|---|
| 183 | n/a |  | 
|---|
| 184 | n/a | while (isdigit((uchar)*s)) | 
|---|
| 185 | n/a | s++; | 
|---|
| 186 | n/a | *end = s; | 
|---|
| 187 | n/a |  | 
|---|
| 188 | n/a | return (*s == '\0') ? coeff : NULL; | 
|---|
| 189 | n/a | } | 
|---|
| 190 | n/a |  | 
|---|
| 191 | n/a | /* convert a character string to a decimal */ | 
|---|
| 192 | n/a | void | 
|---|
| 193 | n/a | mpd_qset_string(mpd_t *dec, const char *s, const mpd_context_t *ctx, | 
|---|
| 194 | n/a | uint32_t *status) | 
|---|
| 195 | n/a | { | 
|---|
| 196 | n/a | mpd_ssize_t q, r, len; | 
|---|
| 197 | n/a | const char *coeff, *end; | 
|---|
| 198 | n/a | const char *dpoint = NULL, *exp = NULL; | 
|---|
| 199 | n/a | size_t digits; | 
|---|
| 200 | n/a | uint8_t sign = MPD_POS; | 
|---|
| 201 | n/a |  | 
|---|
| 202 | n/a | mpd_set_flags(dec, 0); | 
|---|
| 203 | n/a | dec->len = 0; | 
|---|
| 204 | n/a | dec->exp = 0; | 
|---|
| 205 | n/a |  | 
|---|
| 206 | n/a | /* sign */ | 
|---|
| 207 | n/a | if (*s == '+') { | 
|---|
| 208 | n/a | s++; | 
|---|
| 209 | n/a | } | 
|---|
| 210 | n/a | else if (*s == '-') { | 
|---|
| 211 | n/a | mpd_set_negative(dec); | 
|---|
| 212 | n/a | sign = MPD_NEG; | 
|---|
| 213 | n/a | s++; | 
|---|
| 214 | n/a | } | 
|---|
| 215 | n/a |  | 
|---|
| 216 | n/a | if (_mpd_strneq(s, "nan", "NAN", 3)) { /* NaN */ | 
|---|
| 217 | n/a | s += 3; | 
|---|
| 218 | n/a | mpd_setspecial(dec, sign, MPD_NAN); | 
|---|
| 219 | n/a | if (*s == '\0') | 
|---|
| 220 | n/a | return; | 
|---|
| 221 | n/a | /* validate payload: digits only */ | 
|---|
| 222 | n/a | if ((coeff = scan_payload(s, &end)) == NULL) | 
|---|
| 223 | n/a | goto conversion_error; | 
|---|
| 224 | n/a | /* payload consists entirely of zeros */ | 
|---|
| 225 | n/a | if (*coeff == '\0') | 
|---|
| 226 | n/a | return; | 
|---|
| 227 | n/a | digits = end - coeff; | 
|---|
| 228 | n/a | /* prec >= 1, clamp is 0 or 1 */ | 
|---|
| 229 | n/a | if (digits > (size_t)(ctx->prec-ctx->clamp)) | 
|---|
| 230 | n/a | goto conversion_error; | 
|---|
| 231 | n/a | } /* sNaN */ | 
|---|
| 232 | n/a | else if (_mpd_strneq(s, "snan", "SNAN", 4)) { | 
|---|
| 233 | n/a | s += 4; | 
|---|
| 234 | n/a | mpd_setspecial(dec, sign, MPD_SNAN); | 
|---|
| 235 | n/a | if (*s == '\0') | 
|---|
| 236 | n/a | return; | 
|---|
| 237 | n/a | /* validate payload: digits only */ | 
|---|
| 238 | n/a | if ((coeff = scan_payload(s, &end)) == NULL) | 
|---|
| 239 | n/a | goto conversion_error; | 
|---|
| 240 | n/a | /* payload consists entirely of zeros */ | 
|---|
| 241 | n/a | if (*coeff == '\0') | 
|---|
| 242 | n/a | return; | 
|---|
| 243 | n/a | digits = end - coeff; | 
|---|
| 244 | n/a | if (digits > (size_t)(ctx->prec-ctx->clamp)) | 
|---|
| 245 | n/a | goto conversion_error; | 
|---|
| 246 | n/a | } | 
|---|
| 247 | n/a | else if (_mpd_strneq(s, "inf", "INF", 3)) { | 
|---|
| 248 | n/a | s += 3; | 
|---|
| 249 | n/a | if (*s == '\0' || _mpd_strneq(s, "inity", "INITY", 6)) { | 
|---|
| 250 | n/a | /* numeric-value: infinity */ | 
|---|
| 251 | n/a | mpd_setspecial(dec, sign, MPD_INF); | 
|---|
| 252 | n/a | return; | 
|---|
| 253 | n/a | } | 
|---|
| 254 | n/a | goto conversion_error; | 
|---|
| 255 | n/a | } | 
|---|
| 256 | n/a | else { | 
|---|
| 257 | n/a | /* scan for start of coefficient, decimal point, indicator, end */ | 
|---|
| 258 | n/a | if ((coeff = scan_dpoint_exp(s, &dpoint, &exp, &end)) == NULL) | 
|---|
| 259 | n/a | goto conversion_error; | 
|---|
| 260 | n/a |  | 
|---|
| 261 | n/a | /* numeric-value: [exponent-part] */ | 
|---|
| 262 | n/a | if (exp) { | 
|---|
| 263 | n/a | /* exponent-part */ | 
|---|
| 264 | n/a | end = exp; exp++; | 
|---|
| 265 | n/a | dec->exp = strtoexp(exp); | 
|---|
| 266 | n/a | if (errno) { | 
|---|
| 267 | n/a | if (!(errno == ERANGE && | 
|---|
| 268 | n/a | (dec->exp == MPD_SSIZE_MAX || | 
|---|
| 269 | n/a | dec->exp == MPD_SSIZE_MIN))) | 
|---|
| 270 | n/a | goto conversion_error; | 
|---|
| 271 | n/a | } | 
|---|
| 272 | n/a | } | 
|---|
| 273 | n/a |  | 
|---|
| 274 | n/a | digits = end - coeff; | 
|---|
| 275 | n/a | if (dpoint) { | 
|---|
| 276 | n/a | size_t fracdigits = end-dpoint-1; | 
|---|
| 277 | n/a | if (dpoint > coeff) digits--; | 
|---|
| 278 | n/a |  | 
|---|
| 279 | n/a | if (fracdigits > MPD_MAX_PREC) { | 
|---|
| 280 | n/a | goto conversion_error; | 
|---|
| 281 | n/a | } | 
|---|
| 282 | n/a | if (dec->exp < MPD_SSIZE_MIN+(mpd_ssize_t)fracdigits) { | 
|---|
| 283 | n/a | dec->exp = MPD_SSIZE_MIN; | 
|---|
| 284 | n/a | } | 
|---|
| 285 | n/a | else { | 
|---|
| 286 | n/a | dec->exp -= (mpd_ssize_t)fracdigits; | 
|---|
| 287 | n/a | } | 
|---|
| 288 | n/a | } | 
|---|
| 289 | n/a | if (digits > MPD_MAX_PREC) { | 
|---|
| 290 | n/a | goto conversion_error; | 
|---|
| 291 | n/a | } | 
|---|
| 292 | n/a | if (dec->exp > MPD_EXP_INF) { | 
|---|
| 293 | n/a | dec->exp = MPD_EXP_INF; | 
|---|
| 294 | n/a | } | 
|---|
| 295 | n/a | if (dec->exp == MPD_SSIZE_MIN) { | 
|---|
| 296 | n/a | dec->exp = MPD_SSIZE_MIN+1; | 
|---|
| 297 | n/a | } | 
|---|
| 298 | n/a | } | 
|---|
| 299 | n/a |  | 
|---|
| 300 | n/a | _mpd_idiv_word(&q, &r, (mpd_ssize_t)digits, MPD_RDIGITS); | 
|---|
| 301 | n/a |  | 
|---|
| 302 | n/a | len = (r == 0) ? q : q+1; | 
|---|
| 303 | n/a | if (len == 0) { | 
|---|
| 304 | n/a | goto conversion_error; /* GCOV_NOT_REACHED */ | 
|---|
| 305 | n/a | } | 
|---|
| 306 | n/a | if (!mpd_qresize(dec, len, status)) { | 
|---|
| 307 | n/a | mpd_seterror(dec, MPD_Malloc_error, status); | 
|---|
| 308 | n/a | return; | 
|---|
| 309 | n/a | } | 
|---|
| 310 | n/a | dec->len = len; | 
|---|
| 311 | n/a |  | 
|---|
| 312 | n/a | string_to_coeff(dec->data, coeff, dpoint, (int)r, len); | 
|---|
| 313 | n/a |  | 
|---|
| 314 | n/a | mpd_setdigits(dec); | 
|---|
| 315 | n/a | mpd_qfinalize(dec, ctx, status); | 
|---|
| 316 | n/a | return; | 
|---|
| 317 | n/a |  | 
|---|
| 318 | n/a | conversion_error: | 
|---|
| 319 | n/a | /* standard wants a positive NaN */ | 
|---|
| 320 | n/a | mpd_seterror(dec, MPD_Conversion_syntax, status); | 
|---|
| 321 | n/a | } | 
|---|
| 322 | n/a |  | 
|---|
| 323 | n/a | /* Print word x with n decimal digits to string s. dot is either NULL | 
|---|
| 324 | n/a | or the location of a decimal point. */ | 
|---|
| 325 | n/a | #define EXTRACT_DIGIT(s, x, d, dot) \ | 
|---|
| 326 | n/a | if (s == dot) *s++ = '.'; *s++ = '0' + (char)(x / d); x %= d | 
|---|
| 327 | n/a | static inline char * | 
|---|
| 328 | n/a | word_to_string(char *s, mpd_uint_t x, int n, char *dot) | 
|---|
| 329 | n/a | { | 
|---|
| 330 | n/a | switch(n) { | 
|---|
| 331 | n/a | #ifdef CONFIG_64 | 
|---|
| 332 | n/a | case 20: EXTRACT_DIGIT(s, x, 10000000000000000000ULL, dot); /* GCOV_NOT_REACHED */ | 
|---|
| 333 | n/a | case 19: EXTRACT_DIGIT(s, x, 1000000000000000000ULL, dot); | 
|---|
| 334 | n/a | case 18: EXTRACT_DIGIT(s, x, 100000000000000000ULL, dot); | 
|---|
| 335 | n/a | case 17: EXTRACT_DIGIT(s, x, 10000000000000000ULL, dot); | 
|---|
| 336 | n/a | case 16: EXTRACT_DIGIT(s, x, 1000000000000000ULL, dot); | 
|---|
| 337 | n/a | case 15: EXTRACT_DIGIT(s, x, 100000000000000ULL, dot); | 
|---|
| 338 | n/a | case 14: EXTRACT_DIGIT(s, x, 10000000000000ULL, dot); | 
|---|
| 339 | n/a | case 13: EXTRACT_DIGIT(s, x, 1000000000000ULL, dot); | 
|---|
| 340 | n/a | case 12: EXTRACT_DIGIT(s, x, 100000000000ULL, dot); | 
|---|
| 341 | n/a | case 11: EXTRACT_DIGIT(s, x, 10000000000ULL, dot); | 
|---|
| 342 | n/a | #endif | 
|---|
| 343 | n/a | case 10: EXTRACT_DIGIT(s, x, 1000000000UL, dot); | 
|---|
| 344 | n/a | case 9:  EXTRACT_DIGIT(s, x, 100000000UL, dot); | 
|---|
| 345 | n/a | case 8:  EXTRACT_DIGIT(s, x, 10000000UL, dot); | 
|---|
| 346 | n/a | case 7:  EXTRACT_DIGIT(s, x, 1000000UL, dot); | 
|---|
| 347 | n/a | case 6:  EXTRACT_DIGIT(s, x, 100000UL, dot); | 
|---|
| 348 | n/a | case 5:  EXTRACT_DIGIT(s, x, 10000UL, dot); | 
|---|
| 349 | n/a | case 4:  EXTRACT_DIGIT(s, x, 1000UL, dot); | 
|---|
| 350 | n/a | case 3:  EXTRACT_DIGIT(s, x, 100UL, dot); | 
|---|
| 351 | n/a | case 2:  EXTRACT_DIGIT(s, x, 10UL, dot); | 
|---|
| 352 | n/a | default: if (s == dot) *s++ = '.'; *s++ = '0' + (char)x; | 
|---|
| 353 | n/a | } | 
|---|
| 354 | n/a |  | 
|---|
| 355 | n/a | *s = '\0'; | 
|---|
| 356 | n/a | return s; | 
|---|
| 357 | n/a | } | 
|---|
| 358 | n/a |  | 
|---|
| 359 | n/a | /* Print exponent x to string s. Undefined for MPD_SSIZE_MIN. */ | 
|---|
| 360 | n/a | static inline char * | 
|---|
| 361 | n/a | exp_to_string(char *s, mpd_ssize_t x) | 
|---|
| 362 | n/a | { | 
|---|
| 363 | n/a | char sign = '+'; | 
|---|
| 364 | n/a |  | 
|---|
| 365 | n/a | if (x < 0) { | 
|---|
| 366 | n/a | sign = '-'; | 
|---|
| 367 | n/a | x = -x; | 
|---|
| 368 | n/a | } | 
|---|
| 369 | n/a | *s++ = sign; | 
|---|
| 370 | n/a |  | 
|---|
| 371 | n/a | return word_to_string(s, x, mpd_word_digits(x), NULL); | 
|---|
| 372 | n/a | } | 
|---|
| 373 | n/a |  | 
|---|
| 374 | n/a | /* Print the coefficient of dec to string s. len(dec) > 0. */ | 
|---|
| 375 | n/a | static inline char * | 
|---|
| 376 | n/a | coeff_to_string(char *s, const mpd_t *dec) | 
|---|
| 377 | n/a | { | 
|---|
| 378 | n/a | mpd_uint_t x; | 
|---|
| 379 | n/a | mpd_ssize_t i; | 
|---|
| 380 | n/a |  | 
|---|
| 381 | n/a | /* most significant word */ | 
|---|
| 382 | n/a | x = mpd_msword(dec); | 
|---|
| 383 | n/a | s = word_to_string(s, x, mpd_word_digits(x), NULL); | 
|---|
| 384 | n/a |  | 
|---|
| 385 | n/a | /* remaining full words */ | 
|---|
| 386 | n/a | for (i=dec->len-2; i >= 0; --i) { | 
|---|
| 387 | n/a | x = dec->data[i]; | 
|---|
| 388 | n/a | s = word_to_string(s, x, MPD_RDIGITS, NULL); | 
|---|
| 389 | n/a | } | 
|---|
| 390 | n/a |  | 
|---|
| 391 | n/a | return s; | 
|---|
| 392 | n/a | } | 
|---|
| 393 | n/a |  | 
|---|
| 394 | n/a | /* Print the coefficient of dec to string s. len(dec) > 0. dot is either | 
|---|
| 395 | n/a | NULL or a pointer to the location of a decimal point. */ | 
|---|
| 396 | n/a | static inline char * | 
|---|
| 397 | n/a | coeff_to_string_dot(char *s, char *dot, const mpd_t *dec) | 
|---|
| 398 | n/a | { | 
|---|
| 399 | n/a | mpd_uint_t x; | 
|---|
| 400 | n/a | mpd_ssize_t i; | 
|---|
| 401 | n/a |  | 
|---|
| 402 | n/a | /* most significant word */ | 
|---|
| 403 | n/a | x = mpd_msword(dec); | 
|---|
| 404 | n/a | s = word_to_string(s, x, mpd_word_digits(x), dot); | 
|---|
| 405 | n/a |  | 
|---|
| 406 | n/a | /* remaining full words */ | 
|---|
| 407 | n/a | for (i=dec->len-2; i >= 0; --i) { | 
|---|
| 408 | n/a | x = dec->data[i]; | 
|---|
| 409 | n/a | s = word_to_string(s, x, MPD_RDIGITS, dot); | 
|---|
| 410 | n/a | } | 
|---|
| 411 | n/a |  | 
|---|
| 412 | n/a | return s; | 
|---|
| 413 | n/a | } | 
|---|
| 414 | n/a |  | 
|---|
| 415 | n/a | /* Format type */ | 
|---|
| 416 | n/a | #define MPD_FMT_LOWER      0x00000000 | 
|---|
| 417 | n/a | #define MPD_FMT_UPPER      0x00000001 | 
|---|
| 418 | n/a | #define MPD_FMT_TOSCI      0x00000002 | 
|---|
| 419 | n/a | #define MPD_FMT_TOENG      0x00000004 | 
|---|
| 420 | n/a | #define MPD_FMT_EXP        0x00000008 | 
|---|
| 421 | n/a | #define MPD_FMT_FIXED      0x00000010 | 
|---|
| 422 | n/a | #define MPD_FMT_PERCENT    0x00000020 | 
|---|
| 423 | n/a | #define MPD_FMT_SIGN_SPACE 0x00000040 | 
|---|
| 424 | n/a | #define MPD_FMT_SIGN_PLUS  0x00000080 | 
|---|
| 425 | n/a |  | 
|---|
| 426 | n/a | /* Default place of the decimal point for MPD_FMT_TOSCI, MPD_FMT_EXP */ | 
|---|
| 427 | n/a | #define MPD_DEFAULT_DOTPLACE 1 | 
|---|
| 428 | n/a |  | 
|---|
| 429 | n/a | /* | 
|---|
| 430 | n/a | * Set *result to the string representation of a decimal. Return the length | 
|---|
| 431 | n/a | * of *result, not including the terminating '\0' character. | 
|---|
| 432 | n/a | * | 
|---|
| 433 | n/a | * Formatting is done according to 'flags'. A return value of -1 with *result | 
|---|
| 434 | n/a | * set to NULL indicates MPD_Malloc_error. | 
|---|
| 435 | n/a | * | 
|---|
| 436 | n/a | * 'dplace' is the default place of the decimal point. It is always set to | 
|---|
| 437 | n/a | * MPD_DEFAULT_DOTPLACE except for zeros in combination with MPD_FMT_EXP. | 
|---|
| 438 | n/a | */ | 
|---|
| 439 | n/a | static mpd_ssize_t | 
|---|
| 440 | n/a | _mpd_to_string(char **result, const mpd_t *dec, int flags, mpd_ssize_t dplace) | 
|---|
| 441 | n/a | { | 
|---|
| 442 | n/a | char *decstring = NULL, *cp = NULL; | 
|---|
| 443 | n/a | mpd_ssize_t ldigits; | 
|---|
| 444 | n/a | mpd_ssize_t mem = 0, k; | 
|---|
| 445 | n/a |  | 
|---|
| 446 | n/a | if (mpd_isspecial(dec)) { | 
|---|
| 447 | n/a |  | 
|---|
| 448 | n/a | mem = sizeof "-Infinity%"; | 
|---|
| 449 | n/a | if (mpd_isnan(dec) && dec->len > 0) { | 
|---|
| 450 | n/a | /* diagnostic code */ | 
|---|
| 451 | n/a | mem += dec->digits; | 
|---|
| 452 | n/a | } | 
|---|
| 453 | n/a | cp = decstring = mpd_alloc(mem, sizeof *decstring); | 
|---|
| 454 | n/a | if (cp == NULL) { | 
|---|
| 455 | n/a | *result = NULL; | 
|---|
| 456 | n/a | return -1; | 
|---|
| 457 | n/a | } | 
|---|
| 458 | n/a |  | 
|---|
| 459 | n/a | if (mpd_isnegative(dec)) { | 
|---|
| 460 | n/a | *cp++ = '-'; | 
|---|
| 461 | n/a | } | 
|---|
| 462 | n/a | else if (flags&MPD_FMT_SIGN_SPACE) { | 
|---|
| 463 | n/a | *cp++ = ' '; | 
|---|
| 464 | n/a | } | 
|---|
| 465 | n/a | else if (flags&MPD_FMT_SIGN_PLUS) { | 
|---|
| 466 | n/a | *cp++ = '+'; | 
|---|
| 467 | n/a | } | 
|---|
| 468 | n/a |  | 
|---|
| 469 | n/a | if (mpd_isnan(dec)) { | 
|---|
| 470 | n/a | if (mpd_isqnan(dec)) { | 
|---|
| 471 | n/a | strcpy(cp, "NaN"); | 
|---|
| 472 | n/a | cp += 3; | 
|---|
| 473 | n/a | } | 
|---|
| 474 | n/a | else { | 
|---|
| 475 | n/a | strcpy(cp, "sNaN"); | 
|---|
| 476 | n/a | cp += 4; | 
|---|
| 477 | n/a | } | 
|---|
| 478 | n/a | if (dec->len > 0) { /* diagnostic code */ | 
|---|
| 479 | n/a | cp = coeff_to_string(cp, dec); | 
|---|
| 480 | n/a | } | 
|---|
| 481 | n/a | } | 
|---|
| 482 | n/a | else if (mpd_isinfinite(dec)) { | 
|---|
| 483 | n/a | strcpy(cp, "Infinity"); | 
|---|
| 484 | n/a | cp += 8; | 
|---|
| 485 | n/a | } | 
|---|
| 486 | n/a | else { /* debug */ | 
|---|
| 487 | n/a | abort(); /* GCOV_NOT_REACHED */ | 
|---|
| 488 | n/a | } | 
|---|
| 489 | n/a | } | 
|---|
| 490 | n/a | else { | 
|---|
| 491 | n/a | assert(dec->len > 0); | 
|---|
| 492 | n/a |  | 
|---|
| 493 | n/a | /* | 
|---|
| 494 | n/a | * For easier manipulation of the decimal point's location | 
|---|
| 495 | n/a | * and the exponent that is finally printed, the number is | 
|---|
| 496 | n/a | * rescaled to a virtual representation with exp = 0. Here | 
|---|
| 497 | n/a | * ldigits denotes the number of decimal digits to the left | 
|---|
| 498 | n/a | * of the decimal point and remains constant once initialized. | 
|---|
| 499 | n/a | * | 
|---|
| 500 | n/a | * dplace is the location of the decimal point relative to | 
|---|
| 501 | n/a | * the start of the coefficient. Note that 3) always holds | 
|---|
| 502 | n/a | * when dplace is shifted. | 
|---|
| 503 | n/a | * | 
|---|
| 504 | n/a | *   1) ldigits := dec->digits - dec->exp | 
|---|
| 505 | n/a | *   2) dplace  := ldigits            (initially) | 
|---|
| 506 | n/a | *   3) exp     := ldigits - dplace   (initially exp = 0) | 
|---|
| 507 | n/a | * | 
|---|
| 508 | n/a | *   0.00000_.____._____000000. | 
|---|
| 509 | n/a | *    ^      ^    ^           ^ | 
|---|
| 510 | n/a | *    |      |    |           | | 
|---|
| 511 | n/a | *    |      |    |           `- dplace >= digits | 
|---|
| 512 | n/a | *    |      |    `- dplace in the middle of the coefficient | 
|---|
| 513 | n/a | *    |      ` dplace = 1 (after the first coefficient digit) | 
|---|
| 514 | n/a | *    `- dplace <= 0 | 
|---|
| 515 | n/a | */ | 
|---|
| 516 | n/a |  | 
|---|
| 517 | n/a | ldigits = dec->digits + dec->exp; | 
|---|
| 518 | n/a |  | 
|---|
| 519 | n/a | if (flags&MPD_FMT_EXP) { | 
|---|
| 520 | n/a | ; | 
|---|
| 521 | n/a | } | 
|---|
| 522 | n/a | else if (flags&MPD_FMT_FIXED || (dec->exp <= 0 && ldigits > -6)) { | 
|---|
| 523 | n/a | /* MPD_FMT_FIXED: always use fixed point notation. | 
|---|
| 524 | n/a | * MPD_FMT_TOSCI, MPD_FMT_TOENG: for a certain range, | 
|---|
| 525 | n/a | * override exponent notation. */ | 
|---|
| 526 | n/a | dplace = ldigits; | 
|---|
| 527 | n/a | } | 
|---|
| 528 | n/a | else if (flags&MPD_FMT_TOENG) { | 
|---|
| 529 | n/a | if (mpd_iszero(dec)) { | 
|---|
| 530 | n/a | /* If the exponent is divisible by three, | 
|---|
| 531 | n/a | * dplace = 1. Otherwise, move dplace one | 
|---|
| 532 | n/a | * or two places to the left. */ | 
|---|
| 533 | n/a | dplace = -1 + mod_mpd_ssize_t(dec->exp+2, 3); | 
|---|
| 534 | n/a | } | 
|---|
| 535 | n/a | else { /* ldigits-1 is the adjusted exponent, which | 
|---|
| 536 | n/a | * should be divisible by three. If not, move | 
|---|
| 537 | n/a | * dplace one or two places to the right. */ | 
|---|
| 538 | n/a | dplace += mod_mpd_ssize_t(ldigits-1, 3); | 
|---|
| 539 | n/a | } | 
|---|
| 540 | n/a | } | 
|---|
| 541 | n/a |  | 
|---|
| 542 | n/a | /* | 
|---|
| 543 | n/a | * Basic space requirements: | 
|---|
| 544 | n/a | * | 
|---|
| 545 | n/a | * [-][.][coeffdigits][E][-][expdigits+1][%]['\0'] | 
|---|
| 546 | n/a | * | 
|---|
| 547 | n/a | * If the decimal point lies outside of the coefficient digits, | 
|---|
| 548 | n/a | * space is adjusted accordingly. | 
|---|
| 549 | n/a | */ | 
|---|
| 550 | n/a | if (dplace <= 0) { | 
|---|
| 551 | n/a | mem = -dplace + dec->digits + 2; | 
|---|
| 552 | n/a | } | 
|---|
| 553 | n/a | else if (dplace >= dec->digits) { | 
|---|
| 554 | n/a | mem = dplace; | 
|---|
| 555 | n/a | } | 
|---|
| 556 | n/a | else { | 
|---|
| 557 | n/a | mem = dec->digits; | 
|---|
| 558 | n/a | } | 
|---|
| 559 | n/a | mem += (MPD_EXPDIGITS+1+6); | 
|---|
| 560 | n/a |  | 
|---|
| 561 | n/a | cp = decstring = mpd_alloc(mem, sizeof *decstring); | 
|---|
| 562 | n/a | if (cp == NULL) { | 
|---|
| 563 | n/a | *result = NULL; | 
|---|
| 564 | n/a | return -1; | 
|---|
| 565 | n/a | } | 
|---|
| 566 | n/a |  | 
|---|
| 567 | n/a |  | 
|---|
| 568 | n/a | if (mpd_isnegative(dec)) { | 
|---|
| 569 | n/a | *cp++ = '-'; | 
|---|
| 570 | n/a | } | 
|---|
| 571 | n/a | else if (flags&MPD_FMT_SIGN_SPACE) { | 
|---|
| 572 | n/a | *cp++ = ' '; | 
|---|
| 573 | n/a | } | 
|---|
| 574 | n/a | else if (flags&MPD_FMT_SIGN_PLUS) { | 
|---|
| 575 | n/a | *cp++ = '+'; | 
|---|
| 576 | n/a | } | 
|---|
| 577 | n/a |  | 
|---|
| 578 | n/a | if (dplace <= 0) { | 
|---|
| 579 | n/a | /* space: -dplace+dec->digits+2 */ | 
|---|
| 580 | n/a | *cp++ = '0'; | 
|---|
| 581 | n/a | *cp++ = '.'; | 
|---|
| 582 | n/a | for (k = 0; k < -dplace; k++) { | 
|---|
| 583 | n/a | *cp++ = '0'; | 
|---|
| 584 | n/a | } | 
|---|
| 585 | n/a | cp = coeff_to_string(cp, dec); | 
|---|
| 586 | n/a | } | 
|---|
| 587 | n/a | else if (dplace >= dec->digits) { | 
|---|
| 588 | n/a | /* space: dplace */ | 
|---|
| 589 | n/a | cp = coeff_to_string(cp, dec); | 
|---|
| 590 | n/a | for (k = 0; k < dplace-dec->digits; k++) { | 
|---|
| 591 | n/a | *cp++ = '0'; | 
|---|
| 592 | n/a | } | 
|---|
| 593 | n/a | } | 
|---|
| 594 | n/a | else { | 
|---|
| 595 | n/a | /* space: dec->digits+1 */ | 
|---|
| 596 | n/a | cp = coeff_to_string_dot(cp, cp+dplace, dec); | 
|---|
| 597 | n/a | } | 
|---|
| 598 | n/a |  | 
|---|
| 599 | n/a | /* | 
|---|
| 600 | n/a | * Conditions for printing an exponent: | 
|---|
| 601 | n/a | * | 
|---|
| 602 | n/a | *   MPD_FMT_TOSCI, MPD_FMT_TOENG: only if ldigits != dplace | 
|---|
| 603 | n/a | *   MPD_FMT_FIXED:                never (ldigits == dplace) | 
|---|
| 604 | n/a | *   MPD_FMT_EXP:                  always | 
|---|
| 605 | n/a | */ | 
|---|
| 606 | n/a | if (ldigits != dplace || flags&MPD_FMT_EXP) { | 
|---|
| 607 | n/a | /* space: expdigits+2 */ | 
|---|
| 608 | n/a | *cp++ = (flags&MPD_FMT_UPPER) ? 'E' : 'e'; | 
|---|
| 609 | n/a | cp = exp_to_string(cp, ldigits-dplace); | 
|---|
| 610 | n/a | } | 
|---|
| 611 | n/a | } | 
|---|
| 612 | n/a |  | 
|---|
| 613 | n/a | if (flags&MPD_FMT_PERCENT) { | 
|---|
| 614 | n/a | *cp++ = '%'; | 
|---|
| 615 | n/a | } | 
|---|
| 616 | n/a |  | 
|---|
| 617 | n/a | assert(cp < decstring+mem); | 
|---|
| 618 | n/a | assert(cp-decstring < MPD_SSIZE_MAX); | 
|---|
| 619 | n/a |  | 
|---|
| 620 | n/a | *cp = '\0'; | 
|---|
| 621 | n/a | *result = decstring; | 
|---|
| 622 | n/a | return (mpd_ssize_t)(cp-decstring); | 
|---|
| 623 | n/a | } | 
|---|
| 624 | n/a |  | 
|---|
| 625 | n/a | char * | 
|---|
| 626 | n/a | mpd_to_sci(const mpd_t *dec, int fmt) | 
|---|
| 627 | n/a | { | 
|---|
| 628 | n/a | char *res; | 
|---|
| 629 | n/a | int flags = MPD_FMT_TOSCI; | 
|---|
| 630 | n/a |  | 
|---|
| 631 | n/a | flags |= fmt ? MPD_FMT_UPPER : MPD_FMT_LOWER; | 
|---|
| 632 | n/a | (void)_mpd_to_string(&res, dec, flags, MPD_DEFAULT_DOTPLACE); | 
|---|
| 633 | n/a | return res; | 
|---|
| 634 | n/a | } | 
|---|
| 635 | n/a |  | 
|---|
| 636 | n/a | char * | 
|---|
| 637 | n/a | mpd_to_eng(const mpd_t *dec, int fmt) | 
|---|
| 638 | n/a | { | 
|---|
| 639 | n/a | char *res; | 
|---|
| 640 | n/a | int flags = MPD_FMT_TOENG; | 
|---|
| 641 | n/a |  | 
|---|
| 642 | n/a | flags |= fmt ? MPD_FMT_UPPER : MPD_FMT_LOWER; | 
|---|
| 643 | n/a | (void)_mpd_to_string(&res, dec, flags, MPD_DEFAULT_DOTPLACE); | 
|---|
| 644 | n/a | return res; | 
|---|
| 645 | n/a | } | 
|---|
| 646 | n/a |  | 
|---|
| 647 | n/a | mpd_ssize_t | 
|---|
| 648 | n/a | mpd_to_sci_size(char **res, const mpd_t *dec, int fmt) | 
|---|
| 649 | n/a | { | 
|---|
| 650 | n/a | int flags = MPD_FMT_TOSCI; | 
|---|
| 651 | n/a |  | 
|---|
| 652 | n/a | flags |= fmt ? MPD_FMT_UPPER : MPD_FMT_LOWER; | 
|---|
| 653 | n/a | return _mpd_to_string(res, dec, flags, MPD_DEFAULT_DOTPLACE); | 
|---|
| 654 | n/a | } | 
|---|
| 655 | n/a |  | 
|---|
| 656 | n/a | mpd_ssize_t | 
|---|
| 657 | n/a | mpd_to_eng_size(char **res, const mpd_t *dec, int fmt) | 
|---|
| 658 | n/a | { | 
|---|
| 659 | n/a | int flags = MPD_FMT_TOENG; | 
|---|
| 660 | n/a |  | 
|---|
| 661 | n/a | flags |= fmt ? MPD_FMT_UPPER : MPD_FMT_LOWER; | 
|---|
| 662 | n/a | return _mpd_to_string(res, dec, flags, MPD_DEFAULT_DOTPLACE); | 
|---|
| 663 | n/a | } | 
|---|
| 664 | n/a |  | 
|---|
| 665 | n/a | /* Copy a single UTF-8 char to dest. See: The Unicode Standard, version 5.2, | 
|---|
| 666 | n/a | chapter 3.9: Well-formed UTF-8 byte sequences. */ | 
|---|
| 667 | n/a | static int | 
|---|
| 668 | n/a | _mpd_copy_utf8(char dest[5], const char *s) | 
|---|
| 669 | n/a | { | 
|---|
| 670 | n/a | const uchar *cp = (const uchar *)s; | 
|---|
| 671 | n/a | uchar lb, ub; | 
|---|
| 672 | n/a | int count, i; | 
|---|
| 673 | n/a |  | 
|---|
| 674 | n/a |  | 
|---|
| 675 | n/a | if (*cp == 0) { | 
|---|
| 676 | n/a | /* empty string */ | 
|---|
| 677 | n/a | dest[0] = '\0'; | 
|---|
| 678 | n/a | return 0; | 
|---|
| 679 | n/a | } | 
|---|
| 680 | n/a | else if (*cp <= 0x7f) { | 
|---|
| 681 | n/a | /* ascii */ | 
|---|
| 682 | n/a | dest[0] = *cp; | 
|---|
| 683 | n/a | dest[1] = '\0'; | 
|---|
| 684 | n/a | return 1; | 
|---|
| 685 | n/a | } | 
|---|
| 686 | n/a | else if (0xc2 <= *cp && *cp <= 0xdf) { | 
|---|
| 687 | n/a | lb = 0x80; ub = 0xbf; | 
|---|
| 688 | n/a | count = 2; | 
|---|
| 689 | n/a | } | 
|---|
| 690 | n/a | else if (*cp == 0xe0) { | 
|---|
| 691 | n/a | lb = 0xa0; ub = 0xbf; | 
|---|
| 692 | n/a | count = 3; | 
|---|
| 693 | n/a | } | 
|---|
| 694 | n/a | else if (*cp <= 0xec) { | 
|---|
| 695 | n/a | lb = 0x80; ub = 0xbf; | 
|---|
| 696 | n/a | count = 3; | 
|---|
| 697 | n/a | } | 
|---|
| 698 | n/a | else if (*cp == 0xed) { | 
|---|
| 699 | n/a | lb = 0x80; ub = 0x9f; | 
|---|
| 700 | n/a | count = 3; | 
|---|
| 701 | n/a | } | 
|---|
| 702 | n/a | else if (*cp <= 0xef) { | 
|---|
| 703 | n/a | lb = 0x80; ub = 0xbf; | 
|---|
| 704 | n/a | count = 3; | 
|---|
| 705 | n/a | } | 
|---|
| 706 | n/a | else if (*cp == 0xf0) { | 
|---|
| 707 | n/a | lb = 0x90; ub = 0xbf; | 
|---|
| 708 | n/a | count = 4; | 
|---|
| 709 | n/a | } | 
|---|
| 710 | n/a | else if (*cp <= 0xf3) { | 
|---|
| 711 | n/a | lb = 0x80; ub = 0xbf; | 
|---|
| 712 | n/a | count = 4; | 
|---|
| 713 | n/a | } | 
|---|
| 714 | n/a | else if (*cp == 0xf4) { | 
|---|
| 715 | n/a | lb = 0x80; ub = 0x8f; | 
|---|
| 716 | n/a | count = 4; | 
|---|
| 717 | n/a | } | 
|---|
| 718 | n/a | else { | 
|---|
| 719 | n/a | /* invalid */ | 
|---|
| 720 | n/a | goto error; | 
|---|
| 721 | n/a | } | 
|---|
| 722 | n/a |  | 
|---|
| 723 | n/a | dest[0] = *cp++; | 
|---|
| 724 | n/a | if (*cp < lb || ub < *cp) { | 
|---|
| 725 | n/a | goto error; | 
|---|
| 726 | n/a | } | 
|---|
| 727 | n/a | dest[1] = *cp++; | 
|---|
| 728 | n/a | for (i = 2; i < count; i++) { | 
|---|
| 729 | n/a | if (*cp < 0x80 || 0xbf < *cp) { | 
|---|
| 730 | n/a | goto error; | 
|---|
| 731 | n/a | } | 
|---|
| 732 | n/a | dest[i] = *cp++; | 
|---|
| 733 | n/a | } | 
|---|
| 734 | n/a | dest[i] = '\0'; | 
|---|
| 735 | n/a |  | 
|---|
| 736 | n/a | return count; | 
|---|
| 737 | n/a |  | 
|---|
| 738 | n/a | error: | 
|---|
| 739 | n/a | dest[0] = '\0'; | 
|---|
| 740 | n/a | return -1; | 
|---|
| 741 | n/a | } | 
|---|
| 742 | n/a |  | 
|---|
| 743 | n/a | int | 
|---|
| 744 | n/a | mpd_validate_lconv(mpd_spec_t *spec) | 
|---|
| 745 | n/a | { | 
|---|
| 746 | n/a | size_t n; | 
|---|
| 747 | n/a | #if CHAR_MAX == SCHAR_MAX | 
|---|
| 748 | n/a | const char *cp = spec->grouping; | 
|---|
| 749 | n/a | while (*cp != '\0') { | 
|---|
| 750 | n/a | if (*cp++ < 0) { | 
|---|
| 751 | n/a | return -1; | 
|---|
| 752 | n/a | } | 
|---|
| 753 | n/a | } | 
|---|
| 754 | n/a | #endif | 
|---|
| 755 | n/a | n = strlen(spec->dot); | 
|---|
| 756 | n/a | if (n == 0 || n > 4) { | 
|---|
| 757 | n/a | return -1; | 
|---|
| 758 | n/a | } | 
|---|
| 759 | n/a | if (strlen(spec->sep) > 4) { | 
|---|
| 760 | n/a | return -1; | 
|---|
| 761 | n/a | } | 
|---|
| 762 | n/a |  | 
|---|
| 763 | n/a | return 0; | 
|---|
| 764 | n/a | } | 
|---|
| 765 | n/a |  | 
|---|
| 766 | n/a | int | 
|---|
| 767 | n/a | mpd_parse_fmt_str(mpd_spec_t *spec, const char *fmt, int caps) | 
|---|
| 768 | n/a | { | 
|---|
| 769 | n/a | char *cp = (char *)fmt; | 
|---|
| 770 | n/a | int have_align = 0, n; | 
|---|
| 771 | n/a |  | 
|---|
| 772 | n/a | /* defaults */ | 
|---|
| 773 | n/a | spec->min_width = 0; | 
|---|
| 774 | n/a | spec->prec = -1; | 
|---|
| 775 | n/a | spec->type = caps ? 'G' : 'g'; | 
|---|
| 776 | n/a | spec->align = '>'; | 
|---|
| 777 | n/a | spec->sign = '-'; | 
|---|
| 778 | n/a | spec->dot = ""; | 
|---|
| 779 | n/a | spec->sep = ""; | 
|---|
| 780 | n/a | spec->grouping = ""; | 
|---|
| 781 | n/a |  | 
|---|
| 782 | n/a |  | 
|---|
| 783 | n/a | /* presume that the first character is a UTF-8 fill character */ | 
|---|
| 784 | n/a | if ((n = _mpd_copy_utf8(spec->fill, cp)) < 0) { | 
|---|
| 785 | n/a | return 0; | 
|---|
| 786 | n/a | } | 
|---|
| 787 | n/a |  | 
|---|
| 788 | n/a | /* alignment directive, prefixed by a fill character */ | 
|---|
| 789 | n/a | if (*cp && (*(cp+n) == '<' || *(cp+n) == '>' || | 
|---|
| 790 | n/a | *(cp+n) == '=' || *(cp+n) == '^')) { | 
|---|
| 791 | n/a | cp += n; | 
|---|
| 792 | n/a | spec->align = *cp++; | 
|---|
| 793 | n/a | have_align = 1; | 
|---|
| 794 | n/a | } /* alignment directive */ | 
|---|
| 795 | n/a | else { | 
|---|
| 796 | n/a | /* default fill character */ | 
|---|
| 797 | n/a | spec->fill[0] = ' '; | 
|---|
| 798 | n/a | spec->fill[1] = '\0'; | 
|---|
| 799 | n/a | if (*cp == '<' || *cp == '>' || | 
|---|
| 800 | n/a | *cp == '=' || *cp == '^') { | 
|---|
| 801 | n/a | spec->align = *cp++; | 
|---|
| 802 | n/a | have_align = 1; | 
|---|
| 803 | n/a | } | 
|---|
| 804 | n/a | } | 
|---|
| 805 | n/a |  | 
|---|
| 806 | n/a | /* sign formatting */ | 
|---|
| 807 | n/a | if (*cp == '+' || *cp == '-' || *cp == ' ') { | 
|---|
| 808 | n/a | spec->sign = *cp++; | 
|---|
| 809 | n/a | } | 
|---|
| 810 | n/a |  | 
|---|
| 811 | n/a | /* zero padding */ | 
|---|
| 812 | n/a | if (*cp == '0') { | 
|---|
| 813 | n/a | /* zero padding implies alignment, which should not be | 
|---|
| 814 | n/a | * specified twice. */ | 
|---|
| 815 | n/a | if (have_align) { | 
|---|
| 816 | n/a | return 0; | 
|---|
| 817 | n/a | } | 
|---|
| 818 | n/a | spec->align = 'z'; | 
|---|
| 819 | n/a | spec->fill[0] = *cp++; | 
|---|
| 820 | n/a | spec->fill[1] = '\0'; | 
|---|
| 821 | n/a | } | 
|---|
| 822 | n/a |  | 
|---|
| 823 | n/a | /* minimum width */ | 
|---|
| 824 | n/a | if (isdigit((uchar)*cp)) { | 
|---|
| 825 | n/a | if (*cp == '0') { | 
|---|
| 826 | n/a | return 0; | 
|---|
| 827 | n/a | } | 
|---|
| 828 | n/a | errno = 0; | 
|---|
| 829 | n/a | spec->min_width = mpd_strtossize(cp, &cp, 10); | 
|---|
| 830 | n/a | if (errno == ERANGE || errno == EINVAL) { | 
|---|
| 831 | n/a | return 0; | 
|---|
| 832 | n/a | } | 
|---|
| 833 | n/a | } | 
|---|
| 834 | n/a |  | 
|---|
| 835 | n/a | /* thousands separator */ | 
|---|
| 836 | n/a | if (*cp == ',') { | 
|---|
| 837 | n/a | spec->dot = "."; | 
|---|
| 838 | n/a | spec->sep = ","; | 
|---|
| 839 | n/a | spec->grouping = "\003\003"; | 
|---|
| 840 | n/a | cp++; | 
|---|
| 841 | n/a | } | 
|---|
| 842 | n/a |  | 
|---|
| 843 | n/a | /* fraction digits or significant digits */ | 
|---|
| 844 | n/a | if (*cp == '.') { | 
|---|
| 845 | n/a | cp++; | 
|---|
| 846 | n/a | if (!isdigit((uchar)*cp)) { | 
|---|
| 847 | n/a | return 0; | 
|---|
| 848 | n/a | } | 
|---|
| 849 | n/a | errno = 0; | 
|---|
| 850 | n/a | spec->prec = mpd_strtossize(cp, &cp, 10); | 
|---|
| 851 | n/a | if (errno == ERANGE || errno == EINVAL) { | 
|---|
| 852 | n/a | return 0; | 
|---|
| 853 | n/a | } | 
|---|
| 854 | n/a | } | 
|---|
| 855 | n/a |  | 
|---|
| 856 | n/a | /* type */ | 
|---|
| 857 | n/a | if (*cp == 'E' || *cp == 'e' || *cp == 'F' || *cp == 'f' || | 
|---|
| 858 | n/a | *cp == 'G' || *cp == 'g' || *cp == '%') { | 
|---|
| 859 | n/a | spec->type = *cp++; | 
|---|
| 860 | n/a | } | 
|---|
| 861 | n/a | else if (*cp == 'N' || *cp == 'n') { | 
|---|
| 862 | n/a | /* locale specific conversion */ | 
|---|
| 863 | n/a | struct lconv *lc; | 
|---|
| 864 | n/a | /* separator has already been specified */ | 
|---|
| 865 | n/a | if (*spec->sep) { | 
|---|
| 866 | n/a | return 0; | 
|---|
| 867 | n/a | } | 
|---|
| 868 | n/a | spec->type = *cp++; | 
|---|
| 869 | n/a | spec->type = (spec->type == 'N') ? 'G' : 'g'; | 
|---|
| 870 | n/a | lc = localeconv(); | 
|---|
| 871 | n/a | spec->dot = lc->decimal_point; | 
|---|
| 872 | n/a | spec->sep = lc->thousands_sep; | 
|---|
| 873 | n/a | spec->grouping = lc->grouping; | 
|---|
| 874 | n/a | if (mpd_validate_lconv(spec) < 0) { | 
|---|
| 875 | n/a | return 0; /* GCOV_NOT_REACHED */ | 
|---|
| 876 | n/a | } | 
|---|
| 877 | n/a | } | 
|---|
| 878 | n/a |  | 
|---|
| 879 | n/a | /* check correctness */ | 
|---|
| 880 | n/a | if (*cp != '\0') { | 
|---|
| 881 | n/a | return 0; | 
|---|
| 882 | n/a | } | 
|---|
| 883 | n/a |  | 
|---|
| 884 | n/a | return 1; | 
|---|
| 885 | n/a | } | 
|---|
| 886 | n/a |  | 
|---|
| 887 | n/a | /* | 
|---|
| 888 | n/a | * The following functions assume that spec->min_width <= MPD_MAX_PREC, which | 
|---|
| 889 | n/a | * is made sure in mpd_qformat_spec. Then, even with a spec that inserts a | 
|---|
| 890 | n/a | * four-byte separator after each digit, nbytes in the following struct | 
|---|
| 891 | n/a | * cannot overflow. | 
|---|
| 892 | n/a | */ | 
|---|
| 893 | n/a |  | 
|---|
| 894 | n/a | /* Multibyte string */ | 
|---|
| 895 | n/a | typedef struct { | 
|---|
| 896 | n/a | mpd_ssize_t nbytes; /* length in bytes */ | 
|---|
| 897 | n/a | mpd_ssize_t nchars; /* length in chars */ | 
|---|
| 898 | n/a | mpd_ssize_t cur;    /* current write index */ | 
|---|
| 899 | n/a | char *data; | 
|---|
| 900 | n/a | } mpd_mbstr_t; | 
|---|
| 901 | n/a |  | 
|---|
| 902 | n/a | static inline void | 
|---|
| 903 | n/a | _mpd_bcopy(char *dest, const char *src, mpd_ssize_t n) | 
|---|
| 904 | n/a | { | 
|---|
| 905 | n/a | while (--n >= 0) { | 
|---|
| 906 | n/a | dest[n] = src[n]; | 
|---|
| 907 | n/a | } | 
|---|
| 908 | n/a | } | 
|---|
| 909 | n/a |  | 
|---|
| 910 | n/a | static inline void | 
|---|
| 911 | n/a | _mbstr_copy_char(mpd_mbstr_t *dest, const char *src, mpd_ssize_t n) | 
|---|
| 912 | n/a | { | 
|---|
| 913 | n/a | dest->nbytes += n; | 
|---|
| 914 | n/a | dest->nchars += (n > 0 ? 1 : 0); | 
|---|
| 915 | n/a | dest->cur -= n; | 
|---|
| 916 | n/a |  | 
|---|
| 917 | n/a | if (dest->data != NULL) { | 
|---|
| 918 | n/a | _mpd_bcopy(dest->data+dest->cur, src, n); | 
|---|
| 919 | n/a | } | 
|---|
| 920 | n/a | } | 
|---|
| 921 | n/a |  | 
|---|
| 922 | n/a | static inline void | 
|---|
| 923 | n/a | _mbstr_copy_ascii(mpd_mbstr_t *dest, const char *src, mpd_ssize_t n) | 
|---|
| 924 | n/a | { | 
|---|
| 925 | n/a | dest->nbytes += n; | 
|---|
| 926 | n/a | dest->nchars += n; | 
|---|
| 927 | n/a | dest->cur -= n; | 
|---|
| 928 | n/a |  | 
|---|
| 929 | n/a | if (dest->data != NULL) { | 
|---|
| 930 | n/a | _mpd_bcopy(dest->data+dest->cur, src, n); | 
|---|
| 931 | n/a | } | 
|---|
| 932 | n/a | } | 
|---|
| 933 | n/a |  | 
|---|
| 934 | n/a | static inline void | 
|---|
| 935 | n/a | _mbstr_copy_pad(mpd_mbstr_t *dest, mpd_ssize_t n) | 
|---|
| 936 | n/a | { | 
|---|
| 937 | n/a | dest->nbytes += n; | 
|---|
| 938 | n/a | dest->nchars += n; | 
|---|
| 939 | n/a | dest->cur -= n; | 
|---|
| 940 | n/a |  | 
|---|
| 941 | n/a | if (dest->data != NULL) { | 
|---|
| 942 | n/a | char *cp = dest->data + dest->cur; | 
|---|
| 943 | n/a | while (--n >= 0) { | 
|---|
| 944 | n/a | cp[n] = '0'; | 
|---|
| 945 | n/a | } | 
|---|
| 946 | n/a | } | 
|---|
| 947 | n/a | } | 
|---|
| 948 | n/a |  | 
|---|
| 949 | n/a | /* | 
|---|
| 950 | n/a | * Copy a numeric string to dest->data, adding separators in the integer | 
|---|
| 951 | n/a | * part according to spec->grouping. If leading zero padding is enabled | 
|---|
| 952 | n/a | * and the result is smaller than spec->min_width, continue adding zeros | 
|---|
| 953 | n/a | * and separators until the minimum width is reached. | 
|---|
| 954 | n/a | * | 
|---|
| 955 | n/a | * The final length of dest->data is stored in dest->nbytes. The number | 
|---|
| 956 | n/a | * of UTF-8 characters is stored in dest->nchars. | 
|---|
| 957 | n/a | * | 
|---|
| 958 | n/a | * First run (dest->data == NULL): determine the length of the result | 
|---|
| 959 | n/a | * string and store it in dest->nbytes. | 
|---|
| 960 | n/a | * | 
|---|
| 961 | n/a | * Second run (write to dest->data): data is written in chunks and in | 
|---|
| 962 | n/a | * reverse order, starting with the rest of the numeric string. | 
|---|
| 963 | n/a | */ | 
|---|
| 964 | n/a | static void | 
|---|
| 965 | n/a | _mpd_add_sep_dot(mpd_mbstr_t *dest, | 
|---|
| 966 | n/a | const char *sign, /* location of optional sign */ | 
|---|
| 967 | n/a | const char *src, mpd_ssize_t n_src, /* integer part and length */ | 
|---|
| 968 | n/a | const char *dot, /* location of optional decimal point */ | 
|---|
| 969 | n/a | const char *rest, mpd_ssize_t n_rest, /* remaining part and length */ | 
|---|
| 970 | n/a | const mpd_spec_t *spec) | 
|---|
| 971 | n/a | { | 
|---|
| 972 | n/a | mpd_ssize_t n_sep, n_sign, consume; | 
|---|
| 973 | n/a | const char *g; | 
|---|
| 974 | n/a | int pad = 0; | 
|---|
| 975 | n/a |  | 
|---|
| 976 | n/a | n_sign = sign ? 1 : 0; | 
|---|
| 977 | n/a | n_sep = (mpd_ssize_t)strlen(spec->sep); | 
|---|
| 978 | n/a | /* Initial write index: set to location of '\0' in the output string. | 
|---|
| 979 | n/a | * Irrelevant for the first run. */ | 
|---|
| 980 | n/a | dest->cur = dest->nbytes; | 
|---|
| 981 | n/a | dest->nbytes = dest->nchars = 0; | 
|---|
| 982 | n/a |  | 
|---|
| 983 | n/a | _mbstr_copy_ascii(dest, rest, n_rest); | 
|---|
| 984 | n/a |  | 
|---|
| 985 | n/a | if (dot) { | 
|---|
| 986 | n/a | _mbstr_copy_char(dest, dot, (mpd_ssize_t)strlen(dot)); | 
|---|
| 987 | n/a | } | 
|---|
| 988 | n/a |  | 
|---|
| 989 | n/a | g = spec->grouping; | 
|---|
| 990 | n/a | consume = *g; | 
|---|
| 991 | n/a | while (1) { | 
|---|
| 992 | n/a | /* If the group length is 0 or CHAR_MAX or greater than the | 
|---|
| 993 | n/a | * number of source bytes, consume all remaining bytes. */ | 
|---|
| 994 | n/a | if (*g == 0 || *g == CHAR_MAX || consume > n_src) { | 
|---|
| 995 | n/a | consume = n_src; | 
|---|
| 996 | n/a | } | 
|---|
| 997 | n/a | n_src -= consume; | 
|---|
| 998 | n/a | if (pad) { | 
|---|
| 999 | n/a | _mbstr_copy_pad(dest, consume); | 
|---|
| 1000 | n/a | } | 
|---|
| 1001 | n/a | else { | 
|---|
| 1002 | n/a | _mbstr_copy_ascii(dest, src+n_src, consume); | 
|---|
| 1003 | n/a | } | 
|---|
| 1004 | n/a |  | 
|---|
| 1005 | n/a | if (n_src == 0) { | 
|---|
| 1006 | n/a | /* Either the real source of intpart digits or the virtual | 
|---|
| 1007 | n/a | * source of padding zeros is exhausted. */ | 
|---|
| 1008 | n/a | if (spec->align == 'z' && | 
|---|
| 1009 | n/a | dest->nchars + n_sign < spec->min_width) { | 
|---|
| 1010 | n/a | /* Zero padding is set and length < min_width: | 
|---|
| 1011 | n/a | * Generate n_src additional characters. */ | 
|---|
| 1012 | n/a | n_src = spec->min_width - (dest->nchars + n_sign); | 
|---|
| 1013 | n/a | /* Next iteration: | 
|---|
| 1014 | n/a | *   case *g == 0 || *g == CHAR_MAX: | 
|---|
| 1015 | n/a | *      consume all padding characters | 
|---|
| 1016 | n/a | *   case consume < g*: | 
|---|
| 1017 | n/a | *      fill remainder of current group | 
|---|
| 1018 | n/a | *   case consume == g* | 
|---|
| 1019 | n/a | *      copying is a no-op */ | 
|---|
| 1020 | n/a | consume = *g - consume; | 
|---|
| 1021 | n/a | /* Switch on virtual source of zeros. */ | 
|---|
| 1022 | n/a | pad = 1; | 
|---|
| 1023 | n/a | continue; | 
|---|
| 1024 | n/a | } | 
|---|
| 1025 | n/a | break; | 
|---|
| 1026 | n/a | } | 
|---|
| 1027 | n/a |  | 
|---|
| 1028 | n/a | if (n_sep > 0) { | 
|---|
| 1029 | n/a | /* If padding is switched on, separators are counted | 
|---|
| 1030 | n/a | * as padding characters. This rule does not apply if | 
|---|
| 1031 | n/a | * the separator would be the first character of the | 
|---|
| 1032 | n/a | * result string. */ | 
|---|
| 1033 | n/a | if (pad && n_src > 1) n_src -= 1; | 
|---|
| 1034 | n/a | _mbstr_copy_char(dest, spec->sep, n_sep); | 
|---|
| 1035 | n/a | } | 
|---|
| 1036 | n/a |  | 
|---|
| 1037 | n/a | /* If non-NUL, use the next value for grouping. */ | 
|---|
| 1038 | n/a | if (*g && *(g+1)) g++; | 
|---|
| 1039 | n/a | consume = *g; | 
|---|
| 1040 | n/a | } | 
|---|
| 1041 | n/a |  | 
|---|
| 1042 | n/a | if (sign) { | 
|---|
| 1043 | n/a | _mbstr_copy_ascii(dest, sign, 1); | 
|---|
| 1044 | n/a | } | 
|---|
| 1045 | n/a |  | 
|---|
| 1046 | n/a | if (dest->data) { | 
|---|
| 1047 | n/a | dest->data[dest->nbytes] = '\0'; | 
|---|
| 1048 | n/a | } | 
|---|
| 1049 | n/a | } | 
|---|
| 1050 | n/a |  | 
|---|
| 1051 | n/a | /* | 
|---|
| 1052 | n/a | * Convert a numeric-string to its locale-specific appearance. | 
|---|
| 1053 | n/a | * The string must have one of these forms: | 
|---|
| 1054 | n/a | * | 
|---|
| 1055 | n/a | *     1) [sign] digits [exponent-part] | 
|---|
| 1056 | n/a | *     2) [sign] digits '.' [digits] [exponent-part] | 
|---|
| 1057 | n/a | * | 
|---|
| 1058 | n/a | * Not allowed, since _mpd_to_string() never returns this form: | 
|---|
| 1059 | n/a | * | 
|---|
| 1060 | n/a | *     3) [sign] '.' digits [exponent-part] | 
|---|
| 1061 | n/a | * | 
|---|
| 1062 | n/a | * Input: result->data := original numeric string (ASCII) | 
|---|
| 1063 | n/a | *        result->bytes := strlen(result->data) | 
|---|
| 1064 | n/a | *        result->nchars := strlen(result->data) | 
|---|
| 1065 | n/a | * | 
|---|
| 1066 | n/a | * Output: result->data := modified or original string | 
|---|
| 1067 | n/a | *         result->bytes := strlen(result->data) | 
|---|
| 1068 | n/a | *         result->nchars := number of characters (possibly UTF-8) | 
|---|
| 1069 | n/a | */ | 
|---|
| 1070 | n/a | static int | 
|---|
| 1071 | n/a | _mpd_apply_lconv(mpd_mbstr_t *result, const mpd_spec_t *spec, uint32_t *status) | 
|---|
| 1072 | n/a | { | 
|---|
| 1073 | n/a | const char *sign = NULL, *intpart = NULL, *dot = NULL; | 
|---|
| 1074 | n/a | const char *rest, *dp; | 
|---|
| 1075 | n/a | char *decstring; | 
|---|
| 1076 | n/a | mpd_ssize_t n_int, n_rest; | 
|---|
| 1077 | n/a |  | 
|---|
| 1078 | n/a | /* original numeric string */ | 
|---|
| 1079 | n/a | dp = result->data; | 
|---|
| 1080 | n/a |  | 
|---|
| 1081 | n/a | /* sign */ | 
|---|
| 1082 | n/a | if (*dp == '+' || *dp == '-' || *dp == ' ') { | 
|---|
| 1083 | n/a | sign = dp++; | 
|---|
| 1084 | n/a | } | 
|---|
| 1085 | n/a | /* integer part */ | 
|---|
| 1086 | n/a | assert(isdigit((uchar)*dp)); | 
|---|
| 1087 | n/a | intpart = dp++; | 
|---|
| 1088 | n/a | while (isdigit((uchar)*dp)) { | 
|---|
| 1089 | n/a | dp++; | 
|---|
| 1090 | n/a | } | 
|---|
| 1091 | n/a | n_int = (mpd_ssize_t)(dp-intpart); | 
|---|
| 1092 | n/a | /* decimal point */ | 
|---|
| 1093 | n/a | if (*dp == '.') { | 
|---|
| 1094 | n/a | dp++; dot = spec->dot; | 
|---|
| 1095 | n/a | } | 
|---|
| 1096 | n/a | /* rest */ | 
|---|
| 1097 | n/a | rest = dp; | 
|---|
| 1098 | n/a | n_rest = result->nbytes - (mpd_ssize_t)(dp-result->data); | 
|---|
| 1099 | n/a |  | 
|---|
| 1100 | n/a | if (dot == NULL && (*spec->sep == '\0' || *spec->grouping == '\0')) { | 
|---|
| 1101 | n/a | /* _mpd_add_sep_dot() would not change anything */ | 
|---|
| 1102 | n/a | return 1; | 
|---|
| 1103 | n/a | } | 
|---|
| 1104 | n/a |  | 
|---|
| 1105 | n/a | /* Determine the size of the new decimal string after inserting the | 
|---|
| 1106 | n/a | * decimal point, optional separators and optional padding. */ | 
|---|
| 1107 | n/a | decstring = result->data; | 
|---|
| 1108 | n/a | result->data = NULL; | 
|---|
| 1109 | n/a | _mpd_add_sep_dot(result, sign, intpart, n_int, dot, | 
|---|
| 1110 | n/a | rest, n_rest, spec); | 
|---|
| 1111 | n/a |  | 
|---|
| 1112 | n/a | result->data = mpd_alloc(result->nbytes+1, 1); | 
|---|
| 1113 | n/a | if (result->data == NULL) { | 
|---|
| 1114 | n/a | *status |= MPD_Malloc_error; | 
|---|
| 1115 | n/a | mpd_free(decstring); | 
|---|
| 1116 | n/a | return 0; | 
|---|
| 1117 | n/a | } | 
|---|
| 1118 | n/a |  | 
|---|
| 1119 | n/a | /* Perform actual writes. */ | 
|---|
| 1120 | n/a | _mpd_add_sep_dot(result, sign, intpart, n_int, dot, | 
|---|
| 1121 | n/a | rest, n_rest, spec); | 
|---|
| 1122 | n/a |  | 
|---|
| 1123 | n/a | mpd_free(decstring); | 
|---|
| 1124 | n/a | return 1; | 
|---|
| 1125 | n/a | } | 
|---|
| 1126 | n/a |  | 
|---|
| 1127 | n/a | /* Add padding to the formatted string if necessary. */ | 
|---|
| 1128 | n/a | static int | 
|---|
| 1129 | n/a | _mpd_add_pad(mpd_mbstr_t *result, const mpd_spec_t *spec, uint32_t *status) | 
|---|
| 1130 | n/a | { | 
|---|
| 1131 | n/a | if (result->nchars < spec->min_width) { | 
|---|
| 1132 | n/a | mpd_ssize_t add_chars, add_bytes; | 
|---|
| 1133 | n/a | size_t lpad = 0, rpad = 0; | 
|---|
| 1134 | n/a | size_t n_fill, len, i, j; | 
|---|
| 1135 | n/a | char align = spec->align; | 
|---|
| 1136 | n/a | uint8_t err = 0; | 
|---|
| 1137 | n/a | char *cp; | 
|---|
| 1138 | n/a |  | 
|---|
| 1139 | n/a | n_fill = strlen(spec->fill); | 
|---|
| 1140 | n/a | add_chars = (spec->min_width - result->nchars); | 
|---|
| 1141 | n/a | /* max value: MPD_MAX_PREC * 4 */ | 
|---|
| 1142 | n/a | add_bytes = add_chars * (mpd_ssize_t)n_fill; | 
|---|
| 1143 | n/a |  | 
|---|
| 1144 | n/a | cp = result->data = mpd_realloc(result->data, | 
|---|
| 1145 | n/a | result->nbytes+add_bytes+1, | 
|---|
| 1146 | n/a | sizeof *result->data, &err); | 
|---|
| 1147 | n/a | if (err) { | 
|---|
| 1148 | n/a | *status |= MPD_Malloc_error; | 
|---|
| 1149 | n/a | mpd_free(result->data); | 
|---|
| 1150 | n/a | return 0; | 
|---|
| 1151 | n/a | } | 
|---|
| 1152 | n/a |  | 
|---|
| 1153 | n/a | if (align == 'z') { | 
|---|
| 1154 | n/a | align = '='; | 
|---|
| 1155 | n/a | } | 
|---|
| 1156 | n/a |  | 
|---|
| 1157 | n/a | if (align == '<') { | 
|---|
| 1158 | n/a | rpad = add_chars; | 
|---|
| 1159 | n/a | } | 
|---|
| 1160 | n/a | else if (align == '>' || align == '=') { | 
|---|
| 1161 | n/a | lpad = add_chars; | 
|---|
| 1162 | n/a | } | 
|---|
| 1163 | n/a | else { /* align == '^' */ | 
|---|
| 1164 | n/a | lpad = add_chars/2; | 
|---|
| 1165 | n/a | rpad = add_chars-lpad; | 
|---|
| 1166 | n/a | } | 
|---|
| 1167 | n/a |  | 
|---|
| 1168 | n/a | len = result->nbytes; | 
|---|
| 1169 | n/a | if (align == '=' && (*cp == '-' || *cp == '+' || *cp == ' ')) { | 
|---|
| 1170 | n/a | /* leave sign in the leading position */ | 
|---|
| 1171 | n/a | cp++; len--; | 
|---|
| 1172 | n/a | } | 
|---|
| 1173 | n/a |  | 
|---|
| 1174 | n/a | memmove(cp+n_fill*lpad, cp, len); | 
|---|
| 1175 | n/a | for (i = 0; i < lpad; i++) { | 
|---|
| 1176 | n/a | for (j = 0; j < n_fill; j++) { | 
|---|
| 1177 | n/a | cp[i*n_fill+j] = spec->fill[j]; | 
|---|
| 1178 | n/a | } | 
|---|
| 1179 | n/a | } | 
|---|
| 1180 | n/a | cp += (n_fill*lpad + len); | 
|---|
| 1181 | n/a | for (i = 0; i < rpad; i++) { | 
|---|
| 1182 | n/a | for (j = 0; j < n_fill; j++) { | 
|---|
| 1183 | n/a | cp[i*n_fill+j] = spec->fill[j]; | 
|---|
| 1184 | n/a | } | 
|---|
| 1185 | n/a | } | 
|---|
| 1186 | n/a |  | 
|---|
| 1187 | n/a | result->nbytes += add_bytes; | 
|---|
| 1188 | n/a | result->nchars += add_chars; | 
|---|
| 1189 | n/a | result->data[result->nbytes] = '\0'; | 
|---|
| 1190 | n/a | } | 
|---|
| 1191 | n/a |  | 
|---|
| 1192 | n/a | return 1; | 
|---|
| 1193 | n/a | } | 
|---|
| 1194 | n/a |  | 
|---|
| 1195 | n/a | /* Round a number to prec digits. The adjusted exponent stays the same | 
|---|
| 1196 | n/a | or increases by one if rounding up crosses a power of ten boundary. | 
|---|
| 1197 | n/a | If result->digits would exceed MPD_MAX_PREC+1, MPD_Invalid_operation | 
|---|
| 1198 | n/a | is set and the result is NaN. */ | 
|---|
| 1199 | n/a | static inline void | 
|---|
| 1200 | n/a | _mpd_round(mpd_t *result, const mpd_t *a, mpd_ssize_t prec, | 
|---|
| 1201 | n/a | const mpd_context_t *ctx, uint32_t *status) | 
|---|
| 1202 | n/a | { | 
|---|
| 1203 | n/a | mpd_ssize_t exp = a->exp + a->digits - prec; | 
|---|
| 1204 | n/a |  | 
|---|
| 1205 | n/a | if (prec <= 0) { | 
|---|
| 1206 | n/a | mpd_seterror(result, MPD_Invalid_operation, status); /* GCOV_NOT_REACHED */ | 
|---|
| 1207 | n/a | return; /* GCOV_NOT_REACHED */ | 
|---|
| 1208 | n/a | } | 
|---|
| 1209 | n/a | if (mpd_isspecial(a) || mpd_iszero(a)) { | 
|---|
| 1210 | n/a | mpd_qcopy(result, a, status); /* GCOV_NOT_REACHED */ | 
|---|
| 1211 | n/a | return; /* GCOV_NOT_REACHED */ | 
|---|
| 1212 | n/a | } | 
|---|
| 1213 | n/a |  | 
|---|
| 1214 | n/a | mpd_qrescale_fmt(result, a, exp, ctx, status); | 
|---|
| 1215 | n/a | if (result->digits > prec) { | 
|---|
| 1216 | n/a | mpd_qrescale_fmt(result, result, exp+1, ctx, status); | 
|---|
| 1217 | n/a | } | 
|---|
| 1218 | n/a | } | 
|---|
| 1219 | n/a |  | 
|---|
| 1220 | n/a | /* | 
|---|
| 1221 | n/a | * Return the string representation of an mpd_t, formatted according to 'spec'. | 
|---|
| 1222 | n/a | * The format specification is assumed to be valid. Memory errors are indicated | 
|---|
| 1223 | n/a | * as usual. This function is quiet. | 
|---|
| 1224 | n/a | */ | 
|---|
| 1225 | n/a | char * | 
|---|
| 1226 | n/a | mpd_qformat_spec(const mpd_t *dec, const mpd_spec_t *spec, | 
|---|
| 1227 | n/a | const mpd_context_t *ctx, uint32_t *status) | 
|---|
| 1228 | n/a | { | 
|---|
| 1229 | n/a | mpd_uint_t dt[MPD_MINALLOC_MAX]; | 
|---|
| 1230 | n/a | mpd_t tmp = {MPD_STATIC|MPD_STATIC_DATA,0,0,0,MPD_MINALLOC_MAX,dt}; | 
|---|
| 1231 | n/a | mpd_ssize_t dplace = MPD_DEFAULT_DOTPLACE; | 
|---|
| 1232 | n/a | mpd_mbstr_t result; | 
|---|
| 1233 | n/a | mpd_spec_t stackspec; | 
|---|
| 1234 | n/a | char type = spec->type; | 
|---|
| 1235 | n/a | int flags = 0; | 
|---|
| 1236 | n/a |  | 
|---|
| 1237 | n/a |  | 
|---|
| 1238 | n/a | if (spec->min_width > MPD_MAX_PREC) { | 
|---|
| 1239 | n/a | *status |= MPD_Invalid_operation; | 
|---|
| 1240 | n/a | return NULL; | 
|---|
| 1241 | n/a | } | 
|---|
| 1242 | n/a |  | 
|---|
| 1243 | n/a | if (isupper((uchar)type)) { | 
|---|
| 1244 | n/a | type = tolower((uchar)type); | 
|---|
| 1245 | n/a | flags |= MPD_FMT_UPPER; | 
|---|
| 1246 | n/a | } | 
|---|
| 1247 | n/a | if (spec->sign == ' ') { | 
|---|
| 1248 | n/a | flags |= MPD_FMT_SIGN_SPACE; | 
|---|
| 1249 | n/a | } | 
|---|
| 1250 | n/a | else if (spec->sign == '+') { | 
|---|
| 1251 | n/a | flags |= MPD_FMT_SIGN_PLUS; | 
|---|
| 1252 | n/a | } | 
|---|
| 1253 | n/a |  | 
|---|
| 1254 | n/a | if (mpd_isspecial(dec)) { | 
|---|
| 1255 | n/a | if (spec->align == 'z') { | 
|---|
| 1256 | n/a | stackspec = *spec; | 
|---|
| 1257 | n/a | stackspec.fill[0] = ' '; | 
|---|
| 1258 | n/a | stackspec.fill[1] = '\0'; | 
|---|
| 1259 | n/a | stackspec.align = '>'; | 
|---|
| 1260 | n/a | spec = &stackspec; | 
|---|
| 1261 | n/a | } | 
|---|
| 1262 | n/a | if (type == '%') { | 
|---|
| 1263 | n/a | flags |= MPD_FMT_PERCENT; | 
|---|
| 1264 | n/a | } | 
|---|
| 1265 | n/a | } | 
|---|
| 1266 | n/a | else { | 
|---|
| 1267 | n/a | uint32_t workstatus = 0; | 
|---|
| 1268 | n/a | mpd_ssize_t prec; | 
|---|
| 1269 | n/a |  | 
|---|
| 1270 | n/a | switch (type) { | 
|---|
| 1271 | n/a | case 'g': flags |= MPD_FMT_TOSCI; break; | 
|---|
| 1272 | n/a | case 'e': flags |= MPD_FMT_EXP; break; | 
|---|
| 1273 | n/a | case '%': flags |= MPD_FMT_PERCENT; | 
|---|
| 1274 | n/a | if (!mpd_qcopy(&tmp, dec, status)) { | 
|---|
| 1275 | n/a | return NULL; | 
|---|
| 1276 | n/a | } | 
|---|
| 1277 | n/a | tmp.exp += 2; | 
|---|
| 1278 | n/a | dec = &tmp; | 
|---|
| 1279 | n/a | type = 'f'; /* fall through */ | 
|---|
| 1280 | n/a | case 'f': flags |= MPD_FMT_FIXED; break; | 
|---|
| 1281 | n/a | default: abort(); /* debug: GCOV_NOT_REACHED */ | 
|---|
| 1282 | n/a | } | 
|---|
| 1283 | n/a |  | 
|---|
| 1284 | n/a | if (spec->prec >= 0) { | 
|---|
| 1285 | n/a | if (spec->prec > MPD_MAX_PREC) { | 
|---|
| 1286 | n/a | *status |= MPD_Invalid_operation; | 
|---|
| 1287 | n/a | goto error; | 
|---|
| 1288 | n/a | } | 
|---|
| 1289 | n/a |  | 
|---|
| 1290 | n/a | switch (type) { | 
|---|
| 1291 | n/a | case 'g': | 
|---|
| 1292 | n/a | prec = (spec->prec == 0) ? 1 : spec->prec; | 
|---|
| 1293 | n/a | if (dec->digits > prec) { | 
|---|
| 1294 | n/a | _mpd_round(&tmp, dec, prec, ctx, | 
|---|
| 1295 | n/a | &workstatus); | 
|---|
| 1296 | n/a | dec = &tmp; | 
|---|
| 1297 | n/a | } | 
|---|
| 1298 | n/a | break; | 
|---|
| 1299 | n/a | case 'e': | 
|---|
| 1300 | n/a | if (mpd_iszero(dec)) { | 
|---|
| 1301 | n/a | dplace = 1-spec->prec; | 
|---|
| 1302 | n/a | } | 
|---|
| 1303 | n/a | else { | 
|---|
| 1304 | n/a | _mpd_round(&tmp, dec, spec->prec+1, ctx, | 
|---|
| 1305 | n/a | &workstatus); | 
|---|
| 1306 | n/a | dec = &tmp; | 
|---|
| 1307 | n/a | } | 
|---|
| 1308 | n/a | break; | 
|---|
| 1309 | n/a | case 'f': | 
|---|
| 1310 | n/a | mpd_qrescale(&tmp, dec, -spec->prec, ctx, | 
|---|
| 1311 | n/a | &workstatus); | 
|---|
| 1312 | n/a | dec = &tmp; | 
|---|
| 1313 | n/a | break; | 
|---|
| 1314 | n/a | } | 
|---|
| 1315 | n/a | } | 
|---|
| 1316 | n/a |  | 
|---|
| 1317 | n/a | if (type == 'f') { | 
|---|
| 1318 | n/a | if (mpd_iszero(dec) && dec->exp > 0) { | 
|---|
| 1319 | n/a | mpd_qrescale(&tmp, dec, 0, ctx, &workstatus); | 
|---|
| 1320 | n/a | dec = &tmp; | 
|---|
| 1321 | n/a | } | 
|---|
| 1322 | n/a | } | 
|---|
| 1323 | n/a |  | 
|---|
| 1324 | n/a | if (workstatus&MPD_Errors) { | 
|---|
| 1325 | n/a | *status |= (workstatus&MPD_Errors); | 
|---|
| 1326 | n/a | goto error; | 
|---|
| 1327 | n/a | } | 
|---|
| 1328 | n/a | } | 
|---|
| 1329 | n/a |  | 
|---|
| 1330 | n/a | /* | 
|---|
| 1331 | n/a | * At this point, for all scaled or non-scaled decimals: | 
|---|
| 1332 | n/a | *   1) 1 <= digits <= MAX_PREC+1 | 
|---|
| 1333 | n/a | *   2) adjexp(scaled) = adjexp(orig) [+1] | 
|---|
| 1334 | n/a | *   3)   case 'g': MIN_ETINY <= exp <= MAX_EMAX+1 | 
|---|
| 1335 | n/a | *        case 'e': MIN_ETINY-MAX_PREC <= exp <= MAX_EMAX+1 | 
|---|
| 1336 | n/a | *        case 'f': MIN_ETINY <= exp <= MAX_EMAX+1 | 
|---|
| 1337 | n/a | *   4) max memory alloc in _mpd_to_string: | 
|---|
| 1338 | n/a | *        case 'g': MAX_PREC+36 | 
|---|
| 1339 | n/a | *        case 'e': MAX_PREC+36 | 
|---|
| 1340 | n/a | *        case 'f': 2*MPD_MAX_PREC+30 | 
|---|
| 1341 | n/a | */ | 
|---|
| 1342 | n/a | result.nbytes = _mpd_to_string(&result.data, dec, flags, dplace); | 
|---|
| 1343 | n/a | result.nchars = result.nbytes; | 
|---|
| 1344 | n/a | if (result.nbytes < 0) { | 
|---|
| 1345 | n/a | *status |= MPD_Malloc_error; | 
|---|
| 1346 | n/a | goto error; | 
|---|
| 1347 | n/a | } | 
|---|
| 1348 | n/a |  | 
|---|
| 1349 | n/a | if (*spec->dot != '\0' && !mpd_isspecial(dec)) { | 
|---|
| 1350 | n/a | if (result.nchars > MPD_MAX_PREC+36) { | 
|---|
| 1351 | n/a | /* Since a group length of one is not explicitly | 
|---|
| 1352 | n/a | * disallowed, ensure that it is always possible to | 
|---|
| 1353 | n/a | * insert a four byte separator after each digit. */ | 
|---|
| 1354 | n/a | *status |= MPD_Invalid_operation; | 
|---|
| 1355 | n/a | mpd_free(result.data); | 
|---|
| 1356 | n/a | goto error; | 
|---|
| 1357 | n/a | } | 
|---|
| 1358 | n/a | if (!_mpd_apply_lconv(&result, spec, status)) { | 
|---|
| 1359 | n/a | goto error; | 
|---|
| 1360 | n/a | } | 
|---|
| 1361 | n/a | } | 
|---|
| 1362 | n/a |  | 
|---|
| 1363 | n/a | if (spec->min_width) { | 
|---|
| 1364 | n/a | if (!_mpd_add_pad(&result, spec, status)) { | 
|---|
| 1365 | n/a | goto error; | 
|---|
| 1366 | n/a | } | 
|---|
| 1367 | n/a | } | 
|---|
| 1368 | n/a |  | 
|---|
| 1369 | n/a | mpd_del(&tmp); | 
|---|
| 1370 | n/a | return result.data; | 
|---|
| 1371 | n/a |  | 
|---|
| 1372 | n/a | error: | 
|---|
| 1373 | n/a | mpd_del(&tmp); | 
|---|
| 1374 | n/a | return NULL; | 
|---|
| 1375 | n/a | } | 
|---|
| 1376 | n/a |  | 
|---|
| 1377 | n/a | char * | 
|---|
| 1378 | n/a | mpd_qformat(const mpd_t *dec, const char *fmt, const mpd_context_t *ctx, | 
|---|
| 1379 | n/a | uint32_t *status) | 
|---|
| 1380 | n/a | { | 
|---|
| 1381 | n/a | mpd_spec_t spec; | 
|---|
| 1382 | n/a |  | 
|---|
| 1383 | n/a | if (!mpd_parse_fmt_str(&spec, fmt, 1)) { | 
|---|
| 1384 | n/a | *status |= MPD_Invalid_operation; | 
|---|
| 1385 | n/a | return NULL; | 
|---|
| 1386 | n/a | } | 
|---|
| 1387 | n/a |  | 
|---|
| 1388 | n/a | return mpd_qformat_spec(dec, &spec, ctx, status); | 
|---|
| 1389 | n/a | } | 
|---|
| 1390 | n/a |  | 
|---|
| 1391 | n/a | /* | 
|---|
| 1392 | n/a | * The specification has a *condition* called Invalid_operation and an | 
|---|
| 1393 | n/a | * IEEE *signal* called Invalid_operation. The former corresponds to | 
|---|
| 1394 | n/a | * MPD_Invalid_operation, the latter to MPD_IEEE_Invalid_operation. | 
|---|
| 1395 | n/a | * MPD_IEEE_Invalid_operation comprises the following conditions: | 
|---|
| 1396 | n/a | * | 
|---|
| 1397 | n/a | * [MPD_Conversion_syntax, MPD_Division_impossible, MPD_Division_undefined, | 
|---|
| 1398 | n/a | *  MPD_Fpu_error, MPD_Invalid_context, MPD_Invalid_operation, | 
|---|
| 1399 | n/a | *  MPD_Malloc_error] | 
|---|
| 1400 | n/a | * | 
|---|
| 1401 | n/a | * In the following functions, 'flag' denotes the condition, 'signal' | 
|---|
| 1402 | n/a | * denotes the IEEE signal. | 
|---|
| 1403 | n/a | */ | 
|---|
| 1404 | n/a |  | 
|---|
| 1405 | n/a | static const char *mpd_flag_string[MPD_NUM_FLAGS] = { | 
|---|
| 1406 | n/a | "Clamped", | 
|---|
| 1407 | n/a | "Conversion_syntax", | 
|---|
| 1408 | n/a | "Division_by_zero", | 
|---|
| 1409 | n/a | "Division_impossible", | 
|---|
| 1410 | n/a | "Division_undefined", | 
|---|
| 1411 | n/a | "Fpu_error", | 
|---|
| 1412 | n/a | "Inexact", | 
|---|
| 1413 | n/a | "Invalid_context", | 
|---|
| 1414 | n/a | "Invalid_operation", | 
|---|
| 1415 | n/a | "Malloc_error", | 
|---|
| 1416 | n/a | "Not_implemented", | 
|---|
| 1417 | n/a | "Overflow", | 
|---|
| 1418 | n/a | "Rounded", | 
|---|
| 1419 | n/a | "Subnormal", | 
|---|
| 1420 | n/a | "Underflow", | 
|---|
| 1421 | n/a | }; | 
|---|
| 1422 | n/a |  | 
|---|
| 1423 | n/a | static const char *mpd_signal_string[MPD_NUM_FLAGS] = { | 
|---|
| 1424 | n/a | "Clamped", | 
|---|
| 1425 | n/a | "IEEE_Invalid_operation", | 
|---|
| 1426 | n/a | "Division_by_zero", | 
|---|
| 1427 | n/a | "IEEE_Invalid_operation", | 
|---|
| 1428 | n/a | "IEEE_Invalid_operation", | 
|---|
| 1429 | n/a | "IEEE_Invalid_operation", | 
|---|
| 1430 | n/a | "Inexact", | 
|---|
| 1431 | n/a | "IEEE_Invalid_operation", | 
|---|
| 1432 | n/a | "IEEE_Invalid_operation", | 
|---|
| 1433 | n/a | "IEEE_Invalid_operation", | 
|---|
| 1434 | n/a | "Not_implemented", | 
|---|
| 1435 | n/a | "Overflow", | 
|---|
| 1436 | n/a | "Rounded", | 
|---|
| 1437 | n/a | "Subnormal", | 
|---|
| 1438 | n/a | "Underflow", | 
|---|
| 1439 | n/a | }; | 
|---|
| 1440 | n/a |  | 
|---|
| 1441 | n/a | /* print conditions to buffer, separated by spaces */ | 
|---|
| 1442 | n/a | int | 
|---|
| 1443 | n/a | mpd_snprint_flags(char *dest, int nmemb, uint32_t flags) | 
|---|
| 1444 | n/a | { | 
|---|
| 1445 | n/a | char *cp; | 
|---|
| 1446 | n/a | int n, j; | 
|---|
| 1447 | n/a |  | 
|---|
| 1448 | n/a | assert(nmemb >= MPD_MAX_FLAG_STRING); | 
|---|
| 1449 | n/a |  | 
|---|
| 1450 | n/a | *dest = '\0'; cp = dest; | 
|---|
| 1451 | n/a | for (j = 0; j < MPD_NUM_FLAGS; j++) { | 
|---|
| 1452 | n/a | if (flags & (1U<<j)) { | 
|---|
| 1453 | n/a | n = snprintf(cp, nmemb, "%s ", mpd_flag_string[j]); | 
|---|
| 1454 | n/a | if (n < 0 || n >= nmemb) return -1; | 
|---|
| 1455 | n/a | cp += n; nmemb -= n; | 
|---|
| 1456 | n/a | } | 
|---|
| 1457 | n/a | } | 
|---|
| 1458 | n/a |  | 
|---|
| 1459 | n/a | if (cp != dest) { | 
|---|
| 1460 | n/a | *(--cp) = '\0'; | 
|---|
| 1461 | n/a | } | 
|---|
| 1462 | n/a |  | 
|---|
| 1463 | n/a | return (int)(cp-dest); | 
|---|
| 1464 | n/a | } | 
|---|
| 1465 | n/a |  | 
|---|
| 1466 | n/a | /* print conditions to buffer, in list form */ | 
|---|
| 1467 | n/a | int | 
|---|
| 1468 | n/a | mpd_lsnprint_flags(char *dest, int nmemb, uint32_t flags, const char *flag_string[]) | 
|---|
| 1469 | n/a | { | 
|---|
| 1470 | n/a | char *cp; | 
|---|
| 1471 | n/a | int n, j; | 
|---|
| 1472 | n/a |  | 
|---|
| 1473 | n/a | assert(nmemb >= MPD_MAX_FLAG_LIST); | 
|---|
| 1474 | n/a | if (flag_string == NULL) { | 
|---|
| 1475 | n/a | flag_string = mpd_flag_string; | 
|---|
| 1476 | n/a | } | 
|---|
| 1477 | n/a |  | 
|---|
| 1478 | n/a | *dest = '['; | 
|---|
| 1479 | n/a | *(dest+1) = '\0'; | 
|---|
| 1480 | n/a | cp = dest+1; | 
|---|
| 1481 | n/a | --nmemb; | 
|---|
| 1482 | n/a |  | 
|---|
| 1483 | n/a | for (j = 0; j < MPD_NUM_FLAGS; j++) { | 
|---|
| 1484 | n/a | if (flags & (1U<<j)) { | 
|---|
| 1485 | n/a | n = snprintf(cp, nmemb, "%s, ", flag_string[j]); | 
|---|
| 1486 | n/a | if (n < 0 || n >= nmemb) return -1; | 
|---|
| 1487 | n/a | cp += n; nmemb -= n; | 
|---|
| 1488 | n/a | } | 
|---|
| 1489 | n/a | } | 
|---|
| 1490 | n/a |  | 
|---|
| 1491 | n/a | /* erase the last ", " */ | 
|---|
| 1492 | n/a | if (cp != dest+1) { | 
|---|
| 1493 | n/a | cp -= 2; | 
|---|
| 1494 | n/a | } | 
|---|
| 1495 | n/a |  | 
|---|
| 1496 | n/a | *cp++ = ']'; | 
|---|
| 1497 | n/a | *cp = '\0'; | 
|---|
| 1498 | n/a |  | 
|---|
| 1499 | n/a | return (int)(cp-dest); /* strlen, without NUL terminator */ | 
|---|
| 1500 | n/a | } | 
|---|
| 1501 | n/a |  | 
|---|
| 1502 | n/a | /* print signals to buffer, in list form */ | 
|---|
| 1503 | n/a | int | 
|---|
| 1504 | n/a | mpd_lsnprint_signals(char *dest, int nmemb, uint32_t flags, const char *signal_string[]) | 
|---|
| 1505 | n/a | { | 
|---|
| 1506 | n/a | char *cp; | 
|---|
| 1507 | n/a | int n, j; | 
|---|
| 1508 | n/a | int ieee_invalid_done = 0; | 
|---|
| 1509 | n/a |  | 
|---|
| 1510 | n/a | assert(nmemb >= MPD_MAX_SIGNAL_LIST); | 
|---|
| 1511 | n/a | if (signal_string == NULL) { | 
|---|
| 1512 | n/a | signal_string = mpd_signal_string; | 
|---|
| 1513 | n/a | } | 
|---|
| 1514 | n/a |  | 
|---|
| 1515 | n/a | *dest = '['; | 
|---|
| 1516 | n/a | *(dest+1) = '\0'; | 
|---|
| 1517 | n/a | cp = dest+1; | 
|---|
| 1518 | n/a | --nmemb; | 
|---|
| 1519 | n/a |  | 
|---|
| 1520 | n/a | for (j = 0; j < MPD_NUM_FLAGS; j++) { | 
|---|
| 1521 | n/a | uint32_t f = flags & (1U<<j); | 
|---|
| 1522 | n/a | if (f) { | 
|---|
| 1523 | n/a | if (f&MPD_IEEE_Invalid_operation) { | 
|---|
| 1524 | n/a | if (ieee_invalid_done) { | 
|---|
| 1525 | n/a | continue; | 
|---|
| 1526 | n/a | } | 
|---|
| 1527 | n/a | ieee_invalid_done = 1; | 
|---|
| 1528 | n/a | } | 
|---|
| 1529 | n/a | n = snprintf(cp, nmemb, "%s, ", signal_string[j]); | 
|---|
| 1530 | n/a | if (n < 0 || n >= nmemb) return -1; | 
|---|
| 1531 | n/a | cp += n; nmemb -= n; | 
|---|
| 1532 | n/a | } | 
|---|
| 1533 | n/a | } | 
|---|
| 1534 | n/a |  | 
|---|
| 1535 | n/a | /* erase the last ", " */ | 
|---|
| 1536 | n/a | if (cp != dest+1) { | 
|---|
| 1537 | n/a | cp -= 2; | 
|---|
| 1538 | n/a | } | 
|---|
| 1539 | n/a |  | 
|---|
| 1540 | n/a | *cp++ = ']'; | 
|---|
| 1541 | n/a | *cp = '\0'; | 
|---|
| 1542 | n/a |  | 
|---|
| 1543 | n/a | return (int)(cp-dest); /* strlen, without NUL terminator */ | 
|---|
| 1544 | n/a | } | 
|---|
| 1545 | n/a |  | 
|---|
| 1546 | n/a | /* The following two functions are mainly intended for debugging. */ | 
|---|
| 1547 | n/a | void | 
|---|
| 1548 | n/a | mpd_fprint(FILE *file, const mpd_t *dec) | 
|---|
| 1549 | n/a | { | 
|---|
| 1550 | n/a | char *decstring; | 
|---|
| 1551 | n/a |  | 
|---|
| 1552 | n/a | decstring = mpd_to_sci(dec, 1); | 
|---|
| 1553 | n/a | if (decstring != NULL) { | 
|---|
| 1554 | n/a | fprintf(file, "%s\n", decstring); | 
|---|
| 1555 | n/a | mpd_free(decstring); | 
|---|
| 1556 | n/a | } | 
|---|
| 1557 | n/a | else { | 
|---|
| 1558 | n/a | fputs("mpd_fprint: output error\n", file); /* GCOV_NOT_REACHED */ | 
|---|
| 1559 | n/a | } | 
|---|
| 1560 | n/a | } | 
|---|
| 1561 | n/a |  | 
|---|
| 1562 | n/a | void | 
|---|
| 1563 | n/a | mpd_print(const mpd_t *dec) | 
|---|
| 1564 | n/a | { | 
|---|
| 1565 | n/a | char *decstring; | 
|---|
| 1566 | n/a |  | 
|---|
| 1567 | n/a | decstring = mpd_to_sci(dec, 1); | 
|---|
| 1568 | n/a | if (decstring != NULL) { | 
|---|
| 1569 | n/a | printf("%s\n", decstring); | 
|---|
| 1570 | n/a | mpd_free(decstring); | 
|---|
| 1571 | n/a | } | 
|---|
| 1572 | n/a | else { | 
|---|
| 1573 | n/a | fputs("mpd_fprint: output error\n", stderr); /* GCOV_NOT_REACHED */ | 
|---|
| 1574 | n/a | } | 
|---|
| 1575 | n/a | } | 
|---|
| 1576 | n/a |  | 
|---|
| 1577 | n/a |  | 
|---|