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)
47 bool FILEIO::IsFileExisting(const _TCHAR *file_path)
49 #if defined(_USE_QT) || defined(_USE_SDL)
51 f = fopen(file_path, "r");
58 DWORD attr = GetFileAttributes(file_path);
62 return ((attr & FILE_ATTRIBUTE_DIRECTORY) == 0);
64 return (_taccess(file_path, 0) == 0);
69 # include <sys/types.h>
70 # include <sys/stat.h>
71 # if !defined(Q_OS_WIN)
75 bool FILEIO::IsFileProtected(const _TCHAR *file_path)
77 #if defined(_USE_QT) || defined(_USE_SDL)
79 if(stat(file_path, &st) == 0) {
81 if((st.st_mode & S_IWUSR) == 0) {
83 if((st.st_mode & (S_IWUSR | S_IWGRP | S_IWOTH)) == 0) {
90 return ((GetFileAttributes(file_path) & FILE_ATTRIBUTE_READONLY) != 0);
92 return (_taccess(file_path, 2) != 0);
96 bool FILEIO::RemoveFile(const _TCHAR *file_path)
98 #if defined(_USE_QT) || defined(_USE_SDL)
99 return (remove(file_path) == 0);
100 #elif defined(_WIN32)
101 return (DeleteFile(file_path) != 0);
103 return (_tremove(file_path) == 0); // not supported on wince ???
107 bool FILEIO::RenameFile(const _TCHAR *existing_file_path, const _TCHAR *new_file_path)
109 #if defined(_USE_QT) || defined(_USE_SDL)
110 return (rename(existing_file_path, new_file_path) == 0);
111 #elif defined(_WIN32)
112 return (MoveFile(existing_file_path, new_file_path) != 0);
114 return (_trename(existing_file_path, new_file_path) == 0);
118 bool FILEIO::Fopen(const _TCHAR *file_path, int mode)
123 case FILEIO_READ_BINARY:
124 return ((fp = _tfopen(file_path, _T("rb"))) != NULL);
125 case FILEIO_WRITE_BINARY:
126 return ((fp = _tfopen(file_path, _T("wb"))) != NULL);
127 case FILEIO_READ_WRITE_BINARY:
128 return ((fp = _tfopen(file_path, _T("r+b"))) != NULL);
129 case FILEIO_READ_WRITE_NEW_BINARY:
130 return ((fp = _tfopen(file_path, _T("w+b"))) != NULL);
131 case FILEIO_READ_ASCII:
132 return ((fp = _tfopen(file_path, _T("r"))) != NULL);
133 case FILEIO_WRITE_ASCII:
134 return ((fp = _tfopen(file_path, _T("w"))) != NULL);
135 case FILEIO_WRITE_APPEND_ASCII:
136 return ((fp = _tfopen(file_path, _T("a"))) != NULL);
137 case FILEIO_READ_WRITE_ASCII:
138 return ((fp = _tfopen(file_path, _T("r+"))) != NULL);
139 case FILEIO_READ_WRITE_NEW_ASCII:
140 return ((fp = _tfopen(file_path, _T("w+"))) != NULL);
141 case FILEIO_READ_WRITE_APPEND_ASCII:
142 return ((fp = _tfopen(file_path, _T("a+"))) != NULL);
147 void FILEIO::Fclose()
155 uint32_t FILEIO::FileLength()
157 long pos = ftell(fp);
158 fseek(fp, 0, SEEK_END);
159 long len = ftell(fp);
160 fseek(fp, pos, SEEK_SET);
161 return (uint32_t)len;
164 #define GET_VALUE(type) \
165 uint8_t buffer[sizeof(type)]; \
166 type *tmpv = (type *)buffer; \
167 fread(buffer, sizeof(buffer), 1, fp); \
170 #define PUT_VALUE(type, v) \
171 fwrite(&v, sizeof(type), 1, fp)
173 bool FILEIO::FgetBool()
178 void FILEIO::FputBool(bool val)
180 PUT_VALUE(bool, val);
183 uint8_t FILEIO::FgetUint8()
188 void FILEIO::FputUint8(uint8_t val)
190 PUT_VALUE(uint8_t, val);
193 uint16_t FILEIO::FgetUint16()
198 void FILEIO::FputUint16(uint16_t val)
200 PUT_VALUE(uint16_t, val);
203 uint32_t FILEIO::FgetUint32()
208 void FILEIO::FputUint32(uint32_t val)
210 PUT_VALUE(uint32_t, val);
213 uint64_t FILEIO::FgetUint64()
218 void FILEIO::FputUint64(uint64_t val)
220 PUT_VALUE(uint64_t, val);
223 int8_t FILEIO::FgetInt8()
228 void FILEIO::FputInt8(int8_t val)
230 PUT_VALUE(int8_t, val);
233 int16_t FILEIO::FgetInt16()
238 void FILEIO::FputInt16(int16_t val)
240 PUT_VALUE(int16_t, val);
243 int32_t FILEIO::FgetInt32()
248 void FILEIO::FputInt32(int32_t val)
250 PUT_VALUE(int32_t, val);
253 int64_t FILEIO::FgetInt64()
258 void FILEIO::FputInt64(int64_t val)
260 PUT_VALUE(int64_t, val);
263 float FILEIO::FgetFloat()
268 void FILEIO::FputFloat(float val)
270 PUT_VALUE(float, val);
273 double FILEIO::FgetDouble()
278 void FILEIO::FputDouble(double val)
280 PUT_VALUE(double, val);
285 #ifdef __BIG_ENDIAN__
297 #ifdef __BIG_ENDIAN__
298 uint8_t h3, h2, h, l;
300 uint8_t l, h, h2, h3;
309 #ifdef __BIG_ENDIAN__
310 uint8_t h7, h6, h5, h4, h3, h2, h, l;
312 uint8_t l, h, h2, h3, h4, h5, h6, h7;
319 uint16_t FILEIO::FgetUint16_LE()
322 tmp.b.l = FgetUint8();
323 tmp.b.h = FgetUint8();
327 void FILEIO::FputUint16_LE(uint16_t val)
335 uint32_t FILEIO::FgetUint32_LE()
338 tmp.b.l = FgetUint8();
339 tmp.b.h = FgetUint8();
340 tmp.b.h2 = FgetUint8();
341 tmp.b.h3 = FgetUint8();
345 void FILEIO::FputUint32_LE(uint32_t val)
355 uint64_t FILEIO::FgetUint64_LE()
358 tmp.b.l = FgetUint8();
359 tmp.b.h = FgetUint8();
360 tmp.b.h2 = FgetUint8();
361 tmp.b.h3 = FgetUint8();
362 tmp.b.h4 = FgetUint8();
363 tmp.b.h5 = FgetUint8();
364 tmp.b.h6 = FgetUint8();
365 tmp.b.h7 = FgetUint8();
369 void FILEIO::FputUint64_LE(uint64_t val)
383 int16_t FILEIO::FgetInt16_LE()
386 tmp.b.l = FgetUint8();
387 tmp.b.h = FgetUint8();
391 void FILEIO::FputInt16_LE(int16_t val)
399 int32_t FILEIO::FgetInt32_LE()
402 tmp.b.l = FgetUint8();
403 tmp.b.h = FgetUint8();
404 tmp.b.h2 = FgetUint8();
405 tmp.b.h3 = FgetUint8();
409 void FILEIO::FputInt32_LE(int32_t val)
419 int64_t FILEIO::FgetInt64_LE()
422 tmp.b.l = FgetUint8();
423 tmp.b.h = FgetUint8();
424 tmp.b.h2 = FgetUint8();
425 tmp.b.h3 = FgetUint8();
426 tmp.b.h4 = FgetUint8();
427 tmp.b.h5 = FgetUint8();
428 tmp.b.h6 = FgetUint8();
429 tmp.b.h7 = FgetUint8();
433 void FILEIO::FputInt64_LE(int64_t val)
447 uint16_t FILEIO::FgetUint16_BE()
450 tmp.b.h = FgetUint8();
451 tmp.b.l = FgetUint8();
455 void FILEIO::FputUint16_BE(uint16_t val)
463 uint32_t FILEIO::FgetUint32_BE()
466 tmp.b.h3 = FgetUint8();
467 tmp.b.h2 = FgetUint8();
468 tmp.b.h = FgetUint8();
469 tmp.b.l = FgetUint8();
473 void FILEIO::FputUint32_BE(uint32_t val)
483 uint64_t FILEIO::FgetUint64_BE()
486 tmp.b.h7 = FgetUint8();
487 tmp.b.h6 = FgetUint8();
488 tmp.b.h5 = FgetUint8();
489 tmp.b.h4 = FgetUint8();
490 tmp.b.h3 = FgetUint8();
491 tmp.b.h2 = FgetUint8();
492 tmp.b.h = FgetUint8();
493 tmp.b.l = FgetUint8();
497 void FILEIO::FputUint64_BE(uint64_t val)
511 int16_t FILEIO::FgetInt16_BE()
514 tmp.b.h = FgetUint8();
515 tmp.b.l = FgetUint8();
519 void FILEIO::FputInt16_BE(int16_t val)
527 int32_t FILEIO::FgetInt32_BE()
530 tmp.b.h3 = FgetUint8();
531 tmp.b.h2 = FgetUint8();
532 tmp.b.h = FgetUint8();
533 tmp.b.l = FgetUint8();
537 void FILEIO::FputInt32_BE(int32_t val)
547 int64_t FILEIO::FgetInt64_BE()
550 tmp.b.h7 = FgetUint8();
551 tmp.b.h6 = FgetUint8();
552 tmp.b.h5 = FgetUint8();
553 tmp.b.h4 = FgetUint8();
554 tmp.b.h3 = FgetUint8();
555 tmp.b.h2 = FgetUint8();
556 tmp.b.h = FgetUint8();
557 tmp.b.l = FgetUint8();
561 void FILEIO::FputInt64_BE(int64_t val)
580 int FILEIO::Fputc(int c)
585 char *FILEIO::Fgets(char *str, int n)
587 return fgets(str, n, fp);
590 int FILEIO::Fprintf(const char* format, ...)
595 va_start(ap, format);
596 my_vsprintf_s(buffer, 1024, format, ap);
599 return my_fprintf_s(fp, "%s", buffer);
602 uint32_t FILEIO::Fread(void* buffer, uint32_t size, uint32_t count)
604 return fread(buffer, size, count, fp);
607 uint32_t FILEIO::Fwrite(void* buffer, uint32_t size, uint32_t count)
609 return fwrite(buffer, size, count, fp);
612 uint32_t FILEIO::Fseek(long offset, int origin)
615 case FILEIO_SEEK_CUR:
616 return fseek(fp, offset, SEEK_CUR);
617 case FILEIO_SEEK_END:
618 return fseek(fp, offset, SEEK_END);
619 case FILEIO_SEEK_SET:
620 return fseek(fp, offset, SEEK_SET);
625 uint32_t FILEIO::Ftell()