xoreos  0.0.5
functions_string.cpp
Go to the documentation of this file.
1 /* xoreos - A reimplementation of BioWare's Aurora engine
2  *
3  * xoreos is the legal property of its developers, whose names
4  * can be found in the AUTHORS file distributed with this source
5  * distribution.
6  *
7  * xoreos is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 3
10  * of the License, or (at your option) any later version.
11  *
12  * xoreos is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with xoreos. If not, see <http://www.gnu.org/licenses/>.
19  */
20 
25 #include "src/common/util.h"
26 #include "src/common/ustring.h"
27 #include "src/common/debug.h"
28 #include "src/common/error.h"
29 #include "src/common/strutil.h"
30 #include "src/common/datetime.h"
31 
32 #include "src/aurora/language.h"
33 #include "src/aurora/talkman.h"
34 
38 
40 
41 namespace Engines {
42 
43 namespace DragonAge {
44 
47 
48  status("DragonAge: %s: %s", tstamp.c_str(), ctx.getParams()[0].getString().c_str());
49 }
50 
52  // TODO: We're already flushing our log file after every line...
53 
55 
56  status("DragonAge: %s: %s", tstamp.c_str(), ctx.getParams()[0].getString().c_str());
57 }
58 
61 
62  status("DragonAge: %s: [%d]<%s>: %s", tstamp.c_str(), ctx.getParams()[0].getInt(),
63  Aurora::NWScript::formatTag(ctx.getParams()[2].getObject()).c_str(),
64  ctx.getParams()[1].getString().c_str());
65 }
66 
68  status("DragonAge: WARNING: %s", ctx.getParams()[0].getString().c_str());
69 }
70 
72  status("DragonAge: DEBUG: %s", ctx.getParams()[0].getString().c_str());
73 }
74 
76  status("DragonAge: LOG(%s): %s", ctx.getParams()[1].getString().c_str(),
77  ctx.getParams()[0].getString().c_str());
78 }
79 
81  status("DragonAge: %d", ctx.getParams()[0].getInt());
82 }
83 
85  const float value = ctx.getParams()[0].getFloat();
86  const int width = ctx.getParams()[1].getInt();
87  const int decimals = ctx.getParams()[2].getInt();
88 
89  status("DragonAge: %s", formatFloat(value, width, decimals).c_str());
90 }
91 
93  status("DragonAge: %s", ctx.getParams()[0].getString().c_str());
94 }
95 
97  Aurora::NWScript::Object *object = ctx.getParams()[0].getObject();
98 
99  status("DragonAge: object<%s,%p)", Aurora::NWScript::formatTag(object).c_str(),
100  static_cast<void *>(object));
101 }
102 
104  float x, y, z;
105  ctx.getParams()[0].getVector(x, y, z);
106 
107  const bool prepend = ctx.getParams()[1].getInt() != 0;
108 
109  status("DragonAge: %s%f, %f, %f", prepend ? "PRINTVECTOR:" : "", x, y, z);
110 }
111 
113  const bool prepend = ctx.getParams()[1].getInt() != 0;
114 
115  status("DragonAge: %s%s", prepend ? "PRINTRESOURCE" : "", ctx.getParams()[0].getString().c_str());
116 }
117 
119  ctx.getReturn() = Common::composeString(ctx.getParams()[0].getInt());
120 }
121 
123  const float value = ctx.getParams()[0].getFloat();
124  const int width = ctx.getParams()[1].getInt();
125  const int decimals = ctx.getParams()[2].getInt();
126 
127  ctx.getReturn() = formatFloat(value, width, decimals);
128 }
129 
131  Aurora::NWScript::Object *object = ctx.getParams()[0].getObject();
132 
133  ctx.getReturn() = Common::UString::format("object<%s,%p)", Aurora::NWScript::formatTag(object).c_str(),
134  static_cast<void *>(object));
135 }
136 
138  float x, y, z;
139  ctx.getParams()[0].getVector(x, y, z);
140 
141  ctx.getReturn() = Common::UString::format("%f %f %f", x, y, z);
142 }
143 
145  ctx.getReturn() = ctx.getParams()[0];
146 }
147 
149  ctx.getReturn() = "";
150 
151  switch (ctx.getParams()[0].getType()) {
153  intToString(ctx);
154  break;
155 
157  floatToString(ctx);
158  break;
159 
161  ctx.getReturn() = ctx.getParams()[0];
162  break;
163 
165  objectToString(ctx);
166  break;
167 
169  vectorToString(ctx);
170  break;
171 
172  default:
173  break;
174  }
175 }
176 
178  ctx.getReturn() = Common::UString::format("0x%08x", (uint32) ctx.getParams()[0].getInt());
179 }
180 
182  int32 i = 0;
183 
184  try {
185  Common::parseString(ctx.getParams()[0].getString(), i);
186  } catch (Common::Exception &e) {
187  debugC(Common::kDebugEngineScripts, 1, "Functions::%s: %s (\"%s\")",
188  ctx.getName().c_str(), e.what(), ctx.getParams()[0].getString().c_str());
189  }
190 
191  ctx.getReturn() = i;
192 }
193 
195  float f = 0.0f;
196 
197  try {
198  Common::parseString(ctx.getParams()[0].getString(), f);
199  } catch (Common::Exception &e) {
200  debugC(Common::kDebugEngineScripts, 1, "Functions::%s: %s (\"%s\")",
201  ctx.getName().c_str(), e.what(), ctx.getParams()[0].getString().c_str());
202  }
203 
204  ctx.getReturn() = f;
205 }
206 
208  float x = 0.0f, y = 0.0f, z = 0.0f;
209  ctx.getReturn().setVector(x, y, z);
210 
211  std::vector<Common::UString> parts;
212  if (Common::UString::split(ctx.getParams()[0].getString(), ' ', parts) != 3) {
213  debugC(Common::kDebugEngineScripts, 1, "Functions::%s: \"%s\"",
214  ctx.getName().c_str(), ctx.getParams()[0].getString().c_str());
215  return;
216  }
217 
218  try {
219  Common::parseString(parts[0], x);
220  Common::parseString(parts[1], y);
221  Common::parseString(parts[2], z);
222  } catch (Common::Exception &e) {
223  debugC(Common::kDebugEngineScripts, 1, "Functions::%s: %s (\"%s\")",
224  ctx.getName().c_str(), e.what(), ctx.getParams()[0].getString().c_str());
225  return;
226  }
227 
228  ctx.getReturn().setVector(x, y, z);
229 }
230 
232  uint32 c = 0;
233 
234  const Common::UString &str = ctx.getParams()[0].getString();
235  if (!str.empty())
236  c = *str.begin();
237 
238  if (!Common::UString::isASCII(c))
239  c = 0;
240 
241  ctx.getReturn() = (int32) c;
242 }
243 
245  ctx.getReturn() = Common::UString((uint32) (ctx.getParams()[0].getInt() & 0x7F), 1);
246 }
247 
249  ctx.getReturn() = ctx.getParams()[0].getString().empty();
250 }
251 
253  ctx.getReturn() = (int32) ctx.getParams()[0].getString().size();
254 }
255 
257  ctx.getReturn() = ctx.getParams()[0].getString().toUpper();
258 }
259 
261  ctx.getReturn() = ctx.getParams()[0].getString().toLower();
262 }
263 
265  ctx.getReturn().getString().clear();
266 
267  const Common::UString &str = ctx.getParams()[0].getString();
268 
269  const int32 n = ctx.getParams()[1].getInt();
270  if ((n <= 0) || ((size_t)n > str.size())) {
271  debugC(Common::kDebugEngineScripts, 1, "Functions::%s: \"%s\", %d",
272  ctx.getName().c_str(), str.c_str(), n);
273  return;
274  }
275 
276  ctx.getReturn() = str.substr(str.getPosition(str.size() - (size_t) n), str.end());
277 }
278 
280  ctx.getReturn().getString().clear();
281 
282  const Common::UString &str = ctx.getParams()[0].getString();
283 
284  const int32 n = ctx.getParams()[1].getInt();
285  if ((n < 0) || ((size_t)n >= str.size())) {
286  debugC(Common::kDebugEngineScripts, 1, "Functions::%s: \"%s\", %d",
287  ctx.getName().c_str(), str.c_str(), n);
288  return;
289  }
290 
291  ctx.getReturn() = str.substr(str.begin(), str.getPosition((size_t) n));
292 }
293 
295  ctx.getReturn().getString().clear();
296  if (ctx.getParams()[2].getInt() < 0) {
297  debugC(Common::kDebugEngineScripts, 1, "Functions::%s: %d",
298  ctx.getName().c_str(), ctx.getParams()[2].getInt());
299  return;
300  }
301 
302  Common::UString str = ctx.getParams()[0].getString();
303 
304  str.insert(str.getPosition((size_t) ctx.getParams()[2].getInt()), ctx.getParams()[1].getString());
305 
306  ctx.getReturn() = str;
307 }
308 
310  ctx.getReturn().getString().clear();
311 
312  const Common::UString &str = ctx.getParams()[0].getString();
313 
314  const int32 offset = ctx.getParams()[1].getInt();
315  const int32 count = ctx.getParams()[2].getInt();
316 
317  if ((offset < 0) || ((size_t)offset >= str.size()) || (count <= 0)) {
318  debugC(Common::kDebugEngineScripts, 1, "Functions::%s: \"%s\", %d, %d",
319  ctx.getName().c_str(), str.c_str(), offset, count);
320  return;
321  }
322 
323  Common::UString::iterator from = str.getPosition((size_t) offset);
324  Common::UString::iterator to = str.getPosition(MIN<size_t>(offset + count, str.size()));
325 
326  ctx.getReturn() = str.substr(from, to);
327 }
328 
330  const Common::UString &str = ctx.getParams()[0].getString();
331  const Common::UString &sub = ctx.getParams()[1].getString();
332  const int32 start = ctx.getParams()[2].getInt();
333 
334  ctx.getReturn() = -1;
335 
336  if ((start < 0) || ((size_t)start >= str.size())) {
337  debugC(Common::kDebugEngineScripts, 1, "Functions::%s: \"%s\", \"%s\", %d",
338  ctx.getName().c_str(), str.c_str(), sub.c_str(), start);
339  return;
340  }
341 
342  const Common::UString subStr = str.substr(str.getPosition(start), str.end());
343 
344  Common::UString::iterator it = subStr.findFirst(sub);
345  if (it == subStr.end())
346  return;
347 
348  ctx.getReturn() = (int32) (subStr.getPosition(it) + start);
349 }
350 
352  const uint32 strRef = (uint32) ctx.getParams()[0].getInt();
353 
354  ctx.getReturn() = TalkMan.getString(strRef, Aurora::kLanguageGenderMale);
355 }
356 
358  const uint32 strRef = (uint32) ctx.getParams()[0].getInt();
359  const Aurora::LanguageGender gender = (Aurora::LanguageGender) ctx.getParams()[1].getInt();
360 
361  ctx.getReturn() = TalkMan.getString(strRef, gender);
362 }
363 
365  ctx.getReturn() = "";
366 
367  const Aurora::NWScript::NCSFile *ncs = ctx.getCurrentScript();
368  if (!ncs)
369  return;
370 
371  ctx.getReturn() = ncs->getName();
372 }
373 
375  ctx.getReturn() = "";
376 
377  const Aurora::NWScript::NCSFile *ncs = ctx.getCurrentScript();
378  if (!ncs)
379  return;
380 
381  ctx.getReturn() = ncs->getName() + ".ncs";
382 }
383 
384 } // End of namespace DragonAge
385 
386 } // End of namespace Engines
LanguageGender
Definition: language.h:67
void stringRight(Aurora::NWScript::FunctionContext &ctx)
void insert(iterator pos, uint32 c)
Insert character c in front of this position.
Definition: ustring.cpp:513
void toString(Aurora::NWScript::FunctionContext &ctx)
#define TalkMan
Shortcut for accessing the talk manager.
Definition: talkman.h:111
void debugC(Common::DebugChannel channel, uint32 level, const char *s,...)
Definition: debug.cpp:34
Common::UString formatTag(const Object *object)
Construct a string with the tag of this object.
Definition: util.cpp:34
A class holding an UTF-8 string.
Definition: ustring.h:48
void floatToString(Aurora::NWScript::FunctionContext &ctx)
Utility functions for debug output.
UString composeString(T value)
Convert any POD integer, float/double or bool type into a string.
Definition: strutil.cpp:276
A date/time object, storing a specific point in time.
Definition: datetime.h:36
void intToString(Aurora::NWScript::FunctionContext &ctx)
Context of an NWScript function.
"EScripts", engine scripts.
Definition: debugman.h:59
void printInteger(Aurora::NWScript::FunctionContext &ctx)
iterator getPosition(size_t n) const
Convert a numerical position into an iterator.
Definition: ustring.cpp:501
void getStringLength(Aurora::NWScript::FunctionContext &ctx)
iterator begin() const
Definition: ustring.cpp:253
void subString(Aurora::NWScript::FunctionContext &ctx)
void logTrace(Aurora::NWScript::FunctionContext &ctx)
UString formatDateTimeISO(uint32 sep=0, uint32 sepDate=0, uint32 sepTime=0) const
Return a string representation of the date and time in ISO 8601 format.
Definition: datetime.cpp:79
void isStringEmpty(Aurora::NWScript::FunctionContext &ctx)
iterator findFirst(uint32 c) const
Definition: ustring.cpp:261
Utility templates and functions for working with strings and streams.
An NCS, BioWare&#39;s NWN Compile Script.
Definition: ncsfile.h:86
void stringToInt(Aurora::NWScript::FunctionContext &ctx)
Exception that provides a stack of explanations.
Definition: error.h:36
void charToInt(Aurora::NWScript::FunctionContext &ctx)
Utility functions for manipulating date and time.
void printString(Aurora::NWScript::FunctionContext &ctx)
void getCurrentScriptResource(Aurora::NWScript::FunctionContext &ctx)
Basic exceptions to throw.
UString substr(iterator from, iterator to) const
Definition: ustring.cpp:706
utf8::iterator< std::string::const_iterator > iterator
Definition: ustring.h:50
const char * c_str() const
Return the (utf8 encoded) string data.
Definition: ustring.cpp:249
static UString format(const char *s,...) GCC_PRINTF(1
Print formatted data into an UString object, similar to sprintf().
Definition: ustring.cpp:718
Utility templates and functions.
void vectorToString(Aurora::NWScript::FunctionContext &ctx)
void printToLog(Aurora::NWScript::FunctionContext &ctx)
void setVector(float x, float y, float z)
Definition: variable.cpp:335
void printVector(Aurora::NWScript::FunctionContext &ctx)
Types and functions related to language.
void getTlkTableString(Aurora::NWScript::FunctionContext &ctx)
static Common::UString formatFloat(float f, int width=18, int decimals=9)
Definition: functions.cpp:97
bool empty() const
Is the string empty?
Definition: ustring.cpp:245
const Common::UString & getName() const
void getStringByStringId(Aurora::NWScript::FunctionContext &ctx)
void resourceToString(Aurora::NWScript::FunctionContext &ctx)
Handling BioWare&#39;s NWN Compiled Scripts.
Unicode string handling.
void intToChar(Aurora::NWScript::FunctionContext &ctx)
void printResource(Aurora::NWScript::FunctionContext &ctx)
void intToHexString(Aurora::NWScript::FunctionContext &ctx)
void stringLowerCase(Aurora::NWScript::FunctionContext &ctx)
void findSubString(Aurora::NWScript::FunctionContext &ctx)
static bool isASCII(uint32 c)
Is the character an ASCII character?
Definition: ustring.cpp:785
void stringUpperCase(Aurora::NWScript::FunctionContext &ctx)
size_t size() const
Return the size of the string, in characters.
Definition: ustring.cpp:241
void stringToFloat(Aurora::NWScript::FunctionContext &ctx)
uint32_t uint32
Definition: types.h:204
The global talk manager for Aurora strings.
Coordinated Universal Time (UTC).
Definition: datetime.h:39
Common::UString & getString()
Definition: variable.cpp:314
void DEBUG_printToScreen(Aurora::NWScript::FunctionContext &ctx)
void status(const char *s,...)
Definition: util.cpp:52
Dragon Age: Origins engine functions.
void objectToString(Aurora::NWScript::FunctionContext &ctx)
const Common::UString & getName() const
Definition: ncsfile.cpp:294
void printFloat(Aurora::NWScript::FunctionContext &ctx)
void split(iterator splitPoint, UString &left, UString &right, bool remove=false) const
Definition: ustring.cpp:621
void printObject(Aurora::NWScript::FunctionContext &ctx)
iterator end() const
Definition: ustring.cpp:257
void getCurrentScriptName(Aurora::NWScript::FunctionContext &ctx)
const char * what() const
Definition: error.cpp:73
void stringLeft(Aurora::NWScript::FunctionContext &ctx)
void insertString(Aurora::NWScript::FunctionContext &ctx)
void printToLogAndFlush(Aurora::NWScript::FunctionContext &ctx)
NWScript utility functions.
void clear()
Clear the string&#39;s contents.
Definition: ustring.cpp:236
void parseString(const UString &str, T &value, bool allowEmpty)
Parse a string into any POD integer, float/double or bool type.
Definition: strutil.cpp:215
void stringToVector(Aurora::NWScript::FunctionContext &ctx)
void printWarning(Aurora::NWScript::FunctionContext &ctx)
void printToLogWindow(Aurora::NWScript::FunctionContext &ctx)
int32_t int32
Definition: types.h:203