OSDN Git Service

[STATE][FILEIO] Change StateValue(scrntype_t&) to StateValueScrnType_t(&) due to...
[csp-qt/common_source_project-fm7.git] / source / src / fileio.cpp
1 /*
2         Skelton for retropc emulator
3
4         Author : Takeda.Toshiya
5         Date   : 2006.08.18 -
6
7         [ file i/o ]
8 */
9
10 #if defined(_USE_QT) || defined(_USE_SDL)
11         #include <stdarg.h>
12         #include <fcntl.h>
13         #include <stdio.h>
14         #include <iostream>
15         #include <fstream>
16         #include <cstdio>
17         #if defined(_USE_QT)
18                 #include <sys/types.h>
19                 #include <sys/stat.h>
20                 #if !defined(Q_OS_WIN)
21                         #include <unistd.h>
22                 #endif
23         #endif
24 #elif defined(_WIN32)
25         #include <windows.h>
26 #endif
27 #include "fileio.h"
28
29 #ifdef USE_ZLIB
30         #if defined(USE_QT)
31                 #include <zlib.h>
32                 #include <zconf.h>
33         #else
34                 #ifdef _WIN32
35                         #define ZLIB_WINAPI
36                 #endif
37                 #include "zlib-1.2.11/zlib.h"
38                 #include "zlib-1.2.11/zconf.h"
39         #endif
40         #if defined(ZLIB_VERNUM) && (ZLIB_VERNUM < 0x1290)
41                 inline size_t gzfread(void *buffer, size_t size, size_t count, gzFile file)
42                 {
43                         uint8_t *p = (uint8_t *)buffer;
44                         int i = 0;
45                         for(i = 0; i < count; i++) {
46                                 for(int j = 0; j < size; j++) {
47                                         int s = gzgetc(file);
48                                         if(s < 0) return 0; // EOF
49                                         *p++ = (uint8_t)s; 
50                                 }
51                         }
52                         return i + 1;
53                 }
54                 inline size_t gzfwrite(void *buffer, size_t size, size_t count, gzFile file)
55                 {
56                         uint8_t *p = (uint8_t *)buffer;
57                         int i = 0;
58                         for(i = 0; i < count; i++) {
59                                 for(int j = 0; j < size; j++) {
60                                         uint8_t n = *p++; 
61                                         int s = gzputc(file, n);
62                                         if(s < 0) return 0; // EOF
63                                 }
64                         }
65                         return i + 1;
66                 }
67         #endif
68 #endif
69
70 FILEIO::FILEIO()
71 {
72 #ifdef USE_ZLIB
73         gz = NULL;
74 #endif
75         fp = NULL;
76         path[0] = _T('\0');
77 }
78
79 FILEIO::~FILEIO(void)
80 {
81         Fclose();
82 }
83
84 bool FILEIO::IsFileExisting(const _TCHAR *file_path)
85 {
86 #if defined(_USE_QT) || defined(_USE_SDL)
87         FILE *f = fopen(file_path, "r");
88         if(f != NULL) {
89                 fclose(f);
90                 return true;
91         }
92         return false;
93 #elif defined(_WIN32)
94         DWORD attr = GetFileAttributes(file_path);
95         if(attr == -1) {
96                 return false;
97         }
98         return ((attr & FILE_ATTRIBUTE_DIRECTORY) == 0);
99 #else
100         return (_taccess(file_path, 0) == 0);
101 #endif
102 }
103
104 bool FILEIO::IsFileProtected(const _TCHAR *file_path)
105 {
106 #if defined(_USE_QT) || defined(_USE_SDL)
107         struct stat st;
108         if(stat(file_path, &st) == 0) {
109 #if defined(_WIN32)
110                 if((st.st_mode & S_IWUSR) == 0) {
111 #else
112                 if((st.st_mode & (S_IWUSR | S_IWGRP | S_IWOTH)) == 0) {
113 #endif
114                         return true;
115                 }
116         }
117         return false;
118 #elif defined(_WIN32)
119         return ((GetFileAttributes(file_path) & FILE_ATTRIBUTE_READONLY) != 0);
120 #else
121         return (_taccess(file_path, 2) != 0);
122 #endif
123 }
124
125 bool FILEIO::RemoveFile(const _TCHAR *file_path)
126 {
127 #if defined(_USE_QT) || defined(_USE_SDL)
128         return (remove(file_path) == 0);
129 #elif defined(_WIN32)
130         return (DeleteFile(file_path) != 0);
131 #else
132         return (_tremove(file_path) == 0);
133 #endif
134 }
135
136 bool FILEIO::RenameFile(const _TCHAR *existing_file_path, const _TCHAR *new_file_path)
137 {
138 #if defined(_USE_QT) || defined(_USE_SDL)
139         return (rename(existing_file_path, new_file_path) == 0);
140 #elif defined(_WIN32)
141         return (MoveFile(existing_file_path, new_file_path) != 0);
142 #else
143         return (_trename(existing_file_path, new_file_path) == 0);
144 #endif
145 }
146
147 bool FILEIO::Fopen(const _TCHAR *file_path, int mode)
148 {
149         Fclose();
150         
151         // store file path
152         my_tcscpy_s(path, _MAX_PATH, file_path);
153         open_mode = mode;
154         
155 #ifdef USE_ZLIB
156         if(check_file_extension(file_path, _T(".gz"))) {
157                 return Gzopen(file_path, mode);
158         }
159 #endif
160         switch(mode) {
161         case FILEIO_READ_BINARY:
162                 return ((fp = _tfopen(file_path, _T("rb"))) != NULL);
163         case FILEIO_WRITE_BINARY:
164                 return ((fp = _tfopen(file_path, _T("wb"))) != NULL);
165         case FILEIO_READ_WRITE_BINARY:
166                 return ((fp = _tfopen(file_path, _T("r+b"))) != NULL);
167         case FILEIO_READ_WRITE_NEW_BINARY:
168                 return ((fp = _tfopen(file_path, _T("w+b"))) != NULL);
169         case FILEIO_READ_ASCII:
170                 return ((fp = _tfopen(file_path, _T("r"))) != NULL);
171         case FILEIO_WRITE_ASCII:
172                 return ((fp = _tfopen(file_path, _T("w"))) != NULL);
173         case FILEIO_WRITE_APPEND_ASCII:
174                 return ((fp = _tfopen(file_path, _T("a"))) != NULL);
175         case FILEIO_READ_WRITE_ASCII:
176                 return ((fp = _tfopen(file_path, _T("r+"))) != NULL);
177         case FILEIO_READ_WRITE_NEW_ASCII:
178                 return ((fp = _tfopen(file_path, _T("w+"))) != NULL);
179         case FILEIO_READ_WRITE_APPEND_ASCII:
180                 return ((fp = _tfopen(file_path, _T("a+"))) != NULL);
181         }
182         return false;
183 }
184
185 #ifdef USE_ZLIB
186 bool FILEIO::Gzopen(const _TCHAR *file_path, int mode)
187 {
188         gz_size = 0;
189         
190         // store file path
191         my_tcscpy_s(path, _MAX_PATH, file_path);
192         open_mode = mode;
193         
194         switch(mode) {
195         case FILEIO_READ_BINARY:
196 //      case FILEIO_READ_WRITE_BINARY:
197         case FILEIO_READ_ASCII:
198 //      case FILEIO_READ_WRITE_ASCII:
199 //      case FILEIO_READ_WRITE_APPEND_ASCII:
200                 if((fp = _tfopen(file_path, _T("rb"))) != NULL) {
201                         // check gzip header
202                         uint8_t data[10], name[_MAX_PATH] = {0};
203                         fread(data, 10, 1, fp);
204                         if(data[0] == 0x1f && data[1] == 0x8b && data[2] == 0x08) {
205                                 if(data[3] & 2) {
206                                         // skip part number
207                                         fseek(fp, 2, SEEK_CUR);
208                                 }
209                                 if(data[3] & 4) {
210                                         // skip extra field
211                                         fread(data + 4, 2, 1, fp);
212                                         fseek(fp, data[4] | (data[5] << 8), SEEK_CUR);
213                                 }
214                                 if(data[3] & 8) {
215                                         // read original file name
216                                         fread(name, sizeof(name), 1, fp);
217                                         my_stprintf_s(path, _MAX_PATH, _T("%s%s"), get_parent_dir(path), (char *)name);
218                                 }
219                                 // get uncompressed input size
220                                 fseek(fp, -4, SEEK_END);
221                                 fread(data, 4, 1, fp);
222                                 gz_size = data[0] | (data[1] << 8) | (data[2] << 16) | (data[3] << 24);
223                         }
224                         fclose(fp);
225                         fp = NULL;
226                 }
227                 if(gz_size == 0) {
228                         return false;
229                 }
230                 break;
231         }
232         switch(mode) {
233         case FILEIO_READ_BINARY:
234                 return ((gz = gzopen(tchar_to_char(file_path), "rb")) != NULL);
235         case FILEIO_WRITE_BINARY:
236                 return ((gz = gzopen(tchar_to_char(file_path), "wb")) != NULL);
237 //      case FILEIO_READ_WRITE_BINARY:
238 //              return ((gz = gzopen(tchar_to_char(file_path), "r+b")) != NULL);
239 //      case FILEIO_READ_WRITE_NEW_BINARY:
240 //              return ((gz = gzopen(tchar_to_char(file_path), "w+b")) != NULL);
241         case FILEIO_READ_ASCII:
242                 return ((gz = gzopen(tchar_to_char(file_path), "r")) != NULL);
243         case FILEIO_WRITE_ASCII:
244                 return ((gz = gzopen(tchar_to_char(file_path), "w")) != NULL);
245 //      case FILEIO_WRITE_APPEND_ASCII:
246 //              return ((gz = gzopen(tchar_to_char(file_path), "a")) != NULL);
247 //      case FILEIO_READ_WRITE_ASCII:
248 //              return ((gz = gzopen(tchar_to_char(file_path), "r+")) != NULL);
249 //      case FILEIO_READ_WRITE_NEW_ASCII:
250 //              return ((gz = gzopen(tchar_to_char(file_path), "w+")) != NULL);
251 //      case FILEIO_READ_WRITE_APPEND_ASCII:
252 //              return ((gz = gzopen(tchar_to_char(file_path), "a+")) != NULL);
253         }
254         return false;
255 }
256 #endif
257
258 void FILEIO::Fclose()
259 {
260 #ifdef USE_ZLIB
261         if(gz != NULL) {
262                 gzclose(gz);
263                 gz = NULL;
264         }
265 #endif
266         if(fp != NULL) {
267                 fclose(fp);
268                 fp = NULL;
269         }
270         path[0] = _T('\0');
271 }
272
273 long FILEIO::FileLength()
274 {
275         long pos = Ftell();
276         Fseek(0, FILEIO_SEEK_END);
277         long len = Ftell();
278         Fseek(pos, FILEIO_SEEK_SET);
279         return len;
280 }
281
282 #define GET_VALUE(type) \
283         uint8_t buffer[sizeof(type)] = {0}; \
284         Fread(buffer, sizeof(buffer), 1); \
285         return *(type *)buffer
286
287 #define PUT_VALUE(type, v) \
288         Fwrite(&v, sizeof(type), 1)
289
290 bool FILEIO::FgetBool()
291 {
292         GET_VALUE(bool);
293 }
294
295 void FILEIO::FputBool(bool val)
296 {
297         PUT_VALUE(bool, val);
298 }
299
300 uint8_t FILEIO::FgetUint8()
301 {
302         GET_VALUE(uint8_t);
303 }
304
305 void FILEIO::FputUint8(uint8_t val)
306 {
307         PUT_VALUE(uint8_t, val);
308 }
309
310 uint16_t FILEIO::FgetUint16()
311 {
312         GET_VALUE(uint16_t);
313 }
314
315 void FILEIO::FputUint16(uint16_t val)
316 {
317         PUT_VALUE(uint16_t, val);
318 }
319
320 uint32_t FILEIO::FgetUint32()
321 {
322         GET_VALUE(uint32_t);
323 }
324
325 void FILEIO::FputUint32(uint32_t val)
326 {
327         PUT_VALUE(uint32_t, val);
328 }
329
330 uint64_t FILEIO::FgetUint64()
331 {
332         GET_VALUE(uint64_t);
333 }
334
335 void FILEIO::FputUint64(uint64_t val)
336 {
337         PUT_VALUE(uint64_t, val);
338 }
339
340 int8_t FILEIO::FgetInt8()
341 {
342         GET_VALUE(int8_t);
343 }
344
345 void FILEIO::FputInt8(int8_t val)
346 {
347         PUT_VALUE(int8_t, val);
348 }
349
350 int16_t FILEIO::FgetInt16()
351 {
352         GET_VALUE(int16_t);
353 }
354
355 void FILEIO::FputInt16(int16_t val)
356 {
357         PUT_VALUE(int16_t, val);
358 }
359
360 int32_t FILEIO::FgetInt32()
361 {
362         GET_VALUE(int32_t);
363 }
364
365 void FILEIO::FputInt32(int32_t val)
366 {
367         PUT_VALUE(int32_t, val);
368 }
369
370 int64_t FILEIO::FgetInt64()
371 {
372         GET_VALUE(int64_t);
373 }
374
375 void FILEIO::FputInt64(int64_t val)
376 {
377         PUT_VALUE(int64_t, val);
378 }
379
380 float FILEIO::FgetFloat()
381 {
382         GET_VALUE(float);
383 }
384
385 void FILEIO::FputFloat(float val)
386 {
387         PUT_VALUE(float, val);
388 }
389
390 double FILEIO::FgetDouble()
391 {
392         GET_VALUE(double);
393 }
394
395 void FILEIO::FputDouble(double val)
396 {
397         PUT_VALUE(double, val);
398 }
399
400 uint16_t FILEIO::FgetUint16_LE()
401 {
402         pair16_t tmp;
403         tmp.b.l = FgetUint8();
404         tmp.b.h = FgetUint8();
405         return tmp.u16;
406 }
407
408 void FILEIO::FputUint16_LE(uint16_t val)
409 {
410         pair16_t tmp;
411         tmp.u16 = val;
412         FputUint8(tmp.b.l);
413         FputUint8(tmp.b.h);
414 }
415
416 uint32_t FILEIO::FgetUint32_LE()
417 {
418         pair32_t tmp;
419         tmp.b.l  = FgetUint8();
420         tmp.b.h  = FgetUint8();
421         tmp.b.h2 = FgetUint8();
422         tmp.b.h3 = FgetUint8();
423         return tmp.d;
424 }
425
426 void FILEIO::FputUint32_LE(uint32_t val)
427 {
428         pair32_t tmp;
429         tmp.d = val;
430         FputUint8(tmp.b.l);
431         FputUint8(tmp.b.h);
432         FputUint8(tmp.b.h2);
433         FputUint8(tmp.b.h3);
434 }
435
436 uint64_t FILEIO::FgetUint64_LE()
437 {
438         pair64_t tmp;
439         tmp.b.l  = FgetUint8();
440         tmp.b.h  = FgetUint8();
441         tmp.b.h2 = FgetUint8();
442         tmp.b.h3 = FgetUint8();
443         tmp.b.h4 = FgetUint8();
444         tmp.b.h5 = FgetUint8();
445         tmp.b.h6 = FgetUint8();
446         tmp.b.h7 = FgetUint8();
447         return tmp.q;
448 }
449
450 void FILEIO::FputUint64_LE(uint64_t val)
451 {
452         pair64_t tmp;
453         tmp.q = val;
454         FputUint8(tmp.b.l);
455         FputUint8(tmp.b.h);
456         FputUint8(tmp.b.h2);
457         FputUint8(tmp.b.h3);
458         FputUint8(tmp.b.h4);
459         FputUint8(tmp.b.h5);
460         FputUint8(tmp.b.h6);
461         FputUint8(tmp.b.h7);
462 }
463
464 int16_t FILEIO::FgetInt16_LE()
465 {
466         pair16_t tmp;
467         tmp.b.l = FgetUint8();
468         tmp.b.h = FgetUint8();
469         return tmp.s16;
470 }
471
472 void FILEIO::FputInt16_LE(int16_t val)
473 {
474         pair16_t tmp;
475         tmp.s16 = val;
476         FputUint8(tmp.b.l);
477         FputUint8(tmp.b.h);
478 }
479
480 int32_t FILEIO::FgetInt32_LE()
481 {
482         pair32_t tmp;
483         tmp.b.l  = FgetUint8();
484         tmp.b.h  = FgetUint8();
485         tmp.b.h2 = FgetUint8();
486         tmp.b.h3 = FgetUint8();
487         return tmp.sd;
488 }
489
490 void FILEIO::FputInt32_LE(int32_t val)
491 {
492         pair32_t tmp;
493         tmp.sd = val;
494         FputUint8(tmp.b.l);
495         FputUint8(tmp.b.h);
496         FputUint8(tmp.b.h2);
497         FputUint8(tmp.b.h3);
498 }
499
500 int64_t FILEIO::FgetInt64_LE()
501 {
502         pair64_t tmp;
503         tmp.b.l  = FgetUint8();
504         tmp.b.h  = FgetUint8();
505         tmp.b.h2 = FgetUint8();
506         tmp.b.h3 = FgetUint8();
507         tmp.b.h4 = FgetUint8();
508         tmp.b.h5 = FgetUint8();
509         tmp.b.h6 = FgetUint8();
510         tmp.b.h7 = FgetUint8();
511         return tmp.sq;
512 }
513
514 void FILEIO::FputInt64_LE(int64_t val)
515 {
516         pair64_t tmp;
517         tmp.sq = val;
518         FputUint8(tmp.b.l);
519         FputUint8(tmp.b.h);
520         FputUint8(tmp.b.h2);
521         FputUint8(tmp.b.h3);
522         FputUint8(tmp.b.h4);
523         FputUint8(tmp.b.h5);
524         FputUint8(tmp.b.h6);
525         FputUint8(tmp.b.h7);
526 }
527
528 float FILEIO::FgetFloat_LE()
529 {
530         pair32_t tmp;
531         tmp.b.l  = FgetUint8();
532         tmp.b.h  = FgetUint8();
533         tmp.b.h2 = FgetUint8();
534         tmp.b.h3 = FgetUint8();
535         return tmp.f;
536 }
537
538 void FILEIO::FputFloat_LE(float val)
539 {
540         pair32_t tmp;
541         tmp.f = val;
542         FputUint8(tmp.b.l);
543         FputUint8(tmp.b.h);
544         FputUint8(tmp.b.h2);
545         FputUint8(tmp.b.h3);
546 }
547
548 double FILEIO::FgetDouble_LE()
549 {
550         pair64_t tmp;
551         tmp.b.l  = FgetUint8();
552         tmp.b.h  = FgetUint8();
553         tmp.b.h2 = FgetUint8();
554         tmp.b.h3 = FgetUint8();
555         tmp.b.h4 = FgetUint8();
556         tmp.b.h5 = FgetUint8();
557         tmp.b.h6 = FgetUint8();
558         tmp.b.h7 = FgetUint8();
559         return tmp.df;
560 }
561
562 void FILEIO::FputDouble_LE(double val)
563 {
564         pair64_t tmp;
565         tmp.df = val;
566         FputUint8(tmp.b.l);
567         FputUint8(tmp.b.h);
568         FputUint8(tmp.b.h2);
569         FputUint8(tmp.b.h3);
570         FputUint8(tmp.b.h4);
571         FputUint8(tmp.b.h5);
572         FputUint8(tmp.b.h6);
573         FputUint8(tmp.b.h7);
574 }
575
576 _TCHAR FILEIO::FgetTCHAR_LE()
577 {
578         switch(sizeof(_TCHAR)) {
579         case 2: return (_TCHAR)FgetUint16_LE();
580         case 4: return (_TCHAR)FgetUint32_LE();
581         case 8: return (_TCHAR)FgetUint64_LE();
582         }
583         return (_TCHAR)FgetUint8();
584 }
585
586 void FILEIO::FputTCHAR_LE(_TCHAR val)
587 {
588         switch(sizeof(_TCHAR)) {
589         case 2: FputUint16_LE((uint16_t)val); return;
590         case 4: FputUint32_LE((uint32_t)val); return;
591         case 8: FputUint32_LE((uint64_t)val); return;
592         }
593         FputUint8((uint8_t )val);
594 }
595
596 uint16_t FILEIO::FgetUint16_BE()
597 {
598         pair16_t tmp;
599         tmp.b.h = FgetUint8();
600         tmp.b.l = FgetUint8();
601         return tmp.u16;
602 }
603
604 void FILEIO::FputUint16_BE(uint16_t val)
605 {
606         pair16_t tmp;
607         tmp.u16 = val;
608         FputUint8(tmp.b.h);
609         FputUint8(tmp.b.l);
610 }
611
612 uint32_t FILEIO::FgetUint32_BE()
613 {
614         pair32_t tmp;
615         tmp.b.h3 = FgetUint8();
616         tmp.b.h2 = FgetUint8();
617         tmp.b.h  = FgetUint8();
618         tmp.b.l  = FgetUint8();
619         return tmp.d;
620 }
621
622 void FILEIO::FputUint32_BE(uint32_t val)
623 {
624         pair32_t tmp;
625         tmp.d = val;
626         FputUint8(tmp.b.h3);
627         FputUint8(tmp.b.h2);
628         FputUint8(tmp.b.h);
629         FputUint8(tmp.b.l);
630 }
631
632 uint64_t FILEIO::FgetUint64_BE()
633 {
634         pair64_t tmp;
635         tmp.b.h7 = FgetUint8();
636         tmp.b.h6 = FgetUint8();
637         tmp.b.h5 = FgetUint8();
638         tmp.b.h4 = FgetUint8();
639         tmp.b.h3 = FgetUint8();
640         tmp.b.h2 = FgetUint8();
641         tmp.b.h  = FgetUint8();
642         tmp.b.l  = FgetUint8();
643         return tmp.q;
644 }
645
646 void FILEIO::FputUint64_BE(uint64_t val)
647 {
648         pair64_t tmp;
649         tmp.q = val;
650         FputUint8(tmp.b.h7);
651         FputUint8(tmp.b.h6);
652         FputUint8(tmp.b.h5);
653         FputUint8(tmp.b.h4);
654         FputUint8(tmp.b.h3);
655         FputUint8(tmp.b.h2);
656         FputUint8(tmp.b.h);
657         FputUint8(tmp.b.l);
658 }
659
660 int16_t FILEIO::FgetInt16_BE()
661 {
662         pair16_t tmp;
663         tmp.b.h = FgetUint8();
664         tmp.b.l = FgetUint8();
665         return tmp.s16;
666 }
667
668 void FILEIO::FputInt16_BE(int16_t val)
669 {
670         pair16_t tmp;
671         tmp.s16 = val;
672         FputUint8(tmp.b.h);
673         FputUint8(tmp.b.l);
674 }
675
676 int32_t FILEIO::FgetInt32_BE()
677 {
678         pair32_t tmp;
679         tmp.b.h3 = FgetUint8();
680         tmp.b.h2 = FgetUint8();
681         tmp.b.h  = FgetUint8();
682         tmp.b.l  = FgetUint8();
683         return tmp.sd;
684 }
685
686 void FILEIO::FputInt32_BE(int32_t val)
687 {
688         pair32_t tmp;
689         tmp.sd = val;
690         FputUint8(tmp.b.h3);
691         FputUint8(tmp.b.h2);
692         FputUint8(tmp.b.h);
693         FputUint8(tmp.b.l);
694 }
695
696 int64_t FILEIO::FgetInt64_BE()
697 {
698         pair64_t tmp;
699         tmp.b.h7 = FgetUint8();
700         tmp.b.h6 = FgetUint8();
701         tmp.b.h5 = FgetUint8();
702         tmp.b.h4 = FgetUint8();
703         tmp.b.h3 = FgetUint8();
704         tmp.b.h2 = FgetUint8();
705         tmp.b.h  = FgetUint8();
706         tmp.b.l  = FgetUint8();
707         return tmp.sq;
708 }
709
710 void FILEIO::FputInt64_BE(int64_t val)
711 {
712         pair64_t tmp;
713         tmp.sq = val;
714         FputUint8(tmp.b.h7);
715         FputUint8(tmp.b.h6);
716         FputUint8(tmp.b.h5);
717         FputUint8(tmp.b.h4);
718         FputUint8(tmp.b.h3);
719         FputUint8(tmp.b.h2);
720         FputUint8(tmp.b.h);
721         FputUint8(tmp.b.l);
722 }
723
724 float FILEIO::FgetFloat_BE()
725 {
726         pair32_t tmp;
727         tmp.b.h3 = FgetUint8();
728         tmp.b.h2 = FgetUint8();
729         tmp.b.h  = FgetUint8();
730         tmp.b.l  = FgetUint8();
731         return tmp.f;
732 }
733
734 void FILEIO::FputFloat_BE(float val)
735 {
736         pair32_t tmp;
737         tmp.f = val;
738         FputUint8(tmp.b.h3);
739         FputUint8(tmp.b.h2);
740         FputUint8(tmp.b.h);
741         FputUint8(tmp.b.l);
742 }
743
744 double FILEIO::FgetDouble_BE()
745 {
746         pair64_t tmp;
747         tmp.b.h7 = FgetUint8();
748         tmp.b.h6 = FgetUint8();
749         tmp.b.h5 = FgetUint8();
750         tmp.b.h4 = FgetUint8();
751         tmp.b.h3 = FgetUint8();
752         tmp.b.h2 = FgetUint8();
753         tmp.b.h  = FgetUint8();
754         tmp.b.l  = FgetUint8();
755         return tmp.df;
756 }
757
758 void FILEIO::FputDouble_BE(double val)
759 {
760         pair64_t tmp;
761         tmp.df = val;
762         FputUint8(tmp.b.h7);
763         FputUint8(tmp.b.h6);
764         FputUint8(tmp.b.h5);
765         FputUint8(tmp.b.h4);
766         FputUint8(tmp.b.h3);
767         FputUint8(tmp.b.h2);
768         FputUint8(tmp.b.h);
769         FputUint8(tmp.b.l);
770 }
771
772 _TCHAR FILEIO::FgetTCHAR_BE()
773 {
774         switch(sizeof(_TCHAR)) {
775         case 2: return (_TCHAR)FgetUint16_BE();
776         case 4: return (_TCHAR)FgetUint32_BE();
777         case 8: return (_TCHAR)FgetUint64_BE();
778         }
779         return (_TCHAR)FgetUint8();
780 }
781
782 void FILEIO::FputTCHAR_BE(_TCHAR val)
783 {
784         switch(sizeof(_TCHAR)) {
785         case 2: FputUint16_BE((uint16_t)val); return;
786         case 4: FputUint32_BE((uint32_t)val); return;
787         case 8: FputUint32_BE((uint64_t)val); return;
788         }
789         FputUint8((uint8_t )val);
790 }
791
792 int FILEIO::Fgetc()
793 {
794 #ifdef USE_ZLIB
795         if(gz != NULL) {
796                 return gzgetc(gz);
797         } else
798 #endif
799         if(fp != NULL) {
800                 return fgetc(fp);
801         }
802         return 0;
803 }
804
805 int FILEIO::Fputc(int c)
806 {
807 #ifdef USE_ZLIB
808         if(gz != NULL) {
809                 return gzputc(gz, c);
810         } else
811 #endif
812         if(fp != NULL) {
813                 return fputc(c, fp);
814         }
815         return 0;
816 }
817
818 char *FILEIO::Fgets(char *str, int n)
819 {
820 #ifdef USE_ZLIB
821         if(gz != NULL) {
822                 return gzgets(gz, str, n);
823         } else
824 #endif
825         if(fp != NULL) {
826                 return fgets(str, n, fp);
827         }
828         return 0;
829 }
830
831 _TCHAR *FILEIO::Fgetts(_TCHAR *str, int n)
832 {
833 #ifdef USE_ZLIB
834         if(gz != NULL) {
835 #if defined(_UNICODE) && defined(SUPPORT_TCHAR_TYPE)
836                 char *str_mb = (char *)calloc(sizeof(char), n + 1);
837                 gzgets(gz, str_mb, n);
838                 my_swprintf_s(str, n, L"%s", char_to_wchar(str_mb));
839                 free(str_mb);
840                 return str;
841 #else
842                 return gzgets(gz, str, n);
843 #endif
844         } else
845 #endif
846         if(fp != NULL) {
847                 return _fgetts(str, n, fp);
848         }
849         return 0;
850 }
851
852 int FILEIO::Fprintf(const char* format, ...)
853 {
854         va_list ap;
855         char buffer[1024];
856         
857         va_start(ap, format);
858         my_vsprintf_s(buffer, 1024, format, ap);
859         va_end(ap);
860         
861 #ifdef USE_ZLIB
862         if(gz != NULL) {
863                 return gzprintf(gz, "%s", buffer);
864         } else
865 #endif
866         if(fp != NULL) {
867                 return my_fprintf_s(fp, "%s", buffer);
868         }
869         return 0;
870 }
871
872 int FILEIO::Ftprintf(const _TCHAR* format, ...)
873 {
874         va_list ap;
875         _TCHAR buffer[1024];
876         
877         va_start(ap, format);
878         my_vstprintf_s(buffer, 1024, format, ap);
879         va_end(ap);
880         
881 #ifdef USE_ZLIB
882         if(gz != NULL) {
883                 return gzprintf(gz, "%s", tchar_to_char(buffer));
884         } else
885 #endif
886         if(fp != NULL) {
887                 return my_ftprintf_s(fp, _T("%s"), buffer);
888         }
889         return 0;
890 }
891
892 size_t FILEIO::Fread(void* buffer, size_t size, size_t count)
893 {
894 #ifdef USE_ZLIB
895         if(gz != NULL) {
896                 return gzfread(buffer, size, count, gz);
897         } else
898 #endif
899         if(fp != NULL) {
900                 return fread(buffer, size, count, fp);
901         }
902         return 0;
903 }
904
905 size_t FILEIO::Fwrite(const void* buffer, size_t size, size_t count)
906 {
907 #ifdef USE_ZLIB
908         if(gz != NULL) {
909                 return gzfwrite(buffer, size, count, gz);
910         } else
911 #endif
912         if(fp != NULL) {
913                 return fwrite(buffer, size, count, fp);
914         }
915         return 0;
916 }
917
918 int FILEIO::Fseek(long offset, int origin)
919 {
920 #ifdef USE_ZLIB
921         if(gz != NULL) {
922                 switch(origin) {
923                 case FILEIO_SEEK_CUR:
924                         return (gzseek(gz, offset, SEEK_CUR) == -1 ? -1 : 0);
925                 case FILEIO_SEEK_END:
926                         return (gzseek(gz, offset + gz_size, SEEK_SET) == -1 ? -1 : 0);
927                 case FILEIO_SEEK_SET:
928                         return (gzseek(gz, offset, SEEK_SET) == -1 ? -1 : 0);
929                 }
930         } else
931 #endif
932         if(fp != NULL) {
933                 switch(origin) {
934                 case FILEIO_SEEK_CUR:
935                         return fseek(fp, offset, SEEK_CUR);
936                 case FILEIO_SEEK_END:
937                         return fseek(fp, offset, SEEK_END);
938                 case FILEIO_SEEK_SET:
939                         return fseek(fp, offset, SEEK_SET);
940                 }
941         }
942         return -1;
943 }
944
945 long FILEIO::Ftell()
946 {
947 #ifdef USE_ZLIB
948         if(gz != NULL) {
949                 return gztell(gz);
950         } else
951 #endif
952         if(fp != NULL) {
953                 return ftell(fp);
954         }
955         return 0;
956 }
957
958
959 bool FILEIO::Fflush()
960 {
961 #ifdef USE_ZLIB
962         if(gz != NULL) {
963                 return (gzflush(gz, Z_SYNC_FLUSH) == Z_OK) ? true : false;
964         } else
965 #endif
966         {
967                 return (fflush(fp) == 0) ? true : false;
968         }
969 }
970
971
972 bool FILEIO::StateCheckUint32(uint32_t val)
973 {
974         if(open_mode == FILEIO_READ_BINARY) {
975                 return (val == FgetUint32_LE());
976         } else {
977                 FputUint32_LE(val);
978                 return true;
979         }
980 }
981
982 bool FILEIO::StateCheckInt32(int32_t val)
983 {
984         if(open_mode == FILEIO_READ_BINARY) {
985                 return (val == FgetInt32_LE());
986         } else {
987                 FputInt32_LE(val);
988                 return true;
989         }
990 }
991
992 bool FILEIO::StateCheckBuffer(const _TCHAR *buffer, size_t size, size_t count)
993 {
994         for(unsigned int i = 0; i < size / sizeof(buffer[0]) * count; i++) {
995                 if(open_mode == FILEIO_READ_BINARY) {
996                         if(buffer[i] != FgetTCHAR_LE()) {
997                                 return false;
998                         }
999                 } else {
1000                         FputTCHAR_LE(buffer[i]);
1001                 }
1002         }
1003         return true;
1004 }
1005
1006 void FILEIO::StateValue(bool &val)
1007 {
1008         if(open_mode == FILEIO_READ_BINARY) {
1009                 val = FgetBool();
1010         } else {
1011                 FputBool(val);
1012         }
1013 }
1014
1015 void FILEIO::StateValue(uint8_t &val)
1016 {
1017         if(open_mode == FILEIO_READ_BINARY) {
1018                 val = FgetUint8();
1019         } else {
1020                 FputUint8(val);
1021         }
1022 }
1023
1024 void FILEIO::StateValue(uint16_t &val)
1025 {
1026         if(open_mode == FILEIO_READ_BINARY) {
1027                 val = FgetUint16_LE();
1028         } else {
1029                 FputUint16_LE(val);
1030         }
1031 }
1032
1033 void FILEIO::StateValue(uint32_t &val)
1034 {
1035         if(open_mode == FILEIO_READ_BINARY) {
1036                 val = FgetUint32_LE();
1037         } else {
1038                 FputUint32_LE(val);
1039         }
1040 }
1041
1042 void FILEIO::StateValue(uint64_t &val)
1043 {
1044         if(open_mode == FILEIO_READ_BINARY) {
1045                 val = FgetUint64_LE();
1046         } else {
1047                 FputUint64_LE(val);
1048         }
1049 }
1050
1051 void FILEIO::StateValue(int8_t &val)
1052 {
1053         if(open_mode == FILEIO_READ_BINARY) {
1054                 val = FgetInt8();
1055         } else {
1056                 FputInt8(val);
1057         }
1058 }
1059
1060 void FILEIO::StateValue(int16_t &val)
1061 {
1062         if(open_mode == FILEIO_READ_BINARY) {
1063                 val = FgetInt16_LE();
1064         } else {
1065                 FputInt16_LE(val);
1066         }
1067 }
1068
1069 void FILEIO::StateValue(int32_t &val)
1070 {
1071         if(open_mode == FILEIO_READ_BINARY) {
1072                 val = FgetInt32_LE();
1073         } else {
1074                 FputInt32_LE(val);
1075         }
1076 }
1077
1078 void FILEIO::StateValue(int64_t &val)
1079 {
1080         if(open_mode == FILEIO_READ_BINARY) {
1081                 val = FgetInt64_LE();
1082         } else {
1083                 FputInt64_LE(val);
1084         }
1085 }
1086
1087 void FILEIO::StateValue(pair16_t &val)
1088 {
1089         if(open_mode == FILEIO_READ_BINARY) {
1090                 val.u16 = FgetUint16_LE();
1091         } else {
1092                 FputUint16_LE(val.u16);
1093         }
1094 }
1095
1096 void FILEIO::StateValue(pair32_t &val)
1097 {
1098         if(open_mode == FILEIO_READ_BINARY) {
1099                 val.d = FgetUint32_LE();
1100         } else {
1101                 FputUint32_LE(val.d);
1102         }
1103 }
1104
1105 void FILEIO::StateValue(pair64_t &val)
1106 {
1107         if(open_mode == FILEIO_READ_BINARY) {
1108                 val.q = FgetUint64_LE();
1109         } else {
1110                 FputUint64_LE(val.q);
1111         }
1112 }
1113
1114 void FILEIO::StateValue(float &val)
1115 {
1116         if(open_mode == FILEIO_READ_BINARY) {
1117                 val = FgetFloat_LE();
1118         } else {
1119                 FputFloat_LE(val);
1120         }
1121 }
1122
1123 void FILEIO::StateValue(double &val)
1124 {
1125         if(open_mode == FILEIO_READ_BINARY) {
1126                 val = FgetDouble_LE();
1127         } else {
1128                 FputDouble_LE(val);
1129         }
1130 }
1131
1132 void FILEIO::StateValue(_TCHAR &val)
1133 {
1134         if(open_mode == FILEIO_READ_BINARY) {
1135                 val = FgetTCHAR_LE();
1136         } else {
1137                 FputTCHAR_LE(val);
1138         }
1139 }
1140
1141 // 20181025 K.O
1142 // Note: scrntype_t is variable size type.2 bytes@15/16bpp, 4bytes@24/32bpp.
1143 void FILEIO::StateValueScrnType_t(scrntype_t &val)
1144 {
1145         uint8_t r, g, b;
1146         if(open_mode == FILEIO_READ_BINARY) {
1147                 r = FgetUint8();
1148                 g = FgetUint8();
1149                 b = FgetUint8();
1150                 val = RGB_COLOR(r, g, b);
1151         } else {
1152                 r = R_OF_COLOR(val);
1153                 g = G_OF_COLOR(val);
1154                 b = B_OF_COLOR(val);
1155                 FputUint8(r);
1156                 FputUint8(g);
1157                 FputUint8(b);
1158         }
1159 }
1160
1161
1162 void FILEIO::StateArray(bool *buffer, size_t size, size_t count)
1163 {
1164         for(unsigned int i = 0; i < size / sizeof(buffer[0]) * count; i++) {
1165                 StateValue(buffer[i]);
1166         }
1167 }
1168
1169 void FILEIO::StateArray(uint8_t *buffer, size_t size, size_t count)
1170 {
1171         for(unsigned int i = 0; i < size / sizeof(buffer[0]) * count; i++) {
1172                 StateValue(buffer[i]);
1173         }
1174 }
1175
1176 void FILEIO::StateArray(uint16_t *buffer, size_t size, size_t count)
1177 {
1178         for(unsigned int i = 0; i < size / sizeof(buffer[0]) * count; i++) {
1179                 StateValue(buffer[i]);
1180         }
1181 }
1182
1183 void FILEIO::StateArray(uint32_t *buffer, size_t size, size_t count)
1184 {
1185         for(unsigned int i = 0; i < size / sizeof(buffer[0]) * count; i++) {
1186                 StateValue(buffer[i]);
1187         }
1188 }
1189
1190 void FILEIO::StateArray(uint64_t *buffer, size_t size, size_t count)
1191 {
1192         for(unsigned int i = 0; i < size / sizeof(buffer[0]) * count; i++) {
1193                 StateValue(buffer[i]);
1194         }
1195 }
1196
1197 void FILEIO::StateArray(int8_t *buffer, size_t size, size_t count)
1198 {
1199         for(unsigned int i = 0; i < size / sizeof(buffer[0]) * count; i++) {
1200                 StateValue(buffer[i]);
1201         }
1202 }
1203
1204 void FILEIO::StateArray(int16_t *buffer, size_t size, size_t count)
1205 {
1206         for(unsigned int i = 0; i < size / sizeof(buffer[0]) * count; i++) {
1207                 StateValue(buffer[i]);
1208         }
1209 }
1210
1211 void FILEIO::StateArray(int32_t *buffer, size_t size, size_t count)
1212 {
1213         for(unsigned int i = 0; i < size / sizeof(buffer[0]) * count; i++) {
1214                 StateValue(buffer[i]);
1215         }
1216 }
1217
1218 void FILEIO::StateArray(int64_t *buffer, size_t size, size_t count)
1219 {
1220         for(unsigned int i = 0; i < size / sizeof(buffer[0]) * count; i++) {
1221                 StateValue(buffer[i]);
1222         }
1223 }
1224
1225 void FILEIO::StateArray(pair16_t *buffer, size_t size, size_t count)
1226 {
1227         for(unsigned int i = 0; i < size / sizeof(buffer[0]) * count; i++) {
1228                 StateValue(buffer[i]);
1229         }
1230 }
1231
1232 void FILEIO::StateArray(pair32_t *buffer, size_t size, size_t count)
1233 {
1234         for(unsigned int i = 0; i < size / sizeof(buffer[0]) * count; i++) {
1235                 StateValue(buffer[i]);
1236         }
1237 }
1238
1239 void FILEIO::StateArray(pair64_t *buffer, size_t size, size_t count)
1240 {
1241         for(unsigned int i = 0; i < size / sizeof(buffer[0]) * count; i++) {
1242                 StateValue(buffer[i]);
1243         }
1244 }
1245
1246 void FILEIO::StateArray(float *buffer, size_t size, size_t count)
1247 {
1248         for(unsigned int i = 0; i < size / sizeof(buffer[0]) * count; i++) {
1249                 StateValue(buffer[i]);
1250         }
1251 }
1252
1253 void FILEIO::StateArray(double *buffer, size_t size, size_t count)
1254 {
1255         for(unsigned int i = 0; i < size / sizeof(buffer[0]) * count; i++) {
1256                 StateValue(buffer[i]);
1257         }
1258 }
1259
1260 void FILEIO::StateArray(_TCHAR *buffer, size_t size, size_t count)
1261 {
1262         for(unsigned int i = 0; i < size / sizeof(buffer[0]) * count; i++) {
1263                 StateValue(buffer[i]);
1264         }
1265 }
1266
1267 // 20181025 K.O
1268 // Note: scrntype_t is variable size type.2 bytes@15/16bpp, 4bytes@24/32bpp.
1269 void FILEIO::StateArrayScrnType_t(scrntype_t *buffer, size_t size, size_t count)
1270 {
1271         for(unsigned int i = 0; i < size / sizeof(buffer[0]) * count; i++) {
1272                 StateValue(buffer[i]);
1273         }
1274 }
1275
1276
1277 void FILEIO::StateBuffer(void *buffer, size_t size, size_t count)
1278 {
1279         if(open_mode == FILEIO_READ_BINARY) {
1280                 Fread(buffer, size, count);
1281         } else {
1282                 Fwrite(buffer, size, count);
1283         }
1284 }