1- #pragma once
21#ifndef _STD_EXT_internal
32#define _STD_EXT_internal
43
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
1013
1114#ifdef abs
1215#undef abs
2831#undef max
2932#endif
3033
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
3274IC char * strncpy_s (char * strDestination, size_t sizeInBytes, const char * strSource, size_t count)
3375{
3476 return strncpy (strDestination, strSource, count);
@@ -39,14 +81,26 @@ IC char* xr_strcpy(char* strDestination, size_t sizeInBytes, const char* strSour
3981 return strcpy (strDestination, strSource);
4082}
4183
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+
4494IC char * xr_strcat (char * strDestination, size_t sizeInBytes, const char * strSource)
4595{
4696 return strncat (strDestination, strSource, sizeInBytes);
4797}
4898
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+
50104IC int xr_sprintf (char * dest, size_t sizeOfBuffer, const char * format, ...)
51105{
52106 va_list mark;
@@ -56,89 +110,133 @@ IC int xr_sprintf(char* dest, size_t sizeOfBuffer, const char* format, ...)
56110 va_end (mark);
57111 return sz;
58112}
59- #endif // _EDITOR
113+ #endif
60114
61- // generic
62- template <class T >
63- IC T _min (T a, T b)
115+ // token type definition
116+ struct XRCORE_API xr_token
64117{
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)
69123{
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 " " ;
71127}
72- template < class T >
73- IC T _sqr (T a )
128+
129+ IC int get_token_id (xr_token* tokens, LPCSTR key )
74130{
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 ;
76135}
77136
78- IC bool _valid ( const float x) noexcept
137+ struct XRCORE_API xr_token2
79138{
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+ };
85143
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);
93157}
94158
159+
95160// 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)
97166{
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);
111168}
112169
113- // XXX: "magic" specializations, that really require profiling to see if they are worth this effort.
114170// int8
115171IC s8 _abs (s8 x) { return (x >= 0 ) ? x : s8 (-x); }
116172IC s8 _min (s8 x, s8 y) { return y + ((x - y) & ((x - y) >> (sizeof (s8) * 8 - 1 ))); };
117173IC s8 _max (s8 x, s8 y) { return x - ((x - y) & ((x - y) >> (sizeof (s8) * 8 - 1 ))); };
174+
118175// unsigned int8
119176IC u8 _abs (u8 x) { return x; }
177+
120178// int16
121179IC s16 _abs (s16 x) { return (x >= 0 ) ? x : s16 (-x); }
122180IC s16 _min (s16 x, s16 y) { return y + ((x - y) & ((x - y) >> (sizeof (s16) * 8 - 1 ))); };
123181IC s16 _max (s16 x, s16 y) { return x - ((x - y) & ((x - y) >> (sizeof (s16) * 8 - 1 ))); };
182+
124183// unsigned int16
125184IC u16 _abs (u16 x) { return x; }
185+
126186// int32
127187IC s32 _abs (s32 x) { return (x >= 0 ) ? x : s32 (-x); }
128188IC s32 _min (s32 x, s32 y) { return y + ((x - y) & ((x - y) >> (sizeof (s32) * 8 - 1 ))); };
129189IC s32 _max (s32 x, s32 y) { return x - ((x - y) & ((x - y) >> (sizeof (s32) * 8 - 1 ))); };
190+
130191// int64
131192IC s64 _abs (s64 x) { return (x >= 0 ) ? x : s64 (-x); }
132193IC s64 _min (s64 x, s64 y) { return y + ((x - y) & ((x - y) >> (sizeof (s64) * 8 - 1 ))); };
133194IC s64 _max (s64 x, s64 y) { return x - ((x - y) & ((x - y) >> (sizeof (s64) * 8 - 1 ))); };
134195
196+ IC u32 xr_strlen (const char * S);
197+
135198// string management
136199
137200// 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+ }
140205
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
142240#ifndef MASTER_GOLD
143241
144242inline 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
159257}
160258
161259template <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, ...)
163261{
164262 va_list args;
165263 va_start (args, format_string);
@@ -169,7 +267,12 @@ inline int __cdecl xr_sprintf(char (&destination)[count], LPCSTR format_string,
169267
170268inline int xr_strcpy (LPSTR destination, size_t const destination_size, LPCSTR source)
171269{
270+ #ifdef _WIN32
172271 return strncpy_s (destination, destination_size, source, destination_size);
272+ #else
273+ strncpy (destination, source, destination_size);
274+ return 0 ;
275+ #endif
173276}
174277
175278inline 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
191294{
192295 va_list args;
193296 va_start (args, format_string);
297+ #ifdef _WIN32
194298 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
195302}
196303
197304template <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, ...)
199306{
200307 va_list args;
201308 va_start (args, format_string);
309+ #ifdef _WIN32
202310 return vsnprintf_s (destination, count, count - 1 , format_string, args);
311+ #else
312+ return vsnprintf (destination, count, format_string, args);
313+ #endif
203314}
204315#endif // #ifndef MASTER_GOLD
205316
206317template <int count>
207- inline int xr_strcpy (char (&destination)[count], LPCSTR source)
318+ inline int xr_strcpy (char (&destination)[count], LPCSTR source)
208319{
209320 return xr_strcpy (destination, count, source);
210321}
211322
212323template <int count>
213- inline int xr_strcat (char (&destination)[count], LPCSTR source)
324+ inline int xr_strcat (char (&destination)[count], LPCSTR source)
214325{
215326 return xr_strcat (destination, count, source);
216327}
217- // #endif // #ifndef _EDITOR
328+ #endif // #ifndef _EDITOR
218329
219- inline void MemFill32 (void * dst, u32 value, size_t dstSize)
330+ inline void MemFill32 (void * dst, u32 value, size_t dstSize)
220331{
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)
224335 *ptr++ = value;
225336}
226337
0 commit comments