1
- #pragma once
2
1
#ifndef _STD_EXT_internal
3
2
#define _STD_EXT_internal
4
3
5
- #include < math.h>
6
- #include < float.h>
7
- #include < stdio.h>
8
- #include " xrCommon/math_funcs_inline.h"
9
- // #include "xr_token.h"
4
+ #include < cmath>
5
+
6
+ #define BREAK_AT_STRCMP
7
+ #ifndef DEBUG
8
+ #undef BREAK_AT_STRCMP
9
+ #endif
10
+ #ifdef _EDITOR
11
+ #undef BREAK_AT_STRCMP
12
+ #endif
10
13
11
14
#ifdef abs
12
15
#undef abs
28
31
#undef max
29
32
#endif
30
33
31
- #if 0 //def _EDITOR
34
+ #include < cmath>
35
+
36
+ #include < stdio.h>
37
+ #include < string.h>
38
+
39
+ #ifndef __WIN32
40
+ #define stricmp strcasecmp
41
+ #define strnicmp strncasecmp
42
+
43
+ inline char *itoa (int value, char *str, int base)
44
+ {
45
+ switch (base) {
46
+ case 8 :
47
+ sprintf (str, " %o" , value);
48
+ break ;
49
+ default :
50
+ case 10 :
51
+ sprintf (str, " %d" , value);
52
+ break ;
53
+ case 16 :
54
+ sprintf (str, " %x" , value);
55
+ break ;
56
+ }
57
+ return str;
58
+ }
59
+
60
+ inline int wcstombs_s (size_t *outsize, char *mbstr, size_t inbytes, const wchar_t *wcstr, size_t max)
61
+ {
62
+ *outsize = wcstombs (mbstr, wcstr, max);
63
+ return 0 ;
64
+ }
65
+
66
+ inline int mbstowcs_s (size_t *outsize, wchar_t *wcstr, size_t inwords, const char *mbstr, size_t max)
67
+ {
68
+ *outsize = mbstowcs (wcstr, mbstr, max);
69
+ return 0 ;
70
+ }
71
+ #endif
72
+
73
+ #ifdef _EDITOR
32
74
IC char * strncpy_s (char * strDestination, size_t sizeInBytes, const char * strSource, size_t count)
33
75
{
34
76
return strncpy (strDestination, strSource, count);
@@ -39,14 +81,26 @@ IC char* xr_strcpy(char* strDestination, size_t sizeInBytes, const char* strSour
39
81
return strcpy (strDestination, strSource);
40
82
}
41
83
42
- IC char* xr_strcpy(char* strDestination, const char* strSource) { return strcpy(strDestination, strSource); }
43
- IC char* _strlwr_s(char* strDestination, size_t sizeInBytes) { return xr_strlwr(strDestination); }
84
+ IC char * xr_strcpy (char * strDestination, const char * strSource)
85
+ {
86
+ return strcpy (strDestination, strSource);
87
+ }
88
+
89
+ IC char * _strlwr_s (char * strDestination, size_t sizeInBytes)
90
+ {
91
+ return strlwr (strDestination);
92
+ }
93
+
44
94
IC char * xr_strcat (char * strDestination, size_t sizeInBytes, const char * strSource)
45
95
{
46
96
return strncat (strDestination, strSource, sizeInBytes);
47
97
}
48
98
49
- IC char* xr_strcat(char* strDestination, const char* strSource) { return strcat(strDestination, strSource); }
99
+ IC char * xr_strcat (char * strDestination, const char * strSource)
100
+ {
101
+ return strcat (strDestination, strSource);
102
+ }
103
+
50
104
IC int xr_sprintf (char * dest, size_t sizeOfBuffer, const char * format, ...)
51
105
{
52
106
va_list mark;
@@ -56,89 +110,133 @@ IC int xr_sprintf(char* dest, size_t sizeOfBuffer, const char* format, ...)
56
110
va_end (mark);
57
111
return sz;
58
112
}
59
- #endif // _EDITOR
113
+ #endif
60
114
61
- // generic
62
- template <class T >
63
- IC T _min (T a, T b)
115
+ // token type definition
116
+ struct XRCORE_API xr_token
64
117
{
65
- return a < b ? a : b;
66
- }
67
- template <class T >
68
- IC T _max (T a, T b)
118
+ LPCSTR name;
119
+ int id;
120
+ };
121
+
122
+ IC LPCSTR get_token_name (xr_token* tokens, int key)
69
123
{
70
- return a > b ? a : b;
124
+ for (int k = 0 ; tokens[k].name ; k++)
125
+ if (key == tokens[k].id ) return tokens[k].name ;
126
+ return " " ;
71
127
}
72
- template < class T >
73
- IC T _sqr (T a )
128
+
129
+ IC int get_token_id (xr_token* tokens, LPCSTR key )
74
130
{
75
- return a * a;
131
+ for (int k = 0 ; tokens[k].name ; k++)
132
+ if (stricmp (tokens[k].name , key) == 0 )
133
+ return tokens[k].id ;
134
+ return -1 ;
76
135
}
77
136
78
- IC bool _valid ( const float x) noexcept
137
+ struct XRCORE_API xr_token2
79
138
{
80
- // check for: Signaling NaN, Quiet NaN, Negative infinity ( –INF), Positive infinity (+INF), Negative denormalized,
81
- // Positive denormalized
82
- int cls = _fpclass (double (x));
83
- if (cls & (_FPCLASS_SNAN + _FPCLASS_QNAN + _FPCLASS_NINF + _FPCLASS_PINF + _FPCLASS_ND + _FPCLASS_PD))
84
- return false ;
139
+ LPCSTR name;
140
+ LPCSTR info;
141
+ int id;
142
+ };
85
143
86
- /* *****other cases are*****
87
- _FPCLASS_NN Negative normalized non-zero
88
- _FPCLASS_NZ Negative zero ( – 0)
89
- _FPCLASS_PZ Positive 0 (+0)
90
- _FPCLASS_PN Positive normalized non-zero
91
- */
92
- return true ;
144
+ // generic
145
+ template <class T > IC T _min (T a, T b) { return a < b ? a : b; }
146
+ template <class T > IC T _max (T a, T b) { return a > b ? a : b; }
147
+ template <class T > IC T _sqr (T a) { return a*a; }
148
+
149
+ // float
150
+ IC float _abs (float x) { return fabsf (x); }
151
+ IC float _sqrt (float x) { return sqrtf (x); }
152
+ IC float _sin (float x) { return sinf (x); }
153
+ IC float _cos (float x) { return cosf (x); }
154
+ IC BOOL _valid (const float x)
155
+ {
156
+ return std::isnormal (x);
93
157
}
94
158
159
+
95
160
// double
96
- IC bool _valid (const double x)
161
+ IC double _abs (double x) { return fabs (x); }
162
+ IC double _sqrt (double x) { return sqrt (x); }
163
+ IC double _sin (double x) { return sin (x); }
164
+ IC double _cos (double x) { return cos (x); }
165
+ IC BOOL _valid (const double x)
97
166
{
98
- // check for: Signaling NaN, Quiet NaN, Negative infinity ( –INF), Positive infinity (+INF), Negative denormalized,
99
- // Positive denormalized
100
- int cls = _fpclass (x);
101
- if (cls & (_FPCLASS_SNAN + _FPCLASS_QNAN + _FPCLASS_NINF + _FPCLASS_PINF + _FPCLASS_ND + _FPCLASS_PD))
102
- return false ;
103
-
104
- /* *****other cases are*****
105
- _FPCLASS_NN Negative normalized non-zero
106
- _FPCLASS_NZ Negative zero ( – 0)
107
- _FPCLASS_PZ Positive 0 (+0)
108
- _FPCLASS_PN Positive normalized non-zero
109
- */
110
- return true ;
167
+ return std::isnormal (x);
111
168
}
112
169
113
- // XXX: "magic" specializations, that really require profiling to see if they are worth this effort.
114
170
// int8
115
171
IC s8 _abs (s8 x) { return (x >= 0 ) ? x : s8 (-x); }
116
172
IC s8 _min (s8 x, s8 y) { return y + ((x - y) & ((x - y) >> (sizeof (s8) * 8 - 1 ))); };
117
173
IC s8 _max (s8 x, s8 y) { return x - ((x - y) & ((x - y) >> (sizeof (s8) * 8 - 1 ))); };
174
+
118
175
// unsigned int8
119
176
IC u8 _abs (u8 x) { return x; }
177
+
120
178
// int16
121
179
IC s16 _abs (s16 x) { return (x >= 0 ) ? x : s16 (-x); }
122
180
IC s16 _min (s16 x, s16 y) { return y + ((x - y) & ((x - y) >> (sizeof (s16) * 8 - 1 ))); };
123
181
IC s16 _max (s16 x, s16 y) { return x - ((x - y) & ((x - y) >> (sizeof (s16) * 8 - 1 ))); };
182
+
124
183
// unsigned int16
125
184
IC u16 _abs (u16 x) { return x; }
185
+
126
186
// int32
127
187
IC s32 _abs (s32 x) { return (x >= 0 ) ? x : s32 (-x); }
128
188
IC s32 _min (s32 x, s32 y) { return y + ((x - y) & ((x - y) >> (sizeof (s32) * 8 - 1 ))); };
129
189
IC s32 _max (s32 x, s32 y) { return x - ((x - y) & ((x - y) >> (sizeof (s32) * 8 - 1 ))); };
190
+
130
191
// int64
131
192
IC s64 _abs (s64 x) { return (x >= 0 ) ? x : s64 (-x); }
132
193
IC s64 _min (s64 x, s64 y) { return y + ((x - y) & ((x - y) >> (sizeof (s64) * 8 - 1 ))); };
133
194
IC s64 _max (s64 x, s64 y) { return x - ((x - y) & ((x - y) >> (sizeof (s64) * 8 - 1 ))); };
134
195
196
+ IC u32 xr_strlen (const char * S);
197
+
135
198
// string management
136
199
137
200
// return pointer to ".ext"
138
- IC char * strext (const char * S) { return (char *)strrchr (S, ' .' ); }
139
- IC size_t xr_strlen (const char * S) { return strlen (S); }
201
+ IC char * strext (const char * S)
202
+ {
203
+ return (char *)strrchr (S, ' .' );
204
+ }
140
205
141
- // #ifndef _EDITOR
206
+ IC u32 xr_strlen (const char * S)
207
+ {
208
+ return (u32 )strlen (S);
209
+ }
210
+
211
+ IC char * xr_strupr (char * S)
212
+ {
213
+ #ifdef _WIN32
214
+ return _strupr (S);
215
+ #else
216
+ char *p = S;
217
+ for ( ; *S; ++S)
218
+ {
219
+ *S = toupper (*S);
220
+ }
221
+ return p;
222
+ #endif
223
+ }
224
+
225
+ IC char * xr_strlwr (char * S)
226
+ {
227
+ #ifdef _WIN32
228
+ return strlwr (S);
229
+ #else
230
+ char *p = S;
231
+ for ( ; *S; ++S)
232
+ {
233
+ *S = tolower (*S);
234
+ }
235
+ return p;
236
+ #endif
237
+ }
238
+
239
+ #ifndef _EDITOR
142
240
#ifndef MASTER_GOLD
143
241
144
242
inline int xr_strcpy (LPSTR destination, size_t const destination_size, LPCSTR source)
@@ -159,7 +257,7 @@ inline int __cdecl xr_sprintf(LPSTR destination, size_t const buffer_size, LPCST
159
257
}
160
258
161
259
template <int count>
162
- inline int __cdecl xr_sprintf (char (&destination)[count], LPCSTR format_string, ...)
260
+ inline int __cdecl xr_sprintf (char (&destination)[count], LPCSTR format_string, ...)
163
261
{
164
262
va_list args;
165
263
va_start (args, format_string);
@@ -169,7 +267,12 @@ inline int __cdecl xr_sprintf(char (&destination)[count], LPCSTR format_string,
169
267
170
268
inline int xr_strcpy (LPSTR destination, size_t const destination_size, LPCSTR source)
171
269
{
270
+ #ifdef _WIN32
172
271
return strncpy_s (destination, destination_size, source, destination_size);
272
+ #else
273
+ strncpy (destination, source, destination_size);
274
+ return 0 ;
275
+ #endif
173
276
}
174
277
175
278
inline int xr_strcat (LPSTR destination, size_t const buffer_size, LPCSTR source)
@@ -191,36 +294,44 @@ inline int __cdecl xr_sprintf(LPSTR destination, size_t const buffer_size, LPCST
191
294
{
192
295
va_list args;
193
296
va_start (args, format_string);
297
+ #ifdef _WIN32
194
298
return vsnprintf_s (destination, buffer_size, buffer_size - 1 , format_string, args);
299
+ #else
300
+ return vsnprintf (destination, buffer_size, format_string, args);
301
+ #endif
195
302
}
196
303
197
304
template <int count>
198
- inline int __cdecl xr_sprintf (char (&destination)[count], LPCSTR format_string, ...)
305
+ inline int __cdecl xr_sprintf (char (&destination)[count], LPCSTR format_string, ...)
199
306
{
200
307
va_list args;
201
308
va_start (args, format_string);
309
+ #ifdef _WIN32
202
310
return vsnprintf_s (destination, count, count - 1 , format_string, args);
311
+ #else
312
+ return vsnprintf (destination, count, format_string, args);
313
+ #endif
203
314
}
204
315
#endif // #ifndef MASTER_GOLD
205
316
206
317
template <int count>
207
- inline int xr_strcpy (char (&destination)[count], LPCSTR source)
318
+ inline int xr_strcpy (char (&destination)[count], LPCSTR source)
208
319
{
209
320
return xr_strcpy (destination, count, source);
210
321
}
211
322
212
323
template <int count>
213
- inline int xr_strcat (char (&destination)[count], LPCSTR source)
324
+ inline int xr_strcat (char (&destination)[count], LPCSTR source)
214
325
{
215
326
return xr_strcat (destination, count, source);
216
327
}
217
- // #endif // #ifndef _EDITOR
328
+ #endif // #ifndef _EDITOR
218
329
219
- inline void MemFill32 (void * dst, u32 value, size_t dstSize)
330
+ inline void MemFill32 (void * dst, u32 value, size_t dstSize)
220
331
{
221
- u32 * ptr = static_cast <u32 *>(dst);
222
- u32 * end = ptr + dstSize;
223
- while (ptr != end)
332
+ u32 * ptr = static_cast <u32 *>(dst);
333
+ u32 * end = ptr+ dstSize;
334
+ while (ptr!= end)
224
335
*ptr++ = value;
225
336
}
226
337
0 commit comments