OSDN Git Service

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