misc.cc

Go to the documentation of this file.
00001 /* -*-  Mode:C++; c-basic-offset:8; tab-width:8; indent-tabs-mode:t -*- */
00002 /*
00003  * Copyright (c) 1994 Regents of the University of California.
00004  * All rights reserved.
00005  *
00006  * Redistribution and use in source and binary forms, with or without
00007  * modification, are permitted provided that the following conditions
00008  * are met:
00009  * 1. Redistributions of source code must retain the above copyright
00010  *    notice, this list of conditions and the following disclaimer.
00011  * 2. Redistributions in binary form must reproduce the above copyright
00012  *    notice, this list of conditions and the following disclaimer in the
00013  *    documentation and/or other materials provided with the distribution.
00014  * 3. All advertising materials mentioning features or use of this software
00015  *    must display the following acknowledgement:
00016  *  This product includes software developed by the Computer Systems
00017  *  Engineering Group at Lawrence Berkeley Laboratory.
00018  * 4. Neither the name of the University nor of the Laboratory may be used
00019  *    to endorse or promote products derived from this software without
00020  *    specific prior written permission.
00021  *
00022  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
00023  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00024  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00025  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
00026  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00027  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
00028  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00029  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00030  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
00031  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00032  * SUCH DAMAGE.
00033  *
00034  * miscellaneous "ns" commands
00035  */
00036 
00037 #ifndef lint
00038 static const char rcsid[] =
00039     "@(#) $Header: /nfs/jade/vint/CVSROOT/ns-2/common/misc.cc,v 1.14 2002/06/04 23:25:19 haldar Exp $ (LBL)";
00040 #endif
00041 
00042 #include <stdlib.h>
00043 #include <math.h>
00044 #ifndef WIN32
00045 #include <sys/time.h>
00046 #endif
00047 #include <ctype.h>
00048 #include "config.h"
00049 #include "scheduler.h"
00050 #include "random.h"
00051 
00052 #if defined(HAVE_INT64)
00053 class Add64Command : public TclCommand {
00054 public: 
00055     Add64Command() : TclCommand("ns-add64") {}
00056     virtual int command(int argc, const char*const* argv);
00057 };
00058 
00059 int Add64Command::command(int argc, const char*const* argv)
00060 {
00061     Tcl& tcl = Tcl::instance();
00062     if (argc == 3) {
00063         char res[22]; /* A 64 bit int at most 20 digits */
00064         int64_t d1 = STRTOI64(argv[1], NULL, 0);
00065         int64_t d2 = STRTOI64(argv[2], NULL, 0);
00066         sprintf(res, STRTOI64_FMTSTR, d1+d2);
00067         tcl.resultf("%s", res);
00068         return (TCL_OK);
00069     }
00070     tcl.add_error("ns-add64 requires two arguments.");
00071     return (TCL_ERROR);
00072 }
00073 
00074 class Mult64Command : public TclCommand {
00075 public: 
00076     Mult64Command() : TclCommand("ns-mult64") {}
00077     virtual int command(int argc, const char*const* argv);
00078 };
00079 
00080 int Mult64Command::command(int argc, const char*const* argv)
00081 {
00082     Tcl& tcl = Tcl::instance();
00083     if (argc == 3) {
00084         char res[22]; /* A 64 bit int at most 20 digits */
00085         int64_t d1 = STRTOI64(argv[1], NULL, 0);
00086         int64_t d2 = STRTOI64(argv[2], NULL, 0);
00087         sprintf(res, STRTOI64_FMTSTR, d1*d2);
00088         tcl.resultf("%s", res);
00089         return (TCL_OK);
00090     }
00091     tcl.add_error("ns-mult64 requires two arguments.");
00092     return (TCL_ERROR);
00093 }
00094 
00095 class Int64ToDoubleCommand : public TclCommand {
00096 public: 
00097     Int64ToDoubleCommand() : TclCommand("ns-int64todbl") {}
00098     virtual int command(int argc, const char*const* argv);
00099 };
00100 
00101 int Int64ToDoubleCommand::command(int argc, const char*const* argv)
00102 {
00103     Tcl& tcl = Tcl::instance();
00104     if (argc == 2) {
00105         char res[22]; /* A 64 bit int at most 20 digits */
00106         int64_t d1 = STRTOI64(argv[1], NULL, 0);
00107         double  d2 = d1;
00108         sprintf(res, "%.1f",d2);
00109         tcl.resultf("%s", res);
00110         return (TCL_OK);
00111     }
00112     tcl.add_error("ns-int64todbl requires only one arguments.");
00113     return (TCL_ERROR);
00114 }
00115 #endif
00116 
00117 class HasInt64Command : public TclCommand {
00118 public: 
00119     HasInt64Command() : TclCommand("ns-hasint64") {}
00120     virtual int command(int argc, const char*const* argv);
00121 };
00122 
00123 int HasInt64Command::command(int argc, const char*const* argv)
00124 {
00125     Tcl& tcl = Tcl::instance();
00126     char res[2];
00127     int flag = 0; 
00128 #if defined(HAVE_INT64)
00129     flag = 1; 
00130     #endif
00131     sprintf(res, "%d", flag);
00132     tcl.resultf("%s", res);
00133     return (TCL_OK);
00134 }
00135 
00136 class RandomCommand : public TclCommand {
00137 public:
00138     RandomCommand() : TclCommand("ns-random") { }
00139     virtual int command(int argc, const char*const* argv);
00140 };
00141 
00142 /*
00143  * ns-random
00144  * ns-random $seed 
00145  */
00146 int RandomCommand::command(int argc, const char*const* argv)
00147 {
00148     Tcl& tcl = Tcl::instance();
00149     if (argc == 1) {
00150         sprintf(tcl.buffer(), "%u", Random::random());
00151         tcl.result(tcl.buffer());
00152     } else if (argc == 2) {
00153         int seed = atoi(argv[1]);
00154         if (seed == 0)
00155             seed = Random::seed_heuristically();
00156         else
00157             Random::seed(seed);
00158         tcl.resultf("%d", seed);
00159     }
00160     return (TCL_OK);
00161 }
00162 
00163 extern "C" char version_string[];
00164 
00165 class VersionCommand : public TclCommand {
00166 public:
00167     VersionCommand() : TclCommand("ns-version") { }
00168     virtual int command(int, const char*const*) {
00169         Tcl::instance().result(version_string);
00170         return (TCL_OK);
00171     }
00172 };
00173 
00174 class HasSTLCommand : public TclCommand {
00175 public:
00176     HasSTLCommand() : TclCommand("ns-hasSTL") {}
00177     virtual int command(int argc, const char*const* argv);
00178 };
00179 
00180 int HasSTLCommand::command(int argc, const char*const* argv) 
00181 {
00182     Tcl& tcl = Tcl::instance();
00183     char res[2];
00184     int flag = 0;
00185         #if defined(HAVE_STL)
00186     flag = 1;
00187         #endif
00188     sprintf(res, "%d", flag);
00189     tcl.resultf("%s", res);
00190     return (TCL_OK);
00191 }
00192 
00193 
00194 
00195 class TimeAtofCommand : public TclCommand {
00196 public:
00197     TimeAtofCommand() : TclCommand("time_atof") { }
00198     virtual int command(int argc, const char*const* argv) {
00199         if (argc != 2)
00200             return (TCL_ERROR);
00201         char* s = (char*) argv[1];
00202         char wrk[32];
00203         char* cp = wrk;
00204         while (isdigit(*s) || *s == 'e' ||
00205                *s == '+' || *s == '-' || *s == '.')
00206             *cp++ = *s++;
00207         *cp = 0;
00208         double v = atof(wrk);
00209         switch (*s) {
00210         case 'm':
00211             v *= 1e-3;
00212             break;
00213         case 'u':
00214             v *= 1e-6;
00215             break;
00216         case 'n':
00217             v *= 1e-9;
00218             break;
00219         case 'p':
00220             v *= 1e-12;
00221             break;
00222         }
00223         Tcl::instance().resultf("%g", v);
00224         return (TCL_OK);
00225     }
00226 };
00227 
00228 void init_misc(void)
00229 {
00230     (void)new VersionCommand;
00231     (void)new RandomCommand;
00232     (void)new TimeAtofCommand;
00233     (void)new HasInt64Command;
00234     (void)new HasSTLCommand;
00235 #if defined(HAVE_INT64)
00236     (void)new Add64Command;
00237     (void)new Mult64Command;
00238     (void)new Int64ToDoubleCommand;
00239 #endif
00240 }
00241 

Generated on Tue Mar 6 16:47:47 2007 for ns2 Network Simulator 2.29 by  doxygen 1.4.6