[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 | |
---|
[247] | 49 | int VirtFILE::getSize() |
---|
| 50 | { |
---|
| 51 | int saved_pos = Vtell(); |
---|
| 52 | Vseek(0, SEEK_END); |
---|
| 53 | int size = Vtell(); |
---|
| 54 | Vseek(saved_pos, SEEK_SET); |
---|
| 55 | return size; |
---|
| 56 | } |
---|
| 57 | |
---|
[109] | 58 | void VirtFILE::setVstdin(VirtFILE *f) {Vstdin=f;} |
---|
| 59 | void VirtFILE::setVstdout(VirtFILE *f) {Vstdout=f;} |
---|
| 60 | void VirtFILE::setVstderr(VirtFILE *f) {Vstderr=f;} |
---|
| 61 | VirtFILE* VirtFILE::getVstdin() {return Vstdin;} |
---|
| 62 | VirtFILE* VirtFILE::getVstdout() {return Vstdout;} |
---|
| 63 | VirtFILE* VirtFILE::getVstderr() {return Vstderr;} |
---|
| 64 | ////////////////////////////////////////////////////////////////////////// |
---|
| 65 | |
---|
| 66 | VirtFILE* VirtFileSystem::Vfopen(const char* path,const char*mode) {return 0;} |
---|
| 67 | int VirtFileSystem::Vfexists(const char* path) {return 0;} |
---|
| 68 | VirtDIR* VirtFileSystem::Vopendir(const char* path) {return 0;} |
---|
| 69 | |
---|
| 70 | ////////////////////////////////////////////////////////////////////////// |
---|
| 71 | |
---|
| 72 | |
---|
| 73 | |
---|
| 74 | int fread(void *ptr, size_t size, size_t nmemb, VirtFILE* f) {return f->Vread(ptr,size,nmemb);} |
---|
| 75 | int fwrite(const void *ptr, size_t size, size_t nmemb, VirtFILE* f) {return f->Vwrite(ptr,size,nmemb);} |
---|
[123] | 76 | |
---|
[131] | 77 | |
---|
| 78 | //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] | 79 | #if defined _MSC_VER || defined __CYGWIN__ || defined SHP || defined __ANDROID__ |
---|
[131] | 80 | #pragma push_macro("feof") |
---|
[123] | 81 | #undef feof |
---|
| 82 | #endif |
---|
[131] | 83 | #if defined __BORLANDC__ //does not support #pragma push_macro/pop_macro |
---|
| 84 | #undef feof |
---|
| 85 | #endif |
---|
[123] | 86 | |
---|
| 87 | int feof(VirtFILE* f) {return f->Veof();} |
---|
| 88 | |
---|
| 89 | //...and then restore the original macro: |
---|
[225] | 90 | #if defined _MSC_VER || defined __CYGWIN__ || defined SHP || defined __ANDROID__ |
---|
[131] | 91 | #pragma pop_macro("feof") |
---|
| 92 | #endif |
---|
[225] | 93 | #if defined __BORLANDC__ |
---|
[123] | 94 | #define feof(__f) ((__f)->flags & _F_EOF) |
---|
[109] | 95 | #endif |
---|
| 96 | |
---|
[123] | 97 | |
---|
[109] | 98 | int fputc(int c,VirtFILE* f) {return f->Vputc(c);} |
---|
| 99 | int fputs(const char *s,VirtFILE* f) {return f->Vputs(s);} |
---|
| 100 | int fgetc(VirtFILE* f) {return f->Vgetc();} |
---|
| 101 | int fseek(VirtFILE* f,long offset, int whence) {return f->Vseek(offset,whence);} |
---|
| 102 | int ftell(VirtFILE* f) {return f->Vtell();} |
---|
| 103 | void rewind(VirtFILE* f) {f->Vrewind();} |
---|
| 104 | int fflush(VirtFILE* f) {return f->Vflush();} |
---|
| 105 | char *fgets(char *s, int size, VirtFILE* f) {return f->Vgets(s,size);} |
---|
| 106 | int fprintf(VirtFILE* f,const char *format, ...) |
---|
| 107 | { |
---|
| 108 | int ret; va_list argptr; |
---|
| 109 | va_start(argptr,format); |
---|
| 110 | ret=f->Vprintf(format,argptr); |
---|
| 111 | va_end(argptr); |
---|
| 112 | return ret; |
---|
| 113 | } |
---|
| 114 | int fclose(VirtFILE* f) {delete f; return 0;} |
---|
| 115 | |
---|
| 116 | int closedir(VirtDIR* d) {delete d; return 0;} |
---|
| 117 | dirent* readdir(VirtDIR* d) {return d->Vreaddir();} |
---|
[123] | 118 | |
---|