00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
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];
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];
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];
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
00144
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