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"
48 #if defined(ZLIB_VERNUM) && (ZLIB_VERNUM < 0x1290)
49 inline size_t gzfread(void *buffer, size_t size, size_t count, gzFile file)
51 uint8_t *p = (uint8_t *)buffer;
53 for(i = 0; i < count; i++) {
54 for(int j = 0; j < size; j++) {
56 if(s < 0) return 0; // EOF
62 inline size_t gzfwrite(void *buffer, size_t size, size_t count, gzFile file)
64 uint8_t *p = (uint8_t *)buffer;
66 for(i = 0; i < count; i++) {
67 for(int j = 0; j < size; j++) {
69 int s = gzputc(file, n);
70 if(s < 0) return 0; // EOF
91 bool FILEIO::IsFileExisting(const _TCHAR *file_path)
93 #if defined(_USE_QT) || defined(_USE_SDL)
95 f = fopen(file_path, "r");
101 #elif defined(_WIN32)
102 DWORD attr = GetFileAttributes(file_path);
106 return ((attr & FILE_ATTRIBUTE_DIRECTORY) == 0);
108 return (_taccess(file_path, 0) == 0);
113 # include <sys/types.h>
114 # include <sys/stat.h>
115 # if !defined(Q_OS_WIN)
119 bool FILEIO::IsFileProtected(const _TCHAR *file_path)
121 #if defined(_USE_QT) || defined(_USE_SDL)
123 if(stat(file_path, &st) == 0) {
125 if((st.st_mode & S_IWUSR) == 0) {
127 if((st.st_mode & (S_IWUSR | S_IWGRP | S_IWOTH)) == 0) {
133 #elif defined(_WIN32)
134 return ((GetFileAttributes(file_path) & FILE_ATTRIBUTE_READONLY) != 0);
136 return (_taccess(file_path, 2) != 0);
140 bool FILEIO::RemoveFile(const _TCHAR *file_path)
142 #if defined(_USE_QT) || defined(_USE_SDL)
143 return (remove(file_path) == 0);
144 #elif defined(_WIN32)
145 return (DeleteFile(file_path) != 0);
147 return (_tremove(file_path) == 0); // not supported on wince ???
151 bool FILEIO::RenameFile(const _TCHAR *existing_file_path, const _TCHAR *new_file_path)
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);
158 return (_trename(existing_file_path, new_file_path) == 0);
162 bool FILEIO::Fopen(const _TCHAR *file_path, int mode)
167 my_tcscpy_s(path, _MAX_PATH, file_path);
170 if(check_file_extension(file_path, _T(".gz"))) {
171 return Gzopen(file_path, 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);
200 bool FILEIO::Gzopen(const _TCHAR *file_path, int 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) {
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) {
217 fseek(fp, 2, SEEK_CUR);
221 fread(data + 4, 2, 1, fp);
222 fseek(fp, data[4] | (data[5] << 8), SEEK_CUR);
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);
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);
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);
268 void FILEIO::Fclose()
283 long FILEIO::FileLength()
286 Fseek(0, FILEIO_SEEK_END);
288 Fseek(pos, FILEIO_SEEK_SET);
292 #define GET_VALUE(type) \
293 uint8_t buffer[sizeof(type)] = {0}; \
294 Fread(buffer, sizeof(buffer), 1); \
295 return *(type *)buffer;
297 #define PUT_VALUE(type, v) \
298 Fwrite(&v, sizeof(type), 1)
300 bool FILEIO::FgetBool()
305 void FILEIO::FputBool(bool val)
307 PUT_VALUE(bool, val);
310 uint8_t FILEIO::FgetUint8()
315 void FILEIO::FputUint8(uint8_t val)
317 PUT_VALUE(uint8_t, val);
320 uint16_t FILEIO::FgetUint16()
325 void FILEIO::FputUint16(uint16_t val)
327 PUT_VALUE(uint16_t, val);
330 uint32_t FILEIO::FgetUint32()
335 void FILEIO::FputUint32(uint32_t val)
337 PUT_VALUE(uint32_t, val);
340 uint64_t FILEIO::FgetUint64()
345 void FILEIO::FputUint64(uint64_t val)
347 PUT_VALUE(uint64_t, val);
350 int8_t FILEIO::FgetInt8()
355 void FILEIO::FputInt8(int8_t val)
357 PUT_VALUE(int8_t, val);
360 int16_t FILEIO::FgetInt16()
365 void FILEIO::FputInt16(int16_t val)
367 PUT_VALUE(int16_t, val);
370 int32_t FILEIO::FgetInt32()
375 void FILEIO::FputInt32(int32_t val)
377 PUT_VALUE(int32_t, val);
380 int64_t FILEIO::FgetInt64()
385 void FILEIO::FputInt64(int64_t val)
387 PUT_VALUE(int64_t, val);
390 float FILEIO::FgetFloat()
395 void FILEIO::FputFloat(float val)
397 PUT_VALUE(float, val);
400 double FILEIO::FgetDouble()
405 void FILEIO::FputDouble(double val)
407 PUT_VALUE(double, val);
413 #ifdef __BIG_ENDIAN__
414 uint8_t h3, h2, h, l;
416 uint8_t l, h, h2, h3;
424 uint16_t FILEIO::FgetUint16_LE()
427 tmp.b.l = FgetUint8();
428 tmp.b.h = FgetUint8();
432 void FILEIO::FputUint16_LE(uint16_t val)
440 uint32_t FILEIO::FgetUint32_LE()
443 tmp.b.l = FgetUint8();
444 tmp.b.h = FgetUint8();
445 tmp.b.h2 = FgetUint8();
446 tmp.b.h3 = FgetUint8();
450 void FILEIO::FputUint32_LE(uint32_t val)
460 uint64_t FILEIO::FgetUint64_LE()
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();
474 void FILEIO::FputUint64_LE(uint64_t val)
488 int16_t FILEIO::FgetInt16_LE()
491 tmp.b.l = FgetUint8();
492 tmp.b.h = FgetUint8();
496 void FILEIO::FputInt16_LE(int16_t val)
504 int32_t FILEIO::FgetInt32_LE()
507 tmp.b.l = FgetUint8();
508 tmp.b.h = FgetUint8();
509 tmp.b.h2 = FgetUint8();
510 tmp.b.h3 = FgetUint8();
514 void FILEIO::FputInt32_LE(int32_t val)
524 int64_t FILEIO::FgetInt64_LE()
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();
538 void FILEIO::FputInt64_LE(int64_t val)
552 uint16_t FILEIO::FgetUint16_BE()
555 tmp.b.h = FgetUint8();
556 tmp.b.l = FgetUint8();
560 void FILEIO::FputUint16_BE(uint16_t val)
568 uint32_t FILEIO::FgetUint32_BE()
571 tmp.b.h3 = FgetUint8();
572 tmp.b.h2 = FgetUint8();
573 tmp.b.h = FgetUint8();
574 tmp.b.l = FgetUint8();
578 void FILEIO::FputUint32_BE(uint32_t val)
588 uint64_t FILEIO::FgetUint64_BE()
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();
602 void FILEIO::FputUint64_BE(uint64_t val)
616 int16_t FILEIO::FgetInt16_BE()
619 tmp.b.h = FgetUint8();
620 tmp.b.l = FgetUint8();
624 void FILEIO::FputInt16_BE(int16_t val)
632 int32_t FILEIO::FgetInt32_BE()
635 tmp.b.h3 = FgetUint8();
636 tmp.b.h2 = FgetUint8();
637 tmp.b.h = FgetUint8();
638 tmp.b.l = FgetUint8();
642 void FILEIO::FputInt32_BE(int32_t val)
652 int64_t FILEIO::FgetInt64_BE()
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();
666 void FILEIO::FputInt64_BE(int64_t val)
695 int FILEIO::Fputc(int c)
699 return gzputc(gz, c);
710 char *FILEIO::Fgets(char *str, int n)
714 return gzgets(gz, str, n);
719 return fgets(str, n, fp);
725 _TCHAR *FILEIO::Fgetts(_TCHAR *str, int n)
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));
736 return gzgets(gz, str, n);
740 return _fgetts(str, n, fp);
743 int FILEIO::Fprintf(const char* format, ...)
748 va_start(ap, format);
749 my_vsprintf_s(buffer, 1024, format, ap);
754 return gzprintf(gz, "%s", buffer);
758 return my_fprintf_s(fp, "%s", buffer);
763 int FILEIO::Ftprintf(const _TCHAR* format, ...)
768 va_start(ap, format);
769 my_vstprintf_s(buffer, 1024, format, ap);
774 return gzprintf(gz, "%s", tchar_to_char(buffer));
778 return my_ftprintf_s(fp, _T("%s"), buffer);
783 size_t FILEIO::Fread(void* buffer, size_t size, size_t count)
787 return gzfread(buffer, size, count, gz);
791 return fread(buffer, size, count, fp);
796 bool FILEIO::Fflush()
800 return (gzflush(gz, Z_SYNC_FLUSH) == Z_OK) ? true : false;
804 return (fflush(fp) == 0) ? true : false;
808 size_t FILEIO::Fwrite(const void* buffer, size_t size, size_t count)
812 return gzfwrite(buffer, size, count, gz);
816 return fwrite(buffer, size, count, fp);
821 int FILEIO::Fseek(long offset, int 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);
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);
862 bool FILEIO::Fcompare(const void* buffer, size_t size)
865 void *tmp = malloc(size);
867 if(Fread(tmp, size, 1) == 1) {
868 result = (memcmp(buffer, tmp, size) == 0);