OSDN Git Service

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