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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
|
#ifndef _STD_H
#define _STD_H
#include <string.h>
#include <bfc/platform/platform.h>
#include <bfc/common.h>
#include <bfc/bfc_assert.h>
//#include <bfc/string/string.h>
//#define WANT_UTF8_WARNINGS
// to use this, do #pragma WARNING("your message") <- note the ABSENCE of ';'
#ifndef WARNING
#define WARNING_TOSTR(text) #text
#define WARNING_TOSTR1(text) WARNING_TOSTR(text)
#define WARNING(text) message(__FILE__ "(" WARNING_TOSTR1(__LINE__) ") : " text)
#endif
// and for this one, do #pragma COMPILATION_CHAT("your_nick", "nick_you're_talking_to", "your message")
#define CHAT(from, to, text) message(__FILE__ "(" WARNING_TOSTR1(__LINE__) ") : <" from "> " to ": " text)
#define SELF(from, text) message(__FILE__ "(" WARNING_TOSTR1(__LINE__) ") : * " from"/#wasabi " text)
//#ifndef WASABIDLLEXPORT
//#error go define WASABIDLLEXPORT in your platform .h
//#endif
#ifndef NOVTABLE
#define NOVTABLE
#endif
#ifndef NOVTABLE
#error go define NOVTABLE in your platform .h
#endif
#include "std_mem.h"
#include "std_math.h"
#include "std_string.h"
#include "std_file.h"
#include "wasabi_std_rect.h"
#include "std_keyboard.h"
#include <stdlib.h>
#include <locale.h>
#define WASABI_DEFAULT_FONTNAME "Arial"
#define WASABI_DEFAULT_FONTNAMEW L"Arial"
extern const wchar_t wasabi_default_fontnameW[];
static _locale_t C_locale;
#include <wchar.h>
static __inline char TOUPPER(char c)
{
#ifdef _WIN32
char tmp = c;
CharUpperBuffA(&tmp, 1);
return tmp;
#else
return toupper(c);
#endif
}
static __inline wchar_t TOUPPERW(wchar_t c)
{
return towupper(c);
}
static __inline wchar_t TOLOWERW(wchar_t c)
{
return towlower(c);
}
__inline bool WTOB(const wchar_t *str) {
#ifdef WIN64
if (!str || (unsigned long long)str < WCHAR_MAX) return 0;
#else
if (!str || (unsigned long)str < WCHAR_MAX) return 0;
#endif
if(!C_locale) C_locale = _create_locale(LC_NUMERIC,"C");
return !!_wtoi_l(str,C_locale);
}
__inline int WTOI(const wchar_t *str) {
#ifdef WIN64
if (!str || (unsigned long long)str < WCHAR_MAX) return 0;
#else
if (!str || (unsigned long)str < WCHAR_MAX) return 0;
#endif
if(!C_locale) C_locale = _create_locale(LC_NUMERIC,"C");
return _wtoi_l(str,C_locale);
}
//__inline int ATOI(const char *str) { if (!str) return 0; return atoi(str); }
//__inline double ATOF(const char *str) { if (!str) return 0.0; return atof(str); }
__inline double WTOF(const wchar_t *str) {
#ifdef WIN64
if (!str || (unsigned long long)str < WCHAR_MAX) return 0.0;
#else
if (!str || (unsigned long)str < WCHAR_MAX) return 0.0;
#endif
if(!C_locale) C_locale = _create_locale(LC_NUMERIC,"C");
return _wtof_l(str,C_locale);
}
__inline int STRTOL(const char *str, char **stopstr, int base)
{
if(!C_locale) C_locale = _create_locale(LC_NUMERIC,"C");
return _strtol_l(str, stopstr, base, C_locale);
}
wchar_t *WCSDUP(const wchar_t *ptr);
COMEXP int STRLEN(const char *str);
COMEXP int STRCMP(const char *str1, const char *str2);
COMEXP int STRICMP(const char *str1, const char *str2);
int WCSICMP(const wchar_t *str1, const wchar_t *str2);
int WCSNICMP(const wchar_t *str1, const wchar_t *str2, size_t len);
int WCSCOLL(const wchar_t *str1, const wchar_t *str2);
int WCSICOLL(const wchar_t *str1, const wchar_t *str2);
bool WCSIPREFIX(const wchar_t *str1, const wchar_t *prefix);
wchar_t *WCSTOK(wchar_t *str, const wchar_t *sep, wchar_t **last);
COMEXP int STREQL(const char *str1, const char *str2);
COMEXP int STRCASEEQL(const char *str1, const char *str2);
wchar_t *WCSCASESTR(const wchar_t *str1, const wchar_t *str2);
COMEXP char *STRSTR(const char *str1, const char *str2);
COMEXP void STRCPY(char *dest, const char *src);
COMEXP void STRNCPY(char *dest, const char *src, int maxchar);
COMEXP void WCSCPYN(wchar_t *dest, const wchar_t *src, size_t maxchar);
COMEXP char *STRCHR(const char *str, int c);
COMEXP void STRCAT(char *dest, const char *append);
COMEXP unsigned long STRTOUL(const char *str, char **p, int radix);
#ifdef __cplusplus
COMEXP int STRCMPSAFE(const char *str1, const char *str2, const char *defval1 = "", const char *defval2 = "");
COMEXP int STRICMPSAFE(const char *str1, const char *str2, const char *defval1 = "", const char *defval2 = "");
COMEXP int WCSICMPSAFE(const wchar_t *str1, const wchar_t *str2, const wchar_t *defval1 = L"", const wchar_t *defval2 = L"");
COMEXP int WCSEQLSAFE(const wchar_t *str1, const wchar_t *str2, const wchar_t *defval1 = L"", const wchar_t *defval2 = L"");
COMEXP int STRCASEEQLSAFE(const char *str1, const char *str2, const char *defval1 = "", const char *defval2 = "");
COMEXP int WCSCASEEQLSAFE(const wchar_t *str1, const wchar_t *str2, const wchar_t *defval1 = L"", const wchar_t *defval2 = L"");
#endif
COMEXP int PATHEQL(const wchar_t *str1, const wchar_t *str2);
COMEXP void STRTOUPPER(char *str);
COMEXP void STRTOLOWER(char *str);
COMEXP void WCSTOUPPER(wchar_t *str);
COMEXP void KEYWORDUPPER(wchar_t *p);
COMEXP void WCSTOLOWER(wchar_t *str);
#ifdef _WIN32
#define STRSAFE_NO_DEPRECATE
//#include <tchar.h>
#include <strsafe.h>
#undef STRSAFE_NO_DEPRECATE
#define WCSNPRINTF StringCchPrintfW
#elif defined(__APPLE__)
#define WCSNPRINTF swprintf
#endif
#ifdef WIN32
#define SPRINTF wsprintfA
#else
#define SPRINTF sprintf
#endif
#define SSCANF sscanf
#define WA_MAX_PATH (8*1024)
// seconds since 1970
typedef unsigned int stdtimeval;
// milliseconds since...??
typedef double stdtimevalms;
#ifdef _WIN32
typedef unsigned long THREADID;
#else
typedef pthread_t THREADID;
#endif
#ifdef __cplusplus
#include <bfc/string/bfcstring.h>
namespace Wasabi
{
namespace Std
{
void Initialize();
void getMousePos(POINT* p);
void getMousePos(int* x, int* y);
void getMousePos(long* x, long* y);
void setMousePos(POINT* p);
void setMousePos(int x, int y);
void srandom(unsigned int key = 0); //if key==0, uses time()
int random(int max = RAND_MAX + 1); // from 0 to max-1 (RAND_MAX = 0x7fff)
unsigned int random32(unsigned int max = 0xffffffff); // full 32-bits of randomness
// time functions
void usleep(int ms);
// get time in seconds since 1970
time_t getTimeStamp();
// get time in seconds as double, but not since any specific time
// useful for relative timestamps only
stdtimevalms getTimeStampMS();
// get milliseconds since system started. usefull for relative only
uint32_t getTickCount();
void tolowerString(char* str);
void ensureVisible(RECT* r);
int getScreenWidth();
int getScreenHeight();
// THREADS/PROCESSES/CPUs
int getNumCPUs();
// a unique # returned by the OS
THREADID getCurrentThreadId();
// attempts to adjust thread priority compared to main thread
// normal range is from -2 .. +2, and -32767 for idle, 32767 for time critical
// use the constants from bfc/thread.h
#ifdef _WIN32
void setThreadPriority(int delta, HANDLE thread_handle = NULL);
#endif
String getLastErrorString(int force_errno = -1);
int messageBox(const wchar_t* txt, const wchar_t* title, int flags);
int getDoubleClickDelay();
int getDoubleClickX();
int getDoubleClickY();
// returns how many lines to scroll for wheelie mice (from the OS)
int osparam_getScrollLines();
int osparam_getSmoothScroll();
const wchar_t dirChar(); // \ for win32, / for all else
#define DIRCHAR (Wasabi::Std::dirChar())
const char* dirCharStr(); // "\\" for win32, "/" for all else
#define DIRCHARSTR (Wasabi::Std::dirCharStr())
const wchar_t* dirCharStrW(); // "\\" for win32, "/" for all else
#define DIRCHARSTRW (Wasabi::Std::dirCharStrW())
int isDirChar(int thechar, int allow_multiple_platforms = TRUE);
const wchar_t* matchAllFiles(); // "*.*" on win32, "*" on else
#define MATCHALLFILES (Wasabi::Std::matchAllFiles())
const wchar_t* dotDir(); // usually "."
#define DOTDIR (Wasabi::Std::dotDir())
const wchar_t* dotDotDir(); // usually ".."
#define DOTDOTDIR (Wasabi::Std::dotDotDir())
bool isRootPath(const wchar_t* path); // "c:\" or "\" on win32, "/" on linux
int switchChar(); // '/' on win32, '-' for all else
#define SWITCHCHAR (Wasabi::Std::switchChar())
void getViewport(RECT* r, POINT* p, int full = 0);
void getViewport(RECT* r, RECT* sr, int full = 0);
void getViewport(RECT* r, OSWINDOWHANDLE wnd, int full = 0);
void getViewport(RECT* r, POINT* p, RECT* sr, OSWINDOWHANDLE wnd, int full = 0);
int enumViewports(int monitor_n, RECT* r, int full = 0);
// returns the address of the last occurence of any of the characters of toscan in str string
const char* scanstr_back(const char* str, const char* toscan, const char* defval);
// retrieves extension of a given filename
const wchar_t* extension(const wchar_t* fn);
// retrieves filename from a given path+filename
const wchar_t* filename(const wchar_t* fn);
int getCurDir(wchar_t* str, int maxlen);
int setCurDir(const wchar_t* str);
// regexp match functions
// A match means the entire string TEXT is used up in matching.
// In the pattern string:
// `*' matches any sequence of characters (zero or more)
// `?' matches any character
// [SET] matches any character in the specified set,
// [!SET] or [^SET] matches any character not in the specified set.
// A set is composed of characters or ranges; a range looks like
// character hyphen character (as in 0-9 or A-Z). [0-9a-zA-Z_] is the
// minimal set of characters allowed in the [..] pattern construct.
// Other characters are allowed (ie. 8 bit characters) if your system
// will support them.
// To suppress the special syntactic significance of any of `[]*?!^-\',
// and match the character exactly, precede it with a `\'.
enum {
MATCH_VALID = 1, /* valid match */
MATCH_END, /* premature end of pattern string */
MATCH_ABORT, /* premature end of text string */
MATCH_RANGE, /* match failure on [..] construct */
MATCH_LITERAL, /* match failure on literal match */
MATCH_PATTERN, /* bad pattern */
};
enum {
PATTERN_VALID = 0, /* valid pattern */
PATTERN_ESC = -1, /* literal escape at end of pattern */
PATTERN_RANGE = -2, /* malformed range in [..] construct */
PATTERN_CLOSE = -3, /* no end bracket in [..] construct */
PATTERN_EMPTY = -4, /* [..] contstruct is empty */
};
// return TRUE if PATTERN has any special wildcard characters
bool isMatchPattern(const wchar_t* p);
// return TRUE if PATTERN has is a well formed regular expression
bool isValidMatchPattern(const wchar_t* p, int* error_type);
// return MATCH_VALID if pattern matches, or an errorcode otherwise
int matche(register const wchar_t* p, register const wchar_t* t);
int matche_after_star(register const wchar_t* p, register const wchar_t* t);
// return TRUE if pattern matches, FALSE otherwise.
bool match(const wchar_t* p, const wchar_t* t);
// gets the system font path.
void getFontPath(int bufsize, wchar_t* dst);
// gets the filename of a font file guaranteed to be in the system font path.
void getDefaultFont(int bufsize, wchar_t* dst);
// sets a new default font, ie "blah.ttf"
void setDefaultFont(const wchar_t* newdefaultfont);
// gets the default font scale
int getDefaultFontScale();
// sets the new default font scale
void setDefaultFontScale(int scale);
// creates a directory. returns 0 on error, nonzero on success
int createDirectory(const wchar_t* dirname);
// gets informations about a given filename. returns 0 if file not found
typedef struct
{
unsigned int fileSizeHigh;
unsigned int fileSizeLow;
time_t lastWriteTime;
int readonly;
}
fileInfoStruct;
int getFileInfos(const char* filename, fileInfoStruct* infos);
// removes a directory
void removeDirectory(const char* buf, int recurse);
void shellExec(const wchar_t* cmd, const wchar_t* params = NULL);
int isLittleEndian();
}
}
#endif
#endif
|