OSDN Git Service

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