OSDN Git Service

6e696afd4f83310ba9794ed4b1c5aa493fffd990
[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 #ifdef _WIN32
11 #include <windows.h>
12 #endif
13 #include "fileio.h"
14 #if !defined(_MSC_VER)
15 #include <stdarg.h>
16 #include <fcntl.h>
17 #include <stdio.h>
18 #include <iostream>
19 #include <fstream>
20 #include <cstdio>
21 #endif
22
23 FILEIO::FILEIO()
24 {
25         fp = NULL;
26 }
27
28 FILEIO::~FILEIO(void)
29 {
30         Fclose();
31 }
32
33 bool FILEIO::IsFileExisting(const _TCHAR *file_path)
34 {
35 #if defined(_USE_QT) || defined(_USE_SDL)
36         FILE *f;
37         f = fopen(file_path, "r");
38         if(f != NULL)  {
39                 fclose(f);         
40                 return true;
41         }
42         return false;
43 #else   
44 # ifdef _WIN32
45         DWORD attr = GetFileAttributes(file_path);
46         if(attr == -1) {
47                 return false;
48         }
49         return ((attr & FILE_ATTRIBUTE_DIRECTORY) == 0);
50 # else
51         return (_taccess(file_path, 0) == 0);
52 # endif
53 #endif
54 }
55 #if defined(_USE_QT)
56 # include <sys/types.h>
57 # include <sys/stat.h>
58 # if !defined(Q_OS_WIN)
59 #   include <unistd.h>
60 # endif
61 #endif
62 bool FILEIO::IsFileProtected(const _TCHAR *file_path)
63 {
64 #if defined(_USE_QT) || defined(_USE_SDL)
65         struct stat st;
66         if(stat(file_path, &st) == 0) {
67 # if defined(_WIN32)
68                 if((st.st_mode & S_IWUSR) == 0) return true;
69 # else
70                 if((st.st_mode & (S_IWUSR | S_IWGRP | S_IWOTH)) == 0) return true;
71 # endif
72         }
73     return false;
74 #else
75 # ifdef _WIN32
76         return ((GetFileAttributes(file_path) & FILE_ATTRIBUTE_READONLY) != 0);
77 # else
78         return (_taccess(file_path, 2) != 0);
79 # endif
80 #endif
81 }
82
83 bool FILEIO::RemoveFile(const _TCHAR *file_path)
84 {
85 #if defined(_USE_QT) || defined(_USE_SDL)
86         return (remove(file_path) == 0);
87 #else
88 # ifdef _WIN32
89         return (DeleteFile(file_path) != 0);
90 # else
91         return (_tremove(file_path) == 0);      // not supported on wince ???
92 # endif
93 #endif
94 }
95
96 bool FILEIO::RenameFile(const _TCHAR *existing_file_path, const _TCHAR *new_file_path)
97 {
98 #if defined(_USE_QT)
99         return (rename(existing_file_path, new_file_path) == 0);
100         #else
101                 #ifdef _WIN32
102                         return (MoveFile(existing_file_path, new_file_path) != 0);
103                 #else
104                         return (_trename(existing_file_path, new_file_path) == 0);
105                 #endif
106 #endif                  
107 }
108
109 bool FILEIO::Fopen(const _TCHAR *file_path, int mode)
110 {
111         Fclose();
112         
113         switch(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);
134         }
135         return false;
136 }
137
138 void FILEIO::Fclose()
139 {
140         if(fp) {
141                 fclose(fp);
142         }
143         fp = NULL;
144 }
145
146 uint32_t FILEIO::FileLength()
147 {
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;
153 }
154
155 #define GET_VALUE(type) \
156         uint8_t buffer[sizeof(type)]; \
157         fread(buffer, sizeof(buffer), 1, fp); \
158         return *(type *)buffer
159
160 #define PUT_VALUE(type, v) \
161         fwrite(&v, sizeof(type), 1, fp)
162
163 bool FILEIO::FgetBool()
164 {
165         GET_VALUE(bool);
166 }
167
168 void FILEIO::FputBool(bool val)
169 {
170         PUT_VALUE(bool, val);
171 }
172
173 uint8_t FILEIO::FgetUint8()
174 {
175         GET_VALUE(uint8_t);
176 }
177
178 void FILEIO::FputUint8(uint8_t val)
179 {
180         PUT_VALUE(uint8_t, val);
181 }
182
183 uint16_t FILEIO::FgetUint16()
184 {
185         GET_VALUE(uint16_t);
186 }
187
188 void FILEIO::FputUint16(uint16_t val)
189 {
190         PUT_VALUE(uint16_t, val);
191 }
192
193 uint32_t FILEIO::FgetUint32()
194 {
195         GET_VALUE(uint32_t);
196 }
197
198 void FILEIO::FputUint32(uint32_t val)
199 {
200         PUT_VALUE(uint32_t, val);
201 }
202
203 uint64_t FILEIO::FgetUint64()
204 {
205         GET_VALUE(uint64_t);
206 }
207
208 void FILEIO::FputUint64(uint64_t val)
209 {
210         PUT_VALUE(uint64_t, val);
211 }
212
213 int8_t FILEIO::FgetInt8()
214 {
215         GET_VALUE(int8_t);
216 }
217
218 void FILEIO::FputInt8(int8_t val)
219 {
220         PUT_VALUE(int8_t, val);
221 }
222
223 int16_t FILEIO::FgetInt16()
224 {
225         GET_VALUE(int16_t);
226 }
227
228 void FILEIO::FputInt16(int16_t val)
229 {
230         PUT_VALUE(int16_t, val);
231 }
232
233 int32_t FILEIO::FgetInt32()
234 {
235         GET_VALUE(int32_t);
236 }
237
238 void FILEIO::FputInt32(int32_t val)
239 {
240         PUT_VALUE(int32_t, val);
241 }
242
243 int64_t FILEIO::FgetInt64()
244 {
245         GET_VALUE(int64_t);
246 }
247
248 void FILEIO::FputInt64(int64_t val)
249 {
250         PUT_VALUE(int64_t, val);
251 }
252
253 float FILEIO::FgetFloat()
254 {
255         GET_VALUE(float);
256 }
257
258 void FILEIO::FputFloat(float val)
259 {
260         PUT_VALUE(float, val);
261 }
262
263 double FILEIO::FgetDouble()
264 {
265         GET_VALUE(double);
266 }
267
268 void FILEIO::FputDouble(double val)
269 {
270         PUT_VALUE(double, val);
271 }
272
273 typedef union {
274         struct {
275 #ifdef __BIG_ENDIAN__
276                 uint8_t h, l;
277 #else
278                 uint8_t l, h;
279 #endif
280         } b;
281         uint16_t u16;
282         int16_t s16;
283 } pair16_t;
284
285 typedef union {
286         struct {
287 #ifdef __BIG_ENDIAN__
288                 uint8_t h3, h2, h, l;
289 #else
290                 uint8_t l, h, h2, h3;
291 #endif
292         } b;
293         uint32_t u32;
294         int32_t s32;
295 } pair32_t;
296
297 typedef union {
298         struct {
299 #ifdef __BIG_ENDIAN__
300                 uint8_t h7, h6, h5, h4, h3, h2, h, l;
301 #else
302                 uint8_t l, h, h2, h3, h4, h5, h6, h7;
303 #endif
304         } b;
305         uint64_t u64;
306         int64_t s64;
307 } pair64_t;
308
309 uint16_t FILEIO::FgetUint16_LE()
310 {
311         pair16_t tmp;
312         tmp.b.l = FgetUint8();
313         tmp.b.h = FgetUint8();
314         return tmp.u16;
315 }
316
317 void FILEIO::FputUint16_LE(uint16_t val)
318 {
319         pair16_t tmp;
320         tmp.u16 = val;
321         FputUint8(tmp.b.l);
322         FputUint8(tmp.b.h);
323 }
324
325 uint32_t FILEIO::FgetUint32_LE()
326 {
327         pair32_t tmp;
328         tmp.b.l  = FgetUint8();
329         tmp.b.h  = FgetUint8();
330         tmp.b.h2 = FgetUint8();
331         tmp.b.h3 = FgetUint8();
332         return tmp.u32;
333 }
334
335 void FILEIO::FputUint32_LE(uint32_t val)
336 {
337         pair32_t tmp;
338         tmp.u32 = val;
339         FputUint8(tmp.b.l);
340         FputUint8(tmp.b.h);
341         FputUint8(tmp.b.h2);
342         FputUint8(tmp.b.h3);
343 }
344
345 uint64_t FILEIO::FgetUint64_LE()
346 {
347         pair64_t tmp;
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();
356         return tmp.u64;
357 }
358
359 void FILEIO::FputUint64_LE(uint64_t val)
360 {
361         pair64_t tmp;
362         tmp.u64 = val;
363         FputUint8(tmp.b.l);
364         FputUint8(tmp.b.h);
365         FputUint8(tmp.b.h2);
366         FputUint8(tmp.b.h3);
367         FputUint8(tmp.b.h4);
368         FputUint8(tmp.b.h5);
369         FputUint8(tmp.b.h6);
370         FputUint8(tmp.b.h7);
371 }
372
373 int16_t FILEIO::FgetInt16_LE()
374 {
375         pair16_t tmp;
376         tmp.b.l = FgetUint8();
377         tmp.b.h = FgetUint8();
378         return tmp.s16;
379 }
380
381 void FILEIO::FputInt16_LE(int16_t val)
382 {
383         pair16_t tmp;
384         tmp.s16 = val;
385         FputUint8(tmp.b.l);
386         FputUint8(tmp.b.h);
387 }
388
389 int32_t FILEIO::FgetInt32_LE()
390 {
391         pair32_t tmp;
392         tmp.b.l  = FgetUint8();
393         tmp.b.h  = FgetUint8();
394         tmp.b.h2 = FgetUint8();
395         tmp.b.h3 = FgetUint8();
396         return tmp.s32;
397 }
398
399 void FILEIO::FputInt32_LE(int32_t val)
400 {
401         pair32_t tmp;
402         tmp.s32 = val;
403         FputUint8(tmp.b.l);
404         FputUint8(tmp.b.h);
405         FputUint8(tmp.b.h2);
406         FputUint8(tmp.b.h3);
407 }
408
409 int64_t FILEIO::FgetInt64_LE()
410 {
411         pair64_t tmp;
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();
420         return tmp.s64;
421 }
422
423 void FILEIO::FputInt64_LE(int64_t val)
424 {
425         pair64_t tmp;
426         tmp.s64 = val;
427         FputUint8(tmp.b.l);
428         FputUint8(tmp.b.h);
429         FputUint8(tmp.b.h2);
430         FputUint8(tmp.b.h3);
431         FputUint8(tmp.b.h4);
432         FputUint8(tmp.b.h5);
433         FputUint8(tmp.b.h6);
434         FputUint8(tmp.b.h7);
435 }
436
437 uint16_t FILEIO::FgetUint16_BE()
438 {
439         pair16_t tmp;
440         tmp.b.h = FgetUint8();
441         tmp.b.l = FgetUint8();
442         return tmp.u16;
443 }
444
445 void FILEIO::FputUint16_BE(uint16_t val)
446 {
447         pair16_t tmp;
448         tmp.u16 = val;
449         FputUint8(tmp.b.h);
450         FputUint8(tmp.b.l);
451 }
452
453 uint32_t FILEIO::FgetUint32_BE()
454 {
455         pair32_t tmp;
456         tmp.b.h3 = FgetUint8();
457         tmp.b.h2 = FgetUint8();
458         tmp.b.h  = FgetUint8();
459         tmp.b.l  = FgetUint8();
460         return tmp.u32;
461 }
462
463 void FILEIO::FputUint32_BE(uint32_t val)
464 {
465         pair32_t tmp;
466         tmp.u32 = val;
467         FputUint8(tmp.b.h3);
468         FputUint8(tmp.b.h2);
469         FputUint8(tmp.b.h);
470         FputUint8(tmp.b.l);
471 }
472
473 uint64_t FILEIO::FgetUint64_BE()
474 {
475         pair64_t tmp;
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();
484         return tmp.u64;
485 }
486
487 void FILEIO::FputUint64_BE(uint64_t val)
488 {
489         pair64_t tmp;
490         tmp.u64 = val;
491         FputUint8(tmp.b.h7);
492         FputUint8(tmp.b.h6);
493         FputUint8(tmp.b.h5);
494         FputUint8(tmp.b.h4);
495         FputUint8(tmp.b.h3);
496         FputUint8(tmp.b.h2);
497         FputUint8(tmp.b.h);
498         FputUint8(tmp.b.l);
499 }
500
501 int16_t FILEIO::FgetInt16_BE()
502 {
503         pair16_t tmp;
504         tmp.b.h = FgetUint8();
505         tmp.b.l = FgetUint8();
506         return tmp.s16;
507 }
508
509 void FILEIO::FputInt16_BE(int16_t val)
510 {
511         pair16_t tmp;
512         tmp.s16 = val;
513         FputUint8(tmp.b.h);
514         FputUint8(tmp.b.l);
515 }
516
517 int32_t FILEIO::FgetInt32_BE()
518 {
519         pair32_t tmp;
520         tmp.b.h3 = FgetUint8();
521         tmp.b.h2 = FgetUint8();
522         tmp.b.h  = FgetUint8();
523         tmp.b.l  = FgetUint8();
524         return tmp.s32;
525 }
526
527 void FILEIO::FputInt32_BE(int32_t val)
528 {
529         pair32_t tmp;
530         tmp.s32 = val;
531         FputUint8(tmp.b.h3);
532         FputUint8(tmp.b.h2);
533         FputUint8(tmp.b.h);
534         FputUint8(tmp.b.l);
535 }
536
537 int64_t FILEIO::FgetInt64_BE()
538 {
539         pair64_t tmp;
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();
548         return tmp.s64;
549 }
550
551 void FILEIO::FputInt64_BE(int64_t val)
552 {
553         pair64_t tmp;
554         tmp.s64 = val;
555         FputUint8(tmp.b.h7);
556         FputUint8(tmp.b.h6);
557         FputUint8(tmp.b.h5);
558         FputUint8(tmp.b.h4);
559         FputUint8(tmp.b.h3);
560         FputUint8(tmp.b.h2);
561         FputUint8(tmp.b.h);
562         FputUint8(tmp.b.l);
563 }
564
565 int FILEIO::Fgetc()
566 {
567         return fgetc(fp);
568 }
569
570 int FILEIO::Fputc(int c)
571 {
572         return fputc(c, fp);
573 }
574
575 char *FILEIO::Fgets(char *str, int n)
576 {
577         return fgets(str, n, fp);
578 }
579
580 int FILEIO::Fprintf(const char* format, ...)
581 {
582         va_list ap;
583         char buffer[1024];
584         
585         va_start(ap, format);
586         my_vsprintf_s(buffer, 1024, format, ap);
587         va_end(ap);
588         
589         return my_fprintf_s(fp, "%s", buffer);
590 }
591
592 uint32_t FILEIO::Fread(void* buffer, uint32_t size, uint32_t count)
593 {
594         return fread(buffer, size, count, fp);
595 }
596
597 uint32_t FILEIO::Fwrite(void* buffer, uint32_t size, uint32_t count)
598 {
599         return fwrite(buffer, size, count, fp);
600 }
601
602 uint32_t FILEIO::Fseek(long offset, int origin)
603 {
604         switch(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);
611         }
612         return 0xFFFFFFFF;
613 }
614
615 uint32_t FILEIO::Ftell()
616 {
617         return ftell(fp);
618 }
619