[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 | #include "virtfile.h" |
---|
[206] | 6 | #include <common/stl-util.h> |
---|
[109] | 7 | |
---|
| 8 | VirtFILE *VirtFILE::Vstdin=0; |
---|
| 9 | VirtFILE *VirtFILE::Vstdout=0; |
---|
| 10 | VirtFILE *VirtFILE::Vstderr=0; |
---|
| 11 | |
---|
| 12 | VirtFileSystem *VirtFILE::vfs=0; |
---|
| 13 | |
---|
| 14 | VirtFILE *Vfopen(const char* path,const char*mode) |
---|
| 15 | { |
---|
| 16 | return VirtFILE::vfs ? VirtFILE::vfs->Vfopen(path,mode) : 0; |
---|
| 17 | } |
---|
| 18 | |
---|
| 19 | VirtDIR *Vopendir(const char* path) |
---|
| 20 | { |
---|
| 21 | return VirtFILE::vfs ? VirtFILE::vfs->Vopendir(path) : 0; |
---|
| 22 | } |
---|
| 23 | |
---|
| 24 | int Vfexists(const char* path) |
---|
| 25 | { |
---|
| 26 | return VirtFILE::vfs ? VirtFILE::vfs->Vfexists(path) : 0; |
---|
| 27 | } |
---|
| 28 | |
---|
| 29 | VirtFILE::~VirtFILE() |
---|
| 30 | {} |
---|
| 31 | |
---|
| 32 | void VirtFILE::selectFileSystem(VirtFileSystem *s) {vfs=s;} |
---|
| 33 | |
---|
[206] | 34 | int VirtFILE::Vprintf(const char *format, va_list args) |
---|
| 35 | { |
---|
| 36 | string s=ssprintf_va(format,args); |
---|
| 37 | return Vwrite(s.c_str(),1,s.size()); |
---|
| 38 | } |
---|
| 39 | |
---|
[109] | 40 | int VirtFILE::printf(const char *format, ...) |
---|
| 41 | { |
---|
| 42 | int ret; va_list argptr; |
---|
| 43 | va_start(argptr,format); |
---|
| 44 | ret=Vprintf(format,argptr); |
---|
| 45 | va_end(argptr); |
---|
| 46 | return ret; |
---|
| 47 | } |
---|
| 48 | |
---|
| 49 | void VirtFILE::setVstdin(VirtFILE *f) {Vstdin=f;} |
---|
| 50 | void VirtFILE::setVstdout(VirtFILE *f) {Vstdout=f;} |
---|
| 51 | void VirtFILE::setVstderr(VirtFILE *f) {Vstderr=f;} |
---|
| 52 | VirtFILE* VirtFILE::getVstdin() {return Vstdin;} |
---|
| 53 | VirtFILE* VirtFILE::getVstdout() {return Vstdout;} |
---|
| 54 | VirtFILE* VirtFILE::getVstderr() {return Vstderr;} |
---|
| 55 | ////////////////////////////////////////////////////////////////////////// |
---|
| 56 | |
---|
| 57 | VirtFILE* VirtFileSystem::Vfopen(const char* path,const char*mode) {return 0;} |
---|
| 58 | int VirtFileSystem::Vfexists(const char* path) {return 0;} |
---|
| 59 | VirtDIR* VirtFileSystem::Vopendir(const char* path) {return 0;} |
---|
| 60 | |
---|
| 61 | ////////////////////////////////////////////////////////////////////////// |
---|
| 62 | |
---|
| 63 | |
---|
| 64 | |
---|
| 65 | int fread(void *ptr, size_t size, size_t nmemb, VirtFILE* f) {return f->Vread(ptr,size,nmemb);} |
---|
| 66 | int fwrite(const void *ptr, size_t size, size_t nmemb, VirtFILE* f) {return f->Vwrite(ptr,size,nmemb);} |
---|
[123] | 67 | |
---|
[131] | 68 | |
---|
| 69 | //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] | 70 | #if defined _MSC_VER || defined __CYGWIN__ || defined SHP || defined __ANDROID__ |
---|
[131] | 71 | #pragma push_macro("feof") |
---|
[123] | 72 | #undef feof |
---|
| 73 | #endif |
---|
[131] | 74 | #if defined __BORLANDC__ //does not support #pragma push_macro/pop_macro |
---|
| 75 | #undef feof |
---|
| 76 | #endif |
---|
[123] | 77 | |
---|
| 78 | int feof(VirtFILE* f) {return f->Veof();} |
---|
| 79 | |
---|
| 80 | //...and then restore the original macro: |
---|
[225] | 81 | #if defined _MSC_VER || defined __CYGWIN__ || defined SHP || defined __ANDROID__ |
---|
[131] | 82 | #pragma pop_macro("feof") |
---|
| 83 | #endif |
---|
[225] | 84 | #if defined __BORLANDC__ |
---|
[123] | 85 | #define feof(__f) ((__f)->flags & _F_EOF) |
---|
[109] | 86 | #endif |
---|
| 87 | |
---|
[123] | 88 | |
---|
[109] | 89 | int fputc(int c,VirtFILE* f) {return f->Vputc(c);} |
---|
| 90 | int fputs(const char *s,VirtFILE* f) {return f->Vputs(s);} |
---|
| 91 | int fgetc(VirtFILE* f) {return f->Vgetc();} |
---|
| 92 | int fseek(VirtFILE* f,long offset, int whence) {return f->Vseek(offset,whence);} |
---|
| 93 | int ftell(VirtFILE* f) {return f->Vtell();} |
---|
| 94 | void rewind(VirtFILE* f) {f->Vrewind();} |
---|
| 95 | int fflush(VirtFILE* f) {return f->Vflush();} |
---|
| 96 | char *fgets(char *s, int size, VirtFILE* f) {return f->Vgets(s,size);} |
---|
| 97 | int fprintf(VirtFILE* f,const char *format, ...) |
---|
| 98 | { |
---|
| 99 | int ret; va_list argptr; |
---|
| 100 | va_start(argptr,format); |
---|
| 101 | ret=f->Vprintf(format,argptr); |
---|
| 102 | va_end(argptr); |
---|
| 103 | return ret; |
---|
| 104 | } |
---|
| 105 | int fclose(VirtFILE* f) {delete f; return 0;} |
---|
| 106 | |
---|
| 107 | int closedir(VirtDIR* d) {delete d; return 0;} |
---|
| 108 | dirent* readdir(VirtDIR* d) {return d->Vreaddir();} |
---|
[123] | 109 | |
---|