SimTime.C
Go to the documentation of this file.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
00038 #ifndef UTIL_SIMTIME_C_DEFINED
00039 #define UTIL_SIMTIME_C_DEFINED
00040
00041 #include "Util/SimTime.H"
00042
00043 #include "Util/StringConversions.H"
00044 #include "Util/log.H"
00045 #include "Util/sformat.H"
00046
00047 #include <cstdio>
00048
00049
00050
00051 std::string SimTime::toString(bool with_suffix) const
00052 {
00053 if (with_suffix)
00054 return sformat("%g", this->secs());
00055 else
00056 return sformat("%gs", this->secs());
00057 }
00058
00059
00060 SimTime SimTime::fromString(const std::string& str,
00061 const char* default_suffix)
00062 {
00063
00064 if (str.compare("max") == 0 || str.compare("MAX") == 0 ||
00065 str.compare("forever") == 0 || str.compare("FOREVER") == 0 ||
00066 str.compare("unlimited") == 0 || str.compare("UNLIMITED") == 0)
00067 return SimTime::MAX();
00068
00069 std::string::size_type last_digit = str.find_last_of("0123456789");
00070
00071 const std::string num = str.substr(0, last_digit+1);
00072
00073 double value; char junk[2];
00074
00075
00076
00077 if (sscanf(num.c_str(), "%lf%1s", &value, &junk[0]) != 1)
00078 LFATAL("while parsing SimTime string '%s':\n"
00079 "\tnumeric conversion failed from '%s'",
00080 str.c_str(), num.c_str());
00081
00082 if (default_suffix==0 && last_digit+1 >= str.length())
00083 LFATAL("while parsing SimTime string '%s':\n"
00084 "\tno suffix found in '%s' and no default suffix was given\n"
00085 "\tin the code -- to avoid this error, either add a 's' or\n"
00086 "\t'ms' suffix (i.e., '%ss' or '%sms'), or change the code\n"
00087 "\tto specify a default suffix",
00088 str.c_str(), str.c_str(), str.c_str(), str.c_str());
00089
00090 const std::string suffix =
00091 last_digit+1 < str.length()
00092 ? str.substr(last_digit+1, str.npos)
00093 : default_suffix;
00094
00095
00096
00097
00098 if (suffix == "s" ||
00099 suffix == "sec" ||
00100 suffix == "secs" ||
00101 suffix == "seconds")
00102 {
00103 value *= 1000000000.0;
00104 }
00105 else if (suffix == "ms" ||
00106 suffix == "msec" ||
00107 suffix == "msecs" ||
00108 suffix == "milliseconds")
00109 {
00110 value *= 1000000.0;
00111 }
00112 else if (suffix == "us" ||
00113 suffix == "usec" ||
00114 suffix == "usecs" ||
00115 suffix == "microseconds")
00116 {
00117 value *= 1000.0;
00118 }
00119 else if (suffix == "ns" ||
00120 suffix == "nsec" ||
00121 suffix == "nsecs" ||
00122 suffix == "nanoseconds")
00123 {
00124
00125 }
00126 else if (suffix == "Hz")
00127 {
00128 value = 1.0/value;
00129 value *= 1000000000.0;
00130 }
00131 else if (suffix == "kHz")
00132 {
00133 value = 1.0/value;
00134 value *= 1000000.0;
00135 }
00136 else if (suffix == "MHz")
00137 {
00138 value = 1.0/value;
00139 value *= 1000.0;
00140 }
00141 else
00142 {
00143 LFATAL("while parsing SimTime string '%s':\n"
00144 "\tunknown units suffix '%s'",
00145 str.c_str(), suffix.c_str());
00146 }
00147
00148 const double int64min = double(std::numeric_limits<int64>::min());
00149 const double int64max = double(std::numeric_limits<int64>::max());
00150
00151 if (value < int64min)
00152 {
00153 LFATAL("time value '%s' (%.2ens) is too small to be represented "
00154 "(min value is %.2ens)",
00155 str.c_str(), value, int64min);
00156 }
00157 else if (value > int64max)
00158 {
00159 LFATAL("time value '%s' (%.2ens) is too large to be represented "
00160 "(max value is %.2ens)",
00161 str.c_str(), value, int64max);
00162 }
00163
00164 return SimTime::NSECS(int64(value));
00165 }
00166
00167
00168
00169
00170
00171
00172
00173 #endif // UTIL_SIMTIME_C_DEFINED