tclGet.c revision 3f54fd611f536639ec30dd53c48e5ec1897cc7d9
/*
* tclGet.c --
*
* This file contains procedures to convert strings into
* other forms, like integers or floating-point numbers or
* booleans, doing syntax checking along the way.
*
* Copyright (c) 1990-1993 The Regents of the University of California.
* Copyright (c) 1994-1995 Sun Microsystems, Inc.
*
* See the file "license.terms" for information on usage and redistribution
* of this file, and for a DISCLAIMER OF ALL WARRANTIES.
*
* SCCS: @(#) tclGet.c 1.24 96/02/15 11:42:47
*/
#include "tclInt.h"
#include "tclPort.h"
/*
*----------------------------------------------------------------------
*
* Tcl_GetInt --
*
* Given a string, produce the corresponding integer value.
*
* Results:
* The return value is normally TCL_OK; in this case *intPtr
* will be set to the integer value equivalent to string. If
* string is improperly formed then TCL_ERROR is returned and
* an error message will be left in interp->result.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
int
char *string; /* String containing a (possibly signed)
* integer in a form acceptable to strtol. */
int *intPtr; /* Place to store converted result. */
{
char *end, *p;
int i;
/*
* Note: use strtoul instead of strtol for integer conversions
* to allow full-size unsigned numbers, but don't depend on strtoul
* to handle sign characters; it won't in some implementations.
*/
errno = 0;
/* Empty loop body. */
}
if (*p == '-') {
p++;
} else if (*p == '+') {
p++;
} else {
}
if (end == p) {
"\"", (char *) NULL);
}
return TCL_ERROR;
}
}
return TCL_ERROR;
}
end++;
}
if (*end != 0) {
goto badInteger;
}
*intPtr = i;
return TCL_OK;
}
/*
*----------------------------------------------------------------------
*
* Tcl_GetDouble --
*
* Given a string, produce the corresponding double-precision
* floating-point value.
*
* Results:
* The return value is normally TCL_OK; in this case *doublePtr
* will be set to the double-precision value equivalent to string.
* If string is improperly formed then TCL_ERROR is returned and
* an error message will be left in interp->result.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
int
char *string; /* String containing a floating-point number
* in a form acceptable to strtod. */
double *doublePtr; /* Place to store converted result. */
{
char *end;
double d;
errno = 0;
"expected floating-point number but got \"",
}
return TCL_ERROR;
}
if (errno != 0) {
TclExprFloatError(interp, d);
}
return TCL_ERROR;
}
end++;
}
if (*end != 0) {
goto badDouble;
}
*doublePtr = d;
return TCL_OK;
}
/*
*----------------------------------------------------------------------
*
* Tcl_GetBoolean --
*
* Given a string, return a 0/1 boolean value corresponding
* to the string.
*
* Results:
* The return value is normally TCL_OK; in this case *boolPtr
* will be set to the 0/1 value equivalent to string. If
* string is improperly formed then TCL_ERROR is returned and
* an error message will be left in interp->result.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
int
char *string; /* String containing a boolean number
int *boolPtr; /* Place to store converted result, which
* will be 0 or 1. */
{
int i;
char lowerCase[10], c;
/*
* Convert the input string to all lower-case.
*/
for (i = 0; i < 9; i++) {
c = string[i];
if (c == 0) {
break;
}
if ((c >= 'A') && (c <= 'Z')) {
c += (char) ('a' - 'A');
}
lowerCase[i] = c;
}
lowerCase[i] = 0;
c = lowerCase[0];
*boolPtr = 0;
*boolPtr = 1;
*boolPtr = 1;
*boolPtr = 0;
*boolPtr = 1;
*boolPtr = 0;
*boolPtr = 1;
*boolPtr = 0;
} else {
goto badBoolean;
}
} else {
}
return TCL_ERROR;
}
return TCL_OK;
}