OSDN Git Service

ca33b6b12c4a493f2a812824c4cadcaafde88c1b
[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 #if !defined(_MSC_VER)
29 #include <stdarg.h>
30 #include <fcntl.h>
31 #include <stdio.h>
32 #include <iostream>
33 #include <fstream>
34 #include <cstdio>
35 #endif
36
37 #ifdef USE_ZLIB
38         #if defined(USE_QT)
39                 #include <zlib.h>
40                 #include <zconf.h>
41         #else
42                 #ifdef _WIN32
43                         #define ZLIB_WINAPI
44                 #endif
45                 #include "zlib-1.2.11/zlib.h"
46                 #include "zlib-1.2.11/zconf.h"
47         #endif
48 #endif
49
50 #if ZLIB_VERNUM < 0x1290
51 inline size_t gzfread(void *buffer, size_t size, size_t count, gzFile file)
52 {
53         uint8_t *p = (uint8_t *)buffer;
54         int s = 0;
55         int i = 0;
56         for(i = 0; i < count; i++) {
57                 for(int j = 0; j < size; j++) {
58                         s = gzgetc(file);
59                         if(s < 0) return 0; // EOF
60                         *p++ = (uint8_t)s; 
61                 }
62         }
63         return i + 1;
64 }
65 inline size_t gzfwrite(void *buffer, size_t size, size_t count, gzFile file)
66 {
67         uint8_t *p = (uint8_t *)buffer;
68         uint8_t n;
69         int s = 0;
70         int i = 0;
71         for(i = 0; i < count; i++) {
72                 for(int j = 0; j < size; j++) {
73                         n = *p++; 
74                         s = gzputc(file, n);
75                         if(s < 0) return 0; // EOF
76                 }
77         }
78         return i + 1;
79 }
80 #endif                  
81 FILEIO::FILEIO()
82 {
83 #ifdef USE_ZLIB
84         gz = NULL;
85 #endif
86         fp = NULL;
87         path[0] = _T('\0');
88 }
89
90 FILEIO::~FILEIO(void)
91 {
92         Fclose();
93 }
94
95 bool FILEIO::IsFileExisting(const _TCHAR *file_path)
96 {
97 #if defined(_USE_QT) || defined(_USE_SDL)
98         FILE *f;
99         f = fopen(file_path, "r");
100         if(f != NULL)  {
101                 fclose(f);         
102                 return true;
103         }
104         return false;
105 #elif defined(_WIN32)
106         DWORD attr = GetFileAttributes(file_path);
107         if(attr == -1) {
108                 return false;
109         }
110         return ((attr & FILE_ATTRIBUTE_DIRECTORY) == 0);
111 #else
112         return (_taccess(file_path, 0) == 0);
113 #endif
114 }
115
116 #if defined(_USE_QT)
117 # include <sys/types.h>
118 # include <sys/stat.h>
119 # if !defined(Q_OS_WIN)
120 #   include <unistd.h>
121 # endif
122 #endif
123 bool FILEIO::IsFileProtected(const _TCHAR *file_path)
124 {
125 #if defined(_USE_QT) || defined(_USE_SDL)
126         struct stat st;
127         if(stat(file_path, &st) == 0) {
128 # if defined(_WIN32)
129                 if((st.st_mode & S_IWUSR) == 0) {
130 # else
131                 if((st.st_mode & (S_IWUSR | S_IWGRP | S_IWOTH)) == 0) {
132 # endif
133                         return true;
134                 }
135         }
136         return false;
137 #elif defined(_WIN32)
138         return ((GetFileAttributes(file_path) & FILE_ATTRIBUTE_READONLY) != 0);
139 #else
140         return (_taccess(file_path, 2) != 0);
141 #endif
142 }
143
144 bool FILEIO::RemoveFile(const _TCHAR *file_path)
145 {
146 #if defined(_USE_QT) || defined(_USE_SDL)
147         return (remove(file_path) == 0);
148 #elif defined(_WIN32)
149         return (DeleteFile(file_path) != 0);
150 #else
151         return (_tremove(file_path) == 0);      // not supported on wince ???
152 #endif
153 }
154
155 bool FILEIO::RenameFile(const _TCHAR *existing_file_path, const _TCHAR *new_file_path)
156 {
157 #if defined(_USE_QT) || defined(_USE_SDL)
158         return (rename(existing_file_path, new_file_path) == 0);
159 #elif defined(_WIN32)
160         return (MoveFile(existing_file_path, new_file_path) != 0);
161 #else
162         return (_trename(existing_file_path, new_file_path) == 0);
163 #endif                  
164 }
165
166 bool FILEIO::Fopen(const _TCHAR *file_path, int mode)
167 {
168         Fclose();
169         
170         // store file path
171         my_tcscpy_s(path, _MAX_PATH, file_path);
172         
173 #ifdef USE_ZLIB
174         if(check_file_extension(file_path, _T(".gz"))) {
175                 switch(mode) {
176                 case FILEIO_READ_BINARY:
177 //              case FILEIO_READ_WRITE_BINARY:
178                 case FILEIO_READ_ASCII:
179 //              case FILEIO_READ_WRITE_ASCII:
180 //              case FILEIO_READ_WRITE_APPEND_ASCII:
181                         if((fp = _tfopen(file_path, _T("rb"))) != NULL) {
182                                 // check gzip header
183                                 uint8_t data[10], name[_MAX_PATH] = {0};
184                                 fread(data, 10, 1, fp);
185                                 if(data[3] & 2) {
186                                         // skip part number
187                                         fseek(fp, 2, SEEK_CUR);
188                                 }
189                                 if(data[3] & 4) {
190                                         // skip extra field
191                                         fread(data + 4, 2, 1, fp);
192                                         fseek(fp, data[4] | (data[5] << 8), SEEK_CUR);
193                                 }
194                                 if(data[3] & 8) {
195                                         // read original file name
196                                         fread(name, sizeof(name), 1, fp);
197                                         my_stprintf_s(path, _MAX_PATH, _T("%s%s"), get_parent_dir(path), (char *)name);
198                                 }
199                                 // get uncompressed input size
200                                 fseek(fp, -4, SEEK_END);
201                                 fread(data, 4, 1, fp);
202                                 gz_size = data[0] | (data[1] << 8) | (data[2] << 16) | (data[3] << 24);
203                                 fclose(fp);
204                                 fp = NULL;
205                         }
206                         break;
207                 }
208                 switch(mode) {
209                 case FILEIO_READ_BINARY:
210                         {
211                                 //memset(path, 0x00, _MAX_PATH);
212                                 gz = gzopen(file_path, _T("rb"));
213                                 if(gz != NULL) {
214                                         my_tcscpy_s(path, _MAX_PATH, get_file_path_without_extensiton(file_path));
215                                         return true;
216                                 } else {
217                                         my_tcscpy_s(path, _MAX_PATH, file_path);
218                                         return false;
219                                 }                                       
220                         }
221                         break;
222 //              case FILEIO_WRITE_BINARY:
223 //                      return ((gz = gzopen(file_path, _T("wb"))) != NULL);
224 //              case FILEIO_READ_WRITE_BINARY:
225 //                      return ((gz = gzopen(file_path, _T("r+b"))) != NULL);
226 //              case FILEIO_READ_WRITE_NEW_BINARY:
227 //                      return ((gz = gzopen(file_path, _T("w+b"))) != NULL);
228                 case FILEIO_READ_ASCII:
229                         {
230                                 gz = gzopen(file_path, _T("r"));
231                                 if(gz != NULL) {
232                                         my_tcscpy_s(path, _MAX_PATH, get_file_path_without_extensiton(file_path));
233                                         return true;
234                                 } else {
235                                         my_tcscpy_s(path, _MAX_PATH, file_path);
236                                         return false;
237                                 }
238                         }
239                         break;
240 //              case FILEIO_WRITE_ASCII:
241 //                      return ((gz = gzopen(file_path, _T("w"))) != NULL);
242 //              case FILEIO_WRITE_APPEND_ASCII:
243 //                      return ((gz = gzopen(file_path, _T("a"))) != NULL);
244 //              case FILEIO_READ_WRITE_ASCII:
245 //                      return ((gz = gzopen(file_path, _T("r+"))) != NULL);
246 //              case FILEIO_READ_WRITE_NEW_ASCII:
247 //                      return ((gz = gzopen(file_path, _T("w+"))) != NULL);
248 //              case FILEIO_READ_WRITE_APPEND_ASCII:
249 //                      return ((gz = gzopen(file_path, _T("a+"))) != NULL);
250                 }
251         } else
252 #endif
253         switch(mode) {
254         case FILEIO_READ_BINARY:
255                 return ((fp = _tfopen(file_path, _T("rb"))) != NULL);
256         case FILEIO_WRITE_BINARY:
257                 return ((fp = _tfopen(file_path, _T("wb"))) != NULL);
258         case FILEIO_READ_WRITE_BINARY:
259                 return ((fp = _tfopen(file_path, _T("r+b"))) != NULL);
260         case FILEIO_READ_WRITE_NEW_BINARY:
261                 return ((fp = _tfopen(file_path, _T("w+b"))) != NULL);
262         case FILEIO_READ_ASCII:
263                 return ((fp = _tfopen(file_path, _T("r"))) != NULL);
264         case FILEIO_WRITE_ASCII:
265                 return ((fp = _tfopen(file_path, _T("w"))) != NULL);
266         case FILEIO_WRITE_APPEND_ASCII:
267                 return ((fp = _tfopen(file_path, _T("a"))) != NULL);
268         case FILEIO_READ_WRITE_ASCII:
269                 return ((fp = _tfopen(file_path, _T("r+"))) != NULL);
270         case FILEIO_READ_WRITE_NEW_ASCII:
271                 return ((fp = _tfopen(file_path, _T("w+"))) != NULL);
272         case FILEIO_READ_WRITE_APPEND_ASCII:
273                 return ((fp = _tfopen(file_path, _T("a+"))) != NULL);
274         }
275         return false;
276 }
277
278 void FILEIO::Fclose()
279 {
280 #ifdef USE_ZLIB
281         if(gz != NULL) {
282                 gzclose(gz);
283                 gz = NULL;
284         }
285 #endif
286         if(fp != NULL) {
287                 fclose(fp);
288                 fp = NULL;
289         }
290         path[0] = _T('\0');
291 }
292
293 long FILEIO::FileLength()
294 {
295         long pos = Ftell();
296         Fseek(0, FILEIO_SEEK_END);
297         long len = Ftell();
298         Fseek(pos, FILEIO_SEEK_SET);
299         return len;
300 }
301
302 #define GET_VALUE(type) \
303         uint8_t buffer[sizeof(type)];                           \
304         type *tmpv = (type *)buffer;                            \
305         Fread(buffer, sizeof(buffer), 1);               \
306         return *tmpv;                                           
307
308 #define PUT_VALUE(type, v) \
309         Fwrite(&v, sizeof(type), 1)
310
311 bool FILEIO::FgetBool()
312 {
313         GET_VALUE(bool);
314 }
315
316 void FILEIO::FputBool(bool val)
317 {
318         PUT_VALUE(bool, val);
319 }
320
321 uint8_t FILEIO::FgetUint8()
322 {
323         GET_VALUE(uint8_t);
324 }
325
326 void FILEIO::FputUint8(uint8_t val)
327 {
328         PUT_VALUE(uint8_t, val);
329 }
330
331 uint16_t FILEIO::FgetUint16()
332 {
333         GET_VALUE(uint16_t);
334 }
335
336 void FILEIO::FputUint16(uint16_t val)
337 {
338         PUT_VALUE(uint16_t, val);
339 }
340
341 uint32_t FILEIO::FgetUint32()
342 {
343         GET_VALUE(uint32_t);
344 }
345
346 void FILEIO::FputUint32(uint32_t val)
347 {
348         PUT_VALUE(uint32_t, val);
349 }
350
351 uint64_t FILEIO::FgetUint64()
352 {
353         GET_VALUE(uint64_t);
354 }
355
356 void FILEIO::FputUint64(uint64_t val)
357 {
358         PUT_VALUE(uint64_t, val);
359 }
360
361 int8_t FILEIO::FgetInt8()
362 {
363         GET_VALUE(int8_t);
364 }
365
366 void FILEIO::FputInt8(int8_t val)
367 {
368         PUT_VALUE(int8_t, val);
369 }
370
371 int16_t FILEIO::FgetInt16()
372 {
373         GET_VALUE(int16_t);
374 }
375
376 void FILEIO::FputInt16(int16_t val)
377 {
378         PUT_VALUE(int16_t, val);
379 }
380
381 int32_t FILEIO::FgetInt32()
382 {
383         GET_VALUE(int32_t);
384 }
385
386 void FILEIO::FputInt32(int32_t val)
387 {
388         PUT_VALUE(int32_t, val);
389 }
390
391 int64_t FILEIO::FgetInt64()
392 {
393         GET_VALUE(int64_t);
394 }
395
396 void FILEIO::FputInt64(int64_t val)
397 {
398         PUT_VALUE(int64_t, val);
399 }
400
401 float FILEIO::FgetFloat()
402 {
403         GET_VALUE(float);
404 }
405
406 void FILEIO::FputFloat(float val)
407 {
408         PUT_VALUE(float, val);
409 }
410
411 double FILEIO::FgetDouble()
412 {
413         GET_VALUE(double);
414 }
415
416 void FILEIO::FputDouble(double val)
417 {
418         PUT_VALUE(double, val);
419 }
420
421 typedef union {
422         struct {
423 #ifdef __BIG_ENDIAN__
424                 uint8_t h, l;
425 #else
426                 uint8_t l, h;
427 #endif
428         } b;
429         uint16_t u16;
430         int16_t s16;
431 } pair16_t;
432
433 typedef union {
434         struct {
435 #ifdef __BIG_ENDIAN__
436                 uint8_t h3, h2, h, l;
437 #else
438                 uint8_t l, h, h2, h3;
439 #endif
440         } b;
441         uint32_t u32;
442         int32_t s32;
443 } pair32_t;
444
445 typedef union {
446         struct {
447 #ifdef __BIG_ENDIAN__
448                 uint8_t h7, h6, h5, h4, h3, h2, h, l;
449 #else
450                 uint8_t l, h, h2, h3, h4, h5, h6, h7;
451 #endif
452         } b;
453         uint64_t u64;
454         int64_t s64;
455 } pair64_t;
456
457 uint16_t FILEIO::FgetUint16_LE()
458 {
459         pair16_t tmp;
460         tmp.b.l = FgetUint8();
461         tmp.b.h = FgetUint8();
462         return tmp.u16;
463 }
464
465 void FILEIO::FputUint16_LE(uint16_t val)
466 {
467         pair16_t tmp;
468         tmp.u16 = val;
469         FputUint8(tmp.b.l);
470         FputUint8(tmp.b.h);
471 }
472
473 uint32_t FILEIO::FgetUint32_LE()
474 {
475         pair32_t tmp;
476         tmp.b.l  = FgetUint8();
477         tmp.b.h  = FgetUint8();
478         tmp.b.h2 = FgetUint8();
479         tmp.b.h3 = FgetUint8();
480         return tmp.u32;
481 }
482
483 void FILEIO::FputUint32_LE(uint32_t val)
484 {
485         pair32_t tmp;
486         tmp.u32 = val;
487         FputUint8(tmp.b.l);
488         FputUint8(tmp.b.h);
489         FputUint8(tmp.b.h2);
490         FputUint8(tmp.b.h3);
491 }
492
493 uint64_t FILEIO::FgetUint64_LE()
494 {
495         pair64_t tmp;
496         tmp.b.l  = FgetUint8();
497         tmp.b.h  = FgetUint8();
498         tmp.b.h2 = FgetUint8();
499         tmp.b.h3 = FgetUint8();
500         tmp.b.h4 = FgetUint8();
501         tmp.b.h5 = FgetUint8();
502         tmp.b.h6 = FgetUint8();
503         tmp.b.h7 = FgetUint8();
504         return tmp.u64;
505 }
506
507 void FILEIO::FputUint64_LE(uint64_t val)
508 {
509         pair64_t tmp;
510         tmp.u64 = val;
511         FputUint8(tmp.b.l);
512         FputUint8(tmp.b.h);
513         FputUint8(tmp.b.h2);
514         FputUint8(tmp.b.h3);
515         FputUint8(tmp.b.h4);
516         FputUint8(tmp.b.h5);
517         FputUint8(tmp.b.h6);
518         FputUint8(tmp.b.h7);
519 }
520
521 int16_t FILEIO::FgetInt16_LE()
522 {
523         pair16_t tmp;
524         tmp.b.l = FgetUint8();
525         tmp.b.h = FgetUint8();
526         return tmp.s16;
527 }
528
529 void FILEIO::FputInt16_LE(int16_t val)
530 {
531         pair16_t tmp;
532         tmp.s16 = val;
533         FputUint8(tmp.b.l);
534         FputUint8(tmp.b.h);
535 }
536
537 int32_t FILEIO::FgetInt32_LE()
538 {
539         pair32_t tmp;
540         tmp.b.l  = FgetUint8();
541         tmp.b.h  = FgetUint8();
542         tmp.b.h2 = FgetUint8();
543         tmp.b.h3 = FgetUint8();
544         return tmp.s32;
545 }
546
547 void FILEIO::FputInt32_LE(int32_t val)
548 {
549         pair32_t tmp;
550         tmp.s32 = val;
551         FputUint8(tmp.b.l);
552         FputUint8(tmp.b.h);
553         FputUint8(tmp.b.h2);
554         FputUint8(tmp.b.h3);
555 }
556
557 int64_t FILEIO::FgetInt64_LE()
558 {
559         pair64_t tmp;
560         tmp.b.l  = FgetUint8();
561         tmp.b.h  = FgetUint8();
562         tmp.b.h2 = FgetUint8();
563         tmp.b.h3 = FgetUint8();
564         tmp.b.h4 = FgetUint8();
565         tmp.b.h5 = FgetUint8();
566         tmp.b.h6 = FgetUint8();
567         tmp.b.h7 = FgetUint8();
568         return tmp.s64;
569 }
570
571 void FILEIO::FputInt64_LE(int64_t val)
572 {
573         pair64_t tmp;
574         tmp.s64 = val;
575         FputUint8(tmp.b.l);
576         FputUint8(tmp.b.h);
577         FputUint8(tmp.b.h2);
578         FputUint8(tmp.b.h3);
579         FputUint8(tmp.b.h4);
580         FputUint8(tmp.b.h5);
581         FputUint8(tmp.b.h6);
582         FputUint8(tmp.b.h7);
583 }
584
585 uint16_t FILEIO::FgetUint16_BE()
586 {
587         pair16_t tmp;
588         tmp.b.h = FgetUint8();
589         tmp.b.l = FgetUint8();
590         return tmp.u16;
591 }
592
593 void FILEIO::FputUint16_BE(uint16_t val)
594 {
595         pair16_t tmp;
596         tmp.u16 = val;
597         FputUint8(tmp.b.h);
598         FputUint8(tmp.b.l);
599 }
600
601 uint32_t FILEIO::FgetUint32_BE()
602 {
603         pair32_t tmp;
604         tmp.b.h3 = FgetUint8();
605         tmp.b.h2 = FgetUint8();
606         tmp.b.h  = FgetUint8();
607         tmp.b.l  = FgetUint8();
608         return tmp.u32;
609 }
610
611 void FILEIO::FputUint32_BE(uint32_t val)
612 {
613         pair32_t tmp;
614         tmp.u32 = val;
615         FputUint8(tmp.b.h3);
616         FputUint8(tmp.b.h2);
617         FputUint8(tmp.b.h);
618         FputUint8(tmp.b.l);
619 }
620
621 uint64_t FILEIO::FgetUint64_BE()
622 {
623         pair64_t tmp;
624         tmp.b.h7 = FgetUint8();
625         tmp.b.h6 = FgetUint8();
626         tmp.b.h5 = FgetUint8();
627         tmp.b.h4 = FgetUint8();
628         tmp.b.h3 = FgetUint8();
629         tmp.b.h2 = FgetUint8();
630         tmp.b.h  = FgetUint8();
631         tmp.b.l  = FgetUint8();
632         return tmp.u64;
633 }
634
635 void FILEIO::FputUint64_BE(uint64_t val)
636 {
637         pair64_t tmp;
638         tmp.u64 = val;
639         FputUint8(tmp.b.h7);
640         FputUint8(tmp.b.h6);
641         FputUint8(tmp.b.h5);
642         FputUint8(tmp.b.h4);
643         FputUint8(tmp.b.h3);
644         FputUint8(tmp.b.h2);
645         FputUint8(tmp.b.h);
646         FputUint8(tmp.b.l);
647 }
648
649 int16_t FILEIO::FgetInt16_BE()
650 {
651         pair16_t tmp;
652         tmp.b.h = FgetUint8();
653         tmp.b.l = FgetUint8();
654         return tmp.s16;
655 }
656
657 void FILEIO::FputInt16_BE(int16_t val)
658 {
659         pair16_t tmp;
660         tmp.s16 = val;
661         FputUint8(tmp.b.h);
662         FputUint8(tmp.b.l);
663 }
664
665 int32_t FILEIO::FgetInt32_BE()
666 {
667         pair32_t tmp;
668         tmp.b.h3 = FgetUint8();
669         tmp.b.h2 = FgetUint8();
670         tmp.b.h  = FgetUint8();
671         tmp.b.l  = FgetUint8();
672         return tmp.s32;
673 }
674
675 void FILEIO::FputInt32_BE(int32_t val)
676 {
677         pair32_t tmp;
678         tmp.s32 = val;
679         FputUint8(tmp.b.h3);
680         FputUint8(tmp.b.h2);
681         FputUint8(tmp.b.h);
682         FputUint8(tmp.b.l);
683 }
684
685 int64_t FILEIO::FgetInt64_BE()
686 {
687         pair64_t tmp;
688         tmp.b.h7 = FgetUint8();
689         tmp.b.h6 = FgetUint8();
690         tmp.b.h5 = FgetUint8();
691         tmp.b.h4 = FgetUint8();
692         tmp.b.h3 = FgetUint8();
693         tmp.b.h2 = FgetUint8();
694         tmp.b.h  = FgetUint8();
695         tmp.b.l  = FgetUint8();
696         return tmp.s64;
697 }
698
699 void FILEIO::FputInt64_BE(int64_t val)
700 {
701         pair64_t tmp;
702         tmp.s64 = val;
703         FputUint8(tmp.b.h7);
704         FputUint8(tmp.b.h6);
705         FputUint8(tmp.b.h5);
706         FputUint8(tmp.b.h4);
707         FputUint8(tmp.b.h3);
708         FputUint8(tmp.b.h2);
709         FputUint8(tmp.b.h);
710         FputUint8(tmp.b.l);
711 }
712
713 int FILEIO::Fgetc()
714 {
715 #ifdef USE_ZLIB
716         if(gz != NULL) {
717                 return gzgetc(gz);
718         } else
719 #endif
720         {
721                 if(fp != NULL) {
722                         return getc(fp);
723                 } else {
724                         return 0;
725                 }
726         }
727 }
728
729 int FILEIO::Fputc(int c)
730 {
731 #ifdef USE_ZLIB
732         if(gz != NULL) {
733                 return gzputc(gz, c);
734         } else
735 #endif
736         {
737                 if(fp != NULL) {
738                         return fputc(c, fp);
739                 } else {
740                         return 0;
741                 }
742         }
743 }
744
745 char *FILEIO::Fgets(char *str, int n)
746 {
747 #ifdef USE_ZLIB
748         if(gz != NULL) {
749                 return gzgets(gz, str, n);
750         } else
751 #endif
752         {
753                 if(fp != NULL) {
754                         return fgets(str, n, fp);
755                 } else {
756                         return 0;
757                 }
758         }
759 }
760
761 int FILEIO::Fprintf(const char* format, ...)
762 {
763         va_list ap;
764         char buffer[1024];
765         
766         va_start(ap, format);
767         my_vsprintf_s(buffer, 1024, format, ap);
768         va_end(ap);
769         
770 #ifdef USE_ZLIB
771         if(gz != NULL) {
772                 return gzprintf(gz, "%s", buffer);
773         } else
774 #endif
775         {
776                 if(fp != NULL) {
777                         return my_fprintf_s(fp, "%s", buffer);
778                 } else {
779                         return 0;
780                 }
781         }
782 }
783
784 size_t FILEIO::Fread(void* buffer, size_t size, size_t count)
785 {
786 #ifdef USE_ZLIB
787         if(gz != NULL) {
788                 return gzfread(buffer, size, count, gz);
789         } else
790 #endif
791         {
792                 if(fp != NULL) {
793                         return fread(buffer, size, count, fp);
794                 } else {
795                         return 0;
796                 }
797         }
798 }
799
800 size_t FILEIO::Fwrite(void* buffer, size_t size, size_t count)
801 {
802 #ifdef USE_ZLIB
803         if(gz != NULL) {
804                 return gzfwrite(buffer, size, count, gz);
805         } else
806 #endif
807         {
808                 if(fp != NULL) {
809                         return fwrite(buffer, size, count, fp);
810                 } else {
811                         return 0;
812                 }
813         }
814 }
815
816 int FILEIO::Fseek(long offset, int origin)
817 {
818 #ifdef USE_ZLIB
819         if(gz != NULL) {
820                 switch(origin) {
821                 case FILEIO_SEEK_CUR:
822                         return gzseek(gz, offset, SEEK_CUR);
823                 case FILEIO_SEEK_END:
824                         return gzseek(gz, offset + gz_size, SEEK_SET);
825                 case FILEIO_SEEK_SET:
826                         return gzseek(gz, offset, SEEK_SET);
827                 }
828         } else
829 #endif
830         {
831                 if(fp == NULL) return -1;
832                 switch(origin) {
833                 case FILEIO_SEEK_CUR:
834                         return fseek(fp, offset, SEEK_CUR);
835                 case FILEIO_SEEK_END:
836                         return fseek(fp, offset, SEEK_END);
837                 case FILEIO_SEEK_SET:
838                         return fseek(fp, offset, SEEK_SET);
839                 }
840         }
841         return -1;
842 }
843
844 long FILEIO::Ftell()
845 {
846 #ifdef USE_ZLIB
847         if(gz != NULL) {
848                 return gztell(gz);
849         } else
850 #endif
851         {
852                 if(fp != NULL) {
853                         return ftell(fp);
854                 } else {
855                         return 0;
856                 }
857         }
858 }
859