Main Page | Class Hierarchy | Data Structures | Directories | File List | Data Fields | Related Pages

tcl_util.c

00001 /*-
00002  * See the file LICENSE for redistribution information.
00003  *
00004  * Copyright (c) 1999-2005
00005  *      Sleepycat Software.  All rights reserved.
00006  *
00007  * $Id: tcl_util.c,v 12.2 2005/07/20 16:52:13 bostic Exp $
00008  */
00009 
00010 #include "db_config.h"
00011 
00012 #ifndef NO_SYSTEM_INCLUDES
00013 #include <sys/types.h>
00014 
00015 #include <fcntl.h>
00016 #include <stdlib.h>
00017 #include <string.h>
00018 #include <tcl.h>
00019 #endif
00020 
00021 #include "db_int.h"
00022 #include "dbinc/tcl_db.h"
00023 
00024 /*
00025  * bdb_RandCommand --
00026  *      Implements rand* functions.
00027  *
00028  * PUBLIC: int bdb_RandCommand __P((Tcl_Interp *, int, Tcl_Obj * CONST*));
00029  */
00030 int
00031 bdb_RandCommand(interp, objc, objv)
00032         Tcl_Interp *interp;             /* Interpreter */
00033         int objc;                       /* How many arguments? */
00034         Tcl_Obj *CONST objv[];          /* The argument objects */
00035 {
00036         static const char *rcmds[] = {
00037                 "rand", "random_int",   "srand",
00038                 NULL
00039         };
00040         enum rcmds {
00041                 RRAND, RRAND_INT, RSRAND
00042         };
00043         Tcl_Obj *res;
00044         int cmdindex, hi, lo, result, ret;
00045 
00046         result = TCL_OK;
00047         /*
00048          * Get the command name index from the object based on the cmds
00049          * defined above.  This SHOULD NOT fail because we already checked
00050          * in the 'berkdb' command.
00051          */
00052         if (Tcl_GetIndexFromObj(interp,
00053             objv[1], rcmds, "command", TCL_EXACT, &cmdindex) != TCL_OK)
00054                 return (IS_HELP(objv[1]));
00055 
00056         res = NULL;
00057         switch ((enum rcmds)cmdindex) {
00058         case RRAND:
00059                 /*
00060                  * Must be 0 args.  Error if different.
00061                  */
00062                 if (objc != 2) {
00063                         Tcl_WrongNumArgs(interp, 2, objv, NULL);
00064                         return (TCL_ERROR);
00065                 }
00066                 ret = rand();
00067                 res = Tcl_NewIntObj(ret);
00068                 break;
00069         case RRAND_INT:
00070                 /*
00071                  * Must be 4 args.  Error if different.
00072                  */
00073                 if (objc != 4) {
00074                         Tcl_WrongNumArgs(interp, 2, objv, "lo hi");
00075                         return (TCL_ERROR);
00076                 }
00077                 if ((result =
00078                     Tcl_GetIntFromObj(interp, objv[2], &lo)) != TCL_OK)
00079                         return (result);
00080                 if ((result =
00081                     Tcl_GetIntFromObj(interp, objv[3], &hi)) != TCL_OK)
00082                         return (result);
00083                 if (lo < 0 || hi < 0) {
00084                         Tcl_SetResult(interp,
00085                             "Range value less than 0", TCL_STATIC);
00086                         return (TCL_ERROR);
00087                 }
00088 
00089                 _debug_check();
00090                 ret = lo + rand() % ((hi - lo) + 1);
00091                 res = Tcl_NewIntObj(ret);
00092                 break;
00093         case RSRAND:
00094                 /*
00095                  * Must be 1 arg.  Error if different.
00096                  */
00097                 if (objc != 3) {
00098                         Tcl_WrongNumArgs(interp, 2, objv, "seed");
00099                         return (TCL_ERROR);
00100                 }
00101                 if ((result =
00102                     Tcl_GetIntFromObj(interp, objv[2], &lo)) == TCL_OK) {
00103                         srand((u_int)lo);
00104                         res = Tcl_NewIntObj(0);
00105                 }
00106                 break;
00107         }
00108 
00109         /*
00110          * Only set result if we have a res.  Otherwise, lower functions have
00111          * already done so.
00112          */
00113         if (result == TCL_OK && res)
00114                 Tcl_SetObjResult(interp, res);
00115         return (result);
00116 }

Generated on Sun Dec 25 12:14:52 2005 for Berkeley DB 4.4.16 by  doxygen 1.4.2