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 #include "src/aurora/2dareg.h"
35 #include "src/aurora/2dafile.h"
36 
39 
41 
43 #include "src/engines/nwn/object.h"
45 
47 
48 namespace Engines {
49 
50 namespace NWN {
51 
54 
55  status("NWN: %s: %s", tstamp.c_str(), ctx.getParams()[0].getString().c_str());
56 }
57 
60  if (!pc) {
61  debugC(Common::kDebugEngineScripts, 1, "Functions::%s: No PC", ctx.getName().c_str());
62  return;
63  }
64 
65  const Common::UString &msg = ctx.getParams()[1].getString();
66 
67  warning("Send message to PC \"%s\": \"%s\"", pc->getName().c_str(), msg.c_str());
68 }
69 
71  status("NWN: %d", ctx.getParams()[0].getInt());
72 }
73 
75  const float value = ctx.getParams()[0].getFloat();
76  const int width = ctx.getParams()[1].getInt();
77  const int decimals = ctx.getParams()[2].getInt();
78 
79  status("NWN: %s", formatFloat(value, width, decimals).c_str());
80 }
81 
83  status("NWN: %s", ctx.getParams()[0].getString().c_str());
84 }
85 
87  Aurora::NWScript::Object *object = ctx.getParams()[0].getObject();
88 
89  status("NWN: object<%s,%p)", Aurora::NWScript::formatTag(object).c_str(), static_cast<void *>(object));
90 }
91 
93  Aurora::NWScript::Object *object = ctx.getParams()[0].getObject();
94 
95  ctx.getReturn() = Common::UString::format("object<%s,%p)", Aurora::NWScript::formatTag(object).c_str(),
96  static_cast<void *>(object));
97 }
98 
100  float x, y, z;
101  ctx.getParams()[0].getVector(x, y, z);
102 
103  const bool prepend = ctx.getParams()[1].getInt() != 0;
104 
105  status("NWN: %s%f, %f, %f", prepend ? "PRINTVECTOR:" : "", x, y, z);
106 }
107 
109  ctx.getReturn() = Common::composeString(ctx.getParams()[0].getInt());
110 }
111 
113  const float value = ctx.getParams()[0].getFloat();
114  const int width = ctx.getParams()[1].getInt();
115  const int decimals = ctx.getParams()[2].getInt();
116 
117  ctx.getReturn() = formatFloat(value, width, decimals);
118 }
119 
121  ctx.getReturn() = Common::UString::format("0x%08x", (uint32) ctx.getParams()[0].getInt());
122 }
123 
125  int32 i = 0;
126 
127  try {
128  Common::parseString(ctx.getParams()[0].getString(), i);
129  } catch (Common::Exception &e) {
130  debugC(Common::kDebugEngineScripts, 1, "Functions::%s: %s (\"%s\")",
131  ctx.getName().c_str(), e.what(), ctx.getParams()[0].getString().c_str());
132  }
133 
134  ctx.getReturn() = i;
135 }
136 
138  float f = 0.0f;
139 
140  try {
141  Common::parseString(ctx.getParams()[0].getString(), f);
142  } catch (Common::Exception &e) {
143  debugC(Common::kDebugEngineScripts, 1, "Functions::%s: %s (\"%s\")",
144  ctx.getName().c_str(), e.what(), ctx.getParams()[0].getString().c_str());
145  }
146 
147  ctx.getReturn() = f;
148 }
149 
151  ctx.getReturn() = (int32) ctx.getParams()[0].getString().size();
152 }
153 
155  ctx.getReturn() = ctx.getParams()[0].getString().toUpper();
156 }
157 
159  ctx.getReturn() = ctx.getParams()[0].getString().toLower();
160 }
161 
163  ctx.getReturn().getString().clear();
164 
165  const Common::UString &str = ctx.getParams()[0].getString();
166 
167  const int32 n = ctx.getParams()[1].getInt();
168  if ((n <= 0) || ((size_t)n > str.size())) {
169  debugC(Common::kDebugEngineScripts, 1, "Functions::%s: \"%s\", %d",
170  ctx.getName().c_str(), str.c_str(), n);
171  return;
172  }
173 
174  ctx.getReturn() = str.substr(str.getPosition(str.size() - (size_t) n), str.end());
175 }
176 
178  ctx.getReturn().getString().clear();
179 
180  const Common::UString &str = ctx.getParams()[0].getString();
181 
182  const int32 n = ctx.getParams()[1].getInt();
183  if ((n < 0) || ((size_t)n >= str.size())) {
184  debugC(Common::kDebugEngineScripts, 1, "Functions::%s: \"%s\", %d",
185  ctx.getName().c_str(), str.c_str(), n);
186  return;
187  }
188 
189  ctx.getReturn() = str.substr(str.begin(), str.getPosition((size_t) n));
190 }
191 
193  ctx.getReturn().getString().clear();
194  if (ctx.getParams()[2].getInt() < 0) {
195  debugC(Common::kDebugEngineScripts, 1, "Functions::%s: %d",
196  ctx.getName().c_str(), ctx.getParams()[2].getInt());
197  return;
198  }
199 
200  Common::UString str = ctx.getParams()[0].getString();
201 
202  str.insert(str.getPosition((size_t) ctx.getParams()[2].getInt()), ctx.getParams()[1].getString());
203 
204  ctx.getReturn() = str;
205 }
206 
208  ctx.getReturn().getString().clear();
209 
210  const Common::UString &str = ctx.getParams()[0].getString();
211 
212  const int32 offset = ctx.getParams()[1].getInt();
213  const int32 count = ctx.getParams()[2].getInt();
214 
215  if ((offset < 0) || ((size_t)offset >= str.size()) || (count <= 0)) {
216  debugC(Common::kDebugEngineScripts, 1, "Functions::%s: \"%s\", %d, %d",
217  ctx.getName().c_str(), str.c_str(), offset, count);
218  return;
219  }
220 
221  Common::UString::iterator from = str.getPosition((size_t) offset);
222  Common::UString::iterator to = str.getPosition(MIN<size_t>(offset + count, str.size()));
223 
224  ctx.getReturn() = str.substr(from, to);
225 }
226 
228  const Common::UString &str = ctx.getParams()[0].getString();
229  const Common::UString &sub = ctx.getParams()[1].getString();
230  const int32 start = ctx.getParams()[2].getInt();
231 
232  ctx.getReturn() = -1;
233 
234  if ((start < 0) || ((size_t)start >= str.size())) {
235  debugC(Common::kDebugEngineScripts, 1, "Functions::%s: \"%s\", \"%s\", %d",
236  ctx.getName().c_str(), str.c_str(), sub.c_str(), start);
237  return;
238  }
239 
240  const Common::UString subStr = str.substr(str.getPosition(start), str.end());
241 
242  Common::UString::iterator it = subStr.findFirst(sub);
243  if (it == subStr.end())
244  return;
245 
246  ctx.getReturn() = (int32) (subStr.getPosition(it) + start);
247 }
248 
250  const uint32 strRef = (uint32) ctx.getParams()[0].getInt();
251  const Aurora::LanguageGender gender = (Aurora::LanguageGender) ctx.getParams()[1].getInt();
252 
253  ctx.getReturn() = TalkMan.getString(strRef, gender);
254 }
255 
257  int32 tokenNumber = ctx.getParams()[0].getInt();
258  const Common::UString &tokenValue = ctx.getParams()[1].getString();
259 
260  const Common::UString tokenName = Common::UString::format("<CUSTOM%d>", tokenNumber);
261 
262  TokenMan.set(tokenName, tokenValue);
263 }
264 
266  ctx.getReturn().getString().clear();
267 
268  const Common::UString &file = ctx.getParams()[0].getString();
269  const Common::UString &col = ctx.getParams()[1].getString();
270  const uint32 row = (uint32) ctx.getParams()[2].getInt();
271 
272  if (file.empty() || col.empty())
273  return;
274 
275  const Aurora::TwoDAFile &twoda = TwoDAReg.get2DA(file);
276 
277  ctx.getReturn() = twoda.getRow(row).getString(col);
278 }
279 
280 } // End of namespace NWN
281 
282 } // End of namespace Engines
LanguageGender
Definition: language.h:67
Class to hold the two-dimensional array of a 2DA file.
Definition: 2dafile.h:124
void printFloat(Aurora::NWScript::FunctionContext &ctx)
void getStringRight(Aurora::NWScript::FunctionContext &ctx)
void insert(iterator pos, uint32 c)
Insert character c in front of this position.
Definition: ustring.cpp:513
#define TalkMan
Shortcut for accessing the talk manager.
Definition: talkman.h:111
A container of Neverwinter Nights objects.
const Common::UString & getString(size_t column) const
Return the contents of a cell as a string.
Definition: 2dafile.cpp:59
void debugC(Common::DebugChannel channel, uint32 level, const char *s,...)
Definition: debug.cpp:34
void printString(Aurora::NWScript::FunctionContext &ctx)
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
Utility functions for debug output.
const Common::UString & getName() const
Return the object&#39;s name.
Definition: object.cpp:90
static Common::UString formatFloat(float f, int width=18, int decimals=9)
Definition: functions.cpp:114
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
Context of an NWScript function.
"EScripts", engine scripts.
Definition: debugman.h:59
iterator getPosition(size_t n) const
Convert a numerical position into an iterator.
Definition: ustring.cpp:501
void printInteger(Aurora::NWScript::FunctionContext &ctx)
iterator begin() const
Definition: ustring.cpp:253
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 getStringUpperCase(Aurora::NWScript::FunctionContext &ctx)
void getStringLeft(Aurora::NWScript::FunctionContext &ctx)
iterator findFirst(uint32 c) const
Definition: ustring.cpp:261
Utility templates and functions for working with strings and streams.
Exception that provides a stack of explanations.
Definition: error.h:36
Utility functions for manipulating date and time.
void getStringLength(Aurora::NWScript::FunctionContext &ctx)
void stringToInt(Aurora::NWScript::FunctionContext &ctx)
Basic exceptions to throw.
UString substr(iterator from, iterator to) const
Definition: ustring.cpp:706
static Aurora::NWScript::Object * getParamObject(const Aurora::NWScript::FunctionContext &ctx, size_t n)
Definition: functions.cpp:118
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 getStringByStrRef(Aurora::NWScript::FunctionContext &ctx)
Handling BioWare&#39;s 2DAs (two-dimensional array).
void stringToFloat(Aurora::NWScript::FunctionContext &ctx)
static Creature * toPC(Aurora::NWScript::Object *object)
void printVector(Aurora::NWScript::FunctionContext &ctx)
Types and functions related to language.
bool empty() const
Is the string empty?
Definition: ustring.cpp:245
#define TwoDAReg
Shortcut for accessing the 2da registry.
Definition: 2dareg.h:101
const Common::UString & getName() const
void intToHexString(Aurora::NWScript::FunctionContext &ctx)
void writeTimestampedLogEntry(Aurora::NWScript::FunctionContext &ctx)
The global 2DA registry.
void warning(const char *s,...)
Definition: util.cpp:33
void get2DAString(Aurora::NWScript::FunctionContext &ctx)
Unicode string handling.
An object in a Neverwinter Nights area.
const TwoDARow & getRow(size_t row) const
Get a row.
Definition: 2dafile.cpp:421
void floatToString(Aurora::NWScript::FunctionContext &ctx)
size_t size() const
Return the size of the string, in characters.
Definition: ustring.cpp:241
uint32_t uint32
Definition: types.h:204
The global talk manager for Aurora strings.
A creature in a Neverwinter Nights area.
Coordinated Universal Time (UTC).
Definition: datetime.h:39
Common::UString & getString()
Definition: variable.cpp:314
void status(const char *s,...)
Definition: util.cpp:52
void setCustomToken(Aurora::NWScript::FunctionContext &ctx)
void intToString(Aurora::NWScript::FunctionContext &ctx)
void getStringLowerCase(Aurora::NWScript::FunctionContext &ctx)
Manager for tokens in Aurora engines text strings.
iterator end() const
Definition: ustring.cpp:257
const char * what() const
Definition: error.cpp:73
void insertString(Aurora::NWScript::FunctionContext &ctx)
void sendMessageToPC(Aurora::NWScript::FunctionContext &ctx)
void objectToString(Aurora::NWScript::FunctionContext &ctx)
void findSubString(Aurora::NWScript::FunctionContext &ctx)
void getSubString(Aurora::NWScript::FunctionContext &ctx)
NWScript utility functions.
void clear()
Clear the string&#39;s contents.
Definition: ustring.cpp:236
Neverwinter Nights engine functions.
#define TokenMan
Shortcut for accessing the token manager.
Definition: tokenman.h:63
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 printObject(Aurora::NWScript::FunctionContext &ctx)
int32_t int32
Definition: types.h:203