-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathsp_util.h
More file actions
257 lines (214 loc) · 7.84 KB
/
sp_util.h
File metadata and controls
257 lines (214 loc) · 7.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
/**
* =============================================================================
* Source Python
* Copyright (C) 2012-2015 Source Python Development Team. All rights reserved.
* =============================================================================
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, version 3.0, as published by the
* Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*
* As a special exception, the Source Python Team gives you permission
* to link the code of this program (as well as its derivative works) to
* "Half-Life 2," the "Source Engine," and any Game MODs that run on software
* by the Valve Corporation. You must obey the GNU General Public License in
* all respects for all other code used. Additionally, the Source.Python
* Development Team grants this exception to all derivative works.
*/
#ifndef _SP_UTIL_H
#define _SP_UTIL_H
//-----------------------------------------------------------------------------
// Includes
//-----------------------------------------------------------------------------
#include "utilities/wrap_macros.h"
#include "boost/python.hpp"
// SDK
#include "strtools.h"
#include "dbg.h"
//-----------------------------------------------------------------------------
// Namespaces to use
//-----------------------------------------------------------------------------
using namespace boost::python;
//-----------------------------------------------------------------------------
// Send a message in chunks
//-----------------------------------------------------------------------------
#define MAX_CHUNK 1024
inline void ChunkedMsg(const char* msg, bool bLogged = true)
{
char* pMsg = (char*) msg;
int iLen = strlen(msg);
while(iLen > 0) {
if (bLogged)
Msg("%s", pMsg);
else
printf("%s", pMsg);
pMsg += MAX_CHUNK-1;
iLen -= MAX_CHUNK-1;
}
}
//-----------------------------------------------------------------------------
// Prints and clear the current exception.
//-----------------------------------------------------------------------------
inline void PrintCurrentException(bool bLogged = true)
{
if (PyErr_Occurred())
{
PyObject *pType;
PyObject *pValue;
PyObject *pTraceback;
PyErr_Fetch(&pType, &pValue, &pTraceback);
PyErr_NormalizeException(&pType, &pValue, &pTraceback);
handle<> hType(pType);
handle<> hValue(allow_null(pValue));
handle<> hTraceback(allow_null(pTraceback));
object format_exception = import("traceback").attr("format_exception");
const char* pMsg = extract<const char *>(str("\n").join(format_exception(hType, hValue, hTraceback)));
// Send the message in chunks, because it can get quite big.
ChunkedMsg(pMsg, bLogged);
PyErr_Clear();
}
}
//-----------------------------------------------------------------------------
// Returns True if the class name of the given object equals the given string.
//-----------------------------------------------------------------------------
inline bool CheckClassname(object obj, char* name)
{
return strcmp(extract<char *>(obj.attr("__class__").attr("__name__")), name) == 0;
}
//-----------------------------------------------------------------------------
// Returns a Python string object.
//-----------------------------------------------------------------------------
inline str make_str(const char* pStr, const char* szErrors = NULL)
{
if (!szErrors) {
return str(pStr);
}
return str(handle<>(PyUnicode_DecodeUTF8(pStr, strlen(pStr), szErrors)));
}
//-----------------------------------------------------------------------------
// Returns the registered python class for T.
//-----------------------------------------------------------------------------
template<class T>
inline object get_class_object()
{
const converter::registration *pRegistration = converter::registry::query(typeid(T));
if (!pRegistration || !pRegistration->m_class_object)
return object();
return object(handle<>(borrowed(upcast<PyObject>(pRegistration->m_class_object))));
}
//-----------------------------------------------------------------------------
// Helper template methods for __getitem__ and __setitem__
//-----------------------------------------------------------------------------
template<class cls, class return_type, int iMin, int iMax>
return_type GetItemIndexer(cls* self, int iIndex)
{
if ((iIndex < iMin) || (iIndex > iMax))
BOOST_RAISE_EXCEPTION(PyExc_IndexError, "Index out of range.");
return (*self)[iIndex];
}
template<class T, class U>
U GetItemIndexer(const T* self, const int i)
{
return (*self)[i];
}
template<class cls, class value_type, int iMin, int iMax>
void SetItemIndexer(cls* self, int iIndex, value_type value)
{
if ((iIndex < iMin) || (iIndex > iMax))
BOOST_RAISE_EXCEPTION(PyExc_IndexError, "Index out of range.");
(*self)[iIndex] = value;
}
template<class T, class U>
void SetItemIndexer(T* self, const int i, const U& value)
{
(*self)[i] = value;
}
template<class T, class U, int iIndex>
void IndexSetter(T *pSelf, U pValue)
{
(*pSelf)[iIndex] = pValue;
}
//-----------------------------------------------------------------------------
// Dummy deleter function for shared_ptr that we are not owning.
//-----------------------------------------------------------------------------
template<class T>
void NeverDeleteDeleter(T pSelf)
{
}
namespace sputils {
//-----------------------------------------------------------------------------
// Convert a string into a float array.
// Copied from util_shared.cpp, adapted to return true on success
//-----------------------------------------------------------------------------
inline bool UTIL_StringToFloatArray( float *pVector, int count, const char *pString )
{
char *pstr, *pfront, tempString[128];
int j;
Q_strncpy( tempString, pString, sizeof(tempString) );
pstr = pfront = tempString;
for ( j = 0; j < count; j++ ) // lifted from pr_edict.c
{
pVector[j] = atof( pfront );
// skip any leading whitespace
while ( *pstr && *pstr <= ' ' )
pstr++;
// skip to next whitespace
while ( *pstr && *pstr > ' ' )
pstr++;
if (!*pstr)
break;
pstr++;
pfront = pstr;
}
return j == count - 1;
}
//-----------------------------------------------------------------------------
// Convert a string into an int array.
// Copied from util_shared.cpp, adapted to return true on success
//-----------------------------------------------------------------------------
inline bool UTIL_StringToIntArray( int *pVector, int count, const char *pString )
{
char *pstr, *pfront, tempString[128];
int j;
Q_strncpy( tempString, pString, sizeof(tempString) );
pstr = pfront = tempString;
for ( j = 0; j < count; j++ ) // lifted from pr_edict.c
{
pVector[j] = atoi( pfront );
while ( *pstr && *pstr != ' ' )
pstr++;
if (!*pstr)
break;
pstr++;
pfront = pstr;
}
return j == count - 1;
}
//-----------------------------------------------------------------------------
// Convert the given string into a long. Return true on success.
//-----------------------------------------------------------------------------
inline bool UTIL_StringToLong(long* pOut, const char* szString)
{
char* p;
*pOut = strtol(szString, &p, 10);
return !*p;
}
//-----------------------------------------------------------------------------
// Convert the given string into a double. Return true on success.
//-----------------------------------------------------------------------------
inline bool UTIL_StringToDouble(double* pOut, const char* szString)
{
char* p;
*pOut = strtod(szString, &p);
return !*p;
}
} // namespace sputils
#endif // _SP_UTIL_H