OSDN Git Service

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