OSDN Git Service

[VM][WIN32] Get rid of foo_s() (a.k.a. "SECURE FUNCTIONS"). These have no support...
[csp-qt/common_source_project-fm7.git] / source / src / common.h
1 /*
2         Skelton for retropc emulator
3
4         Author : Takeda.Toshiya
5         Date   : 2006.08.18 -
6
7         [ common header ]
8 */
9
10 #ifndef _COMMON_H_
11 #define _COMMON_H_
12
13 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
14 #define SUPPORT_TCHAR_TYPE
15 #endif
16 #if defined(_MSC_VER) && (_MSC_VER >= 1400)
17 #define SUPPORT_SECURE_FUNCTIONS
18 #endif
19 #ifdef SUPPORT_TCHAR_TYPE
20  #include <tchar.h>
21 #endif
22 #include <stdio.h>
23
24 #if defined(_MSC_VER)
25 #include <windows.h>
26 #include <windowsx.h>
27 #include <mmsystem.h>
28 #include <process.h>
29 #endif
30
31 #if defined(_USE_QT)
32 #include <SDL.h>
33 #include <stdarg.h>
34 #include "qt_input.h"
35 # if defined(Q_OS_WIN) || defined(Q_OS_CYGWIN)
36 #  include <tchar.h>
37 # endif
38 # if defined(_USE_QT5)
39 #  include <QString>
40 #  include <QFile>
41 #  include <QtEndian>
42 # else
43 #  include <QtCore/QString>
44 #  include <QtCore/QFile>
45 # endif
46 #endif
47
48
49
50 #if defined(_USE_AGAR) || defined(_USE_SDL) || defined(_USE_QT)
51
52 #  ifndef uint8
53    typedef uint8_t uint8;
54 #  endif
55 #  ifndef int8
56    typedef int8_t int8;
57 #  endif
58 #  ifndef uint16
59    typedef uint16_t uint16;
60 #  endif
61 #  ifndef int16
62    typedef int16_t int16;
63 #  endif
64 #  ifndef uint32
65    typedef uint32_t uint32;
66 #  endif
67 #  ifndef int32
68    typedef int32_t int32;
69 #  endif
70 #  ifndef uint64
71    typedef uint64_t uint64;
72 #  endif
73 #  ifndef int64
74    typedef int64_t int64;
75 #  endif
76 #  ifndef BOOL
77    typedef int BOOL;
78 #  endif
79 # if !defined(Q_OS_CYGWIN) && !defined(Q_OS_WIN)
80 #  ifndef BYTE
81    typedef uint8_t BYTE;
82 #  endif
83 #  ifndef WORD
84    typedef uint16_t WORD;
85 #  endif
86 #  ifndef DWORD
87    typedef uint32_t DWORD;
88 #  endif
89 #  ifndef QWORD
90    typedef uint64_t QWORD;
91 #  endif
92 # endif
93 # if !defined(Q_OS_CYGWIN)
94 #  ifndef UINT8
95    typedef uint8_t UINT8;
96 #  endif
97 #  ifndef UINT16
98    typedef uint16_t UINT16;
99 #  endif
100 #  ifndef UINT32
101    typedef uint32_t UINT32;
102 #  endif
103 #  ifndef UINT64
104    typedef uint64_t UINT64;
105 #  endif
106
107 #  ifndef INT8
108    typedef int8_t INT8;
109 #  endif
110 #  ifndef INT16
111    typedef int16_t INT16;
112 #  endif
113 #  ifndef INT32
114    typedef int32_t INT32;
115 #  endif
116 #  ifndef INT64
117    typedef int64_t INT64;
118 #  endif
119
120 # if !defined(Q_OS_CYGWIN) && !defined(Q_OS_WIN)
121 static inline void _stprintf(char *s, const char *fmt, ...) {
122    va_list args;
123    
124    va_start(args, fmt);
125    sprintf(s, fmt, args);
126    va_end(args);
127 }
128 # endif
129 # define stricmp(a,b) strcmp(a,b)
130 # define strnicmp(a,b,n) strncmp(a,b,n)
131
132
133 #    define __T(x)      x
134  
135 #  define _T(x)       __T(x)
136 #  define _TEXT(x)    __T(x)
137
138     typedef char    _TCHAR;
139
140 # if !defined(Q_OS_CYGWIN) && !defined(Q_OS_WIN)
141 #  ifndef LPCTSTR
142     typedef _TCHAR* LPCTSTR;
143 #  endif
144 static inline char *_tcsncpy(_TCHAR *d, _TCHAR *s, int n) {
145    return strncpy((char *)d, (char *)s, n);
146 }
147
148 static inline char *_tcsncat(_TCHAR *d, _TCHAR *s, int n) {
149    return strncat((char *)d, (char *)s, n);
150 }
151 # endif
152
153 # endif
154 #endif
155
156 #if defined(_USE_QT)
157 # if defined(Q_OS_CYGWIN)
158 # define stricmp(a,b) strcmp(a,b)
159 # define strnicmp(a,b,n) strncmp(a,b,n)
160 # endif
161 # if defined(Q_OS_WIN) || defined(Q_OS_CYGWIN)
162
163    typedef char    _TCHAR;
164 # endif
165 static int DeleteFile(_TCHAR *path) 
166 {
167 #ifdef _USE_QT
168        QString fpath = (char *)path;
169        QFile tfp(fpath);
170        if(tfp.remove(fpath)) return (int)true;
171        return 0;
172 #else   
173    return AG_FileDelete((const char *)path);
174 #endif
175 }
176 #include <algorithm>
177
178 # ifndef Q_OS_WIN
179 #  define _N(x) _T(x)
180 # endif
181 #endif
182
183 #if defined(_USE_QT)
184 # if defined(Q_OS_WIN) || defined(Q_OS_CYGWIN)
185 #  include <tchar.h>
186 # endif
187
188 #undef __LITTLE_ENDIAN___
189 #undef __BIG_ENDIAN___
190
191 # if (SDL_BYTEORDER == SDL_LIL_ENDIAN)
192 #  define __LITTLE_ENDIAN__
193 static inline uint32_t EndianToLittle_DWORD(uint32_t x)
194 {
195    return x;
196 }
197
198 static inline uint16_t EndianToLittle_WORD(uint16_t x)
199 {
200    return x;
201 }
202 # else // BIG_ENDIAN
203 #  define __BIG_ENDIAN__
204 static inline uint32_t EndianToLittle_DWORD(uint32_t x)
205 {
206    uint32_t y;
207    y = ((x & 0x000000ff) << 24) | ((x & 0x0000ff00) << 8) |
208        ((x & 0x00ff0000) >> 8)  | ((x & 0xff000000) >> 24);
209    return y;
210 }
211
212 static inline uint16_t EndianToLittle_WORD(uint16_t x)
213 {
214    uint16_t y;
215    y = ((x & 0x00ff) << 8) | ((x & 0xff00) >> 8);
216    return y;
217 }
218 #endif
219 #define ZeroMemory(p,s) memset(p,0x00,s)
220 #define CopyMemory(t,f,s) memcpy(t,f,s)
221
222
223 # if !defined(__LITTLE_ENDIAN__) && !defined(__BIG_ENDIAN__)
224 # if SDL_BYTEORDER == SDL_LIL_ENDIAN
225 #  define __LITTLE_ENDIAN__
226 # else
227 #  define __BIG_ENDIAN__
228 # endif
229 #endif
230
231
232 #else
233 #include <tchar.h>
234
235 // variable scope of 'for' loop for Microsoft Visual C++ 6.0
236
237 #if defined(_MSC_VER) && (_MSC_VER == 1200)
238 #define for if(0);else for
239 #endif
240
241 // disable warnings  for microsoft visual c++ 2005 or later
242 #if defined(_MSC_VER) && (_MSC_VER >= 1400)
243 #pragma warning( disable : 4819 )
244 //#pragma warning( disable : 4995 )
245 #pragma warning( disable : 4996 )
246 #endif
247
248 // endian
249 #if !defined(__LITTLE_ENDIAN__) && !defined(__BIG_ENDIAN__)
250         #if defined(__BYTE_ORDER) && (defined(__LITTLE_ENDIAN) || defined(__BIG_ENDIAN))
251                 #if __BYTE_ORDER == __LITTLE_ENDIAN
252                         #define __LITTLE_ENDIAN__
253                 #elif __BYTE_ORDER == __BIG_ENDIAN
254                         #define __BIG_ENDIAN__
255                 #endif
256         #elif defined(WORDS_LITTLEENDIAN)
257                 #define __LITTLE_ENDIAN__
258         #elif defined(WORDS_BIGENDIAN)
259                 #define __BIG_ENDIAN__
260         #endif
261 #endif
262 #if !defined(__LITTLE_ENDIAN__) && !defined(__BIG_ENDIAN__)
263         // Microsoft Visual C++
264         #define __LITTLE_ENDIAN__
265 #endif
266
267
268 // type definition
269 #ifndef uint8
270 typedef unsigned char uint8;
271 #endif
272 #ifndef uint16
273 typedef unsigned short uint16;
274 #endif
275 #ifndef uint32
276 typedef unsigned int uint32;
277 #endif
278 #ifndef uint64
279 #ifdef _MSC_VER
280 typedef unsigned __int64 uint64;
281 #else
282 typedef unsigned long long uint64;
283 #endif
284 #endif
285 #ifndef int8
286 typedef signed char int8;
287 #endif
288 #ifndef int16
289 typedef signed short int16;
290 #endif
291 #ifndef int32
292 typedef signed int int32;
293 #endif
294 #ifndef int64
295 #ifdef _MSC_VER
296 typedef signed __int64 int64;
297 #else
298 typedef signed long long int64;
299 #endif
300 #endif
301
302 static inline DWORD EndianToLittle_DWORD(DWORD x)
303 {
304    return x;
305 }
306
307 static inline WORD EndianToLittle_WORD(WORD x)
308 {
309    return x;
310 }
311
312
313 #endif
314
315 typedef union {
316         struct {
317 #ifdef __BIG_ENDIAN__
318                 uint8 h3, h2, h, l;
319 #else
320                 uint8 l, h, h2, h3;
321 #endif
322         } b;
323         struct {
324 #ifdef __BIG_ENDIAN__
325                 int8 h3, h2, h, l;
326 #else
327                 int8 l, h, h2, h3;
328 #endif
329         } sb;
330         struct {
331 #ifdef __BIG_ENDIAN__
332                 uint16 h, l;
333 #else
334                 uint16 l, h;
335 #endif
336         } w;
337         struct {
338 #ifdef __BIG_ENDIAN__
339                 int16 h, l;
340 #else
341                 int16 l, h;
342 #endif
343         } sw;
344         uint32 d;
345         int32 sd;
346         inline void read_2bytes_le_from(uint8 *t)
347         {
348                 b.l = t[0]; b.h = t[1]; b.h2 = b.h3 = 0;
349         }
350         inline void write_2bytes_le_to(uint8 *t)
351         {
352                 t[0] = b.l; t[1] = b.h;
353         }
354         inline void read_2bytes_be_from(uint8 *t)
355         {
356                 b.h3 = b.h2 = 0; b.h = t[0]; b.l = t[1];
357         }
358         inline void write_2bytes_be_to(uint8 *t)
359         {
360                 t[0] = b.h; t[1] = b.l;
361         }
362         inline void read_4bytes_le_from(uint8 *t)
363         {
364                 b.l = t[0]; b.h = t[1]; b.h2 = t[2]; b.h3 = t[3];
365         }
366         inline void write_4bytes_le_to(uint8 *t)
367         {
368                 t[0] = b.l; t[1] = b.h; t[2] = b.h2; t[3] = b.h3;
369         }
370         inline void read_4bytes_be_from(uint8 *t)
371         {
372                 b.h3 = t[0]; b.h2 = t[1]; b.h = t[2]; b.l = t[3];
373         }
374         inline void write_4bytes_be_to(uint8 *t)
375         {
376                 t[0] = b.h3; t[1] = b.h2; t[2] = b.h; t[3] = b.l;
377         }
378
379 } pair;
380
381 // max/min from WinDef.h
382 // rgb color
383 #if !defined(_USE_QT)
384 #define _RGB888
385 #else
386 #define _RGBA888
387 #endif
388
389 #if defined(__BIG_ENDIAN__)
390 # if defined(_RGB555)
391 #  define RGB_COLOR(r, g, b) ((uint16)(((uint16)(b) & 0xf8) >>4) | (uint16)(((uint16)(g) & 0xf8) << 2) | (uint16)(((uint16)(r) & 0xf8) << 8))
392 typedef uint16 scrntype;
393 # elif defined(_RGB565)
394 #  define RGB_COLOR(r, g, b) ((uint16)(((uint16)(b) & 0xf8) >>3) | (uint16)(((uint16)(g) & 0xfc) << 2) | (uint16)(((uint16)(r) & 0xf8) << 8))
395 typedef uint16 scrntype;
396 # elif defined(_RGB888) 
397 #  define RGB_COLOR(r, g, b) (((uint32)(r) << 24) | ((uint32)(g) << 16) | ((uint32)(b) << 8))
398 typedef uint32 scrntype;
399 # elif defined(_RGBA888)
400 //#   define RGB_COLOR(r, g, b) (((uint32)(r) << 24) | ((uint32)(g) << 16) | ((uint32)(b) << 8)) | ((uint32)0xff << 0)
401 #   define RGB_COLOR(r, g, b) (((uint32)(b) << 24) | ((uint32)(g) << 16) | ((uint32)(r) << 8)) | ((uint32)0xff << 0)
402 typedef uint32 scrntype;
403 # endif
404
405 #else // LITTLE ENDIAN
406
407 # if defined(_RGB555)
408 #  define RGB_COLOR(r, g, b) ((uint16)(((uint16)(b) & 0xf8) << 7) | (uint16)(((uint16)(g) & 0xf8) << 2) | (uint16)(((uint16)(r) & 0xf8) >> 3))
409 typedef uint16 scrntype;
410 # elif defined(_RGB565)
411 #  define RGB_COLOR(r, g, b) ((uint16)(((uint16)(b) & 0xf8) << 8) | (uint16)(((uint16)(g) & 0xfc) << 3) | (uint16)(((uint16)(r) & 0xf8) >> 3))
412 typedef uint16 scrntype;
413 # elif defined(_RGB888)
414 #  define RGB_COLOR(r, g, b) (((uint32)(r) << 16) | ((uint32)(g) << 8) | ((uint32)(b) << 0))
415 typedef uint32 scrntype;
416 # elif defined(_RGBA888)
417 //#   define RGB_COLOR(r, g, b) (((uint32)(r) << 16) | ((uint32)(g) << 8) | ((uint32)(b) << 0)) | ((uint32)0xff << 24)
418 #   define RGB_COLOR(r, g, b) (((uint32)(b) << 16) | ((uint32)(g) << 8) | ((uint32)(r) << 0)) | ((uint32)0xff << 24)
419 typedef uint32 scrntype;
420 # endif
421
422 #endif  // ENDIAN
423
424
425 // _TCHAR
426 #ifndef SUPPORT_TCHAR_TYPE
427 typedef char _TCHAR;
428 //#define _T(s) (s)
429 #define _tfopen fopen
430 #define _tcscmp strcmp
431 #define _tcscpy strcpy
432 # if !defined(_tcsicmp)
433 # define _tcsicmp stricmp
434 # endif
435 #define _tcslen strlen
436 #define _tcsncat strncat
437 #define _tcsncpy strncpy
438 # if !defined(_tcsncicmp)
439 #define _tcsncicmp strnicmp
440 # endif
441 #define _tcsstr strstr
442 #define _tcstok strtok
443 #define _tcstol strtol
444 #define _tcstoul strtoul
445 #define _stprintf sprintf
446 #define _vstprintf vsprintf
447 #endif
448
449 #if !defined(_MSC_VER)
450 #include <errno.h>
451 typedef int errno_t;
452 #endif
453 // secture functions
454 #if !defined(SUPPORT_SECURE_FUNCTIONS) || defined(Q_OS_WIN)
455 # ifndef errno_t
456 typedef int errno_t;
457 # endif
458 # if defined(Q_OS_WIN)
459 #  define strcpy_s _strcpy_s
460 #  define tcscpy_s _tcscpy_s
461 #  define tcstok_s _tcstok_s
462 #  define stprintf_s _stprintf_s
463 #  define vstprintf_s _vstprintf_s
464 #  define vsprintf_s _vsprintf_s
465 # endif
466 errno_t _strcpy_s(char *strDestination, size_t numberOfElements, const char *strSource);
467 # if !defined(Q_OS_WIN) //&& !defined(Q_OS_CYGWIN)
468 errno_t _tcscpy_s(_TCHAR *strDestination, size_t numberOfElements, const _TCHAR *strSource);
469 _TCHAR *_tcstok_s(_TCHAR *strToken, const char *strDelimit, _TCHAR **context);
470 # endif
471 int _stprintf_s(_TCHAR *buffer, size_t sizeOfBuffer, const _TCHAR *format, ...);
472 int _vstprintf_s(_TCHAR *buffer, size_t numberOfElements, const _TCHAR *format, va_list argptr);
473 #else
474 # define _strcpy_s strcpy_s
475 #endif
476
477 // wav file header
478 #pragma pack(1)
479 typedef struct {
480         char id[4];
481         uint32 size;
482 } wav_chunk_t;
483 #pragma pack()
484
485 #pragma pack(1)
486 typedef struct {
487         wav_chunk_t riff_chunk;
488         char wave[4];
489         wav_chunk_t fmt_chunk;
490         uint16 format_id;
491         uint16 channels;
492         uint32 sample_rate;
493         uint32 data_speed;
494         uint16 block_size;
495         uint16 sample_bits;
496 } wav_header_t;
497 #pragma pack()
498
499
500 // misc
501 #ifdef __cplusplus
502 bool check_file_extension(const _TCHAR* file_path, const _TCHAR* ext);
503 _TCHAR *get_file_path_without_extensiton(const _TCHAR* file_path);
504 uint32 getcrc32(uint8 data[], int size);
505
506
507 #define array_length(array) (sizeof(array) / sizeof(array[0]))
508
509 #define FROM_BCD(v)     (((v) & 0x0f) + (((v) >> 4) & 0x0f) * 10)
510 #define TO_BCD(v)       ((int)(((v) % 100) / 10) << 4) | ((v) % 10)
511 #define TO_BCD_LO(v)    ((v) % 10)
512 #define TO_BCD_HI(v)    (int)(((v) % 100) / 10)
513
514 #define LEAP_YEAR(y) (((y) % 4) == 0 && (((y) % 100) != 0 || ((y) % 400) == 0))
515
516 typedef struct cur_time_s {
517         int year, month, day, day_of_week, hour, minute, second;
518         bool initialized;
519         cur_time_s()
520         {
521                 initialized = false;
522         }
523         void increment();
524         void update_year();
525         void update_day_of_week();
526         void save_state(void *f);
527         bool load_state(void *f);
528 } cur_time_t;
529 #endif
530
531 //#endif
532
533 #endif