OSDN Git Service

[COMMON][FILEIO] common.h : Fix pair16_t and pair64_t to fileio.cpp .
[csp-qt/common_source_project-fm7.git] / source / src / common.cpp
1 /*
2         Skelton for retropc emulator
3
4         Author : Takeda.Toshiya
5         Date   : 2013.01.17-
6
7         [ common ]
8 */
9
10 #if defined(_USE_QT)
11         #include <string.h>
12         #include <fcntl.h>
13         #if !defined(__WIN32) && !defined(__WIN64)
14                 #include <unistd.h>
15         #else
16                 #include <io.h>
17                 #include <direct.h>
18         #endif
19         #include <sys/types.h>
20         #include <sys/stat.h>
21         #include "csp_logger.h"
22         #include <string>
23         #include <algorithm>
24         #include <cctype>
25         #include <QDir>
26         #include <QFileInfo>
27 #elif defined(_WIN32)
28         #include <shlwapi.h>
29         #pragma comment(lib, "shlwapi.lib")
30 #else
31         #include <time.h>
32 #endif
33 #include <math.h>
34 #include "common.h"
35 #include "fileio.h"
36
37 #if defined(__MINGW32__) || defined(__MINGW64__)
38         extern DWORD GetLongPathName(LPCTSTR lpszShortPath, LPTSTR lpszLongPath, DWORD cchBuffer);
39 #endif
40 #if defined(_USE_QT)
41         std::string DLL_PREFIX cpp_homedir;
42         std::string DLL_PREFIX my_procname;
43         std::string DLL_PREFIX sRssDir;
44 #endif
45
46 void DLL_PREFIX common_initialize()
47 {
48         get_initial_current_path();
49 }
50
51 uint32_t DLL_PREFIX EndianToLittle_DWORD(uint32_t x)
52 {
53 #if defined(__LITTLE_ENDIAN__)
54         return x;
55 #else
56         uint32_t y;
57         y = ((x & 0x000000ff) << 24) | ((x & 0x0000ff00) << 8) |
58             ((x & 0x00ff0000) >> 8)  | ((x & 0xff000000) >> 24);
59         return y;
60 #endif
61 }
62
63 uint16_t DLL_PREFIX EndianToLittle_WORD(uint16_t x)
64 {
65 #if defined(__LITTLE_ENDIAN__)
66         return x;
67 #else
68         uint16_t y;
69         y = ((x & 0x00ff) << 8) | ((x & 0xff00) >> 8);
70         return y;
71 #endif
72 }
73
74 uint32_t DLL_PREFIX EndianFromLittle_DWORD(uint32_t x)
75 {
76 #if defined(__LITTLE_ENDIAN__)
77         return x;
78 #else
79         uint32_t y;
80         y = ((x & 0x000000ff) << 24) | ((x & 0x0000ff00) << 8) |
81             ((x & 0x00ff0000) >> 8)  | ((x & 0xff000000) >> 24);
82         return y;
83 #endif
84 }
85
86 uint16_t DLL_PREFIX EndianFromLittle_WORD(uint16_t x)
87 {
88 #if defined(__LITTLE_ENDIAN__)
89         return x;
90 #else
91         uint16_t y;
92         y = ((x & 0x00ff) << 8) | ((x & 0xff00) >> 8);
93         return y;
94 #endif
95 }
96
97
98 uint32_t DLL_PREFIX EndianToBig_DWORD(uint32_t x)
99 {
100 #if defined(__BIG_ENDIAN__)
101         return x;
102 #else
103         uint32_t y;
104         y = ((x & 0x000000ff) << 24) | ((x & 0x0000ff00) << 8) |
105             ((x & 0x00ff0000) >> 8)  | ((x & 0xff000000) >> 24);
106         return y;
107 #endif
108 }
109
110 uint16_t DLL_PREFIX EndianToBig_WORD(uint16_t x)
111 {
112 #if defined(__BIG_ENDIAN__)
113         return x;
114 #else
115         uint16_t y;
116         y = ((x & 0x00ff) << 8) | ((x & 0xff00) >> 8);
117         return y;
118 #endif
119 }
120
121 uint32_t DLL_PREFIX EndianFromBig_DWORD(uint32_t x)
122 {
123 #if defined(__BIG_ENDIAN__)
124         return x;
125 #else
126         uint32_t y;
127         y = ((x & 0x000000ff) << 24) | ((x & 0x0000ff00) << 8) |
128             ((x & 0x00ff0000) >> 8)  | ((x & 0xff000000) >> 24);
129         return y;
130 #endif
131 }
132
133 uint16_t DLL_PREFIX EndianFromBig_WORD(uint16_t x)
134 {
135 #if defined(__BIG_ENDIAN__)
136         return x;
137 #else
138         uint16_t y;
139         y = ((x & 0x00ff) << 8) | ((x & 0xff00) >> 8);
140         return y;
141 #endif
142 }
143
144 #ifndef _MSC_VER
145 int DLL_PREFIX max(int a, int b)
146 {
147         if(a > b) {
148                 return a;
149         } else {
150                 return b;
151         }
152 }
153
154 unsigned DLL_PREFIX int max(unsigned int a, unsigned int b)
155 {
156         if(a > b) {
157                 return a;
158         } else {
159                 return b;
160         }
161 }
162
163 unsigned DLL_PREFIX int max(unsigned int a, int b)
164 {
165         if(b < 0) return a;
166         if(a > (unsigned int)b) {
167                 return a;
168         } else {
169                 return b;
170         }
171 }
172
173 unsigned DLL_PREFIX int max(int a, unsigned int b)
174 {
175         if(a < 0) return b;
176         if((unsigned int)a > b) {
177                 return a;
178         } else {
179                 return b;
180         }
181 }
182
183 int DLL_PREFIX min(int a, int b)
184 {
185         if(a < b) {
186                 return a;
187         } else {
188                 return b;
189         }
190 }
191
192 int DLL_PREFIX min(unsigned int a, int b)
193 {
194         if(b < 0) return b;
195         if(a > INT_MAX) return b;
196         
197         if((int)a < b) {
198                 return (int)a;
199         } else {
200                 return b;
201         }
202 }
203
204 int DLL_PREFIX min(int a, unsigned int b)
205 {
206         if(a < 0) return a;
207         if(b > INT_MAX) return a;
208         
209         if(a < (int)b) {
210                 return a;
211         } else {
212                 return (int)b;
213         }
214 }
215
216 unsigned int DLL_PREFIX min(unsigned int a, unsigned int b)
217 {
218         if(a < b) {
219                 return a;
220         } else {
221                 return b;
222         }
223 }
224 #endif
225
226 #ifndef SUPPORT_SECURE_FUNCTIONS
227 //errno_t my_tfopen_s(FILE** pFile, const _TCHAR *filename, const _TCHAR *mode)
228 //{
229 //      if((*pFile = _tfopen(filename, mode)) != NULL) {
230 //              return 0;
231 //      } else {
232 //              return errno;
233 //      }
234 //}
235
236 errno_t DLL_PREFIX my_tcscat_s(_TCHAR *strDestination, size_t numberOfElements, const _TCHAR *strSource)
237 {
238         _tcscat(strDestination, strSource);
239         return 0;
240 }
241
242 errno_t DLL_PREFIX my_strcpy_s(char *strDestination, size_t numberOfElements, const char *strSource)
243 {
244         strcpy(strDestination, strSource);
245         return 0;
246 }
247
248 errno_t DLL_PREFIX my_tcscpy_s(_TCHAR *strDestination, size_t numberOfElements, const _TCHAR *strSource)
249 {
250         _tcscpy(strDestination, strSource);
251         return 0;
252 }
253
254 errno_t DLL_PREFIX my_strncpy_s(char *strDestination, size_t numberOfElements, const char *strSource, size_t count)
255 {
256         strncpy(strDestination, strSource, count);
257         return 0;
258 }
259
260 errno_t DLL_PREFIX my_tcsncpy_s(_TCHAR *strDestination, size_t numberOfElements, const _TCHAR *strSource, size_t count)
261 {
262         _tcsncpy(strDestination, strSource, count);
263         return 0;
264 }
265
266 char *DLL_PREFIX my_strtok_s(char *strToken, const char *strDelimit, char **context)
267 {
268         return strtok(strToken, strDelimit);
269 }
270
271 _TCHAR *DLL_PREFIX my_tcstok_s(_TCHAR *strToken, const char *strDelimit, _TCHAR **context)
272 {
273         return _tcstok(strToken, strDelimit);
274 }
275
276 int DLL_PREFIX my_sprintf_s(char *buffer, size_t sizeOfBuffer, const char *format, ...)
277 {
278         va_list ap;
279         va_start(ap, format);
280         int result = vsnprintf(buffer, sizeOfBuffer, format, ap);
281         va_end(ap);
282         return result;
283 }
284
285 int DLL_PREFIX my_swprintf_s(wchar_t *buffer, size_t sizeOfBuffer, const wchar_t *format, ...)
286 {
287         va_list ap;
288         va_start(ap, format);
289         int result = vswprintf(buffer, sizeOfBuffer, format, ap);
290         va_end(ap);
291         return result;
292 }
293
294 int DLL_PREFIX my_stprintf_s(_TCHAR *buffer, size_t sizeOfBuffer, const _TCHAR *format, ...)
295 {
296         va_list ap;
297         va_start(ap, format);
298         int result = vsnprintf(buffer, sizeOfBuffer, format, ap);
299         va_end(ap);
300         return result;
301 }
302
303 int DLL_PREFIX my_vsprintf_s(char *buffer, size_t numberOfElements, const char *format, va_list argptr)
304 {
305         return vsnprintf(buffer, numberOfElements * sizeof(char), format, argptr);
306 }
307
308 int DLL_PREFIX my_vstprintf_s(_TCHAR *buffer, size_t numberOfElements, const _TCHAR *format, va_list argptr)
309 {
310         return vsnprintf(buffer, numberOfElements * sizeof(_TCHAR), format, argptr);
311 }
312 #endif
313
314 //#ifdef USE_FAST_MEMCPY
315
316 void DLL_PREFIX *my_memcpy(void *dst, void *src, size_t len)
317 {
318         size_t len1;
319         register size_t len2;
320         register uint32_t s_align = (uint32_t)(((size_t)src) & 0x1f);
321         register uint32_t d_align = (uint32_t)(((size_t)dst) & 0x1f);
322         int i;
323         
324         if(len == 0) return dst;
325         if(len < 8) {
326                 return memcpy(dst, src, len);
327         }
328         len1 = len;
329
330 #if defined(WITHOUT_UNALIGNED_SIMD)
331 // Using SIMD without un-aligned instructions.
332         switch(s_align) {
333         case 0: // Align 256
334                 {
335                         uint64_t b64[4];
336                         register uint64_t *s64 = (uint64_t *)src;
337                         register uint64_t *d64 = (uint64_t *)dst;
338                         switch(d_align) {
339                         case 0: // 256 vs 256
340                                 {
341                                         len2 = len1 >> 5;
342                                         while(len2 > 0) {
343                                                 for(i = 0; i < 4; i++) b64[i] = s64[i];
344                                                 for(i = 0; i < 4; i++) d64[i] = b64[i];
345                                                 s64 += 4;
346                                                 d64 += 4;
347                                                 --len2;
348                                         }
349                                         len1 = len1 & 0x1f;
350                                         if(len1 != 0) return memcpy(d64, s64, len1);
351                                         return dst;
352                                 }
353                                 break;
354                         case 0x10: // 256 vs 128
355                                 {
356                                         len2 = len1 >> 5;
357                                         while(len2 > 0) {
358                                                 for(i = 0; i < 4; i++) b64[i] = s64[i];
359                                                 for(i = 0; i < 2; i++) d64[i] = b64[i];
360                                                 d64 += 2;
361                                                 for(i = 2; i < 4; i++) d64[i - 2] = b64[i];
362                                                 d64 += 2;
363                                                 s64 += 4;
364                                                 --len2;
365                                         }
366                                         len1 = len1 & 0x1f;
367                                         if(len1 != 0) return memcpy(d64, s64, len1);
368                                         return dst;
369                                 }
370                                 break;
371                         case 0x08:
372                         case 0x18: // 256 vs 64
373                                 {
374                                         len2 = len1 >> 5;
375                                         while(len2 > 0) {
376                                                 for(i = 0; i < 4; ++i) b64[i] = s64[i];
377                                                 for(i = 0; i < 4; ++i) {
378                                                         *d64 = b64[i];
379                                                         ++d64;
380                                                 }
381                                                 s64 += 4;
382                                                 --len2;
383                                         }
384                                         len1 = len1 & 0x1f;
385                                         if(len1 != 0) return memcpy(d64, s64, len1);
386                                         return dst;
387                                 }
388                                 break;
389                         case 0x04:
390                         case 0x0c: 
391                         case 0x14:
392                         case 0x1c: // 256 vs 32
393                                 {
394                                         uint32_t b32[8];
395                                         register uint32_t *s32 = (uint32_t *)src;
396                                         register uint32_t *d32 = (uint32_t *)dst;
397                                         len2 = len1 >> 5;
398                                         while(len2 > 0) {
399                                                 for(i = 0; i < 8; ++i) b32[i] = s32[i];
400                                                 *d32 = b32[0];
401                                                 ++d32;
402                                                 *d32 = b32[1];
403                                                 ++d32;
404                                                 *d32 = b32[2];
405                                                 ++d32;
406                                                 *d32 = b32[3];
407                                                 ++d32;
408                                                 *d32 = b32[4];
409                                                 ++d32;
410                                                 *d32 = b32[5];
411                                                 ++d32;
412                                                 *d32 = b32[6];
413                                                 ++d32;
414                                                 *d32 = b32[7];
415                                                 ++d32;
416                                                 s32 += 8;
417                                                 --len2;
418                                         }
419                                         len1 = len1 & 0x1f;
420                                         if(len1 != 0) return memcpy(d32, s32, len1);
421                                         return dst;
422                                 }
423                                 break;
424                         default:
425                                 return memcpy(dst, src, len1);
426                                 break;
427                         }
428                 }
429                 break;
430         case 0x10: // Src alignn to 16.
431                 {
432                         uint32_t b32[4];
433                         register uint32_t *s32 = (uint32_t *)src;
434                         register uint32_t *d32 = (uint32_t *)dst;
435                         switch(d_align) {
436                         case 0: // 128 vs 256/128
437                         case 0x10:
438                                 {
439                                         len2 = len1 >> 4;
440                                         while(len2 > 0) {
441                                                 for(i = 0; i < 4; i++) b32[i] = s32[i];
442                                                 for(i = 0; i < 4; i++) d32[i] = b32[i];
443                                                 s32 += 4;
444                                                 d32 += 4;
445                                                 --len2;
446                                         }
447                                         len1 = len1 & 0x0f;
448                                         if(len1 != 0) return memcpy(d32, s32, len1);
449                                         return dst;
450                                 }
451                                 break;
452                         case 0x08:
453                         case 0x18: // 128 vs 64
454                                 {
455                                         len2 = len1 >> 4;
456                                         while(len2 > 0) {
457                                                 for(i = 0; i < 4; ++i) b32[i] = s32[i];
458                                                 for(i = 0; i < 2; ++i) {
459                                                         d32[i] = b32[i];
460                                                 }
461                                                 d32 += 2;
462                                                 for(i = 2; i < 4; ++i) {
463                                                         d32[i - 2] = b32[i];
464                                                 }
465                                                 d32 += 2;
466                                                 s32 += 4;
467                                                 --len2;
468                                         }
469                                         len1 = len1 & 0x0f;
470                                         if(len1 != 0) return memcpy(d32, s32, len1);
471                                         return dst;
472                                 }
473                                 break;
474                         case 0x04:
475                         case 0x0c:
476                         case 0x14:
477                         case 0x1c: // 128 vs 32
478                                 {
479                                         len2 = len1 >> 4;
480                                         while(len2 > 0) {
481                                                 for(i = 0; i < 4; ++i) b32[i] = s32[i];
482                                                 *d32 = b32[0];
483                                                 ++d32;
484                                                 *d32 = b32[1];
485                                                 ++d32;
486                                                 *d32 = b32[2];
487                                                 ++d32;
488                                                 *d32 = b32[3];
489                                                 ++d32;
490                                                 s32 += 4;
491                                                 --len2;
492                                         }
493                                         len1 = len1 & 0x0f;
494                                         if(len1 != 0) return memcpy(d32, s32, len1);
495                                         return dst;
496                                 }
497                                 break;
498                         default:
499                                 return memcpy(dst, src, len1);
500                                 break;
501                         }
502                 }
503                 break;
504         case 0x08:
505         case 0x18: // Src alignn to 64.
506                 {
507                         register uint32_t *s32 = (uint32_t *)src;
508                         register uint32_t *d32 = (uint32_t *)dst;
509                         register uint64_t *s64 = (uint64_t *)src;
510                         register uint64_t *d64 = (uint64_t *)dst;
511                         switch(d_align) {
512                         case 0:
513                         case 0x10: // 64 vs 128
514                                 {
515                                         uint64_t b128[2];
516                                         len2 = len1 >> 4;
517                                         while(len2 > 0) {
518                                                 b128[0] = *s64;
519                                                 ++s64;
520                                                 b128[1] = *s64;
521                                                 ++s64;
522                                                 for(i = 0; i < 2; i++) d64[i] = b128[i];
523                                                 d64 += 2;
524                                                 --len2;
525                                         }
526                                         len1 = len1 & 0x0f;
527                                         if(len1 != 0) return memcpy(d64, s64, len1);
528                                         return dst;
529                                 }
530                                 break;
531                         case 0x08:
532                         case 0x18: // 64 vs 64
533                                 {
534                                         len2 = len1 >> 3;
535                                         while(len2 > 0) {
536                                                 *d64 = *s64;
537                                                 ++s64;
538                                                 ++d64;
539                                                 --len2;
540                                         }
541                                         len1 = len1 & 0x07;
542                                         if(len1 != 0) return memcpy(d64, s64, len1);
543                                         return dst;
544                                 }
545                                 break;
546                         case 0x04:
547                         case 0x0c: // 64 vs 32
548                         case 0x14:
549                         case 0x1c: // 64 vs 32
550                                 {
551                                         uint32_t b32[2];
552                                         len2 = len1 >> 3;
553                                         while(len2 > 0) {
554                                                 for(i = 0; i < 2; ++i) b32[i] = s32[i];
555                                                 d32[0] = b32[0];
556                                                 d32[1] = b32[1];
557                                                 s32 += 2;
558                                                 d32 += 2;
559                                                 --len2;
560                                         }
561                                         len1 = len1 & 0x07;
562                                         if(len1 != 0) return memcpy(d32, s32, len1);
563                                         return dst;
564                                 }
565                                 break;
566                         default:
567                                 return memcpy(dst, src, len1);
568                                 break;
569                         }
570                 }
571         case 0x04:
572         case 0x0c:
573         case 0x14:
574         case 0x1c:  // Src align 32
575                 {
576                         register uint32_t *s32 = (uint32_t *)src;
577                         register uint32_t *d32 = (uint32_t *)dst;
578                         register uint64_t *d64 = (uint64_t *)dst;
579                         switch(d_align) {
580                         case 0:
581                         case 0x10: // 32 vs 128
582                                 {
583                                         uint32_t b128[4];
584                                         len2 = len1 >> 4;
585                                         while(len2 > 0) {
586                                                 b128[0] = *s32;
587                                                 ++s32;
588                                                 b128[1] = *s32;
589                                                 ++s32;
590                                                 b128[3] = *s32;
591                                                 ++s32;
592                                                 b128[4] = *s32;
593                                                 ++s32;
594                                                 for(i = 0; i < 4; i++) d32[i] = b128[i];
595                                                 d32 += 4;
596                                                 --len2;
597                                         }
598                                         len1 = len1 & 0x0f;
599                                         if(len1 != 0) return memcpy(d32, s32, len1);
600                                         return dst;
601                                 }
602                                 break;
603                         case 0x08:
604                         case 0x18: // 32 vs 64
605                                 {
606                                         uint32_t b64[2];
607                                         len2 = len1 >> 3;
608                                         while(len2 > 0) {
609                                                 b64[0] = *s32;
610                                                 ++s32;
611                                                 b64[1] = *s32;
612                                                 ++s32;
613
614                                                 for(i = 0; i < 2; i++) d32[i] = b64[i];
615                                                 d32 += 2;
616                                                 --len2;
617                                         }
618                                         len1 = len1 & 0x07;
619                                         if(len1 != 0) return memcpy(d32, s32, len1);
620                                         return dst;
621                                 }
622                                 break;
623                         case 0x04:
624                         case 0x0c: 
625                         case 0x14:
626                         case 0x1c: // 32 vs 32
627                                 {
628                                         len2 = len1 >> 2;
629                                         while(len2 > 0) {
630                                                 *d32 = *s32;
631                                                 ++s32;
632                                                 ++d32;
633                                                 --len2;
634                                         }
635                                         len1 = len1 & 0x03;
636                                         if(len1 != 0) return memcpy(d32, s32, len1);
637                                         return dst;
638                                 }
639                                 break;
640                         default:
641                                 return memcpy(dst, src, len1);
642                                 break;
643                         }
644                 }
645                 break;
646         default:
647                 if(len1 != 0) return memcpy(dst, src, len1);
648                 break;
649         }
650
651 #else
652 // Using SIMD *with* un-aligned instructions.
653         register uint32_t *s32 = (uint32_t *)src;
654         register uint32_t *d32 = (uint32_t *)dst;
655         if(((s_align & 0x07) != 0x0) && ((d_align & 0x07) != 0x0)) { // None align.
656                 return memcpy(dst, src, len);
657         }
658         if((s_align == 0x0) || (d_align == 0x0)) { // Align to 256bit
659                 uint32_t b256[8];
660                 len2 = len1 >> 5;
661                 while(len2 > 0) {
662                         for(i = 0; i < 8; i++) b256[i] = s32[i];
663                         for(i = 0; i < 8; i++) d32[i] = b256[i];
664                         s32 += 8;
665                         d32 += 8;
666                         --len2;
667                 }
668                 len1 = len1 & 0x1f;
669                 if(len1 != 0) return memcpy(d32, s32, len1);
670                 return dst;
671         }
672         if(((s_align & 0x0f) == 0x0) || ((d_align & 0x0f) == 0x0)) { // Align to 128bit
673                 uint32_t b128[4];
674                 len2 = len1 >> 4;
675                 while(len2 > 0) {
676                         for(i = 0; i < 4; i++) b128[i] = s32[i];
677                         for(i = 0; i < 4; i++) d32[i] = b128[i];
678                         s32 += 4;
679                         d32 += 4;
680                         --len2;
681                 }
682                 len1 = len1 & 0x0f;
683                 if(len1 != 0) return memcpy(d32, s32, len1);
684                 return dst;
685         }               
686         if(((s_align & 0x07) == 0x0) || ((d_align & 0x07) == 0x0)) { // Align to 64bit
687                 uint32_t b64[2];
688                 len2 = len1 >> 3;
689                 while(len2 > 0) {
690                         for(i = 0; i < 2; i++) b64[i] = s32[i];
691                         for(i = 0; i < 2; i++) d32[i] = b64[i];
692                         s32 += 2;
693                         d32 += 2;
694                         --len2;
695                 }
696                 len1 = len1 & 0x07;
697                 if(len1 != 0) return memcpy(d32, s32, len1);
698                 return dst;
699         }               
700         //if(len1 != 0) return memcpy(dst, src, len1);
701 #endif
702         // Trap
703         return dst;
704 }
705 //#endif
706
707
708 #ifndef _WIN32
709 BOOL DLL_PREFIX MyWritePrivateProfileString(LPCTSTR lpAppName, LPCTSTR lpKeyName, LPCTSTR lpString, LPCTSTR lpFileName)
710 {
711         BOOL result = FALSE;
712         FILEIO* fio_i = new FILEIO();
713         if(fio_i->Fopen(lpFileName, FILEIO_READ_ASCII)) {
714                 char tmp_path[_MAX_PATH];
715                 my_sprintf_s(tmp_path, _MAX_PATH, "%s.$$$", lpFileName);
716                 FILEIO* fio_o = new FILEIO();
717                 if(fio_o->Fopen(tmp_path, FILEIO_WRITE_ASCII)) {
718                         bool in_section = false;
719                         char section[1024], line[1024], *equal;
720                         my_sprintf_s(section, 1024, "[%s]", lpAppName);
721                         while(fio_i->Fgets(line, 1024) != NULL && strlen(line) > 0) {
722                                 if(line[strlen(line) - 1] == '\n') {
723                                         line[strlen(line) - 1] = '\0';
724                                 }
725                                 if(!result) {
726                                         if(line[0] == '[') {
727                                                 if(in_section) {
728                                                         fio_o->Fprintf("%s=%s\n", lpKeyName, lpString);
729                                                         result = TRUE;
730                                                 } else if(strcmp(line, section) == 0) {
731                                                         in_section = true;
732                                                 }
733                                         } else if(in_section && (equal = strstr(line, "=")) != NULL) {
734                                                 *equal = '\0';
735                                                 if(strcmp(line, lpKeyName) == 0) {
736                                                         fio_o->Fprintf("%s=%s\n", lpKeyName, lpString);
737                                                         result = TRUE;
738                                                         continue;
739                                                 }
740                                                 *equal = '=';
741                                         }
742                                 }
743                                 fio_o->Fprintf("%s\n", line);
744                         }
745                         if(!result) {
746                                 if(!in_section) {
747                                         fio_o->Fprintf("[%s]\n", lpAppName);
748                                 }
749                                 fio_o->Fprintf("%s=%s\n", lpKeyName, lpString);
750                                 result = TRUE;
751                         }
752                         fio_o->Fclose();
753                 }
754                 delete fio_o;
755                 fio_i->Fclose();
756                 if(result) {
757                         if(!(FILEIO::RemoveFile(lpFileName) && FILEIO::RenameFile(tmp_path, lpFileName))) {
758                                 result = FALSE;
759                         }
760                 }
761         } else {
762                 FILEIO* fio_o = new FILEIO();
763                 if(fio_o->Fopen(lpFileName, FILEIO_WRITE_ASCII)) {
764                         fio_o->Fprintf("[%s]\n", lpAppName);
765                         fio_o->Fprintf("%s=%s\n", lpKeyName, lpString);
766                         fio_o->Fclose();
767                 }
768                 delete fio_o;
769         }
770         delete fio_i;
771         return result;
772 }
773
774
775 DWORD DLL_PREFIX MyGetPrivateProfileString(LPCTSTR lpAppName, LPCTSTR lpKeyName, LPCTSTR lpDefault, LPTSTR lpReturnedString, DWORD nSize, LPCTSTR lpFileName)
776 {
777         _TCHAR *lpp = (_TCHAR *)lpReturnedString;
778         if(lpDefault != NULL) {
779                 my_strcpy_s(lpp, nSize, lpDefault);
780         } else {
781                 lpp[0] = '\0';
782         }
783         FILEIO* fio = new FILEIO();
784         if(fio->Fopen(lpFileName, FILEIO_READ_ASCII)) {
785                 bool in_section = false;
786                 char section[1024], line[1024], *equal;
787                 my_sprintf_s(section, 1024, "[%s]", lpAppName);
788                 while(fio->Fgets(line, 1024) != NULL && strlen(line) > 0) {
789                         if(line[strlen(line) - 1] == '\n') {
790                                 line[strlen(line) - 1] = '\0';
791                         }
792                         if(line[0] == '[') {
793                                 if(in_section) {
794                                         break;
795                                 } else if(strcmp(line, section) == 0) {
796                                         in_section = true;
797                                 }
798                         } else if(in_section && (equal = strstr(line, "=")) != NULL) {
799                                 *equal = '\0';
800                                 if(strcmp(line, lpKeyName) == 0) {
801                                         my_strcpy_s(lpp, nSize, equal + 1);
802                                         break;
803                                 }
804                         }
805                 }
806                 fio->Fclose();
807         }
808         delete fio;
809         //csp_logger->debug_log(CSP_LOG_DEBUG, CSP_LOG_TYPE_GENERAL, "Try App: %s Key: %s", lpAppName, lpKeyName);
810         return strlen(lpp);
811 }
812
813 UINT DLL_PREFIX MyGetPrivateProfileInt(LPCTSTR lpAppName, LPCTSTR lpKeyName, INT nDefault, LPCTSTR lpFileName)
814 {
815         int i;
816         char sstr[128];
817         char sval[128];
818         std::string s;
819         memset(sstr, 0x00, sizeof(sstr));
820         memset(sval, 0x00, sizeof(sval));
821         snprintf(sval, 128, "%d", nDefault); 
822         MyGetPrivateProfileString(lpAppName,lpKeyName, sval, sstr, 128, lpFileName);
823         s = sstr;
824         
825         if(s.empty()) {
826                 i = nDefault;
827         } else {
828                 i = strtol(s.c_str(), NULL, 10);
829         }
830         //csp_logger->debug_log(CSP_LOG_DEBUG, CSP_LOG_TYPE_GENERAL, "Got Int: %d\n", i);
831         return i;
832 }
833 #endif
834
835 #if defined(_RGB555)
836 scrntype_t DLL_PREFIX RGB_COLOR(uint32_t r, uint32_t g, uint32_t b)
837 {
838         scrntype_t rr = ((scrntype_t)r * 0x1f) / 0xff;
839         scrntype_t gg = ((scrntype_t)g * 0x1f) / 0xff;
840         scrntype_t bb = ((scrntype_t)b * 0x1f) / 0xff;
841         return (rr << 10) | (gg << 5) | bb;
842 }
843
844 scrntype_t DLL_PREFIX RGBA_COLOR(uint32_t r, uint32_t g, uint b, uint32_t a)
845 {
846         return RGB_COLOR(r, g, b);
847 }
848
849 uint8_t DLL_PREFIX R_OF_COLOR(scrntype_t c)
850 {
851         c = (c >> 10) & 0x1f;
852         c = (c * 0xff) / 0x1f;
853         return (uint8_t)c;
854 }
855
856 uint8_t DLL_PREFIX G_OF_COLOR(scrntype_t c)
857 {
858         c = (c >>  5) & 0x1f;
859         c = (c * 0xff) / 0x1f;
860         return (uint8_t)c;
861 }
862
863 uint8_t DLL_PREFIX B_OF_COLOR(scrntype_t c)
864 {
865         c = (c >>  0) & 0x1f;
866         c = (c * 0xff) / 0x1f;
867         return (uint8_t)c;
868 }
869
870 uint8_t DLL_PREFIX A_OF_COLOR(scrntype_t c)
871 {
872         return 0;
873 }
874 #elif defined(_RGB565)
875 scrntype_t DLL_PREFIX RGB_COLOR(uint32_t r, uint32_t g, uint32_t b)
876 {
877         scrntype_t rr = ((scrntype_t)r * 0x1f) / 0xff;
878         scrntype_t gg = ((scrntype_t)g * 0x3f) / 0xff;
879         scrntype_t bb = ((scrntype_t)b * 0x1f) / 0xff;
880         return (rr << 11) | (gg << 5) | bb;
881 }
882
883 scrntype_t DLL_PREFIX RGBA_COLOR(uint32_t r, uint32_t g, uint32_t b, uint32_t a)
884 {
885         return RGB_COLOR(r, g, b);
886 }
887
888 uint8_t DLL_PREFIX R_OF_COLOR(scrntype_t c)
889 {
890         c = (c >> 11) & 0x1f;
891         c = (c * 0xff) / 0x1f;
892         return (uint8_t)c;
893 }
894
895 uint8_t DLL_PREFIX G_OF_COLOR(scrntype_t c)
896 {
897         c = (c >>  5) & 0x3f;
898         c = (c * 0xff) / 0x3f;
899         return (uint8_t)c;
900 }
901
902 uint8_t DLL_PREFIX B_OF_COLOR(scrntype_t c)
903 {
904         c = (c >>  0) & 0x1f;
905         c = (c * 0xff) / 0x1f;
906         return (uint8_t)c;
907 }
908
909 uint8_t DLL_PREFIX A_OF_COLOR(scrntype_t c)
910 {
911         return 0;
912 }
913 #endif
914
915 #ifndef _MSC_VER
916 struct to_upper {  // Refer from documentation of libstdc++, GCC5.
917         char operator() (char c) const { return std::toupper(c); }
918 };
919 #endif
920
921 #if defined(_USE_QT)
922 static void _my_mkdir(std::string t_dir)
923 {
924         struct stat st;
925 //#if !defined(__WIN32) && !defined(__WIN64)
926 //      if(fstatat(AT_FDCWD, csppath.c_str(), &st, 0) != 0) {
927 //              mkdirat(AT_FDCWD, t_dir.c_str(), 0700); // Not found
928 //      }
929 #if defined(_USE_QT)
930         if(stat(t_dir.c_str(), &st) != 0) {
931                 QDir dir = QDir::current();
932                 dir.mkdir(QString::fromStdString(t_dir));
933                 //dir.mkpath(QString::fromUtf8(app_path));
934         }
935 #else
936         if(stat(csppath.c_str(), &st) != 0) {
937                 _mkdir(t_dir.c_str()); // Not found
938         }
939 #endif
940 }
941 #endif
942
943 const _TCHAR *DLL_PREFIX get_application_path()
944 {
945         static _TCHAR app_path[_MAX_PATH];
946         static bool initialized = false;
947         
948         if(!initialized) {
949 #if defined(_WIN32) && !defined(_USE_QT)
950                 _TCHAR tmp_path[_MAX_PATH], *ptr = NULL;
951                 if(GetModuleFileName(NULL, tmp_path, _MAX_PATH) != 0 && GetFullPathName(tmp_path, _MAX_PATH, app_path, &ptr) != 0 && ptr != NULL) {
952                         *ptr = _T('\0');
953                 } else {
954                         my_tcscpy_s(app_path, _MAX_PATH, _T(".\\"));
955                 }
956 #else
957 #if defined(Q_OS_WIN)
958                 std::string delim = "\\";
959 #else
960                 std::string delim = "/";
961 #endif
962                 std::string csppath = cpp_homedir + "CommonSourceCodeProject" + delim ;
963                 _my_mkdir(csppath);
964            
965                 std::string cpath = csppath + my_procname + delim;
966                 _my_mkdir(cpath);
967                 strncpy(app_path, cpath.c_str(), _MAX_PATH - 1);
968 #endif
969                 initialized = true;
970         }
971         return (const _TCHAR *)app_path;
972 }
973
974 const _TCHAR *DLL_PREFIX get_initial_current_path()
975 {
976         static _TCHAR current_path[_MAX_PATH];
977         static bool initialized = false;
978         
979         if(!initialized) {
980 #if defined(_WIN32) && !defined(_USE_QT)
981                 GetCurrentDirectoryA(_MAX_PATH, current_path);
982 #else
983                 getcwd(current_path, _MAX_PATH);
984 #endif
985                 int len = strlen(current_path);
986                 if(current_path[len - 1] != '\\' && current_path[len - 1] != '/') {
987 #if defined(_WIN32) || defined(Q_OS_WIN)
988                         current_path[len] = '\\';
989 #else
990                         current_path[len] = '/';
991 #endif
992                         current_path[len + 1] = '\0';
993                 }
994
995                 initialized = true;
996         }
997         return (const _TCHAR *)current_path;
998 }
999
1000 const _TCHAR *DLL_PREFIX create_local_path(const _TCHAR *format, ...)
1001 {
1002         static _TCHAR file_path[8][_MAX_PATH];
1003         static unsigned int table_index = 0;
1004         unsigned int output_index = (table_index++) & 7;
1005         _TCHAR file_name[_MAX_PATH];
1006         //printf("%d %d\n", table_index, output_index);
1007         va_list ap;
1008         
1009         va_start(ap, format);
1010         my_vstprintf_s(file_name, _MAX_PATH, format, ap);
1011         va_end(ap);
1012         my_stprintf_s(file_path[output_index], _MAX_PATH, _T("%s%s"), get_application_path(), file_name);
1013         return (const _TCHAR *)file_path[output_index];
1014 }
1015
1016 void DLL_PREFIX create_local_path(_TCHAR *file_path, int length, const _TCHAR *format, ...)
1017 {
1018         _TCHAR file_name[_MAX_PATH];
1019         va_list ap;
1020         
1021         va_start(ap, format);
1022         my_vstprintf_s(file_name, _MAX_PATH, format, ap);
1023         va_end(ap);
1024         my_stprintf_s(file_path, length, _T("%s%s"), get_application_path(), file_name);
1025 }
1026
1027 bool DLL_PREFIX is_absolute_path(const _TCHAR *file_path)
1028 {
1029 #ifdef _WIN32
1030         if(_tcslen(file_path) > 2 && ((file_path[0] >= _T('A') && file_path[0] <= _T('Z')) || (file_path[0] >= _T('a') && file_path[0] <= _T('z'))) && file_path[1] == _T(':')) {
1031                 return true;
1032         }
1033 #endif
1034         return (_tcslen(file_path) > 1 && (file_path[0] == _T('/') || file_path[0] == _T('\\')));
1035 }
1036
1037 const _TCHAR *DLL_PREFIX create_date_file_path(const _TCHAR *extension)
1038 {
1039         cur_time_t cur_time;
1040         
1041         get_host_time(&cur_time);
1042         return create_local_path(_T("%d-%0.2d-%0.2d_%0.2d-%0.2d-%0.2d.%s"), cur_time.year, cur_time.month, cur_time.day, cur_time.hour, cur_time.minute, cur_time.second, extension);
1043 }
1044
1045 void DLL_PREFIX create_date_file_path(_TCHAR *file_path, int length, const _TCHAR *extension)
1046 {
1047         my_tcscpy_s(file_path, length, create_date_file_path(extension));
1048 }
1049
1050 bool DLL_PREFIX check_file_extension(const _TCHAR *file_path, const _TCHAR *ext)
1051 {
1052 #if defined(_USE_QT)
1053         std::string s_fpath = file_path;
1054         std::string s_ext = ext;
1055         //bool f = false;
1056         int pos;
1057         std::transform(s_fpath.begin(), s_fpath.end(), s_fpath.begin(), to_upper());
1058         std::transform(s_ext.begin(), s_ext.end(), s_ext.begin(), to_upper());
1059         if(s_fpath.length() < s_ext.length()) return false;
1060         pos = s_fpath.rfind(s_ext.c_str(), s_fpath.length());
1061         if((pos != (int)std::string::npos) && (pos >= ((int)s_fpath.length() - (int)s_ext.length()))) return true; 
1062         return false;
1063 #else
1064         int nam_len = _tcslen(file_path);
1065         int ext_len = _tcslen(ext);
1066         
1067         return (nam_len >= ext_len && _tcsncicmp(&file_path[nam_len - ext_len], ext, ext_len) == 0);
1068 #endif
1069 }
1070
1071 const _TCHAR *DLL_PREFIX get_file_path_without_extensiton(const _TCHAR *file_path)
1072 {
1073         static _TCHAR path[8][_MAX_PATH];
1074         static unsigned int table_index = 0;
1075         unsigned int output_index = (table_index++) & 7;
1076         
1077         my_tcscpy_s(path[output_index], _MAX_PATH, file_path);
1078 #if defined(_WIN32) && defined(_MSC_VER)
1079         PathRemoveExtension(path[output_index]);
1080 #elif defined(_USE_QT)
1081         QString delim;
1082         delim = QString::fromUtf8(".");
1083         QString tmp_path = QString::fromUtf8(file_path);
1084         int n = tmp_path.lastIndexOf(delim);
1085         if(n > 0) {
1086                 tmp_path = tmp_path.left(n);
1087         }
1088         //printf("%s\n", tmp_path.toUtf8().constData());
1089         memset(path[output_index], 0x00, sizeof(_TCHAR) * _MAX_PATH);
1090         strncpy(path[output_index], tmp_path.toUtf8().constData(), _MAX_PATH - 1);
1091 #else
1092 #endif
1093         return (const _TCHAR *)path[output_index];
1094 }
1095
1096 void DLL_PREFIX get_long_full_path_name(const _TCHAR* src, _TCHAR* dst, size_t dst_len)
1097 {
1098 #ifdef _WIN32
1099         _TCHAR tmp[_MAX_PATH];
1100         if(GetFullPathName(src, _MAX_PATH, tmp, NULL) == 0) {
1101                 my_tcscpy_s(dst, dst_len, src);
1102         } else if(GetLongPathName(tmp, dst, _MAX_PATH) == 0) {
1103                 my_tcscpy_s(dst, dst_len, tmp);
1104         }
1105 #elif defined(_USE_QT)
1106         QString tmp_path = QString::fromUtf8(src);
1107         QFileInfo info(tmp_path);
1108         my_tcscpy_s(dst, dst_len, info.absoluteFilePath().toLocal8Bit().constData());
1109 #else
1110         // write code for your environment
1111         
1112 #endif
1113 }
1114
1115 const _TCHAR *DLL_PREFIX get_parent_dir(const _TCHAR* file)
1116 {
1117         static _TCHAR path[8][_MAX_PATH];
1118         static unsigned int table_index = 0;
1119         unsigned int output_index = (table_index++) & 7;
1120         
1121 #ifdef _WIN32
1122         _TCHAR *ptr;
1123         GetFullPathName(file, _MAX_PATH, path[output_index], &ptr);
1124         if(ptr != NULL) {
1125                 *ptr = _T('\0');
1126         }
1127 #elif defined(_USE_QT)
1128         QString delim;
1129 #if defined(Q_OS_WIN)
1130         delim = QString::fromUtf8("\\");
1131 #else
1132         delim = QString::fromUtf8("/");
1133 #endif
1134         QString tmp_path = QString::fromUtf8(file);
1135         int n = tmp_path.lastIndexOf(delim);
1136         if(n > 0) {
1137                 tmp_path = tmp_path.left(n);
1138                 tmp_path.append(delim);
1139         }
1140         //printf("%s\n", tmp_path.toUtf8().constData());
1141         memset(path[output_index], 0x00, sizeof(_TCHAR) * _MAX_PATH);
1142         strncpy(path[output_index], tmp_path.toUtf8().constData(), _MAX_PATH - 1);
1143 #else
1144         // write code for your environment
1145 #endif
1146         return path[output_index];
1147 }
1148
1149 const wchar_t *DLL_PREFIX char_to_wchar(const char *cs)
1150 {
1151         // char to wchar_t
1152         static wchar_t ws[4096];
1153         
1154 #if defined(_WIN32) || defined(_USE_QT)
1155         mbstowcs(ws, cs, strlen(cs));
1156 #else
1157         // write code for your environment
1158 #endif
1159         return ws;
1160 }
1161
1162 const char *DLL_PREFIX wchar_to_char(const wchar_t *ws)
1163 {
1164         // wchar_t to char
1165         static char cs[4096];
1166         
1167 #ifdef _WIN32
1168         wcstombs(cs, ws, wcslen(ws));
1169 #elif defined(_USE_QT)
1170         wcstombs(cs, ws, wcslen(ws));
1171 #else
1172         // write code for your environment
1173 #endif
1174         return cs;
1175 }
1176
1177 const _TCHAR *DLL_PREFIX char_to_tchar(const char *cs)
1178 {
1179 #if defined(_UNICODE) && defined(SUPPORT_TCHAR_TYPE)
1180         // char to wchar_t
1181         return char_to_wchar(cs);
1182 #else
1183         // char to char
1184         return cs;
1185 #endif
1186 }
1187
1188 const char *DLL_PREFIX tchar_to_char(const _TCHAR *ts)
1189 {
1190 #if defined(_UNICODE) && defined(SUPPORT_TCHAR_TYPE)
1191         // wchar_t to char
1192         return wchar_to_char(ts);
1193 #else
1194         // char to char
1195         return ts;
1196 #endif
1197 }
1198
1199 const _TCHAR *DLL_PREFIX wchar_to_tchar(const wchar_t *ws)
1200 {
1201 #if defined(_UNICODE) && defined(SUPPORT_TCHAR_TYPE)
1202         // wchar_t to wchar_t
1203         return ws;
1204 #else
1205         // wchar_t to char
1206         return wchar_to_char(ws);
1207 #endif
1208 }
1209
1210 const wchar_t *DLL_PREFIX tchar_to_wchar(const _TCHAR *ts)
1211 {
1212 #if defined(_UNICODE) && defined(SUPPORT_TCHAR_TYPE)
1213         // wchar_t to wchar_t
1214         return ts;
1215 #else
1216         // char to wchar_t
1217         return char_to_wchar(ts);
1218 #endif
1219 }
1220
1221 const _TCHAR *DLL_PREFIX create_string(const _TCHAR* format, ...)
1222 {
1223         static _TCHAR buffer[8][1024];
1224         static unsigned int table_index = 0;
1225         unsigned int output_index = (table_index++) & 7;
1226         va_list ap;
1227         
1228         va_start(ap, format);
1229         my_vstprintf_s(buffer[output_index], 1024, format, ap);
1230         va_end(ap);
1231         return (const _TCHAR *)buffer[output_index];
1232 }
1233
1234 int32_t DLL_PREFIX muldiv_s32(int32_t nNumber, int32_t nNumerator, int32_t nDenominator)
1235 {
1236         try {
1237                 int64_t tmp;
1238                 tmp  = (int64_t)nNumber;
1239                 tmp *= (int64_t)nNumerator;
1240                 tmp /= (int64_t)nDenominator;
1241                 return (int32_t)tmp;
1242         } catch(...) {
1243                 double tmp;
1244                 tmp  = (double)nNumber;
1245                 tmp *= (double)nNumerator;
1246                 tmp /= (double)nDenominator;
1247                 if(tmp < 0) {
1248                         return (int32_t)(tmp - 0.5);
1249                 } else {
1250                         return (int32_t)(tmp + 0.5);
1251                 }
1252         }
1253 }
1254
1255 uint32_t DLL_PREFIX muldiv_u32(uint32_t nNumber, uint32_t nNumerator, uint32_t nDenominator)
1256 {
1257         try {
1258                 uint64_t tmp;
1259                 tmp  = (uint64_t)nNumber;
1260                 tmp *= (uint64_t)nNumerator;
1261                 tmp /= (uint64_t)nDenominator;
1262                 return (uint32_t)tmp;
1263         } catch(...) {
1264                 double tmp;
1265                 tmp  = (double)nNumber;
1266                 tmp *= (double)nNumerator;
1267                 tmp /= (double)nDenominator;
1268                 return (uint32_t)(tmp + 0.5);
1269         }
1270 }
1271
1272 static bool _crc_initialized = false;
1273 static uint32_t _crc_table[256] = {0};
1274 static void init_crc32_table(void)
1275 {
1276         for(int i = 0; i < 256; i++) {
1277                 uint32_t c = i;
1278                 for(int j = 0; j < 8; j++) {
1279                         if(c & 1) {
1280                                 c = (c >> 1) ^ 0xedb88320;
1281                         } else {
1282                                 c >>= 1;
1283                         }
1284                 }
1285                 _crc_table[i] = c;
1286         }
1287         _crc_initialized = true;
1288 }
1289
1290 uint32_t DLL_PREFIX get_crc32(uint8_t data[], int size)
1291 {
1292         const uint32_t *table = (const uint32_t *)_crc_table;
1293         if(!_crc_initialized) {
1294                 init_crc32_table();
1295         }
1296         
1297         uint32_t c = ~0;
1298         for(int i = 0; i < size; i++) {
1299                 c = table[(c ^ data[i]) & 0xff] ^ (c >> 8);
1300         }
1301         return ~c;
1302 }
1303
1304 uint32_t DLL_PREFIX calc_crc32(uint32_t seed, uint8_t data[], int size)
1305 {
1306 #if 0
1307         if(!_crc_initialized) {
1308                 init_crc32_table();
1309         }
1310         const uint32_t *table = (const uint32_t *)_crc_table;
1311
1312         uint32_t c = ~seed;
1313         for(int i = 0; i < size; i++) {
1314                 c = table[(c ^ data[i]) & 0xff] ^ (c >> 8);
1315         }
1316         return ~c;
1317 #else
1318         // Calculate CRC32
1319         // Refer to : https://qiita.com/mikecat_mixc/items/e5d236e3a3803ef7d3c5
1320         static const uint32_t CRC_MAGIC_WORD = 0x04C11DB7;
1321         uint32_t crc = seed;
1322         uint8_t *ptr = data;
1323         uint8_t d;
1324         int bytes = size;
1325         bool is_overflow;
1326         for(int i = 0; i < bytes; i++) {
1327                 d = *ptr++;
1328                 for(int bit = 0; bit < 8; bit++) {
1329                         is_overflow = ((crc & 0x1) != 0);
1330                         crc = crc >> 1;
1331                         if((d & 0x01) != 0) crc = crc | 0x80000000;
1332                         if(is_overflow) crc = crc ^ ((uint32_t)~CRC_MAGIC_WORD);
1333                         d >>= 1;
1334                 }
1335         }
1336         return crc;
1337 #endif
1338 }
1339
1340 uint16_t DLL_PREFIX jis_to_sjis(uint16_t jis)
1341 {
1342         pair_t tmp;
1343         
1344         tmp.w.l = jis - 0x2121;
1345         if(tmp.w.l & 0x100) {
1346                 tmp.w.l += 0x9e;
1347         } else {
1348                 tmp.w.l += 0x40;
1349         }
1350         if(tmp.b.l > 0x7f) {
1351                 tmp.w.l += 0x01;
1352         }
1353         tmp.b.h = (tmp.b.h >> 1) + 0x81;
1354         if(tmp.w.l >= 0xa000) {
1355                 tmp.w.l += 0x4000;
1356         }
1357         return tmp.w.l;
1358 }
1359
1360 int DLL_PREFIX decibel_to_volume(int decibel)
1361 {
1362         // +1 equals +0.5dB (same as fmgen)
1363         return (int)(1024.0 * pow(10.0, decibel / 40.0) + 0.5);
1364 }
1365
1366 int32_t DLL_PREFIX apply_volume(int32_t sample, int volume)
1367 {
1368 //      int64_t output;
1369         int32_t output;
1370         if(sample < 0) {
1371                 output = -sample;
1372                 output *= volume;
1373                 output >>= 10;
1374                 output = -output;
1375         } else {
1376                 output = sample;
1377                 output *= volume;
1378                 output >>= 10;
1379         }
1380 //      if(output > 2147483647) {
1381 //              return 2147483647;
1382 //      } else if(output < (-2147483647 - 1)) {
1383 //              return (-2147483647 - 1);
1384 //      } else {
1385 //              return (int32_t)output;
1386 //      }
1387         return output;
1388 }
1389
1390 void DLL_PREFIX get_host_time(cur_time_t* cur_time)
1391 {
1392 #ifdef _WIN32
1393         SYSTEMTIME sTime;
1394         GetLocalTime(&sTime);
1395         cur_time->year = sTime.wYear;
1396         cur_time->month = sTime.wMonth;
1397         cur_time->day = sTime.wDay;
1398         cur_time->day_of_week = sTime.wDayOfWeek;
1399         cur_time->hour = sTime.wHour;
1400         cur_time->minute = sTime.wMinute;
1401         cur_time->second = sTime.wSecond;
1402 #else
1403         time_t timer = time(NULL);
1404         struct tm *local = localtime(&timer);
1405         cur_time->year = local->tm_year + 1900;
1406         cur_time->month = local->tm_mon + 1;
1407         cur_time->day = local->tm_mday;
1408         cur_time->day_of_week = local->tm_wday;
1409         cur_time->hour = local->tm_hour;
1410         cur_time->minute = local->tm_min;
1411         cur_time->second = local->tm_sec;
1412 #endif
1413 }
1414
1415
1416
1417 void DLL_PREFIX cur_time_t::increment()
1418 {
1419         if(++second >= 60) {
1420                 second = 0;
1421                 if(++minute >= 60) {
1422                         minute = 0;
1423                         if(++hour >= 24) {
1424                                 hour = 0;
1425                                 // days in this month
1426                                 int days = 31;
1427                                 if(month == 2) {
1428                                         days = LEAP_YEAR(year) ? 29 : 28;
1429                                 } else if(month == 4 || month == 6 || month == 9 || month == 11) {
1430                                         days = 30;
1431                                 }
1432                                 if(++day > days) {
1433                                         day = 1;
1434                                         if(++month > 12) {
1435                                                 month = 1;
1436                                                 year++;
1437                                         }
1438                                 }
1439                                 if(++day_of_week >= 7) {
1440                                         day_of_week = 0;
1441                                 }
1442                         }
1443                 }
1444         }
1445 }
1446
1447 void DLL_PREFIX cur_time_t::update_year()
1448 {
1449         // 1970-2069
1450         if(year < 70) {
1451                 year += 2000;
1452         } else if(year < 100) {
1453                 year += 1900;
1454         }
1455 }
1456
1457 void DLL_PREFIX cur_time_t::update_day_of_week()
1458 {
1459         static const int t[] = {0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4};
1460         int y = year - (month < 3);
1461         day_of_week = (y + y / 4 - y / 100 + y / 400 + t[month - 1] + day) % 7;
1462 }
1463
1464 #define STATE_VERSION   1
1465
1466
1467 bool DLL_PREFIX cur_time_t::process_state(void *f, bool loading)
1468 {
1469         FILEIO *state_fio = (FILEIO *)f;
1470         
1471         if(!state_fio->StateCheckUint32(STATE_VERSION)) {
1472                 return false;
1473         }
1474         state_fio->StateInt32(year);
1475         state_fio->StateInt32(month);
1476         state_fio->StateInt32(day);
1477         state_fio->StateInt32(day_of_week);
1478         state_fio->StateInt32(hour);
1479         state_fio->StateInt32(minute);
1480         state_fio->StateInt32(second);
1481         state_fio->StateBool(initialized);
1482         return true;
1483 }
1484
1485 const _TCHAR *DLL_PREFIX get_symbol(symbol_t *first_symbol, uint32_t addr)
1486 {
1487         static _TCHAR name[8][1024];
1488         static unsigned int table_index = 0;
1489         unsigned int output_index = (table_index++) & 7;
1490         
1491         if(first_symbol != NULL) {
1492                 for(symbol_t* symbol = first_symbol; symbol; symbol = symbol->next_symbol) {
1493                         if(symbol->addr == addr) {
1494                                 my_tcscpy_s(name[output_index], 1024, symbol->name);
1495                                 return name[output_index];
1496                         }
1497                 }
1498         }
1499         return NULL;
1500 }
1501
1502 const _TCHAR *DLL_PREFIX get_value_or_symbol(symbol_t *first_symbol, const _TCHAR *format, uint32_t addr)
1503 {
1504         static _TCHAR name[8][1024];
1505         static unsigned int table_index = 0;
1506         unsigned int output_index = (table_index++) & 7;
1507         
1508         if(first_symbol != NULL) {
1509                 for(symbol_t* symbol = first_symbol; symbol; symbol = symbol->next_symbol) {
1510                         if(symbol->addr == addr) {
1511                                 my_tcscpy_s(name[output_index], 1024, symbol->name);
1512                                 return name[output_index];
1513                         }
1514                 }
1515         }
1516         my_stprintf_s(name[output_index], 1024, format, addr);
1517         return name[output_index];
1518 }
1519
1520 const _TCHAR *DLL_PREFIX get_value_and_symbol(symbol_t *first_symbol, const _TCHAR *format, uint32_t addr)
1521 {
1522         static _TCHAR name[8][1024];
1523         static unsigned int table_index = 0;
1524         unsigned int output_index = (table_index++) & 7;
1525         
1526         my_stprintf_s(name[output_index], 1024, format, addr);
1527         
1528         if(first_symbol != NULL) {
1529                 for(symbol_t* symbol = first_symbol; symbol; symbol = symbol->next_symbol) {
1530                         if(symbol->addr == addr) {
1531                                 _TCHAR temp[1024];
1532 //                              my_stprintf_s(temp, 1024, _T(" (%s)"), symbol->name);
1533                                 my_stprintf_s(temp, 1024, _T(";%s"), symbol->name);
1534                                 my_tcscat_s(name[output_index], 1024, temp);
1535                                 return name[output_index];
1536                         }
1537                 }
1538         }
1539         return name[output_index];
1540 }
1541
1542 // Use this before writing wav_data.
1543 bool DLL_PREFIX write_dummy_wav_header(void *__fio)
1544 {
1545         if(__fio == NULL) return false;
1546
1547         FILEIO *fio = (FILEIO *)__fio;
1548         uint8_t dummy[sizeof(wav_header_t) + sizeof(wav_chunk_t)];
1549
1550         if(!fio->IsOpened()) return false;
1551         
1552         memset(dummy, 0, sizeof(dummy));
1553         fio->Fwrite(dummy, sizeof(dummy), 1);
1554         return true;
1555 }
1556 // Use this after writing wav_data.
1557 bool DLL_PREFIX set_wav_header(wav_header_t *header, wav_chunk_t *first_chunk, uint16_t channels, uint32_t rate,
1558                                                            uint16_t bits, size_t file_length)
1559 {
1560         uint32_t length = (uint32_t) file_length;
1561         
1562         if(header == NULL) return false;
1563         if(first_chunk == NULL) return false;
1564
1565         pair_t __riff_chunk_size;
1566         pair_t __fmt_chunk_size;
1567         pair_t __wav_chunk_size;
1568         pair16_t __fmt_id;
1569         pair16_t __channels;
1570         pair_t __sample_rate;
1571         pair_t __data_speed;
1572         pair16_t __block_size;
1573         pair16_t __sample_bits;
1574
1575         __riff_chunk_size.d = length - 8;
1576         __fmt_chunk_size.d = 16;
1577         __fmt_id.u16 = 1;
1578         __channels.u16 = channels;
1579         __sample_rate.d = rate;
1580         __block_size.u16 = (uint16_t)((channels * bits) / 8);
1581         __sample_bits.u16 = bits;
1582         __data_speed.d = rate * (uint32_t)(__block_size.u16);
1583
1584         memcpy(&(header->riff_chunk.id), "RIFF", 4);
1585         header->riff_chunk.size = __riff_chunk_size.get_4bytes_le_to();
1586         
1587         memcpy(&(header->wave), "WAVE", 4);
1588         memcpy(&(header->fmt_chunk.id), "fmt ", 4);
1589         header->fmt_chunk.size = __fmt_chunk_size.get_4bytes_le_to();
1590         header->format_id = __fmt_id.get_2bytes_le_to();
1591         header->channels = __channels.get_2bytes_le_to();
1592         header->sample_rate = __sample_rate.get_4bytes_le_to();
1593         header->data_speed =  __data_speed.get_4bytes_le_to();
1594         header->block_size = __block_size.get_2bytes_le_to();
1595         header->sample_bits = __sample_bits.get_2bytes_le_to();
1596
1597         memcpy(&(first_chunk->id), "data", 4);
1598         __wav_chunk_size.d = length - sizeof(wav_header_t) - sizeof(wav_chunk_t);
1599         first_chunk->size = __wav_chunk_size.get_4bytes_le_to();
1600
1601         return true;
1602 }
1603 // Note: buffers are allocated by this, You should free() within user class.
1604 bool DLL_PREFIX load_wav_to_stereo(void *__fio, int16_t **left_buf, int16_t **right_buf, uint32_t *rate, int *got_samples)
1605 {
1606
1607         if(__fio == NULL) return false;
1608         if(left_buf == NULL) return false;
1609         if(right_buf == NULL) return false;
1610         if(rate == NULL) return false;
1611         if(got_samples == NULL) return false;
1612         //if((bits != 8) && (bits != 16) && (bits != 32)) return false;
1613
1614         FILEIO *fio = (FILEIO *)__fio;
1615         if(!fio->IsOpened()) return false;
1616
1617         
1618         int16_t *left_buffer = NULL;
1619         int16_t *right_buffer = NULL;
1620         size_t samples = 0;
1621         uint32_t sample_rate = 0;
1622         
1623         wav_header_t header;
1624         wav_chunk_t  chunk;
1625
1626         pair16_t __fmt_id;
1627         pair16_t __sample_bits;
1628         pair16_t __channels;
1629         pair_t __sample_rate;
1630         pair_t __chunk_size;
1631
1632         fio->Fread(&header, sizeof(header), 1);
1633         __fmt_id.set_2bytes_le_from(header.format_id);
1634         __sample_bits.set_2bytes_le_from(header.sample_bits);
1635         __chunk_size.set_4bytes_le_from(header.fmt_chunk.size);
1636         __channels.set_2bytes_le_from(header.channels);
1637         __sample_rate.set_4bytes_le_from(header.sample_rate);
1638
1639         if((__fmt_id.u16 == 1) && ((__sample_bits.u16 == 8) || (__sample_bits.u16 == 16) || (__sample_bits.u16 == 32))) {
1640                 fio->Fseek(__chunk_size.d - 16, FILEIO_SEEK_CUR);
1641                 bool is_eof = false;
1642                 while(1) {
1643                         if(fio->Fread(&chunk, sizeof(chunk), 1) != 1) {
1644                                 is_eof = true;
1645                                 break;
1646                         }
1647                         __chunk_size.set_4bytes_le_from(chunk.size);
1648                         if(strncmp(chunk.id, "data", 4) == 0) {
1649                                 break;
1650                         }
1651                         fio->Fseek(__chunk_size.d, FILEIO_SEEK_CUR);
1652                 }
1653                 __chunk_size.set_4bytes_le_from(chunk.size);
1654                 if(is_eof) {
1655                         fio->Fclose();
1656                         delete fio;
1657                         return false;
1658                 }
1659                 
1660                 samples = (size_t)(__chunk_size.d / __channels.u16);
1661                 int16_t data_l, data_r;
1662                 union {
1663                         int16_t s16;
1664                         struct {
1665                                 uint8_t l, h;
1666                         } b;
1667                 } pair16;
1668                 union {
1669                         int32_t s32;
1670                         struct {
1671                                 uint8_t l, h, h2, h3;
1672                         } b;
1673                 } pair32;
1674                 
1675                 if(samples > 0) {
1676                         if(__sample_bits.u16 == 16) {
1677                                 samples /= 2;
1678                         } else if(__sample_bits.u16 == 32) {
1679                                 samples /= 4;
1680                         }
1681                         if(samples == 0) return false;
1682                         sample_rate = __sample_rate.d;
1683
1684                         left_buffer = (int16_t *)malloc(samples * sizeof(int16_t));
1685                         right_buffer = (int16_t *)malloc(samples * sizeof(int16_t));
1686                         if(left_buffer == NULL) {
1687                                 if(right_buffer != NULL) free(right_buffer);
1688                                 return false;
1689                         }
1690                         if(right_buffer == NULL) {
1691                                 if(left_buffer != NULL) free(left_buffer);
1692                                 return false;
1693                         }
1694                         switch(__sample_bits.u16) {
1695                         case 8:
1696                                 if(__channels.s16 == 1) {
1697                                         for(int i = 0; i < samples; i++) {
1698                                                 data_l = (int16_t)(fio->FgetUint8());
1699                                                 data_l = (data_l - 128) * 256;
1700                                                 left_buffer[i] = data_l;
1701                                                 right_buffer[i] = data_l;
1702                                         }
1703                                 } else if(__channels.s16 == 2) {
1704                                         for(int i = 0; i < samples; i++) {
1705                                                 data_l = (int16_t)(fio->FgetUint8());
1706                                                 data_l = (data_l - 128) * 256;
1707                                                 data_r = (int16_t)(fio->FgetUint8());
1708                                                 data_r = (data_r - 128) * 256;
1709                                                 left_buffer[i] = data_l;
1710                                                 right_buffer[i] = data_r;
1711                                         }
1712                                 }
1713                                 break;
1714                         case 16:
1715                                 if(__channels.s16 == 1) {
1716                                         for(int i = 0; i < samples; i++) {
1717                                                 pair16.b.l = fio->FgetUint8();
1718                                                 pair16.b.h = fio->FgetUint8();
1719                                                 data_l = pair16.s16;
1720                                                 
1721                                                 left_buffer[i] = data_l;
1722                                                 right_buffer[i] = data_l;
1723                                         }
1724                                 } else if(__channels.s16 == 2) {
1725                                         for(int i = 0; i < samples; i++) {
1726                                                 pair16.b.l = fio->FgetUint8();
1727                                                 pair16.b.h = fio->FgetUint8();
1728                                                 data_l = pair16.s16;
1729                                                 
1730                                                 pair16.b.l = fio->FgetUint8();
1731                                                 pair16.b.h = fio->FgetUint8();
1732                                                 data_r = pair16.s16;
1733                                                 left_buffer[i] = data_l;
1734                                                 right_buffer[i] = data_r;
1735                                         }
1736                                 }
1737                                 break;
1738                         case 32:
1739                                 if(__channels.s16 == 1) {
1740                                         for(int i = 0; i < samples; i++) {
1741                                                 pair32.b.l = fio->FgetUint8();
1742                                                 pair32.b.h = fio->FgetUint8();
1743                                                 pair32.b.h2 = fio->FgetUint8();
1744                                                 pair32.b.h3 = fio->FgetUint8();
1745                                                 data_l = (int16_t)(pair32.s32 / 65536);
1746                                                 
1747                                                 left_buffer[i] = data_l;
1748                                                 right_buffer[i] = data_l;
1749                                         }
1750                                 } else if(__channels.s16 == 2) {
1751                                         for(int i = 0; i < samples; i++) {
1752                                                 pair32.b.l = fio->FgetUint8();
1753                                                 pair32.b.h = fio->FgetUint8();
1754                                                 pair32.b.h2 = fio->FgetUint8();
1755                                                 pair32.b.h3 = fio->FgetUint8();
1756                                                 data_l = (int16_t)(pair32.s32 / 65536);
1757                                                 
1758                                                 pair32.b.l = fio->FgetUint8();
1759                                                 pair32.b.h = fio->FgetUint8();
1760                                                 pair32.b.h2 = fio->FgetUint8();
1761                                                 pair32.b.h3 = fio->FgetUint8();
1762                                                 data_r = (int16_t)(pair32.s32 / 65536);
1763                                                 
1764                                                 left_buffer[i] = data_l;
1765                                                 right_buffer[i] = data_r;
1766                                         }
1767                                 }
1768                                 break;
1769                         default:
1770                                 break;
1771                         }
1772                 }
1773         } else {
1774                 return false;
1775         }
1776         *left_buf = left_buffer;
1777         *right_buf = right_buffer;
1778         *rate = sample_rate;
1779         *got_samples = (int)samples;
1780         return true;
1781 }
1782
1783 bool DLL_PREFIX load_wav_to_monoral(void *__fio, int16_t **buffer, uint32_t *rate, int *got_samples)
1784 {
1785
1786         if(__fio == NULL) return false;
1787         if(buffer == NULL) return false;
1788         if(rate == NULL) return false;
1789         if(got_samples == NULL) return false;
1790         //if((bits != 8) && (bits != 16) && (bits != 32)) return false;
1791
1792         FILEIO *fio = (FILEIO *)__fio;
1793         if(!fio->IsOpened()) return false;
1794
1795         
1796         int16_t *left_buffer = NULL;
1797         size_t samples = 0;
1798         uint32_t sample_rate = 0;
1799         
1800         wav_header_t header;
1801         wav_chunk_t  chunk;
1802
1803         pair16_t __fmt_id;
1804         pair16_t __sample_bits;
1805         pair16_t __channels;
1806         pair_t __sample_rate;
1807         pair_t __chunk_size;
1808
1809         fio->Fread(&header, sizeof(header), 1);
1810         __fmt_id.set_2bytes_le_from(header.format_id);
1811         __sample_bits.set_2bytes_le_from(header.sample_bits);
1812         __chunk_size.set_4bytes_le_from(header.fmt_chunk.size);
1813         __channels.set_2bytes_le_from(header.channels);
1814         __sample_rate.set_4bytes_le_from(header.sample_rate);
1815
1816         if((__fmt_id.u16 == 1) && ((__sample_bits.u16 == 8) || (__sample_bits.u16 == 16) || (__sample_bits.u16 == 32))) {
1817                 fio->Fseek(__chunk_size.d - 16, FILEIO_SEEK_CUR);
1818                 bool is_eof = false;
1819                 while(1) {
1820                         if(fio->Fread(&chunk, sizeof(chunk), 1) != 1) {
1821                                 is_eof = true;
1822                                 break;
1823                         }
1824                         __chunk_size.set_4bytes_le_from(chunk.size);
1825                         if(strncmp(chunk.id, "data", 4) == 0) {
1826                                 break;
1827                         }
1828                         fio->Fseek(__chunk_size.d, FILEIO_SEEK_CUR);
1829                 }
1830                 __chunk_size.set_4bytes_le_from(chunk.size);
1831                 if(is_eof) {
1832                         fio->Fclose();
1833                         delete fio;
1834                         return false;
1835                 }
1836                 
1837                 samples = (size_t)(__chunk_size.d / __channels.u16);
1838                 int16_t data_l, data_r;
1839                 int32_t data32_l, data32_r;
1840                 union {
1841                         int16_t s16;
1842                         struct {
1843                                 uint8_t l, h;
1844                         } b;
1845                 } pair16;
1846                 union {
1847                         int32_t s32;
1848                         struct {
1849                                 uint8_t l, h, h2, h3;
1850                         } b;
1851                 } pair32;
1852                 
1853                 if(samples > 0) {
1854                         if(__sample_bits.u16 == 16) {
1855                                 samples /= 2;
1856                         } else if(__sample_bits.u16 == 32) {
1857                                 samples /= 4;
1858                         }
1859                         if(samples == 0) return false;
1860                         sample_rate = __sample_rate.d;
1861
1862                         left_buffer = (int16_t *)malloc(samples * sizeof(int16_t));
1863                         if(left_buffer == NULL) {
1864                                 return false;
1865                         }
1866                         switch(__sample_bits.u16) {
1867                         case 8:
1868                                 if(__channels.s16 == 1) {
1869                                         for(int i = 0; i < samples; i++) {
1870                                                 data_l = (int16_t)(fio->FgetUint8());
1871                                                 data_l = (data_l - 128) * 256;
1872                                                 left_buffer[i] = data_l;
1873                                         }
1874                                 } else if(__channels.s16 == 2) {
1875                                         for(int i = 0; i < samples; i++) {
1876                                                 data_l = (int16_t)(fio->FgetUint8());
1877                                                 data_l = (data_l - 128) * 256;
1878                                                 data_r = (int16_t)(fio->FgetUint8());
1879                                                 data_r = (data_r - 128) * 256;
1880                                                 left_buffer[i] = (data_l + data_r) / 2;
1881                                         }
1882                                 }
1883                                 break;
1884                         case 16:
1885                                 if(__channels.s16 == 1) {
1886                                         for(int i = 0; i < samples; i++) {
1887                                                 pair16.b.l = fio->FgetUint8();
1888                                                 pair16.b.h = fio->FgetUint8();
1889                                                 data_l = pair16.s16;
1890                                                 
1891                                                 left_buffer[i] = data_l;
1892                                         }
1893                                 } else if(__channels.s16 == 2) {
1894                                         for(int i = 0; i < samples; i++) {
1895                                                 pair16.b.l = fio->FgetUint8();
1896                                                 pair16.b.h = fio->FgetUint8();
1897                                                 data_l = pair16.s16;
1898                                                 
1899                                                 pair16.b.l = fio->FgetUint8();
1900                                                 pair16.b.h = fio->FgetUint8();
1901                                                 data_r = pair16.s16;
1902                                                 left_buffer[i] = (data_l + data_r) / 2;
1903                                         }
1904                                 }
1905                                 break;
1906                         case 32:
1907                                 if(__channels.s16 == 1) {
1908                                         for(int i = 0; i < samples; i++) {
1909                                                 pair32.b.l = fio->FgetUint8();
1910                                                 pair32.b.h = fio->FgetUint8();
1911                                                 pair32.b.h2 = fio->FgetUint8();
1912                                                 pair32.b.h3 = fio->FgetUint8();
1913                                                 data_l = (int16_t)(pair32.s32 / 65536);
1914                                                 
1915                                                 left_buffer[i] = data_l;
1916                                         }
1917                                 } else if(__channels.s16 == 2) {
1918                                         for(int i = 0; i < samples; i++) {
1919                                                 pair32.b.l = fio->FgetUint8();
1920                                                 pair32.b.h = fio->FgetUint8();
1921                                                 pair32.b.h2 = fio->FgetUint8();
1922                                                 pair32.b.h3 = fio->FgetUint8();
1923                                                 data32_l = pair32.s32 / 65536;
1924                                                 
1925                                                 pair32.b.l = fio->FgetUint8();
1926                                                 pair32.b.h = fio->FgetUint8();
1927                                                 pair32.b.h2 = fio->FgetUint8();
1928                                                 pair32.b.h3 = fio->FgetUint8();
1929                                                 data32_r = pair32.s32 / 65536;
1930                                                 
1931                                                 left_buffer[i] = (int16_t)((data32_l + data32_r) / 2);
1932                                         }
1933                                 }
1934                                 break;
1935                         default:
1936                                 break;
1937                         }
1938                 }
1939         } else {
1940                 return false;
1941         }
1942         *buffer = left_buffer;
1943         *rate = sample_rate;
1944         *got_samples = (int)samples;
1945         return true;
1946 }
1947
1948 DLL_PREFIX const _TCHAR *get_lib_common_version()
1949 {
1950 #if defined(__LIBEMU_UTIL_VERSION)
1951         return (const _TCHAR *)__LIBEMU_UTIL_VERSION;
1952 #else
1953         return (const _TCHAR *)"\0";
1954 #endif
1955 }