1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
|
/* -*- Mode:C++; c-basic-offset:8; tab-width:8; indent-tabs-mode:t -*- */
/*
* Copyright (c) 1994 Regents of the University of California.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the Computer Systems
* Engineering Group at Lawrence Berkeley Laboratory.
* 4. Neither the name of the University nor of the Laboratory may be used
* to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* miscellaneous "ns" commands
*/
#ifndef lint
static const char rcsid[] =
"@(#) $Header: /cvsroot/nsnam/ns-2/common/misc.cc,v 1.15 2010/03/08 05:54:49 tom_henderson Exp $ (LBL)";
#endif
#include <stdlib.h>
#include <math.h>
#ifndef WIN32
#include <sys/time.h>
#endif
#include <ctype.h>
#include "config.h"
#include "scheduler.h"
#include "random.h"
#if defined(HAVE_INT64)
class Add64Command : public TclCommand {
public:
Add64Command() : TclCommand("ns-add64") {}
virtual int command(int argc, const char*const* argv);
};
int Add64Command::command(int argc, const char*const* argv)
{
Tcl& tcl = Tcl::instance();
if (argc == 3) {
char res[22]; /* A 64 bit int at most 20 digits */
int64_t d1 = STRTOI64(argv[1], NULL, 0);
int64_t d2 = STRTOI64(argv[2], NULL, 0);
sprintf(res, STRTOI64_FMTSTR, d1+d2);
tcl.resultf("%s", res);
return (TCL_OK);
}
tcl.add_error("ns-add64 requires two arguments.");
return (TCL_ERROR);
}
class Mult64Command : public TclCommand {
public:
Mult64Command() : TclCommand("ns-mult64") {}
virtual int command(int argc, const char*const* argv);
};
int Mult64Command::command(int argc, const char*const* argv)
{
Tcl& tcl = Tcl::instance();
if (argc == 3) {
char res[22]; /* A 64 bit int at most 20 digits */
int64_t d1 = STRTOI64(argv[1], NULL, 0);
int64_t d2 = STRTOI64(argv[2], NULL, 0);
sprintf(res, STRTOI64_FMTSTR, d1*d2);
tcl.resultf("%s", res);
return (TCL_OK);
}
tcl.add_error("ns-mult64 requires two arguments.");
return (TCL_ERROR);
}
class Int64ToDoubleCommand : public TclCommand {
public:
Int64ToDoubleCommand() : TclCommand("ns-int64todbl") {}
virtual int command(int argc, const char*const* argv);
};
int Int64ToDoubleCommand::command(int argc, const char*const* argv)
{
Tcl& tcl = Tcl::instance();
if (argc == 2) {
char res[22]; /* A 64 bit int at most 20 digits */
int64_t d1 = STRTOI64(argv[1], NULL, 0);
double d2 = d1;
sprintf(res, "%.1f",d2);
tcl.resultf("%s", res);
return (TCL_OK);
}
tcl.add_error("ns-int64todbl requires only one arguments.");
return (TCL_ERROR);
}
#endif
class HasInt64Command : public TclCommand {
public:
HasInt64Command() : TclCommand("ns-hasint64") {}
virtual int command(int argc, const char*const* argv);
};
int HasInt64Command::command(int, const char*const*)
{
Tcl& tcl = Tcl::instance();
char res[2];
int flag = 0;
#if defined(HAVE_INT64)
flag = 1;
#endif
sprintf(res, "%d", flag);
tcl.resultf("%s", res);
return (TCL_OK);
}
class RandomCommand : public TclCommand {
public:
RandomCommand() : TclCommand("ns-random") { }
virtual int command(int argc, const char*const* argv);
};
/*
* ns-random
* ns-random $seed
*/
int RandomCommand::command(int argc, const char*const* argv)
{
Tcl& tcl = Tcl::instance();
if (argc == 1) {
sprintf(tcl.buffer(), "%u", Random::random());
tcl.result(tcl.buffer());
} else if (argc == 2) {
int seed = atoi(argv[1]);
if (seed == 0)
seed = Random::seed_heuristically();
else
Random::seed(seed);
tcl.resultf("%d", seed);
}
return (TCL_OK);
}
extern "C" char version_string[];
class VersionCommand : public TclCommand {
public:
VersionCommand() : TclCommand("ns-version") { }
virtual int command(int, const char*const*) {
Tcl::instance().result(version_string);
return (TCL_OK);
}
};
class HasSTLCommand : public TclCommand {
public:
HasSTLCommand() : TclCommand("ns-hasSTL") {}
virtual int command(int argc, const char*const* argv);
};
int HasSTLCommand::command(int, const char*const*)
{
Tcl& tcl = Tcl::instance();
char res[2];
int flag = 0;
#if defined(HAVE_STL)
flag = 1;
#endif
sprintf(res, "%d", flag);
tcl.resultf("%s", res);
return (TCL_OK);
}
class TimeAtofCommand : public TclCommand {
public:
TimeAtofCommand() : TclCommand("time_atof") { }
virtual int command(int argc, const char*const* argv) {
if (argc != 2)
return (TCL_ERROR);
char* s = (char*) argv[1];
char wrk[32];
char* cp = wrk;
while (isdigit(*s) || *s == 'e' ||
*s == '+' || *s == '-' || *s == '.')
*cp++ = *s++;
*cp = 0;
double v = atof(wrk);
switch (*s) {
case 'm':
v *= 1e-3;
break;
case 'u':
v *= 1e-6;
break;
case 'n':
v *= 1e-9;
break;
case 'p':
v *= 1e-12;
break;
}
Tcl::instance().resultf("%g", v);
return (TCL_OK);
}
};
void init_misc(void)
{
(void)new VersionCommand;
(void)new RandomCommand;
(void)new TimeAtofCommand;
(void)new HasInt64Command;
(void)new HasSTLCommand;
#if defined(HAVE_INT64)
(void)new Add64Command;
(void)new Mult64Command;
(void)new Int64ToDoubleCommand;
#endif
}
|