OSDN Git Service

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