OSDN Git Service

Merge branch 'master' of github.com:Artanejp/common_source_project-fm7
[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 // check environemnt/language
14 #ifdef _WIN32
15         #ifdef _MSC_VER
16                 // Microsoft Visual C++
17                 #if _MSC_VER == 1200
18                         // variable scope of 'for' loop for Microsoft Visual C++ 6.0
19                         #define for if(0);else for
20                 #endif
21                 #if _MSC_VER >= 1200
22                         // Microsoft Visual C++ 6.0 or later
23                         #define SUPPORT_TCHAR_TYPE
24                 #endif
25                 #if _MSC_VER >= 1400
26                         // Microsoft Visual C++ 8.0 (2005) or later
27                         #define SUPPORT_SECURE_FUNCTIONS
28                         #pragma warning( disable : 4819 )
29                         //#pragma warning( disable : 4995 )
30                         #pragma warning( disable : 4996 )
31                 #endif
32                 #if _MSC_VER >= 1800
33                         // Microsoft Visual C++ 12.0 (2013) or later
34                         #define SUPPORT_CPLUSPLUS_11
35                 #endif
36         #else
37                 // Win32, but not Microsoft Visual C++
38                 #define SUPPORT_TCHAR_TYPE
39 //              #define SUPPORT_SECURE_FUNCTIONS
40         #endif
41 #endif
42 #ifdef __GNUC__
43         #if defined(Q_OS_CYGWIN) 
44                 #define CSP_OS_GCC_CYGWIN
45                 #define CSP_OS_WINDOWS
46         #elif defined(Q_OS_WIN) || defined(__WIN32) || defined(__WIN64)
47                 #define CSP_OS_GCC_WINDOWS
48                 #define CSP_OS_WINDOWS
49                 #define DLL_PREFIX   __declspec(dllexport)
50                 #define DLL_PREFIX_I __declspec(dllimport)
51         #else
52                 #define CSP_OS_GCC_GENERIC
53                 #define CSP_OS_GENERIC
54                 #define DLL_PREFIX
55                 #define DLL_PREFIX_I
56         #endif
57         #if defined(__clang__)
58                 #define __CSP_COMPILER_CLANG
59         #else
60                 #define __CSP_COMPILER_GCC
61         #endif
62         #define SUPPORT_CPLUSPLUS_11
63 #else
64                 #define DLL_PREFIX
65                 #define DLL_PREFIX_I
66 #endif
67
68 #ifndef SUPPORT_CPLUSPLUS_11
69         #if defined(__cplusplus) && (__cplusplus > 199711L)
70                 #define SUPPORT_CPLUSPLUS_11
71         #endif
72 #endif
73 #ifndef SUPPORT_TCHAR_TYPE
74         // secure functions need tchar type
75         #undef SUPPORT_SECURE_FUNCTIONS
76 #endif
77
78 // include common header files
79 #ifdef SUPPORT_TCHAR_TYPE
80         #include <tchar.h>
81 #endif
82 #ifdef SUPPORT_CPLUSPLUS_11
83         #include <stdint.h>
84 #endif
85 #include <stdio.h>
86 #include <stdlib.h>
87 #include <string.h>
88 #include <assert.h>
89 #include <errno.h>
90
91 // include environment/language dependent header files
92 #ifdef _WIN32
93         #include <windows.h>
94         #include <windowsx.h>
95         #include <winuser.h>
96         #include <mmsystem.h>
97         #include <process.h>
98 #endif
99 #ifdef __GNUC__
100         #include <stdarg.h>
101 #endif
102 #ifdef _USE_QT
103         #ifdef _USE_QT5
104                 #include <QString>
105                 #include <QFile>
106                 #include <QtEndian>
107                 #if defined(__MINGW32__) || (__MINGW64__)
108                         #include <windows.h>
109                         #include <winbase.h>
110                 #endif
111         #else
112                 #include <QtCore/QString>
113                 #include <QtCore/QFile>
114         #endif
115         #include <sys/param.h>
116 #endif
117 #ifndef _MAX_PATH
118         #define _MAX_PATH 2048
119 #endif
120
121 // endian
122 #if !defined(__LITTLE_ENDIAN__) && !defined(__BIG_ENDIAN__)
123         #if defined(__BYTE_ORDER) && (defined(__LITTLE_ENDIAN) || defined(__BIG_ENDIAN))
124                 #if __BYTE_ORDER == __LITTLE_ENDIAN
125                         #define __LITTLE_ENDIAN__
126                 #elif __BYTE_ORDER == __BIG_ENDIAN
127                         #define __BIG_ENDIAN__
128                 #endif
129         #elif defined(SDL_BYTEORDER) && (defined(SDL_LIL_ENDIAN) || defined(SDL_BIG_ENDIAN))
130                 #if SDL_BYTEORDER == SDL_LIL_ENDIAN
131                         #define __LITTLE_ENDIAN__
132                 #elif SDL_BYTEORDER == SDL_BIG_ENDIAN
133                         #define __BIG_ENDIAN__
134                 #endif
135         #elif defined(WORDS_LITTLEENDIAN)
136                 #define __LITTLE_ENDIAN__
137         #elif defined(WORDS_BIGENDIAN)
138                 #define __BIG_ENDIAN__
139         #endif
140 #endif
141 #if !defined(__LITTLE_ENDIAN__) && !defined(__BIG_ENDIAN__)
142         // may be Microsoft Visual C++
143         #define __LITTLE_ENDIAN__
144 #endif
145
146 // type definition
147 #ifndef SUPPORT_TCHAR_TYPE
148         #ifndef _TCHAR
149                 typedef char _TCHAR;
150         #endif
151 #endif
152
153 #ifndef SUPPORT_CPLUSPLUS_11
154         #ifndef int8_t
155                 typedef signed char int8_t;
156         #endif
157         #ifndef int16_t
158                 typedef signed short int16_t;
159         #endif
160         #ifndef int32_t
161                 typedef signed int int32_t;
162         #endif
163         #ifndef int64_t
164                 typedef signed long long int64_t;
165         #endif
166         #ifndef uint8_t
167                 typedef unsigned char uint8_t;
168         #endif
169         #ifndef uint16_t
170                 typedef unsigned short uint16_t;
171         #endif
172         #ifndef uint32_t
173                 typedef unsigned int uint32_t;
174         #endif
175         #ifndef uint64_t
176                 typedef unsigned long long uint64_t;
177         #endif
178 #endif
179
180 #ifndef _WIN32
181         #ifndef LPTSTR
182                 typedef _TCHAR* LPTSTR;
183         #endif
184         #ifndef LPCTSTR
185                 typedef const _TCHAR* LPCTSTR;
186         #endif
187         #ifndef BOOL
188                 typedef int BOOL;
189         #endif
190         #ifndef TRUE
191                 #define TRUE 1
192         #endif
193         #ifndef FALSE
194                 #define FALSE 0
195         #endif
196         #ifndef BYTE
197                 typedef uint8_t BYTE;
198         #endif
199         #ifndef WORD
200                 typedef uint16_t WORD;
201         #endif
202         #ifndef DWORD
203                 typedef uint32_t DWORD;
204         #endif
205         #ifndef QWORD
206                 typedef uint64_t QWORD;
207         #endif
208         #ifndef INT8
209                 typedef int8_t INT8;
210         #endif
211         #ifndef INT16
212                 typedef int16_t INT16;
213         #endif
214         #ifndef INT32
215                 typedef int32_t INT32;
216         #endif
217         #ifndef INT64
218                 typedef int64_t INT64;
219         #endif
220         #ifndef UINT8
221                 typedef uint8_t UINT8;
222         #endif
223         #ifndef UINT16
224                 typedef uint16_t UINT16;
225         #endif
226         #ifndef UINT32
227                 typedef uint32_t UINT32;
228         #endif
229         #ifndef UINT64
230                 typedef uint64_t UINT64;
231         #endif
232         #ifndef INT
233                 typedef int INT;
234         #endif
235         #ifndef UINT
236                 typedef unsigned int UINT;
237         #endif
238 #endif
239
240 typedef union {
241         struct {
242 #ifdef __BIG_ENDIAN__
243                 uint8_t h3, h2, h, l;
244 #else
245                 uint8_t l, h, h2, h3;
246 #endif
247         } b;
248         struct {
249 #ifdef __BIG_ENDIAN__
250                 int8_t h3, h2, h, l;
251 #else
252                 int8_t l, h, h2, h3;
253 #endif
254         } sb;
255         struct {
256 #ifdef __BIG_ENDIAN__
257                 uint16_t h, l;
258 #else
259                 uint16_t l, h;
260 #endif
261         } w;
262         struct {
263 #ifdef __BIG_ENDIAN__
264                 int16_t h, l;
265 #else
266                 int16_t l, h;
267 #endif
268         } sw;
269         uint32_t d;
270         int32_t sd;
271         inline void read_2bytes_le_from(uint8_t *t)
272         {
273                 b.l = t[0]; b.h = t[1]; b.h2 = b.h3 = 0;
274         }
275         inline void write_2bytes_le_to(uint8_t *t)
276         {
277                 t[0] = b.l; t[1] = b.h;
278         }
279         inline void read_2bytes_be_from(uint8_t *t)
280         {
281                 b.h3 = b.h2 = 0; b.h = t[0]; b.l = t[1];
282         }
283         inline void write_2bytes_be_to(uint8_t *t)
284         {
285                 t[0] = b.h; t[1] = b.l;
286         }
287         inline void read_4bytes_le_from(uint8_t *t)
288         {
289                 b.l = t[0]; b.h = t[1]; b.h2 = t[2]; b.h3 = t[3];
290         }
291         inline void write_4bytes_le_to(uint8_t *t)
292         {
293                 t[0] = b.l; t[1] = b.h; t[2] = b.h2; t[3] = b.h3;
294         }
295         inline void read_4bytes_be_from(uint8_t *t)
296         {
297                 b.h3 = t[0]; b.h2 = t[1]; b.h = t[2]; b.l = t[3];
298         }
299         inline void write_4bytes_be_to(uint8_t *t)
300         {
301                 t[0] = b.h3; t[1] = b.h2; t[2] = b.h; t[3] = b.l;
302         }
303 } pair_t;
304
305 uint32_t DLL_PREFIX EndianToLittle_DWORD(uint32_t x);
306 uint16_t DLL_PREFIX EndianToLittle_WORD(uint16_t x);
307
308 // max/min
309 #ifndef _MSC_VER
310         #undef max
311         #undef min
312         int max(int a, int b);
313         unsigned int max(unsigned int a, unsigned int b);
314         int min(int a, int b);
315         unsigned int min(unsigned int a, unsigned int b);
316 #endif
317
318 // string
319 #if defined(__GNUC__) || defined(__CYGWIN__) || defined(Q_OS_CYGWIN)
320         #define stricmp(a,b) strcasecmp(a,b)
321         #define strnicmp(a,b,n) strncasecmp(a,b,n)
322 #endif
323
324 #ifndef SUPPORT_TCHAR_TYPE
325         #ifndef _tfopen
326                 #define _tfopen fopen
327         #endif
328         #ifndef _tcscmp
329                 #define _tcscmp strcmp
330         #endif
331         #ifndef _tcscpy
332                 #define _tcscpy strcpy
333         #endif
334         #ifndef _tcsicmp
335                 #define _tcsicmp stricmp
336         #endif
337         #ifndef _tcslen
338                 #define _tcslen strlen
339         #endif
340         #ifndef _tcsncat
341                 #define _tcsncat strncat
342         #endif
343         #ifndef _tcsncpy
344                 #define _tcsncpy strncpy
345         #endif
346         #ifndef _tcsncicmp
347                 #define _tcsncicmp strnicmp
348         #endif
349         #ifndef _tcschr
350                 #define _tcschr strchr
351         #endif
352         #ifndef _tcsrchr
353                 #define _tcsrchr strrchr
354         #endif
355         #ifndef _tcsstr
356                 #define _tcsstr strstr
357         #endif
358         #ifndef _tcstok
359                 #define _tcstok strtok
360         #endif
361         #ifndef _tstoi
362                 #define _tstoi atoi
363         #endif
364         #ifndef _tcstol
365                 #define _tcstol strtol
366         #endif
367         #ifndef _tcstoul
368                 #define _tcstoul strtoul
369         #endif
370         #ifndef _stprintf
371                 #define _stprintf sprintf
372         #endif
373         #ifndef _vstprintf
374                 #define _vstprintf vsprintf
375         #endif
376         #ifndef _taccess
377                 #define _taccess access
378         #endif
379         #ifndef _tremove
380                 #define _tremove remove
381         #endif
382         #ifndef _trename
383                 #define _trename rename
384         #endif
385         #define __T(x) x
386         #define _T(x) __T(x)
387         #define _TEXT(x) __T(x)
388 #endif
389
390 #ifndef SUPPORT_SECURE_FUNCTIONS
391         #ifndef errno_t
392                 typedef int errno_t;
393         #endif
394 //      errno_t my_tfopen_s(FILE** pFile, const _TCHAR *filename, const _TCHAR *mode);
395         errno_t DLL_PREFIX my_strcpy_s(char *strDestination, size_t numberOfElements, const char *strSource);
396         errno_t DLL_PREFIX my_tcscpy_s(_TCHAR *strDestination, size_t numberOfElements, const _TCHAR *strSource);
397         errno_t DLL_PREFIX my_strncpy_s(char *strDestination, size_t numberOfElements, const char *strSource, size_t count);
398         errno_t DLL_PREFIX my_tcsncpy_s(_TCHAR *strDestination, size_t numberOfElements, const _TCHAR *strSource, size_t count);
399         char * DLL_PREFIX my_strtok_s(char *strToken, const char *strDelimit, char **context);
400         _TCHAR * DLL_PREFIX my_tcstok_s(_TCHAR *strToken, const char *strDelimit, _TCHAR **context);
401         #define my_fprintf_s fprintf
402         int DLL_PREFIX my_sprintf_s(char *buffer, size_t sizeOfBuffer, const char *format, ...);
403         int DLL_PREFIX my_stprintf_s(_TCHAR *buffer, size_t sizeOfBuffer, const _TCHAR *format, ...);
404         int DLL_PREFIX my_vsprintf_s(char *buffer, size_t numberOfElements, const char *format, va_list argptr);
405         int DLL_PREFIX my_vstprintf_s(_TCHAR *buffer, size_t numberOfElements, const _TCHAR *format, va_list argptr);
406 #else
407 //      #define my_tfopen_s _tfopen_s
408         #define my_strcpy_s strcpy_s
409         #define my_tcscpy_s _tcscpy_s
410         #define my_strncpy_s strncpy_s
411         #define my_tcsncpy_s _tcsncpy_s
412         #define my_strtok_s strtok_s
413         #define my_tcstok_s _tcstok_s
414         #define my_fprintf_s fprintf_s
415         #define my_sprintf_s sprintf_s
416         #define my_stprintf_s _stprintf_s
417         #define my_vsprintf_s vsprintf_s
418         #define my_vstprintf_s _vstprintf_s
419 #endif
420
421 // win32 api
422 #ifndef _WIN32
423         BOOL MyWritePrivateProfileString(LPCTSTR lpAppName, LPCTSTR lpKeyName, LPCTSTR lpString, LPCTSTR lpFileName);
424         DWORD MyGetPrivateProfileString(LPCTSTR lpAppName, LPCTSTR lpKeyName, LPCTSTR lpDefault, LPTSTR lpReturnedString, DWORD nSize, LPCTSTR lpFileName);
425         UINT MyGetPrivateProfileInt(LPCTSTR lpAppName, LPCTSTR lpKeyName, INT nDefault, LPCTSTR lpFileName);
426         // used only in winmain and win32 osd class
427 //      #define ZeroMemory(p,s) memset(p,0x00,s)
428 //      #define CopyMemory(t,f,s) memcpy(t,f,s)
429 #else
430         #define MyWritePrivateProfileString WritePrivateProfileString
431         #define MyGetPrivateProfileString GetPrivateProfileString
432         #define MyGetPrivateProfileInt GetPrivateProfileInt
433 #endif
434
435 // rgb color
436 #if !defined(_RGB555) && !defined(_RGB565) && !defined(_RGB888)
437         #define _RGB888
438 #endif
439
440 #if defined(_RGB555) || defined(_RGB565)
441         typedef uint16_t scrntype_t;
442         scrntype_t RGB_COLOR(uint32_t r, uint32_t g, uint32_t b);
443         scrntype_t RGBA_COLOR(uint32_t r, uint32_t g, uint32_t b, uint32_t a);
444         uint8_t R_OF_COLOR(scrntype_t c);
445         uint8_t G_OF_COLOR(scrntype_t c);
446         uint8_t B_OF_COLOR(scrntype_t c);
447         uint8_t A_OF_COLOR(scrntype_t c);
448 #elif defined(_RGB888)
449         typedef uint32_t scrntype_t;
450         #define RGB_COLOR(r, g, b)      (((uint32_t)(r) << 16) | ((uint32_t)(g) << 8) | ((uint32_t)(b) << 0))
451         #define RGBA_COLOR(r, g, b, a)  (((uint32_t)(r) << 16) | ((uint32_t)(g) << 8) | ((uint32_t)(b) << 0) | ((uint32_t)(a) << 24))
452         #define R_OF_COLOR(c)           (((c) >> 16) & 0xff)
453         #define G_OF_COLOR(c)           (((c) >>  8) & 0xff)
454         #define B_OF_COLOR(c)           (((c)      ) & 0xff)
455         #define A_OF_COLOR(c)           (((c) >> 24) & 0xff)
456 #endif
457
458 // wav file header
459 #pragma pack(1)
460 typedef struct {
461         char id[4];
462         uint32_t size;
463 } wav_chunk_t;
464 #pragma pack()
465
466 #pragma pack(1)
467 typedef struct {
468         wav_chunk_t riff_chunk;
469         char wave[4];
470         wav_chunk_t fmt_chunk;
471         uint16_t format_id;
472         uint16_t channels;
473         uint32_t sample_rate;
474         uint32_t data_speed;
475         uint16_t block_size;
476         uint16_t sample_bits;
477 } wav_header_t;
478 #pragma pack()
479
480 // file path
481 const _TCHAR *get_application_path();
482 const _TCHAR *create_local_path(const _TCHAR *format, ...);
483 void create_local_path(_TCHAR *file_path, int length, const _TCHAR *format, ...);
484 const _TCHAR *create_date_file_path(const _TCHAR *extension);
485 void create_date_file_path(_TCHAR *file_path, int length, const _TCHAR *extension);
486 bool check_file_extension(const _TCHAR *file_path, const _TCHAR *ext);
487 const _TCHAR *get_file_path_without_extensiton(const _TCHAR *file_path);
488 void get_long_full_path_name(const _TCHAR* src, _TCHAR* dst, size_t dst_len);
489 const _TCHAR* get_parent_dir(const _TCHAR* file);
490
491 // misc
492 const _TCHAR * DLL_PREFIX create_string(const _TCHAR* format, ...);
493 uint32_t DLL_PREFIX get_crc32(uint8_t data[], int size);
494 uint16_t DLL_PREFIX jis_to_sjis(uint16_t jis);
495
496 int DLL_PREFIX decibel_to_volume(int decibel);
497 int32_t DLL_PREFIX apply_volume(int32_t sample, int volume);
498
499 #define array_length(array) (sizeof(array) / sizeof(array[0]))
500
501 #define FROM_BCD(v)     (((v) & 0x0f) + (((v) >> 4) & 0x0f) * 10)
502 #define TO_BCD(v)       ((int)(((v) % 100) / 10) << 4) | ((v) % 10)
503 #define TO_BCD_LO(v)    ((v) % 10)
504 #define TO_BCD_HI(v)    (int)(((v) % 100) / 10)
505
506 #define LEAP_YEAR(y)    (((y) % 4) == 0 && (((y) % 100) != 0 || ((y) % 400) == 0))
507
508 typedef DLL_PREFIX struct cur_time_s {
509         int year, month, day, day_of_week, hour, minute, second;
510         bool initialized;
511         cur_time_s()
512         {
513                 initialized = false;
514         }
515         void increment();
516         void update_year();
517         void update_day_of_week();
518         void save_state(void *f);
519         bool load_state(void *f);
520 } cur_time_t;
521
522 void DLL_PREFIX get_host_time(cur_time_t* cur_time);
523
524 #endif