2 Skelton for retropc emulator
4 Author : Takeda.Toshiya
10 #if defined(_USE_QT) || defined(_USE_SDL)
18 #include <sys/types.h>
20 #if !defined(Q_OS_WIN)
28 #if !defined(_MSC_VER)
45 #include "zlib-1.2.11/zlib.h"
46 #include "zlib-1.2.11/zconf.h"
50 #if ZLIB_VERNUM < 0x1290
51 inline size_t gzfread(void *buffer, size_t size, size_t count, gzFile file)
53 uint8_t *p = (uint8_t *)buffer;
56 for(i = 0; i < count; i++) {
57 for(int j = 0; j < size; j++) {
59 if(s < 0) return 0; // EOF
65 inline size_t gzfwrite(void *buffer, size_t size, size_t count, gzFile file)
67 uint8_t *p = (uint8_t *)buffer;
71 for(i = 0; i < count; i++) {
72 for(int j = 0; j < size; j++) {
75 if(s < 0) return 0; // EOF
95 bool FILEIO::IsFileExisting(const _TCHAR *file_path)
97 #if defined(_USE_QT) || defined(_USE_SDL)
99 f = fopen(file_path, "r");
105 #elif defined(_WIN32)
106 DWORD attr = GetFileAttributes(file_path);
110 return ((attr & FILE_ATTRIBUTE_DIRECTORY) == 0);
112 return (_taccess(file_path, 0) == 0);
117 # include <sys/types.h>
118 # include <sys/stat.h>
119 # if !defined(Q_OS_WIN)
123 bool FILEIO::IsFileProtected(const _TCHAR *file_path)
125 #if defined(_USE_QT) || defined(_USE_SDL)
127 if(stat(file_path, &st) == 0) {
129 if((st.st_mode & S_IWUSR) == 0) {
131 if((st.st_mode & (S_IWUSR | S_IWGRP | S_IWOTH)) == 0) {
137 #elif defined(_WIN32)
138 return ((GetFileAttributes(file_path) & FILE_ATTRIBUTE_READONLY) != 0);
140 return (_taccess(file_path, 2) != 0);
144 bool FILEIO::RemoveFile(const _TCHAR *file_path)
146 #if defined(_USE_QT) || defined(_USE_SDL)
147 return (remove(file_path) == 0);
148 #elif defined(_WIN32)
149 return (DeleteFile(file_path) != 0);
151 return (_tremove(file_path) == 0); // not supported on wince ???
155 bool FILEIO::RenameFile(const _TCHAR *existing_file_path, const _TCHAR *new_file_path)
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);
162 return (_trename(existing_file_path, new_file_path) == 0);
166 bool FILEIO::Fopen(const _TCHAR *file_path, int mode)
171 my_tcscpy_s(path, _MAX_PATH, file_path);
174 if(check_file_extension(file_path, _T(".gz"))) {
175 return Gzopen(file_path, mode);
179 case FILEIO_READ_BINARY:
180 return ((fp = _tfopen(file_path, _T("rb"))) != NULL);
181 case FILEIO_WRITE_BINARY:
182 return ((fp = _tfopen(file_path, _T("wb"))) != NULL);
183 case FILEIO_READ_WRITE_BINARY:
184 return ((fp = _tfopen(file_path, _T("r+b"))) != NULL);
185 case FILEIO_READ_WRITE_NEW_BINARY:
186 return ((fp = _tfopen(file_path, _T("w+b"))) != NULL);
187 case FILEIO_READ_ASCII:
188 return ((fp = _tfopen(file_path, _T("r"))) != NULL);
189 case FILEIO_WRITE_ASCII:
190 return ((fp = _tfopen(file_path, _T("w"))) != NULL);
191 case FILEIO_WRITE_APPEND_ASCII:
192 return ((fp = _tfopen(file_path, _T("a"))) != NULL);
193 case FILEIO_READ_WRITE_ASCII:
194 return ((fp = _tfopen(file_path, _T("r+"))) != NULL);
195 case FILEIO_READ_WRITE_NEW_ASCII:
196 return ((fp = _tfopen(file_path, _T("w+"))) != NULL);
197 case FILEIO_READ_WRITE_APPEND_ASCII:
198 return ((fp = _tfopen(file_path, _T("a+"))) != NULL);
204 bool FILEIO::Gzopen(const _TCHAR *file_path, int mode)
209 case FILEIO_READ_BINARY:
210 // case FILEIO_READ_WRITE_BINARY:
211 case FILEIO_READ_ASCII:
212 // case FILEIO_READ_WRITE_ASCII:
213 // case FILEIO_READ_WRITE_APPEND_ASCII:
214 if((fp = _tfopen(file_path, _T("rb"))) != NULL) {
216 uint8_t data[10], name[_MAX_PATH] = {0};
217 fread(data, 10, 1, fp);
218 if(data[0] == 0x1f && data[1] == 0x8b && data[2] == 0x08) {
221 fseek(fp, 2, SEEK_CUR);
225 fread(data + 4, 2, 1, fp);
226 fseek(fp, data[4] | (data[5] << 8), SEEK_CUR);
229 // read original file name
230 fread(name, sizeof(name), 1, fp);
231 my_stprintf_s(path, _MAX_PATH, _T("%s%s"), get_parent_dir(path), (char *)name);
233 // get uncompressed input size
234 fseek(fp, -4, SEEK_END);
235 fread(data, 4, 1, fp);
236 gz_size = data[0] | (data[1] << 8) | (data[2] << 16) | (data[3] << 24);
247 case FILEIO_READ_BINARY:
248 return ((gz = gzopen(tchar_to_char(file_path), "rb")) != NULL);
249 // case FILEIO_WRITE_BINARY:
250 // return ((fp = _tfopen(file_path, _T("wb"))) != NULL);
251 // case FILEIO_READ_WRITE_BINARY:
252 // return ((fp = _tfopen(file_path, _T("r+b"))) != NULL);
253 // case FILEIO_READ_WRITE_NEW_BINARY:
254 // return ((fp = _tfopen(file_path, _T("w+b"))) != NULL);
255 case FILEIO_READ_ASCII:
256 return ((gz = gzopen(tchar_to_char(file_path), "r")) != NULL);
257 case FILEIO_WRITE_ASCII:
258 return ((gz = gzopen(tchar_to_char(file_path), "w")) != NULL);
259 // case FILEIO_WRITE_APPEND_ASCII:
260 // return ((fp = _tfopen(file_path, _T("a"))) != NULL);
261 // case FILEIO_READ_WRITE_ASCII:
262 // return ((fp = _tfopen(file_path, _T("r+"))) != NULL);
263 // case FILEIO_READ_WRITE_NEW_ASCII:
264 // return ((fp = _tfopen(file_path, _T("w+"))) != NULL);
265 // case FILEIO_READ_WRITE_APPEND_ASCII:
266 // return ((fp = _tfopen(file_path, _T("a+"))) != NULL);
272 void FILEIO::Fclose()
287 long FILEIO::FileLength()
290 Fseek(0, FILEIO_SEEK_END);
292 Fseek(pos, FILEIO_SEEK_SET);
296 #define GET_VALUE(type) \
297 uint8_t buffer[sizeof(type)]; \
298 type *tmpv = (type *)buffer; \
299 Fread(buffer, sizeof(buffer), 1); \
302 #define PUT_VALUE(type, v) \
303 Fwrite(&v, sizeof(type), 1)
305 bool FILEIO::FgetBool()
310 void FILEIO::FputBool(bool val)
312 PUT_VALUE(bool, val);
315 uint8_t FILEIO::FgetUint8()
320 void FILEIO::FputUint8(uint8_t val)
322 PUT_VALUE(uint8_t, val);
325 uint16_t FILEIO::FgetUint16()
330 void FILEIO::FputUint16(uint16_t val)
332 PUT_VALUE(uint16_t, val);
335 uint32_t FILEIO::FgetUint32()
340 void FILEIO::FputUint32(uint32_t val)
342 PUT_VALUE(uint32_t, val);
345 uint64_t FILEIO::FgetUint64()
350 void FILEIO::FputUint64(uint64_t val)
352 PUT_VALUE(uint64_t, val);
355 int8_t FILEIO::FgetInt8()
360 void FILEIO::FputInt8(int8_t val)
362 PUT_VALUE(int8_t, val);
365 int16_t FILEIO::FgetInt16()
370 void FILEIO::FputInt16(int16_t val)
372 PUT_VALUE(int16_t, val);
375 int32_t FILEIO::FgetInt32()
380 void FILEIO::FputInt32(int32_t val)
382 PUT_VALUE(int32_t, val);
385 int64_t FILEIO::FgetInt64()
390 void FILEIO::FputInt64(int64_t val)
392 PUT_VALUE(int64_t, val);
395 float FILEIO::FgetFloat()
400 void FILEIO::FputFloat(float val)
402 PUT_VALUE(float, val);
405 double FILEIO::FgetDouble()
410 void FILEIO::FputDouble(double val)
412 PUT_VALUE(double, val);
417 #ifdef __BIG_ENDIAN__
429 #ifdef __BIG_ENDIAN__
430 uint8_t h3, h2, h, l;
432 uint8_t l, h, h2, h3;
441 #ifdef __BIG_ENDIAN__
442 uint8_t h7, h6, h5, h4, h3, h2, h, l;
444 uint8_t l, h, h2, h3, h4, h5, h6, h7;
451 uint16_t FILEIO::FgetUint16_LE()
454 tmp.b.l = FgetUint8();
455 tmp.b.h = FgetUint8();
459 void FILEIO::FputUint16_LE(uint16_t val)
467 uint32_t FILEIO::FgetUint32_LE()
470 tmp.b.l = FgetUint8();
471 tmp.b.h = FgetUint8();
472 tmp.b.h2 = FgetUint8();
473 tmp.b.h3 = FgetUint8();
477 void FILEIO::FputUint32_LE(uint32_t val)
487 uint64_t FILEIO::FgetUint64_LE()
490 tmp.b.l = FgetUint8();
491 tmp.b.h = FgetUint8();
492 tmp.b.h2 = FgetUint8();
493 tmp.b.h3 = FgetUint8();
494 tmp.b.h4 = FgetUint8();
495 tmp.b.h5 = FgetUint8();
496 tmp.b.h6 = FgetUint8();
497 tmp.b.h7 = FgetUint8();
501 void FILEIO::FputUint64_LE(uint64_t val)
515 int16_t FILEIO::FgetInt16_LE()
518 tmp.b.l = FgetUint8();
519 tmp.b.h = FgetUint8();
523 void FILEIO::FputInt16_LE(int16_t val)
531 int32_t FILEIO::FgetInt32_LE()
534 tmp.b.l = FgetUint8();
535 tmp.b.h = FgetUint8();
536 tmp.b.h2 = FgetUint8();
537 tmp.b.h3 = FgetUint8();
541 void FILEIO::FputInt32_LE(int32_t val)
551 int64_t FILEIO::FgetInt64_LE()
554 tmp.b.l = FgetUint8();
555 tmp.b.h = FgetUint8();
556 tmp.b.h2 = FgetUint8();
557 tmp.b.h3 = FgetUint8();
558 tmp.b.h4 = FgetUint8();
559 tmp.b.h5 = FgetUint8();
560 tmp.b.h6 = FgetUint8();
561 tmp.b.h7 = FgetUint8();
565 void FILEIO::FputInt64_LE(int64_t val)
579 uint16_t FILEIO::FgetUint16_BE()
582 tmp.b.h = FgetUint8();
583 tmp.b.l = FgetUint8();
587 void FILEIO::FputUint16_BE(uint16_t val)
595 uint32_t FILEIO::FgetUint32_BE()
598 tmp.b.h3 = FgetUint8();
599 tmp.b.h2 = FgetUint8();
600 tmp.b.h = FgetUint8();
601 tmp.b.l = FgetUint8();
605 void FILEIO::FputUint32_BE(uint32_t val)
615 uint64_t FILEIO::FgetUint64_BE()
618 tmp.b.h7 = FgetUint8();
619 tmp.b.h6 = FgetUint8();
620 tmp.b.h5 = FgetUint8();
621 tmp.b.h4 = FgetUint8();
622 tmp.b.h3 = FgetUint8();
623 tmp.b.h2 = FgetUint8();
624 tmp.b.h = FgetUint8();
625 tmp.b.l = FgetUint8();
629 void FILEIO::FputUint64_BE(uint64_t val)
643 int16_t FILEIO::FgetInt16_BE()
646 tmp.b.h = FgetUint8();
647 tmp.b.l = FgetUint8();
651 void FILEIO::FputInt16_BE(int16_t val)
659 int32_t FILEIO::FgetInt32_BE()
662 tmp.b.h3 = FgetUint8();
663 tmp.b.h2 = FgetUint8();
664 tmp.b.h = FgetUint8();
665 tmp.b.l = FgetUint8();
669 void FILEIO::FputInt32_BE(int32_t val)
679 int64_t FILEIO::FgetInt64_BE()
682 tmp.b.h7 = FgetUint8();
683 tmp.b.h6 = FgetUint8();
684 tmp.b.h5 = FgetUint8();
685 tmp.b.h4 = FgetUint8();
686 tmp.b.h3 = FgetUint8();
687 tmp.b.h2 = FgetUint8();
688 tmp.b.h = FgetUint8();
689 tmp.b.l = FgetUint8();
693 void FILEIO::FputInt64_BE(int64_t val)
723 int FILEIO::Fputc(int c)
727 return gzputc(gz, c);
739 char *FILEIO::Fgets(char *str, int n)
743 return gzgets(gz, str, n);
748 return fgets(str, n, fp);
755 _TCHAR *FILEIO::Fgetts(_TCHAR *str, int n)
759 #if defined(_UNICODE) && defined(SUPPORT_TCHAR_TYPE)
760 char *str_mb = (char *)calloc(sizeof(char), n + 1);
761 gzgets(gz, str_mb, n);
762 my_swprintf_s(str, n, L"%s", char_to_wchar(str_mb));
766 return gzgets(gz, str, n);
770 return _fgetts(str, n, fp);
773 int FILEIO::Fprintf(const char* format, ...)
778 va_start(ap, format);
779 my_vsprintf_s(buffer, 1024, format, ap);
784 return gzprintf(gz, "%s", buffer);
789 return my_fprintf_s(fp, "%s", buffer);
796 int FILEIO::Ftprintf(const _TCHAR* format, ...)
801 va_start(ap, format);
802 my_vstprintf_s(buffer, 1024, format, ap);
807 return gzprintf(gz, "%s", tchar_to_char(buffer));
810 return my_ftprintf_s(fp, _T("%s"), buffer);
813 size_t FILEIO::Fread(void* buffer, size_t size, size_t count)
817 return gzfread(buffer, size, count, gz);
822 return fread(buffer, size, count, fp);
829 size_t FILEIO::Fwrite(const void* buffer, size_t size, size_t count)
833 return gzfwrite(buffer, size, count, gz);
838 return fwrite(buffer, size, count, fp);
845 int FILEIO::Fseek(long offset, int origin)
850 case FILEIO_SEEK_CUR:
851 return gzseek(gz, offset, SEEK_CUR);
852 case FILEIO_SEEK_END:
853 return gzseek(gz, offset + gz_size, SEEK_SET);
854 case FILEIO_SEEK_SET:
855 return gzseek(gz, offset, SEEK_SET);
860 if(fp == NULL) return -1;
862 case FILEIO_SEEK_CUR:
863 return fseek(fp, offset, SEEK_CUR);
864 case FILEIO_SEEK_END:
865 return fseek(fp, offset, SEEK_END);
866 case FILEIO_SEEK_SET:
867 return fseek(fp, offset, SEEK_SET);
890 bool FILEIO::Fcompare(const void* buffer, size_t size)
893 void *tmp = malloc(size);
895 if(Fread(tmp, size, 1) == 1) {
896 result = (memcmp(buffer, tmp, size) == 0);