home *** CD-ROM | disk | FTP | other *** search
/ ARM Club 3 / TheARMClub_PDCD3.iso / hensa / programming / tcl / tclsrc / c / tclGet < prev    next >
Encoding:
Text File  |  1996-01-28  |  5.7 KB  |  224 lines

  1. /*
  2.  * tclGet.c --
  3.  *
  4.  *    This file contains procedures to convert strings into
  5.  *    other forms, like integers or floating-point numbers or
  6.  *    booleans, doing syntax checking along the way.
  7.  *
  8.  * Copyright (c) 1990-1993 The Regents of the University of California.
  9.  * Copyright (c) 1994-1995 Sun Microsystems, Inc.
  10.  *
  11.  * See the file "license.terms" for information on usage and redistribution
  12.  * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
  13.  */
  14.  
  15. static char sccsid[] = "@(#) tclGet.c 1.20 95/02/14 13:14:34";
  16.  
  17. #include "tclInt.h"
  18. #ifndef TCL_GENERIC_ONLY /* not for RISCOS*/
  19. #include "tclPort.h"
  20. #endif
  21.  
  22.  
  23. /*
  24.  *----------------------------------------------------------------------
  25.  *
  26.  * Tcl_GetInt --
  27.  *
  28.  *    Given a string, produce the corresponding integer value.
  29.  *
  30.  * Results:
  31.  *    The return value is normally TCL_OK;  in this case *intPtr
  32.  *    will be set to the integer value equivalent to string.  If
  33.  *    string is improperly formed then TCL_ERROR is returned and
  34.  *    an error message will be left in interp->result.
  35.  *
  36.  * Side effects:
  37.  *    None.
  38.  *
  39.  *----------------------------------------------------------------------
  40.  */
  41.  
  42. int
  43. Tcl_GetInt(interp, string, intPtr)
  44.     Tcl_Interp *interp;        /* Interpreter to use for error reporting. */
  45.     char *string;        /* String containing a (possibly signed)
  46.                  * integer in a form acceptable to strtol. */
  47.     int *intPtr;        /* Place to store converted result. */
  48. {
  49.     char *end, *p;
  50.     int i;
  51.  
  52.     /*
  53.      * Note: use strtoul instead of strtol for integer conversions
  54.      * to allow full-size unsigned numbers, but don't depend on strtoul
  55.      * to handle sign characters;  it won't in some implementations.
  56.      */
  57.  
  58.     errno = 0;
  59.     for (p = string; isspace(UCHAR(*p)); p++) {
  60.     /* Empty loop body. */
  61.     }
  62.     if (*p == '-') {
  63.     p++;
  64.     i = -strtoul(p, &end, 0);
  65.     } else if (*p == '+') {
  66.     p++;
  67.     i = strtoul(p, &end, 0);
  68.     } else {
  69.     i = strtoul(p, &end, 0);
  70.     }
  71.     if (end == p) {
  72.     badInteger:
  73.     Tcl_AppendResult(interp, "expected integer but got \"", string,
  74.         "\"", (char *) NULL);
  75.     return TCL_ERROR;
  76.     }
  77.     if (errno == ERANGE) {
  78.     interp->result = "integer value too large to represent";
  79.     Tcl_SetErrorCode(interp, "ARITH", "IOVERFLOW",
  80.         interp->result, (char *) NULL);
  81.     return TCL_ERROR;
  82.     }
  83.     while ((*end != '\0') && isspace(UCHAR(*end))) {
  84.     end++;
  85.     }
  86.     if (*end != 0) {
  87.     goto badInteger;
  88.     }
  89.     *intPtr = i;
  90.     return TCL_OK;
  91. }
  92.  
  93. /*
  94.  *----------------------------------------------------------------------
  95.  *
  96.  * Tcl_GetDouble --
  97.  *
  98.  *    Given a string, produce the corresponding double-precision
  99.  *    floating-point value.
  100.  *
  101.  * Results:
  102.  *    The return value is normally TCL_OK;  in this case *doublePtr
  103.  *    will be set to the double-precision value equivalent to string.
  104.  *    If string is improperly formed then TCL_ERROR is returned and
  105.  *    an error message will be left in interp->result.
  106.  *
  107.  * Side effects:
  108.  *    None.
  109.  *
  110.  *----------------------------------------------------------------------
  111.  */
  112.  
  113. int
  114. Tcl_GetDouble(interp, string, doublePtr)
  115.     Tcl_Interp *interp;        /* Interpreter to use for error reporting. */
  116.     char *string;        /* String containing a floating-point number
  117.                  * in a form acceptable to strtod. */
  118.     double *doublePtr;        /* Place to store converted result. */
  119. {
  120.     char *end;
  121.     double d;
  122.  
  123.     errno = 0;
  124.     d = strtod(string, &end);
  125.     if (end == string) {
  126.     badDouble:
  127.     Tcl_AppendResult(interp, "expected floating-point number but got \"",
  128.         string, "\"", (char *) NULL);
  129.     return TCL_ERROR;
  130.     }
  131.     if (errno != 0) {
  132.     TclExprFloatError(interp, d);
  133.     return TCL_ERROR;
  134.     }
  135.     while ((*end != 0) && isspace(UCHAR(*end))) {
  136.     end++;
  137.     }
  138.     if (*end != 0) {
  139.     goto badDouble;
  140.     }
  141.     *doublePtr = d;
  142.     return TCL_OK;
  143. }
  144.  
  145. /*
  146.  *----------------------------------------------------------------------
  147.  *
  148.  * Tcl_GetBoolean --
  149.  *
  150.  *    Given a string, return a 0/1 boolean value corresponding
  151.  *    to the string.
  152.  *
  153.  * Results:
  154.  *    The return value is normally TCL_OK;  in this case *boolPtr
  155.  *    will be set to the 0/1 value equivalent to string.  If
  156.  *    string is improperly formed then TCL_ERROR is returned and
  157.  *    an error message will be left in interp->result.
  158.  *
  159.  * Side effects:
  160.  *    None.
  161.  *
  162.  *----------------------------------------------------------------------
  163.  */
  164.  
  165. int
  166. Tcl_GetBoolean(interp, string, boolPtr)
  167.     Tcl_Interp *interp;        /* Interpreter to use for error reporting. */
  168.     char *string;        /* String containing a boolean number
  169.                  * specified either as 1/0 or true/false or
  170.                  * yes/no. */
  171.     int *boolPtr;        /* Place to store converted result, which
  172.                  * will be 0 or 1. */
  173. {
  174.     int i, c;
  175.     char lowerCase[10];
  176.     size_t length;
  177.  
  178.     /*
  179.      * Convert the input string to all lower-case.
  180.      */
  181.  
  182.     for (i = 0; i < 9; i++) {
  183.     c = string[i];
  184.     if (c == 0) {
  185.         break;
  186.     }
  187.     if ((c >= 'A') && (c <= 'Z')) {
  188.         c += 'a' - 'A';
  189.     }
  190.     lowerCase[i] = c;
  191.     }
  192.     lowerCase[i] = 0;
  193.  
  194.     length = strlen(lowerCase);
  195.     c = lowerCase[0];
  196.     if ((c == '0') && (lowerCase[1] == '\0')) {
  197.     *boolPtr = 0;
  198.     } else if ((c == '1') && (lowerCase[1] == '\0')) {
  199.     *boolPtr = 1;
  200.     } else if ((c == 'y') && (strncmp(lowerCase, "yes", length) == 0)) {
  201.     *boolPtr = 1;
  202.     } else if ((c == 'n') && (strncmp(lowerCase, "no", length) == 0)) {
  203.     *boolPtr = 0;
  204.     } else if ((c == 't') && (strncmp(lowerCase, "true", length) == 0)) {
  205.     *boolPtr = 1;
  206.     } else if ((c == 'f') && (strncmp(lowerCase, "false", length) == 0)) {
  207.     *boolPtr = 0;
  208.     } else if ((c == 'o') && (length >= 2)) {
  209.     if (strncmp(lowerCase, "on", length) == 0) {
  210.         *boolPtr = 1;
  211.     } else if (strncmp(lowerCase, "off", length) == 0) {
  212.         *boolPtr = 0;
  213.     } else {
  214.         goto badBoolean;
  215.     }
  216.     } else {
  217.     badBoolean:
  218.     Tcl_AppendResult(interp, "expected boolean value but got \"",
  219.         string, "\"", (char *) NULL);
  220.     return TCL_ERROR;
  221.     }
  222.     return TCL_OK;
  223. }
  224.