OSDN Git Service

[CONFIG][OpenGL] Fix not effective config for renderer.
[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 #include "./state_data.h"
1467
1468 void DLL_PREFIX cur_time_t::save_state_helper(void *f, uint32_t *sumseed, bool *__stat)
1469 {
1470         csp_state_data_saver *state_saver = (csp_state_data_saver *)f;
1471         const _TCHAR *_ns = "cur_time_t::BEGIN";
1472         const _TCHAR *_ne = "cur_time_t::END";
1473         
1474         if(f == NULL) return;
1475         
1476         state_saver->save_string_data(_ns, sumseed, strlen(_ns) + 1, __stat);
1477         state_saver->put_dword(STATE_VERSION, sumseed, __stat);
1478
1479         state_saver->put_int32(year, sumseed, __stat);
1480         state_saver->put_int8((int8_t)month, sumseed, __stat);
1481         state_saver->put_int8((int8_t)day, sumseed, __stat);
1482         state_saver->put_int8((int8_t)day_of_week, sumseed, __stat);
1483         state_saver->put_int8((int8_t)hour, sumseed, __stat);
1484         state_saver->put_int8((int8_t)minute, sumseed, __stat);
1485         state_saver->put_int16((int16_t)second, sumseed, __stat);
1486         state_saver->put_bool(initialized, sumseed, __stat);
1487         
1488         state_saver->save_string_data(_ne, sumseed, strlen(_ne) + 1, __stat);
1489 }
1490
1491 bool DLL_PREFIX cur_time_t::load_state_helper(void *f, uint32_t *sumseed, bool *__stat)
1492 {
1493         csp_state_data_saver *state_saver = (csp_state_data_saver *)f;
1494         const _TCHAR *_ns = "cur_time_t::BEGIN";
1495         const _TCHAR *_ne = "cur_time_t::END";
1496         _TCHAR sbuf[128];
1497         uint32_t tmpvar;
1498
1499         if(f == NULL) return false;
1500         memset(sbuf, 0x00, sizeof(sbuf));
1501         state_saver->load_string_data(sbuf, sumseed, strlen(_ns) + 1, __stat);
1502         tmpvar = state_saver->get_dword(sumseed, __stat);
1503         if(strncmp(sbuf, _ns, strlen(_ns) + 1) != 0) {
1504                 if(__stat != NULL) *__stat = false;
1505                 return false;
1506         }
1507         if(tmpvar != STATE_VERSION) {
1508                 if(__stat != NULL) *__stat = false;
1509                 return false;
1510         }
1511         year =              state_saver->get_int32(sumseed, __stat);
1512         month =       (int)(state_saver->get_int8(sumseed, __stat));
1513         day =         (int)(state_saver->get_int8(sumseed, __stat));
1514         day_of_week = (int)(state_saver->get_int8(sumseed, __stat));
1515         hour =        (int)(state_saver->get_int8(sumseed, __stat));
1516         minute =      (int)(state_saver->get_int8(sumseed, __stat));
1517         second =      (int)(state_saver->get_int16(sumseed, __stat));
1518         initialized = state_saver->get_bool(sumseed, __stat);
1519         
1520         memset(sbuf, 0x00, sizeof(sbuf));
1521         state_saver->load_string_data(sbuf, sumseed, strlen(_ne) + 1, __stat);
1522         if(strncmp(_ne, sbuf, strlen(_ne) + 1) != 0) {
1523                 if(__stat != NULL) *__stat = false;
1524                 return false;
1525         }
1526         
1527         if(__stat != NULL) {
1528                 if(*__stat == false) return false;
1529                 //*__stat = true;
1530         }
1531         return true;
1532 }
1533
1534 void DLL_PREFIX cur_time_t::save_state(void *f)
1535 {
1536         FILEIO *state_fio = (FILEIO *)f;
1537         
1538         state_fio->FputUint32(STATE_VERSION);
1539         
1540         state_fio->FputInt32(year);
1541         state_fio->FputInt32(month);
1542         state_fio->FputInt32(day);
1543         state_fio->FputInt32(day_of_week);
1544         state_fio->FputInt32(hour);
1545         state_fio->FputInt32(minute);
1546         state_fio->FputInt32(second);
1547         state_fio->FputBool(initialized);
1548 }
1549
1550 bool DLL_PREFIX cur_time_t::load_state(void *f)
1551 {
1552         FILEIO *state_fio = (FILEIO *)f;
1553         
1554         if(state_fio->FgetUint32() != STATE_VERSION) {
1555                 return false;
1556         }
1557         year = state_fio->FgetInt32();
1558         month = state_fio->FgetInt32();
1559         day = state_fio->FgetInt32();
1560         day_of_week = state_fio->FgetInt32();
1561         hour = state_fio->FgetInt32();
1562         minute = state_fio->FgetInt32();
1563         second = state_fio->FgetInt32();
1564         initialized = state_fio->FgetBool();
1565         return true;
1566 }
1567
1568 const _TCHAR *DLL_PREFIX get_symbol(symbol_t *first_symbol, uint32_t addr)
1569 {
1570         static _TCHAR name[8][1024];
1571         static unsigned int table_index = 0;
1572         unsigned int output_index = (table_index++) & 7;
1573         
1574         if(first_symbol != NULL) {
1575                 for(symbol_t* symbol = first_symbol; symbol; symbol = symbol->next_symbol) {
1576                         if(symbol->addr == addr) {
1577                                 my_tcscpy_s(name[output_index], 1024, symbol->name);
1578                                 return name[output_index];
1579                         }
1580                 }
1581         }
1582         return NULL;
1583 }
1584
1585 const _TCHAR *DLL_PREFIX get_value_or_symbol(symbol_t *first_symbol, const _TCHAR *format, uint32_t addr)
1586 {
1587         static _TCHAR name[8][1024];
1588         static unsigned int table_index = 0;
1589         unsigned int output_index = (table_index++) & 7;
1590         
1591         if(first_symbol != NULL) {
1592                 for(symbol_t* symbol = first_symbol; symbol; symbol = symbol->next_symbol) {
1593                         if(symbol->addr == addr) {
1594                                 my_tcscpy_s(name[output_index], 1024, symbol->name);
1595                                 return name[output_index];
1596                         }
1597                 }
1598         }
1599         my_stprintf_s(name[output_index], 1024, format, addr);
1600         return name[output_index];
1601 }
1602
1603 const _TCHAR *DLL_PREFIX get_value_and_symbol(symbol_t *first_symbol, const _TCHAR *format, uint32_t addr)
1604 {
1605         static _TCHAR name[8][1024];
1606         static unsigned int table_index = 0;
1607         unsigned int output_index = (table_index++) & 7;
1608         
1609         my_stprintf_s(name[output_index], 1024, format, addr);
1610         
1611         if(first_symbol != NULL) {
1612                 for(symbol_t* symbol = first_symbol; symbol; symbol = symbol->next_symbol) {
1613                         if(symbol->addr == addr) {
1614                                 _TCHAR temp[1024];
1615 //                              my_stprintf_s(temp, 1024, _T(" (%s)"), symbol->name);
1616                                 my_stprintf_s(temp, 1024, _T(";%s"), symbol->name);
1617                                 my_tcscat_s(name[output_index], 1024, temp);
1618                                 return name[output_index];
1619                         }
1620                 }
1621         }
1622         return name[output_index];
1623 }
1624
1625 // Use this before writing wav_data.
1626 bool DLL_PREFIX write_dummy_wav_header(void *__fio)
1627 {
1628         if(__fio == NULL) return false;
1629
1630         FILEIO *fio = (FILEIO *)__fio;
1631         uint8_t dummy[sizeof(wav_header_t) + sizeof(wav_chunk_t)];
1632
1633         if(!fio->IsOpened()) return false;
1634         
1635         memset(dummy, 0, sizeof(dummy));
1636         fio->Fwrite(dummy, sizeof(dummy), 1);
1637         return true;
1638 }
1639 // Use this after writing wav_data.
1640 bool DLL_PREFIX set_wav_header(wav_header_t *header, wav_chunk_t *first_chunk, uint16_t channels, uint32_t rate,
1641                                                            uint16_t bits, size_t file_length)
1642 {
1643         uint32_t length = (uint32_t) file_length;
1644         
1645         if(header == NULL) return false;
1646         if(first_chunk == NULL) return false;
1647
1648         pair_t __riff_chunk_size;
1649         pair_t __fmt_chunk_size;
1650         pair_t __wav_chunk_size;
1651         pair16_t __fmt_id;
1652         pair16_t __channels;
1653         pair_t __sample_rate;
1654         pair_t __data_speed;
1655         pair16_t __block_size;
1656         pair16_t __sample_bits;
1657
1658         __riff_chunk_size.d = length - 8;
1659         __fmt_chunk_size.d = 16;
1660         __fmt_id.w = 1;
1661         __channels.w = channels;
1662         __sample_rate.d = rate;
1663         __block_size.w = (uint16_t)((channels * bits) / 8);
1664         __sample_bits.w = bits;
1665         __data_speed.d = rate * (uint32_t)(__block_size.w);
1666
1667         memcpy(&(header->riff_chunk.id), "RIFF", 4);
1668         header->riff_chunk.size = __riff_chunk_size.get_4bytes_le_to();
1669         
1670         memcpy(&(header->wave), "WAVE", 4);
1671         memcpy(&(header->fmt_chunk.id), "fmt ", 4);
1672         header->fmt_chunk.size = __fmt_chunk_size.get_4bytes_le_to();
1673         header->format_id = __fmt_id.get_2bytes_le_to();
1674         header->channels = __channels.get_2bytes_le_to();
1675         header->sample_rate = __sample_rate.get_4bytes_le_to();
1676         header->data_speed =  __data_speed.get_4bytes_le_to();
1677         header->block_size = __block_size.get_2bytes_le_to();
1678         header->sample_bits = __sample_bits.get_2bytes_le_to();
1679
1680         memcpy(&(first_chunk->id), "data", 4);
1681         __wav_chunk_size.d = length - sizeof(wav_header_t) - sizeof(wav_chunk_t);
1682         first_chunk->size = __wav_chunk_size.get_4bytes_le_to();
1683
1684         return true;
1685 }
1686 // Note: buffers are allocated by this, You should free() within user class.
1687 bool DLL_PREFIX load_wav_to_stereo(void *__fio, int16_t **left_buf, int16_t **right_buf, uint32_t *rate, int *got_samples)
1688 {
1689
1690         if(__fio == NULL) return false;
1691         if(left_buf == NULL) return false;
1692         if(right_buf == NULL) return false;
1693         if(rate == NULL) return false;
1694         if(got_samples == NULL) return false;
1695         //if((bits != 8) && (bits != 16) && (bits != 32)) return false;
1696
1697         FILEIO *fio = (FILEIO *)__fio;
1698         if(!fio->IsOpened()) return false;
1699
1700         
1701         int16_t *left_buffer = NULL;
1702         int16_t *right_buffer = NULL;
1703         size_t samples = 0;
1704         uint32_t sample_rate = 0;
1705         
1706         wav_header_t header;
1707         wav_chunk_t  chunk;
1708
1709         pair16_t __fmt_id;
1710         pair16_t __sample_bits;
1711         pair16_t __channels;
1712         pair_t __sample_rate;
1713         pair_t __chunk_size;
1714
1715         fio->Fread(&header, sizeof(header), 1);
1716         __fmt_id.set_2bytes_le_from(header.format_id);
1717         __sample_bits.set_2bytes_le_from(header.sample_bits);
1718         __chunk_size.set_4bytes_le_from(header.fmt_chunk.size);
1719         __channels.set_2bytes_le_from(header.channels);
1720         __sample_rate.set_4bytes_le_from(header.sample_rate);
1721
1722         if((__fmt_id.w == 1) && ((__sample_bits.w == 8) || (__sample_bits.w == 16) || (__sample_bits.w == 32))) {
1723                 fio->Fseek(__chunk_size.d - 16, FILEIO_SEEK_CUR);
1724                 bool is_eof = false;
1725                 while(1) {
1726                         if(fio->Fread(&chunk, sizeof(chunk), 1) != 1) {
1727                                 is_eof = true;
1728                                 break;
1729                         }
1730                         __chunk_size.set_4bytes_le_from(chunk.size);
1731                         if(strncmp(chunk.id, "data", 4) == 0) {
1732                                 break;
1733                         }
1734                         fio->Fseek(__chunk_size.d, FILEIO_SEEK_CUR);
1735                 }
1736                 __chunk_size.set_4bytes_le_from(chunk.size);
1737                 if(is_eof) {
1738                         fio->Fclose();
1739                         delete fio;
1740                         return false;
1741                 }
1742                 
1743                 samples = (size_t)(__chunk_size.d / __channels.w);
1744                 int16_t data_l, data_r;
1745                 union {
1746                         int16_t s16;
1747                         struct {
1748                                 uint8_t l, h;
1749                         } b;
1750                 } pair16;
1751                 union {
1752                         int32_t s32;
1753                         struct {
1754                                 uint8_t l, h, h2, h3;
1755                         } b;
1756                 } pair32;
1757                 
1758                 if(samples > 0) {
1759                         if(__sample_bits.w == 16) {
1760                                 samples /= 2;
1761                         } else if(__sample_bits.w == 32) {
1762                                 samples /= 4;
1763                         }
1764                         if(samples == 0) return false;
1765                         sample_rate = __sample_rate.d;
1766
1767                         left_buffer = (int16_t *)malloc(samples * sizeof(int16_t));
1768                         right_buffer = (int16_t *)malloc(samples * sizeof(int16_t));
1769                         if(left_buffer == NULL) {
1770                                 if(right_buffer != NULL) free(right_buffer);
1771                                 return false;
1772                         }
1773                         if(right_buffer == NULL) {
1774                                 if(left_buffer != NULL) free(left_buffer);
1775                                 return false;
1776                         }
1777                         switch(__sample_bits.w) {
1778                         case 8:
1779                                 if(__channels.sw == 1) {
1780                                         for(int i = 0; i < samples; i++) {
1781                                                 data_l = (int16_t)(fio->FgetUint8());
1782                                                 data_l = (data_l - 128) * 256;
1783                                                 left_buffer[i] = data_l;
1784                                                 right_buffer[i] = data_l;
1785                                         }
1786                                 } else if(__channels.sw == 2) {
1787                                         for(int i = 0; i < samples; i++) {
1788                                                 data_l = (int16_t)(fio->FgetUint8());
1789                                                 data_l = (data_l - 128) * 256;
1790                                                 data_r = (int16_t)(fio->FgetUint8());
1791                                                 data_r = (data_r - 128) * 256;
1792                                                 left_buffer[i] = data_l;
1793                                                 right_buffer[i] = data_r;
1794                                         }
1795                                 }
1796                                 break;
1797                         case 16:
1798                                 if(__channels.sw == 1) {
1799                                         for(int i = 0; i < samples; i++) {
1800                                                 pair16.b.l = fio->FgetUint8();
1801                                                 pair16.b.h = fio->FgetUint8();
1802                                                 data_l = pair16.s16;
1803                                                 
1804                                                 left_buffer[i] = data_l;
1805                                                 right_buffer[i] = data_l;
1806                                         }
1807                                 } else if(__channels.sw == 2) {
1808                                         for(int i = 0; i < samples; i++) {
1809                                                 pair16.b.l = fio->FgetUint8();
1810                                                 pair16.b.h = fio->FgetUint8();
1811                                                 data_l = pair16.s16;
1812                                                 
1813                                                 pair16.b.l = fio->FgetUint8();
1814                                                 pair16.b.h = fio->FgetUint8();
1815                                                 data_r = pair16.s16;
1816                                                 left_buffer[i] = data_l;
1817                                                 right_buffer[i] = data_r;
1818                                         }
1819                                 }
1820                                 break;
1821                         case 32:
1822                                 if(__channels.sw == 1) {
1823                                         for(int i = 0; i < samples; i++) {
1824                                                 pair32.b.l = fio->FgetUint8();
1825                                                 pair32.b.h = fio->FgetUint8();
1826                                                 pair32.b.h2 = fio->FgetUint8();
1827                                                 pair32.b.h3 = fio->FgetUint8();
1828                                                 data_l = (int16_t)(pair32.s32 / 65536);
1829                                                 
1830                                                 left_buffer[i] = data_l;
1831                                                 right_buffer[i] = data_l;
1832                                         }
1833                                 } else if(__channels.sw == 2) {
1834                                         for(int i = 0; i < samples; i++) {
1835                                                 pair32.b.l = fio->FgetUint8();
1836                                                 pair32.b.h = fio->FgetUint8();
1837                                                 pair32.b.h2 = fio->FgetUint8();
1838                                                 pair32.b.h3 = fio->FgetUint8();
1839                                                 data_l = (int16_t)(pair32.s32 / 65536);
1840                                                 
1841                                                 pair32.b.l = fio->FgetUint8();
1842                                                 pair32.b.h = fio->FgetUint8();
1843                                                 pair32.b.h2 = fio->FgetUint8();
1844                                                 pair32.b.h3 = fio->FgetUint8();
1845                                                 data_r = (int16_t)(pair32.s32 / 65536);
1846                                                 
1847                                                 left_buffer[i] = data_l;
1848                                                 right_buffer[i] = data_r;
1849                                         }
1850                                 }
1851                                 break;
1852                         default:
1853                                 break;
1854                         }
1855                 }
1856         } else {
1857                 return false;
1858         }
1859         *left_buf = left_buffer;
1860         *right_buf = right_buffer;
1861         *rate = sample_rate;
1862         *got_samples = (int)samples;
1863         return true;
1864 }
1865
1866 bool DLL_PREFIX load_wav_to_monoral(void *__fio, int16_t **buffer, uint32_t *rate, int *got_samples)
1867 {
1868
1869         if(__fio == NULL) return false;
1870         if(buffer == NULL) return false;
1871         if(rate == NULL) return false;
1872         if(got_samples == NULL) return false;
1873         //if((bits != 8) && (bits != 16) && (bits != 32)) return false;
1874
1875         FILEIO *fio = (FILEIO *)__fio;
1876         if(!fio->IsOpened()) return false;
1877
1878         
1879         int16_t *left_buffer = NULL;
1880         size_t samples = 0;
1881         uint32_t sample_rate = 0;
1882         
1883         wav_header_t header;
1884         wav_chunk_t  chunk;
1885
1886         pair16_t __fmt_id;
1887         pair16_t __sample_bits;
1888         pair16_t __channels;
1889         pair_t __sample_rate;
1890         pair_t __chunk_size;
1891
1892         fio->Fread(&header, sizeof(header), 1);
1893         __fmt_id.set_2bytes_le_from(header.format_id);
1894         __sample_bits.set_2bytes_le_from(header.sample_bits);
1895         __chunk_size.set_4bytes_le_from(header.fmt_chunk.size);
1896         __channels.set_2bytes_le_from(header.channels);
1897         __sample_rate.set_4bytes_le_from(header.sample_rate);
1898
1899         if((__fmt_id.w == 1) && ((__sample_bits.w == 8) || (__sample_bits.w == 16) || (__sample_bits.w == 32))) {
1900                 fio->Fseek(__chunk_size.d - 16, FILEIO_SEEK_CUR);
1901                 bool is_eof = false;
1902                 while(1) {
1903                         if(fio->Fread(&chunk, sizeof(chunk), 1) != 1) {
1904                                 is_eof = true;
1905                                 break;
1906                         }
1907                         __chunk_size.set_4bytes_le_from(chunk.size);
1908                         if(strncmp(chunk.id, "data", 4) == 0) {
1909                                 break;
1910                         }
1911                         fio->Fseek(__chunk_size.d, FILEIO_SEEK_CUR);
1912                 }
1913                 __chunk_size.set_4bytes_le_from(chunk.size);
1914                 if(is_eof) {
1915                         fio->Fclose();
1916                         delete fio;
1917                         return false;
1918                 }
1919                 
1920                 samples = (size_t)(__chunk_size.d / __channels.w);
1921                 int16_t data_l, data_r;
1922                 int32_t data32_l, data32_r;
1923                 union {
1924                         int16_t s16;
1925                         struct {
1926                                 uint8_t l, h;
1927                         } b;
1928                 } pair16;
1929                 union {
1930                         int32_t s32;
1931                         struct {
1932                                 uint8_t l, h, h2, h3;
1933                         } b;
1934                 } pair32;
1935                 
1936                 if(samples > 0) {
1937                         if(__sample_bits.w == 16) {
1938                                 samples /= 2;
1939                         } else if(__sample_bits.w == 32) {
1940                                 samples /= 4;
1941                         }
1942                         if(samples == 0) return false;
1943                         sample_rate = __sample_rate.d;
1944
1945                         left_buffer = (int16_t *)malloc(samples * sizeof(int16_t));
1946                         if(left_buffer == NULL) {
1947                                 return false;
1948                         }
1949                         switch(__sample_bits.w) {
1950                         case 8:
1951                                 if(__channels.sw == 1) {
1952                                         for(int i = 0; i < samples; i++) {
1953                                                 data_l = (int16_t)(fio->FgetUint8());
1954                                                 data_l = (data_l - 128) * 256;
1955                                                 left_buffer[i] = data_l;
1956                                         }
1957                                 } else if(__channels.sw == 2) {
1958                                         for(int i = 0; i < samples; i++) {
1959                                                 data_l = (int16_t)(fio->FgetUint8());
1960                                                 data_l = (data_l - 128) * 256;
1961                                                 data_r = (int16_t)(fio->FgetUint8());
1962                                                 data_r = (data_r - 128) * 256;
1963                                                 left_buffer[i] = (data_l + data_r) / 2;
1964                                         }
1965                                 }
1966                                 break;
1967                         case 16:
1968                                 if(__channels.sw == 1) {
1969                                         for(int i = 0; i < samples; i++) {
1970                                                 pair16.b.l = fio->FgetUint8();
1971                                                 pair16.b.h = fio->FgetUint8();
1972                                                 data_l = pair16.s16;
1973                                                 
1974                                                 left_buffer[i] = data_l;
1975                                         }
1976                                 } else if(__channels.sw == 2) {
1977                                         for(int i = 0; i < samples; i++) {
1978                                                 pair16.b.l = fio->FgetUint8();
1979                                                 pair16.b.h = fio->FgetUint8();
1980                                                 data_l = pair16.s16;
1981                                                 
1982                                                 pair16.b.l = fio->FgetUint8();
1983                                                 pair16.b.h = fio->FgetUint8();
1984                                                 data_r = pair16.s16;
1985                                                 left_buffer[i] = (data_l + data_r) / 2;
1986                                         }
1987                                 }
1988                                 break;
1989                         case 32:
1990                                 if(__channels.sw == 1) {
1991                                         for(int i = 0; i < samples; i++) {
1992                                                 pair32.b.l = fio->FgetUint8();
1993                                                 pair32.b.h = fio->FgetUint8();
1994                                                 pair32.b.h2 = fio->FgetUint8();
1995                                                 pair32.b.h3 = fio->FgetUint8();
1996                                                 data_l = (int16_t)(pair32.s32 / 65536);
1997                                                 
1998                                                 left_buffer[i] = data_l;
1999                                         }
2000                                 } else if(__channels.sw == 2) {
2001                                         for(int i = 0; i < samples; i++) {
2002                                                 pair32.b.l = fio->FgetUint8();
2003                                                 pair32.b.h = fio->FgetUint8();
2004                                                 pair32.b.h2 = fio->FgetUint8();
2005                                                 pair32.b.h3 = fio->FgetUint8();
2006                                                 data32_l = pair32.s32 / 65536;
2007                                                 
2008                                                 pair32.b.l = fio->FgetUint8();
2009                                                 pair32.b.h = fio->FgetUint8();
2010                                                 pair32.b.h2 = fio->FgetUint8();
2011                                                 pair32.b.h3 = fio->FgetUint8();
2012                                                 data32_r = pair32.s32 / 65536;
2013                                                 
2014                                                 left_buffer[i] = (int16_t)((data32_l + data32_r) / 2);
2015                                         }
2016                                 }
2017                                 break;
2018                         default:
2019                                 break;
2020                         }
2021                 }
2022         } else {
2023                 return false;
2024         }
2025         *buffer = left_buffer;
2026         *rate = sample_rate;
2027         *got_samples = (int)samples;
2028         return true;
2029 }
2030
2031 DLL_PREFIX const _TCHAR *get_lib_common_version()
2032 {
2033 #if defined(__LIBEMU_UTIL_VERSION)
2034         return (const _TCHAR *)__LIBEMU_UTIL_VERSION;
2035 #else
2036         return (const _TCHAR *)"\0";
2037 #endif
2038 }