2 Skelton for retropc emulator
4 Author : Takeda.Toshiya
14 #if !defined(_MSC_VER)
33 bool FILEIO::IsFileExisting(const _TCHAR *file_path)
35 #if defined(_USE_QT) || defined(_USE_SDL)
37 f = fopen(file_path, "r");
45 DWORD attr = GetFileAttributes(file_path);
49 return ((attr & FILE_ATTRIBUTE_DIRECTORY) == 0);
51 return (_taccess(file_path, 0) == 0);
56 # include <sys/types.h>
57 # include <sys/stat.h>
58 # if !defined(Q_OS_WIN)
62 bool FILEIO::IsFileProtected(const _TCHAR *file_path)
64 #if defined(_USE_QT) || defined(_USE_SDL)
66 if(stat(file_path, &st) == 0) {
68 if((st.st_mode & S_IWUSR) == 0) return true;
70 if((st.st_mode & (S_IWUSR | S_IWGRP | S_IWOTH)) == 0) return true;
76 return ((GetFileAttributes(file_path) & FILE_ATTRIBUTE_READONLY) != 0);
78 return (_taccess(file_path, 2) != 0);
83 bool FILEIO::RemoveFile(const _TCHAR *file_path)
85 #if defined(_USE_QT) || defined(_USE_SDL)
86 return (remove(file_path) == 0);
89 return (DeleteFile(file_path) != 0);
91 return (_tremove(file_path) == 0); // not supported on wince ???
96 bool FILEIO::RenameFile(const _TCHAR *existing_file_path, const _TCHAR *new_file_path)
99 return (rename(existing_file_path, new_file_path) == 0);
102 return (MoveFile(existing_file_path, new_file_path) != 0);
104 return (_trename(existing_file_path, new_file_path) == 0);
109 bool FILEIO::Fopen(const _TCHAR *file_path, int mode)
114 case FILEIO_READ_BINARY:
115 return ((fp = _tfopen(file_path, _T("rb"))) != NULL);
116 case FILEIO_WRITE_BINARY:
117 return ((fp = _tfopen(file_path, _T("wb"))) != NULL);
118 case FILEIO_READ_WRITE_BINARY:
119 return ((fp = _tfopen(file_path, _T("r+b"))) != NULL);
120 case FILEIO_READ_WRITE_NEW_BINARY:
121 return ((fp = _tfopen(file_path, _T("w+b"))) != NULL);
122 case FILEIO_READ_ASCII:
123 return ((fp = _tfopen(file_path, _T("r"))) != NULL);
124 case FILEIO_WRITE_ASCII:
125 return ((fp = _tfopen(file_path, _T("w"))) != NULL);
126 case FILEIO_WRITE_APPEND_ASCII:
127 return ((fp = _tfopen(file_path, _T("a"))) != NULL);
128 case FILEIO_READ_WRITE_ASCII:
129 return ((fp = _tfopen(file_path, _T("r+"))) != NULL);
130 case FILEIO_READ_WRITE_NEW_ASCII:
131 return ((fp = _tfopen(file_path, _T("w+"))) != NULL);
132 case FILEIO_READ_WRITE_APPEND_ASCII:
133 return ((fp = _tfopen(file_path, _T("a+"))) != NULL);
138 void FILEIO::Fclose()
146 uint32_t FILEIO::FileLength()
148 long pos = ftell(fp);
149 fseek(fp, 0, SEEK_END);
150 long len = ftell(fp);
151 fseek(fp, pos, SEEK_SET);
152 return (uint32_t)len;
155 #define GET_VALUE(type) \
156 uint8_t buffer[sizeof(type)]; \
157 fread(buffer, sizeof(buffer), 1, fp); \
158 return *(type *)buffer
160 #define PUT_VALUE(type, v) \
161 fwrite(&v, sizeof(type), 1, fp)
163 bool FILEIO::FgetBool()
168 void FILEIO::FputBool(bool val)
170 PUT_VALUE(bool, val);
173 uint8_t FILEIO::FgetUint8()
178 void FILEIO::FputUint8(uint8_t val)
180 PUT_VALUE(uint8_t, val);
183 uint16_t FILEIO::FgetUint16()
188 void FILEIO::FputUint16(uint16_t val)
190 PUT_VALUE(uint16_t, val);
193 uint32_t FILEIO::FgetUint32()
198 void FILEIO::FputUint32(uint32_t val)
200 PUT_VALUE(uint32_t, val);
203 uint64_t FILEIO::FgetUint64()
208 void FILEIO::FputUint64(uint64_t val)
210 PUT_VALUE(uint64_t, val);
213 int8_t FILEIO::FgetInt8()
218 void FILEIO::FputInt8(int8_t val)
220 PUT_VALUE(int8_t, val);
223 int16_t FILEIO::FgetInt16()
228 void FILEIO::FputInt16(int16_t val)
230 PUT_VALUE(int16_t, val);
233 int32_t FILEIO::FgetInt32()
238 void FILEIO::FputInt32(int32_t val)
240 PUT_VALUE(int32_t, val);
243 int64_t FILEIO::FgetInt64()
248 void FILEIO::FputInt64(int64_t val)
250 PUT_VALUE(int64_t, val);
253 float FILEIO::FgetFloat()
258 void FILEIO::FputFloat(float val)
260 PUT_VALUE(float, val);
263 double FILEIO::FgetDouble()
268 void FILEIO::FputDouble(double val)
270 PUT_VALUE(double, val);
275 #ifdef __BIG_ENDIAN__
287 #ifdef __BIG_ENDIAN__
288 uint8_t h3, h2, h, l;
290 uint8_t l, h, h2, h3;
299 #ifdef __BIG_ENDIAN__
300 uint8_t h7, h6, h5, h4, h3, h2, h, l;
302 uint8_t l, h, h2, h3, h4, h5, h6, h7;
309 uint16_t FILEIO::FgetUint16_LE()
312 tmp.b.l = FgetUint8();
313 tmp.b.h = FgetUint8();
317 void FILEIO::FputUint16_LE(uint16_t val)
325 uint32_t FILEIO::FgetUint32_LE()
328 tmp.b.l = FgetUint8();
329 tmp.b.h = FgetUint8();
330 tmp.b.h2 = FgetUint8();
331 tmp.b.h3 = FgetUint8();
335 void FILEIO::FputUint32_LE(uint32_t val)
345 uint64_t FILEIO::FgetUint64_LE()
348 tmp.b.l = FgetUint8();
349 tmp.b.h = FgetUint8();
350 tmp.b.h2 = FgetUint8();
351 tmp.b.h3 = FgetUint8();
352 tmp.b.h4 = FgetUint8();
353 tmp.b.h5 = FgetUint8();
354 tmp.b.h6 = FgetUint8();
355 tmp.b.h7 = FgetUint8();
359 void FILEIO::FputUint64_LE(uint64_t val)
373 int16_t FILEIO::FgetInt16_LE()
376 tmp.b.l = FgetUint8();
377 tmp.b.h = FgetUint8();
381 void FILEIO::FputInt16_LE(int16_t val)
389 int32_t FILEIO::FgetInt32_LE()
392 tmp.b.l = FgetUint8();
393 tmp.b.h = FgetUint8();
394 tmp.b.h2 = FgetUint8();
395 tmp.b.h3 = FgetUint8();
399 void FILEIO::FputInt32_LE(int32_t val)
409 int64_t FILEIO::FgetInt64_LE()
412 tmp.b.l = FgetUint8();
413 tmp.b.h = FgetUint8();
414 tmp.b.h2 = FgetUint8();
415 tmp.b.h3 = FgetUint8();
416 tmp.b.h4 = FgetUint8();
417 tmp.b.h5 = FgetUint8();
418 tmp.b.h6 = FgetUint8();
419 tmp.b.h7 = FgetUint8();
423 void FILEIO::FputInt64_LE(int64_t val)
437 uint16_t FILEIO::FgetUint16_BE()
440 tmp.b.h = FgetUint8();
441 tmp.b.l = FgetUint8();
445 void FILEIO::FputUint16_BE(uint16_t val)
453 uint32_t FILEIO::FgetUint32_BE()
456 tmp.b.h3 = FgetUint8();
457 tmp.b.h2 = FgetUint8();
458 tmp.b.h = FgetUint8();
459 tmp.b.l = FgetUint8();
463 void FILEIO::FputUint32_BE(uint32_t val)
473 uint64_t FILEIO::FgetUint64_BE()
476 tmp.b.h7 = FgetUint8();
477 tmp.b.h6 = FgetUint8();
478 tmp.b.h5 = FgetUint8();
479 tmp.b.h4 = FgetUint8();
480 tmp.b.h3 = FgetUint8();
481 tmp.b.h2 = FgetUint8();
482 tmp.b.h = FgetUint8();
483 tmp.b.l = FgetUint8();
487 void FILEIO::FputUint64_BE(uint64_t val)
501 int16_t FILEIO::FgetInt16_BE()
504 tmp.b.h = FgetUint8();
505 tmp.b.l = FgetUint8();
509 void FILEIO::FputInt16_BE(int16_t val)
517 int32_t FILEIO::FgetInt32_BE()
520 tmp.b.h3 = FgetUint8();
521 tmp.b.h2 = FgetUint8();
522 tmp.b.h = FgetUint8();
523 tmp.b.l = FgetUint8();
527 void FILEIO::FputInt32_BE(int32_t val)
537 int64_t FILEIO::FgetInt64_BE()
540 tmp.b.h7 = FgetUint8();
541 tmp.b.h6 = FgetUint8();
542 tmp.b.h5 = FgetUint8();
543 tmp.b.h4 = FgetUint8();
544 tmp.b.h3 = FgetUint8();
545 tmp.b.h2 = FgetUint8();
546 tmp.b.h = FgetUint8();
547 tmp.b.l = FgetUint8();
551 void FILEIO::FputInt64_BE(int64_t val)
570 int FILEIO::Fputc(int c)
575 char *FILEIO::Fgets(char *str, int n)
577 return fgets(str, n, fp);
580 int FILEIO::Fprintf(const char* format, ...)
585 va_start(ap, format);
586 my_vsprintf_s(buffer, 1024, format, ap);
589 return my_fprintf_s(fp, "%s", buffer);
592 uint32_t FILEIO::Fread(void* buffer, uint32_t size, uint32_t count)
594 return fread(buffer, size, count, fp);
597 uint32_t FILEIO::Fwrite(void* buffer, uint32_t size, uint32_t count)
599 return fwrite(buffer, size, count, fp);
602 uint32_t FILEIO::Fseek(long offset, int origin)
605 case FILEIO_SEEK_CUR:
606 return fseek(fp, offset, SEEK_CUR);
607 case FILEIO_SEEK_END:
608 return fseek(fp, offset, SEEK_END);
609 case FILEIO_SEEK_SET:
610 return fseek(fp, offset, SEEK_SET);
615 uint32_t FILEIO::Ftell()