diff options
Diffstat (limited to 'src/H5.c')
-rw-r--r-- | src/H5.c | 369 |
1 files changed, 369 insertions, 0 deletions
@@ -35,6 +35,10 @@ static char RcsId[] = "@(#)$Revision$"; H5_init_interface -- initialize the H5 interface + */ +#include <ctype.h> +#include <errno.h> +#include <stdarg.h> + /* private headers */ #include <H5private.h> /*library */ #include <H5ACprivate.h> /*cache */ @@ -369,3 +373,368 @@ H5close (void) H5_term_library (); return SUCCEED; } + + +/*------------------------------------------------------------------------- + * Function: HDfprintf + * + * Purpose: Prints the optional arguments under the control of the format + * string FMT to the stream STREAM. This function takes the + * same format as fprintf(3c) with a few added features: + * + * The conversion modifier `H' refers to the size of an + * `hsize_t' or `hssize_t' type. For instance, "0x%018Hx" + * prints an `hsize_t' value as a hex number right justified and + * zero filled in an 18-character field. + * + * The conversion `a' refers to an `haddr_t*' type. Field widths + * and other flags are ignored and the address is printed by + * calling H5F_addr_print(). + * + * Bugs: Return value will be incorrect if `%a' appears in the format + * string. + * + * Return: Success: Number of characters printed + * + * Failure: -1 + * + * Programmer: Robb Matzke + * Thursday, April 9, 1998 + * + * Modifications: + * + *------------------------------------------------------------------------- + */ +int +HDfprintf (FILE *stream, const char *fmt, ...) +{ + int n, nout = 0; + int fwidth, prec; + int zerofill; + int leftjust; + int plussign; + int ldspace; + int prefix; + int modifier; + int conv; + char *rest, template[128]; + const char *s; + va_list ap; + + assert (stream); + assert (fmt); + + va_start (ap, fmt); + while (*fmt) { + fwidth = prec = 0; + zerofill = 0; + leftjust = 0; + plussign = 0; + prefix = 0; + ldspace = 0; + modifier = 0; + + if ('%'==fmt[0] && '%'==fmt[1]) { + putc ('%', stream); + fmt += 2; + nout++; + } else if ('%'==fmt[0]) { + s = fmt+1; + + /* Flags */ + while (strchr ("-+ #", *s)) { + switch (*s) { + case '-': + leftjust = 1; + break; + case '+': + plussign = 1; + break; + case ' ': + ldspace = 1; + break; + case '#': + prefix = 1; + break; + } + s++; + } + + /* Field width */ + if (isdigit (*s)) { + zerofill = ('0'==*s); + fwidth = strtol (s, &rest, 10); + s = rest; + } else if ('*'==*s) { + fwidth = va_arg (ap, int); + if (fwidth<0) { + leftjust = 1; + fwidth = -fwidth; + } + s++; + } + + /* Precision */ + if ('.'==*s) { + s++; + if (isdigit (*s)) { + prec = strtol (s+1, &rest, 10); + s = rest; + } else if ('*'==*s) { + prec = va_arg (ap, int); + s++; + } + if (prec<1) prec = 1; + } + + /* Type modifier */ + if (strchr ("Hhlq", *s)) { + switch (*s) { + case 'H': + if (sizeof(hsize_t)==sizeof(long)) { + modifier = 'l'; + } else if (sizeof(hsize_t)==sizeof(long long)) { + modifier = 'q'; + } + break; + default: + modifier = *s; + break; + } + s++; + } + + /* Conversion */ + conv = *s++; + + /* Create the format template */ + sprintf (template, "%%%s%s%s%s%s", + leftjust?"-":"", plussign?"+":"", + ldspace?" ":"", prefix?"#":"", zerofill?"0":""); + if (fwidth>0) { + sprintf (template+strlen (template), "%d", fwidth); + } + if (prec>0) { + sprintf (template+strlen (template), ".%d", prec); + } + if (modifier) { + sprintf (template+strlen (template), "%c", modifier); + } + sprintf (template+strlen (template), "%c", conv); + + + /* Conversion */ + switch (conv) { + case 'd': + case 'i': + if ('h'==modifier) { + short x = va_arg (ap, short); + n = fprintf (stream, template, x); + } else if (!modifier) { + int x = va_arg (ap, int); + n = fprintf (stream, template, x); + } else if ('l'==modifier) { + long x = va_arg (ap, long); + n = fprintf (stream, template, x); + } else if ('q'==modifier) { + long long x = va_arg (ap, long long); + n = fprintf (stream, template, x); + } + break; + + case 'o': + case 'u': + case 'x': + case 'X': + if ('h'==modifier) { + unsigned short x = va_arg (ap, unsigned short); + n = fprintf (stream, template, x); + } else if (!modifier) { + unsigned int x = va_arg (ap, unsigned int); + n = fprintf (stream, template, x); + } else if ('l'==modifier) { + unsigned long x = va_arg (ap, unsigned long); + n = fprintf (stream, template, x); + } else if ('q'==modifier) { + unsigned long long x = va_arg (ap, unsigned long long); + n = fprintf (stream, template, x); + } + break; + + case 'f': + case 'e': + case 'E': + case 'g': + case 'G': + if ('h'==modifier) { + float x = va_arg (ap, float); + n = fprintf (stream, template, x); + } else if (!modifier || 'l'==modifier) { + double x = va_arg (ap, double); + n = fprintf (stream, template, x); + } else if ('q'==modifier) { + long double x = va_arg (ap, long double); + n = fprintf (stream, template, x); + } + + case 'a': + if (1) { + haddr_t *x = va_arg (ap, haddr_t*); + H5F_addr_print (stream, x); + n = 0; + } + break; + + case 'c': + if (1) { + char x = va_arg (ap, char); + n = fprintf (stream, template, x); + } + break; + + case 's': + case 'p': + if (1) { + char *x = va_arg (ap, char*); + n = fprintf (stream, template, x); + } + break; + + case 'n': + if (1) { + template[strlen(template)-1] = 'u'; + n = fprintf (stream, template, nout); + } + break; + + default: + fputs (template, stream); + n = strlen (template); + break; + } + nout += n; + fmt = s; + } else { + putc (*fmt, stream); + fmt++; + nout++; + } + } + va_end (ap); + return nout; +} + + +/*------------------------------------------------------------------------- + * Function: HDstrtoll + * + * Purpose: Converts the string S to an int64 value according to the + * given BASE, which must be between 2 and 36 inclusive, or be + * the special value zero. + * + * The string must begin with an arbitrary amount of white space + * (as determined by isspace(3c)) followed by a single optional + * `+' or `-' sign. If BASE is zero or 16 the string may then + * include a `0x' or `0X' prefix, and the number will be read in + * base 16; otherwise a zero BASE is taken as 10 (decimal) + * unless the next character is a `0', in which case it is taken + * as 8 (octal). + * + * The remainder of the string is converted to an int64 in the + * obvious manner, stopping at the first character which is not + * a valid digit in the given base. (In bases above 10, the + * letter `A' in either upper or lower case represetns 10, `B' + * represents 11, and so forth, with `Z' representing 35.) + * + * If REST is not null, the address of the first invalid + * character in S is stored in *REST. If there were no digits + * at all, the original value of S is stored in *REST. Thus, if + * *S is not `\0' but **REST is `\0' on return the entire string + * was valid. + * + * Return: Success: The result. + * + * Failure: If the input string does not contain any + * digits then zero is returned and REST points + * to the original value of S. If an overflow + * or underflow occurs then the maximum or + * minimum possible value is returned and the + * global `errno' is set to ERANGE. If BASE is + * incorrect then zero is returned. + * + * Programmer: Robb Matzke + * Thursday, April 9, 1998 + * + * Modifications: + * + *------------------------------------------------------------------------- + */ +int64 +HDstrtoll (const char *s, const char **rest, int base) +{ + int64 sign=1, acc=0; + hbool_t overflow = FALSE; + + errno = 0; + if (!s || (base && (base<2 || base>36))) { + if (rest) *rest = s; + return 0; + } + + /* Skip white space */ + while (isspace (*s)) s++; + + /* Optional minus or plus sign */ + if ('+'==*s) { + s++; + } else if ('-'==*s) { + sign = -1; + s++; + } + + /* Zero base prefix */ + if (0==base && '0'==*s && ('x'==s[1] || 'X'==s[1])) { + base = 16; + s += 2; + } else if (0==base && '0'==*s) { + base = 8; + s++; + } else if (0==base) { + base = 10; + } + + /* Digits */ + while ((base<=10 && *s>='0' && *s<'0'+base) || + (base>10 && ((*s>='0' && *s<='9') || + (*s>='a' && *s<'a'+base-10) || + (*s>='A' && *s<'A'+base-10)))) { + if (!overflow) { + int64 digit; + if (*s>='0' && *s<='9') digit = *s - '0'; + else if (*s>='a' && *s<='z') digit = *s-'a'+10; + else digit = *s-'A'+10; + + if (acc*base+digit < acc) { + overflow = TRUE; + } else { + acc = acc*base + digit; + } + } + s++; + } + + /* Overflow */ + if (overflow) { + if (sign>0) { + acc = ((uint64)1<<(8*sizeof(int64)-1))-1; + } else { + acc = (uint64)1<<(8*sizeof(int64)-1); + } + errno = ERANGE; + } + + /* Return values */ + acc *= sign; + if (rest) *rest = s; + return acc; +} |