[286] | 1 | // This file is a part of Framsticks SDK. http://www.framsticks.com/ |
---|
| 2 | // Copyright (C) 1999-2015 Maciej Komosinski and Szymon Ulatowski. |
---|
| 3 | // See LICENSE.txt for details. |
---|
[109] | 4 | |
---|
| 5 | #include "virtfile.h" |
---|
[206] | 6 | #include <common/stl-util.h> |
---|
[109] | 7 | |
---|
[298] | 8 | VirtFILE *VirtFILE::Vstdin = NULL; |
---|
| 9 | VirtFILE *VirtFILE::Vstdout = NULL; |
---|
| 10 | VirtFILE *VirtFILE::Vstderr = NULL; |
---|
[109] | 11 | |
---|
[298] | 12 | VirtFileSystem *VirtFILE::vfs = NULL; |
---|
[109] | 13 | |
---|
| 14 | VirtFILE *Vfopen(const char* path,const char*mode) |
---|
| 15 | { |
---|
[298] | 16 | return VirtFILE::vfs ? VirtFILE::vfs->Vfopen(path,mode) : NULL; |
---|
[109] | 17 | } |
---|
| 18 | |
---|
| 19 | VirtDIR *Vopendir(const char* path) |
---|
| 20 | { |
---|
[298] | 21 | return VirtFILE::vfs ? VirtFILE::vfs->Vopendir(path) : NULL; |
---|
[109] | 22 | } |
---|
| 23 | |
---|
[295] | 24 | bool Vfexists(const char* path) |
---|
[109] | 25 | { |
---|
[295] | 26 | return VirtFILE::vfs ? VirtFILE::vfs->Vfexists(path) : false; |
---|
[109] | 27 | } |
---|
| 28 | |
---|
[295] | 29 | bool Vdirexists(const char* path,bool is_writable) |
---|
| 30 | { |
---|
| 31 | return VirtFILE::vfs ? VirtFILE::vfs->Vdirexists(path,is_writable) : false; |
---|
| 32 | } |
---|
| 33 | |
---|
| 34 | bool Vmkdir(const char* path) |
---|
| 35 | { |
---|
| 36 | return VirtFILE::vfs ? VirtFILE::vfs->Vmkdir(path) : false; |
---|
| 37 | } |
---|
| 38 | |
---|
| 39 | bool Vmkdirs(const char* path) |
---|
| 40 | { |
---|
| 41 | return VirtFILE::vfs ? VirtFILE::vfs->Vmkdirs(path) : false; |
---|
| 42 | } |
---|
| 43 | |
---|
[109] | 44 | VirtFILE::~VirtFILE() |
---|
| 45 | {} |
---|
| 46 | |
---|
| 47 | void VirtFILE::selectFileSystem(VirtFileSystem *s) {vfs=s;} |
---|
| 48 | |
---|
[206] | 49 | int VirtFILE::Vprintf(const char *format, va_list args) |
---|
| 50 | { |
---|
| 51 | string s=ssprintf_va(format,args); |
---|
| 52 | return Vwrite(s.c_str(),1,s.size()); |
---|
| 53 | } |
---|
| 54 | |
---|
[109] | 55 | int VirtFILE::printf(const char *format, ...) |
---|
| 56 | { |
---|
| 57 | int ret; va_list argptr; |
---|
| 58 | va_start(argptr,format); |
---|
| 59 | ret=Vprintf(format,argptr); |
---|
| 60 | va_end(argptr); |
---|
| 61 | return ret; |
---|
| 62 | } |
---|
| 63 | |
---|
[247] | 64 | int VirtFILE::getSize() |
---|
| 65 | { |
---|
| 66 | int saved_pos = Vtell(); |
---|
| 67 | Vseek(0, SEEK_END); |
---|
| 68 | int size = Vtell(); |
---|
| 69 | Vseek(saved_pos, SEEK_SET); |
---|
| 70 | return size; |
---|
| 71 | } |
---|
| 72 | |
---|
[109] | 73 | void VirtFILE::setVstdin(VirtFILE *f) {Vstdin=f;} |
---|
| 74 | void VirtFILE::setVstdout(VirtFILE *f) {Vstdout=f;} |
---|
| 75 | void VirtFILE::setVstderr(VirtFILE *f) {Vstderr=f;} |
---|
| 76 | VirtFILE* VirtFILE::getVstdin() {return Vstdin;} |
---|
| 77 | VirtFILE* VirtFILE::getVstdout() {return Vstdout;} |
---|
| 78 | VirtFILE* VirtFILE::getVstderr() {return Vstderr;} |
---|
| 79 | ////////////////////////////////////////////////////////////////////////// |
---|
| 80 | |
---|
[298] | 81 | // base class only returns NULL/false/not supported - implementations perform the actual work |
---|
| 82 | VirtFILE* VirtFileSystem::Vfopen(const char* path,const char*mode) {return NULL;} |
---|
| 83 | bool VirtFileSystem::Vfexists(const char* path) {return false;} |
---|
| 84 | VirtDIR* VirtFileSystem::Vopendir(const char* path) {return NULL;} |
---|
| 85 | bool VirtFileSystem::Vmkdir(const char* path) {return false;} |
---|
[295] | 86 | bool VirtFileSystem::Vdirexists(const char* path,bool is_writable) {return false;} |
---|
[109] | 87 | |
---|
| 88 | ////////////////////////////////////////////////////////////////////////// |
---|
| 89 | |
---|
| 90 | |
---|
| 91 | |
---|
| 92 | int fread(void *ptr, size_t size, size_t nmemb, VirtFILE* f) {return f->Vread(ptr,size,nmemb);} |
---|
| 93 | int fwrite(const void *ptr, size_t size, size_t nmemb, VirtFILE* f) {return f->Vwrite(ptr,size,nmemb);} |
---|
[123] | 94 | |
---|
[131] | 95 | |
---|
| 96 | //since we want our own feof(VirtFILE*) function and some systems unfortunately define feof as a macro, we need to #undef it. Same as in virtfile.h |
---|
[225] | 97 | #if defined _MSC_VER || defined __CYGWIN__ || defined SHP || defined __ANDROID__ |
---|
[131] | 98 | #pragma push_macro("feof") |
---|
[123] | 99 | #undef feof |
---|
| 100 | #endif |
---|
[131] | 101 | #if defined __BORLANDC__ //does not support #pragma push_macro/pop_macro |
---|
| 102 | #undef feof |
---|
| 103 | #endif |
---|
[123] | 104 | |
---|
| 105 | int feof(VirtFILE* f) {return f->Veof();} |
---|
| 106 | |
---|
| 107 | //...and then restore the original macro: |
---|
[225] | 108 | #if defined _MSC_VER || defined __CYGWIN__ || defined SHP || defined __ANDROID__ |
---|
[131] | 109 | #pragma pop_macro("feof") |
---|
| 110 | #endif |
---|
[225] | 111 | #if defined __BORLANDC__ |
---|
[123] | 112 | #define feof(__f) ((__f)->flags & _F_EOF) |
---|
[109] | 113 | #endif |
---|
| 114 | |
---|
[123] | 115 | |
---|
[109] | 116 | int fputc(int c,VirtFILE* f) {return f->Vputc(c);} |
---|
| 117 | int fputs(const char *s,VirtFILE* f) {return f->Vputs(s);} |
---|
| 118 | int fgetc(VirtFILE* f) {return f->Vgetc();} |
---|
| 119 | int fseek(VirtFILE* f,long offset, int whence) {return f->Vseek(offset,whence);} |
---|
| 120 | int ftell(VirtFILE* f) {return f->Vtell();} |
---|
| 121 | void rewind(VirtFILE* f) {f->Vrewind();} |
---|
| 122 | int fflush(VirtFILE* f) {return f->Vflush();} |
---|
| 123 | char *fgets(char *s, int size, VirtFILE* f) {return f->Vgets(s,size);} |
---|
| 124 | int fprintf(VirtFILE* f,const char *format, ...) |
---|
| 125 | { |
---|
| 126 | int ret; va_list argptr; |
---|
| 127 | va_start(argptr,format); |
---|
| 128 | ret=f->Vprintf(format,argptr); |
---|
| 129 | va_end(argptr); |
---|
| 130 | return ret; |
---|
| 131 | } |
---|
| 132 | int fclose(VirtFILE* f) {delete f; return 0;} |
---|
| 133 | |
---|
| 134 | int closedir(VirtDIR* d) {delete d; return 0;} |
---|
| 135 | dirent* readdir(VirtDIR* d) {return d->Vreaddir();} |
---|
[123] | 136 | |
---|
[295] | 137 | ///////// |
---|
| 138 | |
---|
| 139 | bool VirtFileSystem::Vmkdirs(const char* path) |
---|
| 140 | { |
---|
| 141 | if (Vdirexists(path,true)) return true; |
---|
| 142 | string parentdir = getFileDir(path); |
---|
| 143 | if (!Vmkdirs(parentdir.c_str())) return false; |
---|
| 144 | return Vmkdir(path); |
---|
| 145 | } |
---|
| 146 | |
---|
| 147 | ////////// |
---|
| 148 | |
---|
| 149 | VirtFILE *ChainFileSystem::Vfopen(const char* path, const char*mode) |
---|
| 150 | { |
---|
| 151 | return (chain != NULL) ? chain->Vfopen(path, mode) : NULL; |
---|
| 152 | } |
---|
| 153 | |
---|
| 154 | bool ChainFileSystem::Vfexists(const char* path) |
---|
| 155 | { |
---|
| 156 | return (chain != NULL) ? chain->Vfexists(path) : false; |
---|
| 157 | } |
---|
| 158 | |
---|
| 159 | VirtDIR *ChainFileSystem::Vopendir(const char* path) |
---|
| 160 | { |
---|
| 161 | return (chain != NULL) ? chain->Vopendir(path) : NULL; |
---|
| 162 | } |
---|
| 163 | |
---|
| 164 | bool ChainFileSystem::Vmkdir(const char* path) |
---|
| 165 | { |
---|
| 166 | return (chain != NULL) ? chain->Vmkdir(path) : false; |
---|
| 167 | } |
---|
| 168 | |
---|
| 169 | bool ChainFileSystem::Vmkdirs(const char* path) |
---|
| 170 | { |
---|
| 171 | return (chain != NULL) ? chain->Vmkdirs(path) : false; |
---|
| 172 | } |
---|
| 173 | |
---|
| 174 | bool ChainFileSystem::Vdirexists(const char* path,bool is_writable) |
---|
| 175 | { |
---|
| 176 | return (chain != NULL) ? chain->Vdirexists(path,is_writable) : false; |
---|
| 177 | } |
---|