home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / languages / tcl / tcl7.0b1 / tclGet.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-02-06  |  5.7 KB  |  196 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.12 93/02/06 16:20:34 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;
  62.     int i;
  63.  
  64.     i = strtol(string, &end, 0);
  65.     while ((*end != '\0') && isspace(*end)) {
  66.     end++;
  67.     }
  68.     if ((end == string) || (*end != 0)) {
  69.     Tcl_AppendResult(interp, "expected integer but got \"", string,
  70.         "\"", (char *) NULL);
  71.     return TCL_ERROR;
  72.     }
  73.     *intPtr = i;
  74.     return TCL_OK;
  75. }
  76.  
  77. /*
  78.  *----------------------------------------------------------------------
  79.  *
  80.  * Tcl_GetDouble --
  81.  *
  82.  *    Given a string, produce the corresponding double-precision
  83.  *    floating-point value.
  84.  *
  85.  * Results:
  86.  *    The return value is normally TCL_OK;  in this case *doublePtr
  87.  *    will be set to the double-precision value equivalent to string.
  88.  *    If string is improperly formed then TCL_ERROR is returned and
  89.  *    an error message will be left in interp->result.
  90.  *
  91.  * Side effects:
  92.  *    None.
  93.  *
  94.  *----------------------------------------------------------------------
  95.  */
  96.  
  97. int
  98. Tcl_GetDouble(interp, string, doublePtr)
  99.     Tcl_Interp *interp;        /* Interpreter to use for error reporting. */
  100.     char *string;        /* String containing a floating-point number
  101.                  * in a form acceptable to strtod. */
  102.     double *doublePtr;        /* Place to store converted result. */
  103. {
  104.     char *end;
  105.     double d;
  106.  
  107.     d = strtod(string, &end);
  108.     while ((*end != '\0') && isspace(*end)) {
  109.     end++;
  110.     }
  111.     if ((end == string) || (*end != 0)) {
  112.     Tcl_AppendResult(interp, "expected floating-point number but got \"",
  113.         string, "\"", (char *) NULL);
  114.     return TCL_ERROR;
  115.     }
  116.     *doublePtr = d;
  117.     return TCL_OK;
  118. }
  119.  
  120. /*
  121.  *----------------------------------------------------------------------
  122.  *
  123.  * Tcl_GetBoolean --
  124.  *
  125.  *    Given a string, return a 0/1 boolean value corresponding
  126.  *    to the string.
  127.  *
  128.  * Results:
  129.  *    The return value is normally TCL_OK;  in this case *boolPtr
  130.  *    will be set to the 0/1 value equivalent to string.  If
  131.  *    string is improperly formed then TCL_ERROR is returned and
  132.  *    an error message will be left in interp->result.
  133.  *
  134.  * Side effects:
  135.  *    None.
  136.  *
  137.  *----------------------------------------------------------------------
  138.  */
  139.  
  140. int
  141. Tcl_GetBoolean(interp, string, boolPtr)
  142.     Tcl_Interp *interp;        /* Interpreter to use for error reporting. */
  143.     char *string;        /* String containing a boolean number
  144.                  * specified either as 1/0 or true/false or
  145.                  * yes/no. */
  146.     int *boolPtr;        /* Place to store converted result, which
  147.                  * will be 0 or 1. */
  148. {
  149.     char c;
  150.     char lowerCase[10];
  151.     int i, length;
  152.  
  153.     /*
  154.      * Convert the input string to all lower-case.
  155.      */
  156.  
  157.     for (i = 0; i < 9; i++) {
  158.     c = string[i];
  159.     if (c == 0) {
  160.         break;
  161.     }
  162.     if ((c >= 'A') && (c <= 'Z')) {
  163.         c += 'a' - 'A';
  164.     }
  165.     lowerCase[i] = c;
  166.     }
  167.     lowerCase[i] = 0;
  168.  
  169.     length = strlen(lowerCase);
  170.     c = lowerCase[0];
  171.     if ((c == '0') && (lowerCase[1] == '\0')) {
  172.     *boolPtr = 0;
  173.     } else if ((c == '1') && (lowerCase[1] == '\0')) {
  174.     *boolPtr = 1;
  175.     } else if ((c == 'y') && (strncmp(lowerCase, "yes", length) == 0)) {
  176.     *boolPtr = 1;
  177.     } else if ((c == 'n') && (strncmp(lowerCase, "no", length) == 0)) {
  178.     *boolPtr = 0;
  179.     } else if ((c == 't') && (strncmp(lowerCase, "true", length) == 0)) {
  180.     *boolPtr = 1;
  181.     } else if ((c == 'f') && (strncmp(lowerCase, "false", length) == 0)) {
  182.     *boolPtr = 0;
  183.     } else if ((c == 'o') && (length >= 2)) {
  184.     if (strncmp(lowerCase, "on", length) == 0) {
  185.         *boolPtr = 1;
  186.     } else if (strncmp(lowerCase, "off", length) == 0) {
  187.         *boolPtr = 0;
  188.     }
  189.     } else {
  190.     Tcl_AppendResult(interp, "expected boolean value but got \"",
  191.         string, "\"", (char *) NULL);
  192.     return TCL_ERROR;
  193.     }
  194.     return TCL_OK;
  195. }
  196.