[121] | 1 | // This file is a part of the Framsticks GDK. |
---|
[197] | 2 | // Copyright (C) 1999-2014 Maciej Komosinski and Szymon Ulatowski. See LICENSE.txt for details. |
---|
[109] | 3 | // Refer to http://www.framsticks.com/ for further information. |
---|
| 4 | |
---|
| 5 | #ifndef _STDIOFILE_H_ |
---|
| 6 | #define _STDIOFILE_H_ |
---|
| 7 | |
---|
| 8 | #include "virtfile.h" |
---|
| 9 | #include <frams/util/sstring.h> |
---|
[206] | 10 | #ifdef USE_MFILE |
---|
| 11 | #include <common/nonstd_stdio.h> |
---|
| 12 | #else |
---|
[109] | 13 | #include <stdio.h> |
---|
[206] | 14 | #endif |
---|
[109] | 15 | #include <common/nonstd_dir.h> |
---|
| 16 | |
---|
[281] | 17 | class StdioFileSystem : public VirtFileSystem |
---|
[109] | 18 | { |
---|
| 19 | public: |
---|
[281] | 20 | VirtFILE *Vfopen(const char *path, const char *mode); |
---|
| 21 | int Vfexists(const char* path); |
---|
| 22 | VirtDIR *Vopendir(const char* path); |
---|
[109] | 23 | }; |
---|
| 24 | |
---|
[206] | 25 | #ifdef USE_MFILE |
---|
[281] | 26 | class StdioFILE : public VirtFILE |
---|
[109] | 27 | { |
---|
[281] | 28 | protected: |
---|
| 29 | MFILE *file; |
---|
| 30 | SString path; |
---|
| 31 | public: |
---|
| 32 | StdioFILE(MFILE *f) { file = f; } |
---|
| 33 | StdioFILE(MFILE *f, const SString& p) { file = f; path = p; } |
---|
| 34 | static void setStdio(); |
---|
| 35 | size_t Vread(void *ptr, size_t size, size_t nmemb) { return mfread(ptr, size, nmemb, file); } |
---|
| 36 | size_t Vwrite(const void *ptr, size_t size, size_t nmemb) { return mfwrite(ptr, size, nmemb, file); } |
---|
| 37 | int Veof() { return mfeof(file); } |
---|
| 38 | int Vputs(const char *s) { return mfputs(s, file); } |
---|
| 39 | char *Vgets(char *s, int size) { return mfgets(s, size, file); } |
---|
| 40 | int Vseek(long offset, int whence) { return mfseek(file, offset, whence); } |
---|
| 41 | long Vtell() { return mftell(file); } |
---|
| 42 | int Vflush() { return 0; /*NOT IMPLEMENTED!*/ } |
---|
| 43 | const char* VgetPath() { return path; } |
---|
[206] | 44 | |
---|
[281] | 45 | ~StdioFILE() { if (file) mfclose(file); } |
---|
[206] | 46 | }; |
---|
| 47 | #else |
---|
[281] | 48 | class StdioFILE : public VirtFILE |
---|
[206] | 49 | { |
---|
[281] | 50 | protected: |
---|
| 51 | FILE *file; |
---|
| 52 | SString path; |
---|
| 53 | public: |
---|
| 54 | StdioFILE(FILE *f) { file = f; } |
---|
| 55 | StdioFILE(FILE *f, const SString& p) { file = f; path = p; } |
---|
| 56 | static void setStdio(); |
---|
| 57 | size_t Vread(void *ptr, size_t size, size_t nmemb) { return fread(ptr, size, nmemb, file); } |
---|
| 58 | size_t Vwrite(const void *ptr, size_t size, size_t nmemb) { return fwrite(ptr, size, nmemb, file); } |
---|
| 59 | int Veof() { return feof(file); } |
---|
| 60 | int Vputc(int c) { return fputc(c, file); } |
---|
| 61 | int Vputs(const char *s) { return fputs(s, file); } |
---|
| 62 | int Vgetc() { return fgetc(file); } |
---|
| 63 | char *Vgets(char *s, int size) { return fgets(s, size, file); } |
---|
| 64 | int Vprintf(const char *format, va_list args) { return vfprintf(file, format, args); } |
---|
| 65 | int Vseek(long offset, int whence) { return fseek(file, offset, whence); } |
---|
| 66 | long Vtell() { return ftell(file); } |
---|
| 67 | void Vrewind() { rewind(file); } |
---|
| 68 | int Vflush() { return fflush(file); } |
---|
| 69 | const char* VgetPath() { return path; } |
---|
[109] | 70 | |
---|
[281] | 71 | ~StdioFILE() { if (file) fclose(file); } |
---|
[109] | 72 | }; |
---|
[206] | 73 | #endif |
---|
[109] | 74 | |
---|
[281] | 75 | |
---|
| 76 | #ifdef _WIN32 |
---|
| 77 | #ifdef __BORLANDC__ |
---|
| 78 | typedef wDIR DIRTYPE; |
---|
| 79 | #else |
---|
| 80 | typedef WDIR DIRTYPE; |
---|
| 81 | #endif |
---|
| 82 | #else |
---|
| 83 | typedef DIR DIRTYPE; |
---|
| 84 | #endif |
---|
| 85 | |
---|
| 86 | class StdioDIR : public VirtDIR |
---|
[109] | 87 | { |
---|
[281] | 88 | DIRTYPE *dir; |
---|
| 89 | #ifdef _WIN32 |
---|
| 90 | dirent de; //only used to convert wide string names (wdirent) to utf8 (dirent) |
---|
| 91 | #endif |
---|
| 92 | public: |
---|
| 93 | StdioDIR(DIRTYPE *d) : dir(d) {} |
---|
| 94 | ~StdioDIR() |
---|
| 95 | { |
---|
| 96 | #ifdef _WIN32 |
---|
| 97 | if (dir) wclosedir(dir); |
---|
| 98 | #else |
---|
| 99 | if (dir) closedir(dir); |
---|
| 100 | #endif |
---|
| 101 | } |
---|
| 102 | dirent* Vreaddir(); |
---|
[109] | 103 | }; |
---|
| 104 | |
---|
[281] | 105 | class StdioFILEDontClose : public StdioFILE |
---|
[109] | 106 | { |
---|
[281] | 107 | public: |
---|
[206] | 108 | #ifdef USE_MFILE |
---|
[281] | 109 | StdioFILEDontClose(MFILE *f) : StdioFILE(f) {} |
---|
[206] | 110 | #else |
---|
[281] | 111 | StdioFILEDontClose(FILE *f) : StdioFILE(f) {} |
---|
[206] | 112 | #endif |
---|
[281] | 113 | ~StdioFILEDontClose() { file = 0; } |
---|
[109] | 114 | }; |
---|
| 115 | |
---|
| 116 | #endif |
---|
[281] | 117 | |
---|