home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / tcl2-73c.zip / tcl7.3 / tclGet.c < prev    next >
C/C++ Source or Header  |  1993-08-18  |  6KB  |  211 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.  * All rights reserved.
  10.  *
  11.  * Permission is hereby granted, without written agreement and without
  12.  * license or royalty fees, to use, copy, modify, and distribute this
  13.  * software and its documentation for any purpose, provided that the
  14.  * above copyright notice and the following two paragraphs appear in
  15.  * all copies of this software.
  16.  * 
  17.  * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR
  18.  * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
  19.  * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF
  20.  * CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  21.  *
  22.  * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
  23.  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
  24.  * AND FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
  25.  * ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO
  26.  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
  27.  */
  28.  
  29. #ifndef lint
  30. static char rcsid[] = "$Header: /user6/ouster/tcl/RCS/tclGet.c,v 1.14 93/08/18 16:07:24 ouster Exp $ SPRITE (Berkeley)";
  31. #endif /* not lint */
  32.  
  33. #include "tclInt.h"
  34.  
  35. /*
  36.  *----------------------------------------------------------------------
  37.  *
  38.  * Tcl_GetInt --
  39.  *
  40.  *    Given a string, produce the corresponding integer value.
  41.  *
  42.  * Results:
  43.  *    The return value is normally TCL_OK;  in this case *intPtr
  44.  *    will be set to the integer value equivalent to string.  If
  45.  *    string is improperly formed then TCL_ERROR is returned and
  46.  *    an error message will be left in interp->result.
  47.  *
  48.  * Side effects:
  49.  *    None.
  50.  *
  51.  *----------------------------------------------------------------------
  52.  */
  53.  
  54. int
  55. Tcl_GetInt(interp, string, intPtr)
  56.     Tcl_Interp *interp;        /* Interpreter to use for error reporting. */
  57.     char *string;        /* String containing a (possibly signed)
  58.                  * integer in a form acceptable to strtol. */
  59.     int *intPtr;        /* Place to store converted result. */
  60. {
  61.     char *end, *p;
  62.     int i;
  63.  
  64.     /*
  65.      * Note: use strtoul instead of strtol for integer conversions
  66.      * to allow full-size unsigned numbers, but don't depend on strtoul
  67.      * to handle sign characters;  it won't in some implementations.
  68.      */
  69.  
  70.     for (p = string; isspace(UCHAR(*p)); p++) {
  71.     /* Empty loop body. */
  72.     }
  73.     if (*p == '-') {
  74.     i = -strtoul(p+1, &end, 0);
  75.     } else if (*p == '+') {
  76.     i = strtoul(p+1, &end, 0);
  77.     } else {
  78.     i = strtoul(p, &end, 0);
  79.     }
  80.     while ((*end != '\0') && isspace(UCHAR(*end))) {
  81.     end++;
  82.     }
  83.     if ((end == string) || (*end != 0)) {
  84.     Tcl_AppendResult(interp, "expected integer but got \"", string,
  85.         "\"", (char *) NULL);
  86.     return TCL_ERROR;
  87.     }
  88.     *intPtr = i;
  89.     return TCL_OK;
  90. }
  91.  
  92. /*
  93.  *----------------------------------------------------------------------
  94.  *
  95.  * Tcl_GetDouble --
  96.  *
  97.  *    Given a string, produce the corresponding double-precision
  98.  *    floating-point value.
  99.  *
  100.  * Results:
  101.  *    The return value is normally TCL_OK;  in this case *doublePtr
  102.  *    will be set to the double-precision value equivalent to string.
  103.  *    If string is improperly formed then TCL_ERROR is returned and
  104.  *    an error message will be left in interp->result.
  105.  *
  106.  * Side effects:
  107.  *    None.
  108.  *
  109.  *----------------------------------------------------------------------
  110.  */
  111.  
  112. int
  113. Tcl_GetDouble(interp, string, doublePtr)
  114.     Tcl_Interp *interp;        /* Interpreter to use for error reporting. */
  115.     char *string;        /* String containing a floating-point number
  116.                  * in a form acceptable to strtod. */
  117.     double *doublePtr;        /* Place to store converted result. */
  118. {
  119.     char *end;
  120.     double d;
  121.  
  122.     d = strtod(string, &end);
  123.     while ((*end != '\0') && isspace(UCHAR(*end))) {
  124.     end++;
  125.     }
  126.     if ((end == string) || (*end != 0)) {
  127.     Tcl_AppendResult(interp, "expected floating-point number but got \"",
  128.         string, "\"", (char *) NULL);
  129.     return TCL_ERROR;
  130.     }
  131.     *doublePtr = d;
  132.     return TCL_OK;
  133. }
  134.  
  135. /*
  136.  *----------------------------------------------------------------------
  137.  *
  138.  * Tcl_GetBoolean --
  139.  *
  140.  *    Given a string, return a 0/1 boolean value corresponding
  141.  *    to the string.
  142.  *
  143.  * Results:
  144.  *    The return value is normally TCL_OK;  in this case *boolPtr
  145.  *    will be set to the 0/1 value equivalent to string.  If
  146.  *    string is improperly formed then TCL_ERROR is returned and
  147.  *    an error message will be left in interp->result.
  148.  *
  149.  * Side effects:
  150.  *    None.
  151.  *
  152.  *----------------------------------------------------------------------
  153.  */
  154.  
  155. int
  156. Tcl_GetBoolean(interp, string, boolPtr)
  157.     Tcl_Interp *interp;        /* Interpreter to use for error reporting. */
  158.     char *string;        /* String containing a boolean number
  159.                  * specified either as 1/0 or true/false or
  160.                  * yes/no. */
  161.     int *boolPtr;        /* Place to store converted result, which
  162.                  * will be 0 or 1. */
  163. {
  164.     char c;
  165.     char lowerCase[10];
  166.     int i, length;
  167.  
  168.     /*
  169.      * Convert the input string to all lower-case.
  170.      */
  171.  
  172.     for (i = 0; i < 9; i++) {
  173.     c = string[i];
  174.     if (c == 0) {
  175.         break;
  176.     }
  177.     if ((c >= 'A') && (c <= 'Z')) {
  178.         c += 'a' - 'A';
  179.     }
  180.     lowerCase[i] = c;
  181.     }
  182.     lowerCase[i] = 0;
  183.  
  184.     length = strlen(lowerCase);
  185.     c = lowerCase[0];
  186.     if ((c == '0') && (lowerCase[1] == '\0')) {
  187.     *boolPtr = 0;
  188.     } else if ((c == '1') && (lowerCase[1] == '\0')) {
  189.     *boolPtr = 1;
  190.     } else if ((c == 'y') && (strncmp(lowerCase, "yes", length) == 0)) {
  191.     *boolPtr = 1;
  192.     } else if ((c == 'n') && (strncmp(lowerCase, "no", length) == 0)) {
  193.     *boolPtr = 0;
  194.     } else if ((c == 't') && (strncmp(lowerCase, "true", length) == 0)) {
  195.     *boolPtr = 1;
  196.     } else if ((c == 'f') && (strncmp(lowerCase, "false", length) == 0)) {
  197.     *boolPtr = 0;
  198.     } else if ((c == 'o') && (length >= 2)) {
  199.     if (strncmp(lowerCase, "on", length) == 0) {
  200.         *boolPtr = 1;
  201.     } else if (strncmp(lowerCase, "off", length) == 0) {
  202.         *boolPtr = 0;
  203.     }
  204.     } else {
  205.     Tcl_AppendResult(interp, "expected boolean value but got \"",
  206.         string, "\"", (char *) NULL);
  207.     return TCL_ERROR;
  208.     }
  209.     return TCL_OK;
  210. }
  211.