xoreos  0.0.5
util.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/strutil.h"
26 
29 
30 namespace Aurora {
31 
32 namespace NWScript {
33 
35  if (!object)
36  return "0";
37 
38  return "\"" + object->getTag() + "\"";
39 }
40 
41 void formatVariable(Common::UString &str, const Variable &var) {
42  switch (var.getType()) {
43  case kTypeInt:
44  str += Common::composeString(var.getInt());
45  break;
46 
47  case kTypeFloat:
48  str += Common::composeString(var.getFloat());
49  break;
50 
51  case kTypeString:
52  str += "\"" + var.getString() + "\"";
53  break;
54 
55  case kTypeObject:
56  str += "<" + formatType(var.getType()) + ">(" + formatTag(var.getObject()) + ")";
57  break;
58 
59  case kTypeVector:
60  {
61  float x, y, z;
62  var.getVector(x, y, z);
63 
64  str += "[";
65  str += Common::composeString(x) + ", ";
66  str += Common::composeString(y) + ", ";
67  str += Common::composeString(z);
68  str += "]";
69  }
70  break;
71 
72  case kTypeArray:
73  {
74  const Variable::Array &array = var.getArray();
75 
76  str += "{";
77  for (size_t i = 0; i < array.size(); i++) {
78  if (i != 0)
79  str += ", ";
80 
81  if (!array[i].get())
82  str += "/";
83  else
84  formatVariable(str, *array[i]);
85  }
86  str += "}";
87  }
88  break;
89 
90  case kTypeReference:
91  {
92  Variable *ref = var.getReference();
93 
94  str += "<" + formatType(var.getType()) + ">(";
95  if (ref)
96  formatVariable(str, *ref);
97  else
98  str += "0";
99  str += ")";
100  }
101  break;
102 
103  default:
104  str += "<" + formatType(var.getType()) + ">";
105  break;
106  }
107 }
108 
110  Common::UString params;
111  for (size_t i = 0; i < ctx.getParams().size(); i++) {
112  if (i != 0)
113  params += ", ";
114 
115  formatVariable(params, ctx.getParams()[i]);
116  }
117 
118  return params;
119 }
120 
122  Common::UString r;
123 
124  if (ctx.getReturn().getType() != kTypeVoid)
125  formatVariable(r, ctx.getReturn());
126 
127  return r;
128 }
129 
131  switch (type) {
132  case kTypeVoid:
133  return "void";
134 
135  case kTypeInt:
136  return "int";
137 
138  case kTypeFloat:
139  return "float";
140 
141  case kTypeString:
142  return "string";
143 
144  case kTypeObject:
145  return "object";
146 
147  case kTypeVector:
148  return "vector";
149 
150  case kTypeEngineType:
151  return "engine";
152 
153  case kTypeScriptState:
154  return "state";
155 
156  case kTypeArray:
157  return "array";
158 
159  case kTypeReference:
160  return "ref";
161 
162  case kTypeAny:
163  return "any";
164  }
165 
166  return "unknown";
167 }
168 
169 } // End of namespace NWScript
170 
171 } // End of namespace Aurora
std::vector< boost::shared_ptr< Variable > > Array
Definition: variable.h:58
void formatVariable(Common::UString &str, const Variable &var)
Print a description of this variable into that string.
Definition: util.cpp:41
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
Common::UString formatType(Type type)
Construct a string describing this variable type.
Definition: util.cpp:130
UString composeString(T value)
Convert any POD integer, float/double or bool type into a string.
Definition: strutil.cpp:276
Context of an NWScript function.
Utility templates and functions for working with strings and streams.
void getVector(float &x, float &y, float &z) const
Definition: variable.cpp:344
Common::UString formatReturn(const FunctionContext &ctx)
Construct a string describing the return value of this function.
Definition: util.cpp:121
Object * getObject() const
Definition: variable.cpp:321
A reference/pointer to another variable.
Definition: types.h:47
An NWScript object.
"effect", "event", "location", "talent"...
Definition: types.h:43
const Array & getArray() const
Definition: variable.cpp:353
Common::UString & getString()
Definition: variable.cpp:314
Any other type.
Definition: types.h:48
Common::UString formatParams(const FunctionContext &ctx)
Construct a string describing parameters of this function.
Definition: util.cpp:109
Variable * getReference() const
Definition: variable.cpp:408