home *** CD-ROM | disk | FTP | other *** search
- /* This source file is part of the LynxLib miscellaneous library by
- Robert Fischer, and is Copyright 1990 by Robert Fischer. It costs no
- money, and you may not make money off of it, but you may redistribute
- it. It comes with ABSOLUTELY NO WARRANTY. See the file LYNXLIB.DOC
- for more details.
- To contact the author:
- Robert Fischer \\80 Killdeer Rd \\Hamden, CT 06517 USA
- (203) 288-9599 fischer-robert@cs.yale.edu */
-
- #include <stddef.h>
- /* Routines for converting between integers and strings */
- /* By Robert Fischer March 23, 1987 */
- /* ---------------------------------------------------- */
- #define BETWEEN(ch, low, high) ((ch) >= (low) && (ch) <= (high))
- int digit(ch)
- int ch;
- {
- if (BETWEEN(ch, '0', '9')) return ch - '0';
- if (BETWEEN(ch, 'a', 'z')) return ch - 'a' + 10;
- if (BETWEEN(ch, 'A', 'Z')) return ch - 'A' + 10;
- return -1;
- }
-
- char find_char(digit)
- int digit;
- {
- if (BETWEEN(digit, 0, 9)) return (char)(digit + (int)'0');
- return (char)(digit - 10 + (int)'A');
- }
-
- /*---------------------------------------------------*/
- BOOLEAN natoi(stringg, base, result)
- char *stringg;
- int base;
- int *result;
- /* Returning a FALSE indicates an error. */
- {
- int i;
- int dig;
- int number;
- BOOLEAN er;
- BOOLEAN neg;
- char *c;
-
- er = FALSE;
- neg = FALSE;
- number = 0;
-
- for (c = stringg; *c != NIL; c++) {
- if (stringg[i] == '-') neg = TRUE;
- else {
- dig = digit((int)(*c));
- if ((dig >= base) || (dig == -1)) er = TRUE;
- else number = base*number + dig;
- }
- }
-
- if (!er) {
- if (neg) *result = -number;
- else *result = number;
- }
- return !er;
- }
- /*----------------------------------------------------------------*/
- BOOLEAN natol(stringg, base, result)
- char *stringg;
- int base;
- long *result;
- /* Returning a FALSE indicates an error. */
- {
- int i;
- int dig;
- long number;
- BOOLEAN er;
- BOOLEAN neg;
- char *c;
-
- er = FALSE;
- neg = FALSE;
- number = 0;
-
- for (c = stringg; *c != NIL; c++) {
- if (stringg[i] == '-') neg = TRUE;
- else {
- dig = digit((int)(*c));
- if ((dig >= base) || (dig == -1)) er = TRUE;
- else number = base*number + dig;
- }
- }
-
- if (!er) {
- if (neg) *result = -number;
- else *result = number;
- }
- return !er;
- }
- /*----------------------------------------------------------------*/
- /* Converts integer to string of specified length. */
- /* Fills with leading zeros. */
- fitoa(val, len, base, s)
- int val, len, base;
- char *s;
- {
-
- int i,d;
- int oval;
- oval = val;
- val = abs(val);
- for (i = len-1; i >= 0; i--) {
- d = val % base;
- val = val / base;
- s[i] = find_char(d);
- }
- s[len] = '\0';
- if (oval < 0) s[0] = '-';
- }
- /*----------------------------------------------------*/
- /* Converts unsigned integer to string of specified length. */
- /* Fills with leading zeros. */
- ufitoa(val, len, base, s)
- unsigned val, len, base;
- char *s;
- {
- unsigned d;
- int i;
- for (i = len-1; i >= 0; i--) {
- d = val % base;
- val = val / base;
- s[i] = find_char(d);
- }
- s[len] = '\0';
- }
- /*----------------------------------------------------*/
- char *catoi(stringg, base, result)
- /* Converts a string to an integer and
- returns *character of 1st non-numeral */
- char *stringg;
- int base;
- int *result;
- /* If no number found, doesn't destroy the previous contents of *result */
- {
- int dig;
- int number;
- BOOLEAN neg, er;
- char *c;
- if (*stringg == NIL) return stringg;
-
- er = FALSE;
- neg = FALSE;
- number = 0;
-
- for (c = stringg; *c != NIL; c++) {
- if (*c == '-') neg = TRUE;
- else {
- dig = digit((int)(*c));
- if ((dig >= base) || (dig == -1)) return c;
- else number = base*number + dig;
- }
- }
-
- if (neg) *result = -number;
- else *result = number;
-
- return c;
- }
- /* ------------------------------------------------------------------ */
-