wasabi_std.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. #ifndef _STD_H
  2. #define _STD_H
  3. #include <string.h>
  4. #include <bfc/platform/platform.h>
  5. #include <bfc/common.h>
  6. #include <bfc/bfc_assert.h>
  7. //#include <bfc/string/string.h>
  8. //#define WANT_UTF8_WARNINGS
  9. // to use this, do #pragma WARNING("your message") <- note the ABSENCE of ';'
  10. #ifndef WARNING
  11. #define WARNING_TOSTR(text) #text
  12. #define WARNING_TOSTR1(text) WARNING_TOSTR(text)
  13. #define WARNING(text) message(__FILE__ "(" WARNING_TOSTR1(__LINE__) ") : " text)
  14. #endif
  15. // and for this one, do #pragma COMPILATION_CHAT("your_nick", "nick_you're_talking_to", "your message")
  16. #define CHAT(from, to, text) message(__FILE__ "(" WARNING_TOSTR1(__LINE__) ") : <" from "> " to ": " text)
  17. #define SELF(from, text) message(__FILE__ "(" WARNING_TOSTR1(__LINE__) ") : * " from"/#wasabi " text)
  18. //#ifndef WASABIDLLEXPORT
  19. //#error go define WASABIDLLEXPORT in your platform .h
  20. //#endif
  21. #ifndef NOVTABLE
  22. #define NOVTABLE
  23. #endif
  24. #ifndef NOVTABLE
  25. #error go define NOVTABLE in your platform .h
  26. #endif
  27. #include "std_mem.h"
  28. #include "std_math.h"
  29. #include "std_string.h"
  30. #include "std_file.h"
  31. #include "wasabi_std_rect.h"
  32. #include "std_keyboard.h"
  33. #include <stdlib.h>
  34. #include <locale.h>
  35. #define WASABI_DEFAULT_FONTNAME "Arial"
  36. #define WASABI_DEFAULT_FONTNAMEW L"Arial"
  37. extern const wchar_t wasabi_default_fontnameW[];
  38. static _locale_t C_locale;
  39. #include <wchar.h>
  40. static __inline char TOUPPER(char c)
  41. {
  42. #ifdef _WIN32
  43. char tmp = c;
  44. CharUpperBuffA(&tmp, 1);
  45. return tmp;
  46. #else
  47. return toupper(c);
  48. #endif
  49. }
  50. static __inline wchar_t TOUPPERW(wchar_t c)
  51. {
  52. return towupper(c);
  53. }
  54. static __inline wchar_t TOLOWERW(wchar_t c)
  55. {
  56. return towlower(c);
  57. }
  58. __inline bool WTOB(const wchar_t *str) {
  59. #ifdef WIN64
  60. if (!str || (unsigned long long)str < WCHAR_MAX) return 0;
  61. #else
  62. if (!str || (unsigned long)str < WCHAR_MAX) return 0;
  63. #endif
  64. if(!C_locale) C_locale = _create_locale(LC_NUMERIC,"C");
  65. return !!_wtoi_l(str,C_locale);
  66. }
  67. __inline int WTOI(const wchar_t *str) {
  68. #ifdef WIN64
  69. if (!str || (unsigned long long)str < WCHAR_MAX) return 0;
  70. #else
  71. if (!str || (unsigned long)str < WCHAR_MAX) return 0;
  72. #endif
  73. if(!C_locale) C_locale = _create_locale(LC_NUMERIC,"C");
  74. return _wtoi_l(str,C_locale);
  75. }
  76. //__inline int ATOI(const char *str) { if (!str) return 0; return atoi(str); }
  77. //__inline double ATOF(const char *str) { if (!str) return 0.0; return atof(str); }
  78. __inline double WTOF(const wchar_t *str) {
  79. #ifdef WIN64
  80. if (!str || (unsigned long long)str < WCHAR_MAX) return 0.0;
  81. #else
  82. if (!str || (unsigned long)str < WCHAR_MAX) return 0.0;
  83. #endif
  84. if(!C_locale) C_locale = _create_locale(LC_NUMERIC,"C");
  85. return _wtof_l(str,C_locale);
  86. }
  87. __inline int STRTOL(const char *str, char **stopstr, int base)
  88. {
  89. if(!C_locale) C_locale = _create_locale(LC_NUMERIC,"C");
  90. return _strtol_l(str, stopstr, base, C_locale);
  91. }
  92. wchar_t *WCSDUP(const wchar_t *ptr);
  93. COMEXP int STRLEN(const char *str);
  94. COMEXP int STRCMP(const char *str1, const char *str2);
  95. COMEXP int STRICMP(const char *str1, const char *str2);
  96. int WCSICMP(const wchar_t *str1, const wchar_t *str2);
  97. int WCSNICMP(const wchar_t *str1, const wchar_t *str2, size_t len);
  98. int WCSCOLL(const wchar_t *str1, const wchar_t *str2);
  99. int WCSICOLL(const wchar_t *str1, const wchar_t *str2);
  100. bool WCSIPREFIX(const wchar_t *str1, const wchar_t *prefix);
  101. wchar_t *WCSTOK(wchar_t *str, const wchar_t *sep, wchar_t **last);
  102. COMEXP int STREQL(const char *str1, const char *str2);
  103. COMEXP int STRCASEEQL(const char *str1, const char *str2);
  104. wchar_t *WCSCASESTR(const wchar_t *str1, const wchar_t *str2);
  105. COMEXP char *STRSTR(const char *str1, const char *str2);
  106. COMEXP void STRCPY(char *dest, const char *src);
  107. COMEXP void STRNCPY(char *dest, const char *src, int maxchar);
  108. COMEXP void WCSCPYN(wchar_t *dest, const wchar_t *src, size_t maxchar);
  109. COMEXP char *STRCHR(const char *str, int c);
  110. COMEXP void STRCAT(char *dest, const char *append);
  111. COMEXP unsigned long STRTOUL(const char *str, char **p, int radix);
  112. #ifdef __cplusplus
  113. COMEXP int STRCMPSAFE(const char *str1, const char *str2, const char *defval1 = "", const char *defval2 = "");
  114. COMEXP int STRICMPSAFE(const char *str1, const char *str2, const char *defval1 = "", const char *defval2 = "");
  115. COMEXP int WCSICMPSAFE(const wchar_t *str1, const wchar_t *str2, const wchar_t *defval1 = L"", const wchar_t *defval2 = L"");
  116. COMEXP int WCSEQLSAFE(const wchar_t *str1, const wchar_t *str2, const wchar_t *defval1 = L"", const wchar_t *defval2 = L"");
  117. COMEXP int STRCASEEQLSAFE(const char *str1, const char *str2, const char *defval1 = "", const char *defval2 = "");
  118. COMEXP int WCSCASEEQLSAFE(const wchar_t *str1, const wchar_t *str2, const wchar_t *defval1 = L"", const wchar_t *defval2 = L"");
  119. #endif
  120. COMEXP int PATHEQL(const wchar_t *str1, const wchar_t *str2);
  121. COMEXP void STRTOUPPER(char *str);
  122. COMEXP void STRTOLOWER(char *str);
  123. COMEXP void WCSTOUPPER(wchar_t *str);
  124. COMEXP void KEYWORDUPPER(wchar_t *p);
  125. COMEXP void WCSTOLOWER(wchar_t *str);
  126. #ifdef _WIN32
  127. #define STRSAFE_NO_DEPRECATE
  128. //#include <tchar.h>
  129. #include <strsafe.h>
  130. #undef STRSAFE_NO_DEPRECATE
  131. #define WCSNPRINTF StringCchPrintfW
  132. #elif defined(__APPLE__)
  133. #define WCSNPRINTF swprintf
  134. #endif
  135. #ifdef WIN32
  136. #define SPRINTF wsprintfA
  137. #else
  138. #define SPRINTF sprintf
  139. #endif
  140. #define SSCANF sscanf
  141. #define WA_MAX_PATH (8*1024)
  142. // seconds since 1970
  143. typedef unsigned int stdtimeval;
  144. // milliseconds since...??
  145. typedef double stdtimevalms;
  146. #ifdef _WIN32
  147. typedef unsigned long THREADID;
  148. #else
  149. typedef pthread_t THREADID;
  150. #endif
  151. #ifdef __cplusplus
  152. #include <bfc/string/bfcstring.h>
  153. namespace Wasabi
  154. {
  155. namespace Std
  156. {
  157. void Initialize();
  158. void getMousePos(POINT* p);
  159. void getMousePos(int* x, int* y);
  160. void getMousePos(long* x, long* y);
  161. void setMousePos(POINT* p);
  162. void setMousePos(int x, int y);
  163. void srandom(unsigned int key = 0); //if key==0, uses time()
  164. int random(int max = RAND_MAX + 1); // from 0 to max-1 (RAND_MAX = 0x7fff)
  165. unsigned int random32(unsigned int max = 0xffffffff); // full 32-bits of randomness
  166. // time functions
  167. void usleep(int ms);
  168. // get time in seconds since 1970
  169. time_t getTimeStamp();
  170. // get time in seconds as double, but not since any specific time
  171. // useful for relative timestamps only
  172. stdtimevalms getTimeStampMS();
  173. // get milliseconds since system started. usefull for relative only
  174. uint32_t getTickCount();
  175. void tolowerString(char* str);
  176. void ensureVisible(RECT* r);
  177. int getScreenWidth();
  178. int getScreenHeight();
  179. // THREADS/PROCESSES/CPUs
  180. int getNumCPUs();
  181. // a unique # returned by the OS
  182. THREADID getCurrentThreadId();
  183. // attempts to adjust thread priority compared to main thread
  184. // normal range is from -2 .. +2, and -32767 for idle, 32767 for time critical
  185. // use the constants from bfc/thread.h
  186. #ifdef _WIN32
  187. void setThreadPriority(int delta, HANDLE thread_handle = NULL);
  188. #endif
  189. String getLastErrorString(int force_errno = -1);
  190. int messageBox(const wchar_t* txt, const wchar_t* title, int flags);
  191. int getDoubleClickDelay();
  192. int getDoubleClickX();
  193. int getDoubleClickY();
  194. // returns how many lines to scroll for wheelie mice (from the OS)
  195. int osparam_getScrollLines();
  196. int osparam_getSmoothScroll();
  197. const wchar_t dirChar(); // \ for win32, / for all else
  198. #define DIRCHAR (Wasabi::Std::dirChar())
  199. const char* dirCharStr(); // "\\" for win32, "/" for all else
  200. #define DIRCHARSTR (Wasabi::Std::dirCharStr())
  201. const wchar_t* dirCharStrW(); // "\\" for win32, "/" for all else
  202. #define DIRCHARSTRW (Wasabi::Std::dirCharStrW())
  203. int isDirChar(int thechar, int allow_multiple_platforms = TRUE);
  204. const wchar_t* matchAllFiles(); // "*.*" on win32, "*" on else
  205. #define MATCHALLFILES (Wasabi::Std::matchAllFiles())
  206. const wchar_t* dotDir(); // usually "."
  207. #define DOTDIR (Wasabi::Std::dotDir())
  208. const wchar_t* dotDotDir(); // usually ".."
  209. #define DOTDOTDIR (Wasabi::Std::dotDotDir())
  210. bool isRootPath(const wchar_t* path); // "c:\" or "\" on win32, "/" on linux
  211. int switchChar(); // '/' on win32, '-' for all else
  212. #define SWITCHCHAR (Wasabi::Std::switchChar())
  213. void getViewport(RECT* r, POINT* p, int full = 0);
  214. void getViewport(RECT* r, RECT* sr, int full = 0);
  215. void getViewport(RECT* r, OSWINDOWHANDLE wnd, int full = 0);
  216. void getViewport(RECT* r, POINT* p, RECT* sr, OSWINDOWHANDLE wnd, int full = 0);
  217. int enumViewports(int monitor_n, RECT* r, int full = 0);
  218. // returns the address of the last occurence of any of the characters of toscan in str string
  219. const char* scanstr_back(const char* str, const char* toscan, const char* defval);
  220. // retrieves extension of a given filename
  221. const wchar_t* extension(const wchar_t* fn);
  222. // retrieves filename from a given path+filename
  223. const wchar_t* filename(const wchar_t* fn);
  224. int getCurDir(wchar_t* str, int maxlen);
  225. int setCurDir(const wchar_t* str);
  226. // regexp match functions
  227. // A match means the entire string TEXT is used up in matching.
  228. // In the pattern string:
  229. // `*' matches any sequence of characters (zero or more)
  230. // `?' matches any character
  231. // [SET] matches any character in the specified set,
  232. // [!SET] or [^SET] matches any character not in the specified set.
  233. // A set is composed of characters or ranges; a range looks like
  234. // character hyphen character (as in 0-9 or A-Z). [0-9a-zA-Z_] is the
  235. // minimal set of characters allowed in the [..] pattern construct.
  236. // Other characters are allowed (ie. 8 bit characters) if your system
  237. // will support them.
  238. // To suppress the special syntactic significance of any of `[]*?!^-\',
  239. // and match the character exactly, precede it with a `\'.
  240. enum {
  241. MATCH_VALID = 1, /* valid match */
  242. MATCH_END, /* premature end of pattern string */
  243. MATCH_ABORT, /* premature end of text string */
  244. MATCH_RANGE, /* match failure on [..] construct */
  245. MATCH_LITERAL, /* match failure on literal match */
  246. MATCH_PATTERN, /* bad pattern */
  247. };
  248. enum {
  249. PATTERN_VALID = 0, /* valid pattern */
  250. PATTERN_ESC = -1, /* literal escape at end of pattern */
  251. PATTERN_RANGE = -2, /* malformed range in [..] construct */
  252. PATTERN_CLOSE = -3, /* no end bracket in [..] construct */
  253. PATTERN_EMPTY = -4, /* [..] contstruct is empty */
  254. };
  255. // return TRUE if PATTERN has any special wildcard characters
  256. bool isMatchPattern(const wchar_t* p);
  257. // return TRUE if PATTERN has is a well formed regular expression
  258. bool isValidMatchPattern(const wchar_t* p, int* error_type);
  259. // return MATCH_VALID if pattern matches, or an errorcode otherwise
  260. int matche(register const wchar_t* p, register const wchar_t* t);
  261. int matche_after_star(register const wchar_t* p, register const wchar_t* t);
  262. // return TRUE if pattern matches, FALSE otherwise.
  263. bool match(const wchar_t* p, const wchar_t* t);
  264. // gets the system font path.
  265. void getFontPath(int bufsize, wchar_t* dst);
  266. // gets the filename of a font file guaranteed to be in the system font path.
  267. void getDefaultFont(int bufsize, wchar_t* dst);
  268. // sets a new default font, ie "blah.ttf"
  269. void setDefaultFont(const wchar_t* newdefaultfont);
  270. // gets the default font scale
  271. int getDefaultFontScale();
  272. // sets the new default font scale
  273. void setDefaultFontScale(int scale);
  274. // creates a directory. returns 0 on error, nonzero on success
  275. int createDirectory(const wchar_t* dirname);
  276. // gets informations about a given filename. returns 0 if file not found
  277. typedef struct
  278. {
  279. unsigned int fileSizeHigh;
  280. unsigned int fileSizeLow;
  281. time_t lastWriteTime;
  282. int readonly;
  283. }
  284. fileInfoStruct;
  285. int getFileInfos(const char* filename, fileInfoStruct* infos);
  286. // removes a directory
  287. void removeDirectory(const char* buf, int recurse);
  288. void shellExec(const wchar_t* cmd, const wchar_t* params = NULL);
  289. int isLittleEndian();
  290. }
  291. }
  292. #endif
  293. #endif