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 Jade {
51 
54 
55  status("Jade: %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("Jade: %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("Jade: %s", formatFloat(value, width, decimals).c_str());
80 }
81 
83  status("Jade: %s", ctx.getParams()[0].getString().c_str());
84 }
85 
87  Aurora::NWScript::Object *object = ctx.getParams()[0].getObject();
88 
89  status("Jade: 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("Jade: %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 
231  ctx.getReturn() = -1;
232 
233  Common::UString::iterator it = str.findFirst(sub);
234  if (it == str.end())
235  return;
236 
237  ctx.getReturn() = (int32) str.getPosition(it);
238 }
239 
241  const uint32 strRef = (uint32) ctx.getParams()[0].getInt();
242 
243  ctx.getReturn() = TalkMan.getString(strRef);
244 }
245 
247  int32 tokenNumber = ctx.getParams()[0].getInt();
248  const Common::UString &tokenValue = ctx.getParams()[1].getString();
249 
250  const Common::UString tokenName = Common::UString::format("<CUSTOM%d>", tokenNumber);
251 
252  TokenMan.set(tokenName, tokenValue);
253 }
254 
255 } // End of namespace Jade
256 
257 } // End of namespace Engines
An object within a Jade Empire area.
void getStringUpperCase(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
void debugC(Common::DebugChannel channel, uint32 level, const char *s,...)
Definition: debug.cpp:34
void writeTimestampedLogEntry(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 getStringLowerCase(Aurora::NWScript::FunctionContext &ctx)
Utility functions for debug output.
void intToHexString(Aurora::NWScript::FunctionContext &ctx)
void sendMessageToPC(Aurora::NWScript::FunctionContext &ctx)
void intToString(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 insertString(Aurora::NWScript::FunctionContext &ctx)
Context of an NWScript function.
Jade Empire engine functions.
void getSubString(Aurora::NWScript::FunctionContext &ctx)
"EScripts", engine scripts.
Definition: debugman.h:59
static Common::UString formatFloat(float f, int width=18, int decimals=9)
Definition: functions.cpp:114
iterator getPosition(size_t n) const
Convert a numerical position into an iterator.
Definition: ustring.cpp:501
static Aurora::NWScript::Object * getParamObject(const Aurora::NWScript::FunctionContext &ctx, size_t n)
Definition: functions.cpp:118
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 findSubString(Aurora::NWScript::FunctionContext &ctx)
static Creature * toPC(Aurora::NWScript::Object *object)
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)
A container of Jade Empire objects.
void stringToInt(Aurora::NWScript::FunctionContext &ctx)
Basic exceptions to throw.
void setCustomToken(Aurora::NWScript::FunctionContext &ctx)
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.
Handling BioWare&#39;s 2DAs (two-dimensional array).
Types and functions related to language.
void printFloat(Aurora::NWScript::FunctionContext &ctx)
void stringToFloat(Aurora::NWScript::FunctionContext &ctx)
const Common::UString & getName() const
void printVector(Aurora::NWScript::FunctionContext &ctx)
The global 2DA registry.
void warning(const char *s,...)
Definition: util.cpp:33
void printInteger(Aurora::NWScript::FunctionContext &ctx)
void printString(Aurora::NWScript::FunctionContext &ctx)
Unicode string handling.
void getStringByStrRef(Aurora::NWScript::FunctionContext &ctx)
void printObject(Aurora::NWScript::FunctionContext &ctx)
size_t size() const
Return the size of the string, in characters.
Definition: ustring.cpp:241
void objectToString(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 status(const char *s,...)
Definition: util.cpp:52
void getStringLeft(Aurora::NWScript::FunctionContext &ctx)
void getStringRight(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 floatToString(Aurora::NWScript::FunctionContext &ctx)
NWScript utility functions.
void clear()
Clear the string&#39;s contents.
Definition: ustring.cpp:236
#define TokenMan
Shortcut for accessing the token manager.
Definition: tokenman.h:63
const Common::UString & getName() const
Return the object&#39;s name.
Definition: object.cpp:74
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
int32_t int32
Definition: types.h:203
A creature in a Jade Empire area.