Pol  Revision:cb584c9
utilmod.cpp
Go to the documentation of this file.
1 
10 #include "utilmod.h"
11 
12 #include <ctime>
13 #include <string>
14 
15 #include "../../bscript/berror.h"
16 #include "../../bscript/impstr.h"
17 #include "../../clib/clib.h"
18 #include "../../clib/random.h"
19 #include "../dice.h"
20 
21 namespace Pol
22 {
23 namespace Bscript
24 {
25 using namespace Module;
26 template <>
29  {"RandomInt", &UtilExecutorModule::mf_RandomInt},
30  {"RandomFloat", &UtilExecutorModule::mf_RandomFloat},
31  {"RandomDiceRoll", &UtilExecutorModule::mf_RandomDiceRoll},
32  {"StrFormatTime", &UtilExecutorModule::mf_StrFormatTime},
33  {"RandomIntMinMax", &UtilExecutorModule::mf_RandomIntMinMax}};
34 }
35 namespace Module
36 {
37 using namespace Bscript;
38 
40  : Bscript::TmplExecutorModule<UtilExecutorModule>( "util", exec )
41 {
42 }
43 
45 {
46  int value;
47  if ( exec.getParam( 0, value, 1, INT_MAX ) )
48  {
49  if ( value > 0 )
50  return new BLong( Clib::random_int( value - 1 ) );
51  else
52  return new BError( "RandomInt() expects a positive integer" );
53  }
54  else
55  {
56  return new BError( "RandomInt() expects a positive integer" );
57  }
58 }
59 
61 {
62  int minvalue;
63  if ( exec.getParam( 0, minvalue, INT_MIN, INT_MAX ) )
64  {
65  int maxvalue;
66  if ( exec.getParam( 1, maxvalue, INT_MIN, INT_MAX ) )
67  {
68  maxvalue--;
69  return new BLong( Clib::random_int_range( minvalue, maxvalue ) );
70  }
71  else
72  return new BError( "RandomIntMinMax() expects an integer" );
73  }
74  else
75  return new BError( "RandomIntMinMax() expects an integer" );
76 }
77 
79 {
80  double value;
81  if ( exec.getRealParam( 0, value ) )
82  {
83  return new Double( Clib::random_double( value ) );
84  }
85  else
86  {
87  return new BError( "RandomFloat() expects a Real parameter" );
88  }
89 }
90 
92 {
93  const String* dicestr;
94  if ( exec.getStringParam( 0, dicestr ) )
95  {
96  std::string errormsg;
97  Core::Dice dice;
98  if ( dice.load( dicestr->data(), &errormsg ) )
99  {
100  return new BLong( dice.roll() );
101  }
102  else
103  {
104  return new BError( errormsg.c_str() );
105  }
106  }
107  else
108  {
109  return new BError( "RandomDiceRoll() expects a String as parameter" );
110  }
111 }
112 
114 {
115  const String* format_string;
116  if ( !getStringParam( 0, format_string ) )
117  return new BError( "No time string passed." );
118  else if ( format_string->length() > 100 )
119  return new BError( "Format string exceeded 100 characters." );
120 
121  int time_stamp;
122  if ( !getParam( 1, time_stamp, 0, INT_MAX ) )
123  time_stamp = 0;
124 
125  time_t seconds;
126 
127 
128  if ( time_stamp <= 0 )
129  seconds = time( nullptr );
130  else
131  seconds = time_stamp;
132 
133  auto time_struct = Clib::localtime( seconds );
134 
135  // strftime uses assert check for invalid format -> precheck it
136  size_t len = format_string->length();
137  const char* str = format_string->data();
138  while ( len-- > 0 )
139  {
140  if ( *str++ == '%' )
141  {
142  if ( len-- <= 0 )
143  return new BError( "Invalid Format string." );
144  switch ( *str++ )
145  {
146  case ( '%' ):
147  case ( 'a' ):
148  case ( 'A' ):
149  case ( 'b' ):
150  case ( 'B' ):
151  case ( 'c' ):
152  case ( 'd' ):
153  case ( 'H' ):
154  case ( 'I' ):
155  case ( 'j' ):
156  case ( 'm' ):
157  case ( 'M' ):
158  case ( 'p' ):
159  case ( 'S' ):
160  case ( 'U' ):
161  case ( 'w' ):
162  case ( 'W' ):
163  case ( 'x' ):
164  case ( 'X' ):
165  case ( 'y' ):
166  case ( 'Y' ):
167  case ( 'Z' ):
168  continue;
169  case ( '\0' ):
170  len = 0;
171  break;
172  default:
173  return new BError( "Invalid Format string." );
174  }
175  }
176  }
177 
178  char buffer[102]; // +2 for the \0 termination.
179  if ( strftime( buffer, sizeof buffer, format_string->data(), &time_struct ) > 0 )
180  return new String( buffer );
181  else
182  return new BError( "Format string too long." );
183 }
184 }
185 }
Bscript::BObjectImp * mf_RandomFloat()
Definition: utilmod.cpp:78
unsigned short roll(void) const
Definition: dice.cpp:26
std::tm localtime(const std::time_t &t)
threadsafe version of localtime
Definition: clib.h:143
bool getParam(unsigned param, int &value)
Definition: execmodl.cpp:62
Bscript::BObjectImp * mf_RandomInt()
Definition: utilmod.cpp:44
unsigned char buffer[10000]
Definition: UoToolMain.cpp:109
Bscript::BObjectImp * mf_RandomDiceRoll()
Definition: utilmod.cpp:91
UtilExecutorModule(Bscript::Executor &exec)
Definition: utilmod.cpp:39
int random_int(int i)
Definition: random.cpp:34
bool getRealParam(unsigned param, double &value)
Definition: executor.cpp:433
Bscript::BObjectImp * mf_RandomIntMinMax()
Definition: utilmod.cpp:60
const String * getStringParam(unsigned param)
Definition: execmodl.cpp:36
Bscript::BObjectImp * mf_StrFormatTime()
Definition: utilmod.cpp:113
const String * getStringParam(unsigned param)
Definition: executor.cpp:347
int random_int_range(int minI, int maxI)
Definition: random.cpp:43
double random_double(double f)
Definition: random.cpp:25
bool load(const char *dice, std::string *errormsg)
Definition: dice.cpp:42
const char * data() const
Definition: impstr.h:66
Definition: berror.cpp:12
size_t length() const
Definition: impstr.h:68
bool getParam(unsigned param, int &value)
Definition: executor.cpp:363