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 
45 
47 
48 namespace Engines {
49 
50 namespace Witcher {
51 
54 
55  status("Witcher: %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().getString().c_str(), msg.c_str());
68 }
69 
71  status("Witcher: %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("Witcher: %s", formatFloat(value, width, decimals).c_str());
80 }
81 
83  status("Witcher: %s", ctx.getParams()[0].getString().c_str());
84 }
85 
87  Aurora::NWScript::Object *object = ctx.getParams()[0].getObject();
88 
89  ctx.getReturn() = Common::UString::format("object<%s,%p)", Aurora::NWScript::formatTag(object).c_str(),
90  static_cast<void *>(object));
91 
92  status("Witcher: %s", ctx.getReturn().getString().c_str());
93 }
94 
96  Aurora::NWScript::Object *object = ctx.getParams()[0].getObject();
97 
98  ctx.getReturn() = Common::UString::format("object<%s,%p)", Aurora::NWScript::formatTag(object).c_str(),
99  static_cast<void *>(object));
100 }
101 
103  float x, y, z;
104  ctx.getParams()[0].getVector(x, y, z);
105 
106  const bool prepend = ctx.getParams()[1].getInt() != 0;
107 
108  ctx.getReturn() = Common::UString::format("%s%f, %f, %f", prepend ? "PRINTVECTOR:" : "", x, y, z);
109 
110  status("Witcher: %s", ctx.getReturn().getString().c_str());
111 }
112 
114  ctx.getReturn() = Common::composeString(ctx.getParams()[0].getInt());
115 }
116 
118  const float value = ctx.getParams()[0].getFloat();
119  const int width = ctx.getParams()[1].getInt();
120  const int decimals = ctx.getParams()[2].getInt();
121 
122  ctx.getReturn() = formatFloat(value, width, decimals);
123 }
124 
126  ctx.getReturn() = Common::UString::format("0x%08x", (uint32) ctx.getParams()[0].getInt());
127 }
128 
130  int32 i = 0;
131 
132  try {
133  Common::parseString(ctx.getParams()[0].getString(), i);
134  } catch (Common::Exception &e) {
135  debugC(Common::kDebugEngineScripts, 1, "Functions::%s: %s (\"%s\")",
136  ctx.getName().c_str(), e.what(), ctx.getParams()[0].getString().c_str());
137  }
138 
139  ctx.getReturn() = i;
140 }
141 
143  float f = 0.0f;
144 
145  try {
146  Common::parseString(ctx.getParams()[0].getString(), f);
147  } catch (Common::Exception &e) {
148  debugC(Common::kDebugEngineScripts, 1, "Functions::%s: %s (\"%s\")",
149  ctx.getName().c_str(), e.what(), ctx.getParams()[0].getString().c_str());
150  }
151 
152  ctx.getReturn() = f;
153 }
154 
156  ctx.getReturn() = (int32) ctx.getParams()[0].getString().size();
157 }
158 
160  ctx.getReturn() = ctx.getParams()[0].getString().toUpper();
161 }
162 
164  ctx.getReturn() = ctx.getParams()[0].getString().toLower();
165 }
166 
168  ctx.getReturn().getString().clear();
169 
170  const Common::UString &str = ctx.getParams()[0].getString();
171 
172  const int32 n = ctx.getParams()[1].getInt();
173  if ((n <= 0) || ((size_t)n > str.size())) {
174  debugC(Common::kDebugEngineScripts, 1, "Functions::%s: \"%s\", %d",
175  ctx.getName().c_str(), str.c_str(), n);
176  return;
177  }
178 
179  ctx.getReturn() = str.substr(str.getPosition(str.size() - (size_t) n), str.end());
180 }
181 
183  ctx.getReturn().getString().clear();
184 
185  const Common::UString &str = ctx.getParams()[0].getString();
186 
187  const int32 n = ctx.getParams()[1].getInt();
188  if ((n < 0) || ((size_t)n >= str.size())) {
189  debugC(Common::kDebugEngineScripts, 1, "Functions::%s: \"%s\", %d",
190  ctx.getName().c_str(), str.c_str(), n);
191  return;
192  }
193 
194  ctx.getReturn() = str.substr(str.begin(), str.getPosition((size_t) n));
195 }
196 
198  ctx.getReturn().getString().clear();
199  if (ctx.getParams()[2].getInt() < 0) {
200  debugC(Common::kDebugEngineScripts, 1, "Functions::%s: %d",
201  ctx.getName().c_str(), ctx.getParams()[2].getInt());
202  return;
203  }
204 
205  Common::UString str = ctx.getParams()[0].getString();
206 
207  str.insert(str.getPosition((size_t) ctx.getParams()[2].getInt()), ctx.getParams()[1].getString());
208 
209  ctx.getReturn() = str;
210 }
211 
213  ctx.getReturn().getString().clear();
214 
215  const Common::UString &str = ctx.getParams()[0].getString();
216 
217  const int32 offset = ctx.getParams()[1].getInt();
218  const int32 count = ctx.getParams()[2].getInt();
219 
220  if ((offset < 0) || ((size_t)offset >= str.size()) || (count <= 0)) {
221  debugC(Common::kDebugEngineScripts, 1, "Functions::%s: \"%s\", %d, %d",
222  ctx.getName().c_str(), str.c_str(), offset, count);
223  return;
224  }
225 
226  Common::UString::iterator from = str.getPosition((size_t) offset);
227  Common::UString::iterator to = str.getPosition(MIN<size_t>(offset + count, str.size()));
228 
229  ctx.getReturn() = str.substr(from, to);
230 }
231 
233  const Common::UString &str = ctx.getParams()[0].getString();
234  const Common::UString &sub = ctx.getParams()[1].getString();
235 
236  ctx.getReturn() = -1;
237 
238  Common::UString::iterator it = str.findFirst(sub);
239  if (it == str.end())
240  return;
241 
242  ctx.getReturn() = (int32) str.getPosition(it);
243 }
244 
246  const uint32 strRef = (uint32) ctx.getParams()[0].getInt();
247  const Aurora::LanguageGender gender = (Aurora::LanguageGender) ctx.getParams()[1].getInt();
248 
249  ctx.getReturn() = TalkMan.getString(strRef, gender);
250 }
251 
253  ctx.getReturn().getString().clear();
254 
255  const Common::UString &file = ctx.getParams()[0].getString();
256  const Common::UString &col = ctx.getParams()[1].getString();
257  const uint32 row = (uint32) ctx.getParams()[2].getInt();
258 
259  if (file.empty() || col.empty())
260  return;
261 
262  const Aurora::TwoDAFile &twoda = TwoDAReg.get2DA(file);
263 
264  ctx.getReturn() = twoda.getRow(row).getString(col);
265 }
266 
267 } // End of namespace Witcher
268 
269 } // End of namespace Engines
LanguageGender
Definition: language.h:67
Class to hold the two-dimensional array of a 2DA file.
Definition: 2dafile.h:124
A container of The Witcher objects.
void writeTimestampedLogEntry(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
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 intToString(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
void getStringByStrRef(Aurora::NWScript::FunctionContext &ctx)
Utility functions for debug output.
void getStringLength(Aurora::NWScript::FunctionContext &ctx)
void stringToFloat(Aurora::NWScript::FunctionContext &ctx)
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 printVector(Aurora::NWScript::FunctionContext &ctx)
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
iterator begin() const
Definition: ustring.cpp:253
void findSubString(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 stringToInt(Aurora::NWScript::FunctionContext &ctx)
iterator findFirst(uint32 c) const
Definition: ustring.cpp:261
Utility templates and functions for working with strings and streams.
void printInteger(Aurora::NWScript::FunctionContext &ctx)
const Common::UString & getString(Language language, LanguageGender gender=kLanguageGenderCurrent) const
Get the string of that language.
Definition: locstring.cpp:82
static Creature * toPC(Aurora::NWScript::Object *object)
void getStringUpperCase(Aurora::NWScript::FunctionContext &ctx)
Exception that provides a stack of explanations.
Definition: error.h:36
Utility functions for manipulating date and time.
static Aurora::NWScript::Object * getParamObject(const Aurora::NWScript::FunctionContext &ctx, size_t n)
Definition: functions.cpp:124
void printFloat(Aurora::NWScript::FunctionContext &ctx)
Basic exceptions to throw.
A creature in a The Witcher area.
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.
An object in a The Witcher area.
Handling BioWare&#39;s 2DAs (two-dimensional array).
void sendMessageToPC(Aurora::NWScript::FunctionContext &ctx)
const Aurora::LocString & getName() const
Return the object&#39;s name.
Definition: object.cpp:81
Types and functions related to language.
void insertString(Aurora::NWScript::FunctionContext &ctx)
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 getStringLowerCase(Aurora::NWScript::FunctionContext &ctx)
The global 2DA registry.
void warning(const char *s,...)
Definition: util.cpp:33
Unicode string handling.
void getStringRight(Aurora::NWScript::FunctionContext &ctx)
void printString(Aurora::NWScript::FunctionContext &ctx)
const TwoDARow & getRow(size_t row) const
Get a row.
Definition: 2dafile.cpp:421
size_t size() const
Return the size of the string, in characters.
Definition: ustring.cpp:241
static Common::UString formatFloat(float f, int width=18, int decimals=9)
Definition: functions.cpp:120
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 get2DAString(Aurora::NWScript::FunctionContext &ctx)
The Witcher engine functions.
void status(const char *s,...)
Definition: util.cpp:52
void floatToString(Aurora::NWScript::FunctionContext &ctx)
void getSubString(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 printObject(Aurora::NWScript::FunctionContext &ctx)
NWScript utility functions.
void clear()
Clear the string&#39;s contents.
Definition: ustring.cpp:236
void objectToString(Aurora::NWScript::FunctionContext &ctx)
void getStringLeft(Aurora::NWScript::FunctionContext &ctx)
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 intToHexString(Aurora::NWScript::FunctionContext &ctx)
int32_t int32
Definition: types.h:203