[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 | #ifndef _VIRTFILE_H_ |
---|
| 6 | #define _VIRTFILE_H_ |
---|
| 7 | |
---|
| 8 | #include <stdio.h> |
---|
| 9 | #include <stdarg.h> |
---|
| 10 | #include <common/nonstd_dir.h> |
---|
[295] | 11 | #include <string> |
---|
| 12 | using std::string; |
---|
[109] | 13 | //#include <dirent.h> //to jest inkludowane przez powyzsze |
---|
| 14 | //struct dirent; //kiedys byla ta linia jak nie bylo jeszcze implementacji windowsowej dirent, ale borlandowi sie nie podoba jak s¹ obie |
---|
| 15 | |
---|
| 16 | #ifdef DLLEXPORTACTIVE //tylko w tym pliku uzyte |
---|
| 17 | #define DLLEXP __declspec(dllexport) |
---|
| 18 | #else |
---|
| 19 | #define DLLEXP |
---|
| 20 | #endif |
---|
| 21 | |
---|
| 22 | class DLLEXP VirtFileSystem; |
---|
| 23 | |
---|
| 24 | class DLLEXP VirtFILE |
---|
| 25 | { |
---|
[295] | 26 | protected: |
---|
| 27 | string path; |
---|
[109] | 28 | public: |
---|
[247] | 29 | virtual size_t Vread(void *ptr, size_t size, size_t nmemb)=0; |
---|
| 30 | virtual size_t Vwrite(const void *ptr, size_t size, size_t nmemb)=0; |
---|
[109] | 31 | virtual int Veof()=0; |
---|
[211] | 32 | virtual int Vputc(int c) {unsigned char data=(unsigned char)c; return (Vwrite(&data,1,1)==1)?data:EOF;} |
---|
[109] | 33 | virtual int Vputs(const char *s)=0; |
---|
[206] | 34 | virtual int Vgetc() {unsigned char data; if (Vread(&data,1,1)==1) return data; else return EOF;} |
---|
[109] | 35 | virtual int Vseek(long offset, int whence)=0; |
---|
[247] | 36 | virtual long Vtell()=0; |
---|
[206] | 37 | virtual void Vrewind() {Vseek(0,SEEK_SET);} |
---|
[109] | 38 | virtual int Vflush()=0; |
---|
| 39 | virtual char *Vgets(char *s, int size)=0; |
---|
[206] | 40 | virtual int Vprintf(const char *format, va_list args); |
---|
[109] | 41 | int printf(const char *format, ...); |
---|
[295] | 42 | virtual const char *VgetPath() {return path.c_str();} |
---|
[247] | 43 | virtual int getSize(); |
---|
[295] | 44 | VirtFILE(const char* _path):path(_path) {} |
---|
[109] | 45 | virtual ~VirtFILE(); |
---|
| 46 | static VirtFILE *Vstdin,*Vstdout,*Vstderr; |
---|
| 47 | static void setVstdin(VirtFILE *); |
---|
| 48 | static void setVstdout(VirtFILE *); |
---|
| 49 | static void setVstderr(VirtFILE *); |
---|
| 50 | static VirtFILE* getVstdin(); |
---|
| 51 | static VirtFILE* getVstdout(); |
---|
| 52 | static VirtFILE* getVstderr(); |
---|
| 53 | static VirtFileSystem *vfs; |
---|
| 54 | static void selectFileSystem(VirtFileSystem *s); |
---|
| 55 | }; |
---|
| 56 | |
---|
[295] | 57 | /** can be used directly or as a base class for implementations delegating VirtFILE calls to another VirtFILE object */ |
---|
| 58 | class DLLEXP DelegatedFILE: public VirtFILE |
---|
| 59 | { |
---|
| 60 | VirtFILE *delegate; |
---|
| 61 | public: |
---|
| 62 | size_t Vread(void *ptr, size_t size, size_t nmemb) {return delegate->Vread(ptr,size,nmemb);} |
---|
| 63 | size_t Vwrite(const void *ptr, size_t size, size_t nmemb) {return delegate->Vwrite(ptr,size,nmemb);} |
---|
| 64 | int Veof() {return delegate->Veof();} |
---|
| 65 | int Vputc(int c) {return delegate->Vputc(c);} |
---|
| 66 | int Vputs(const char *s) {return delegate->Vputs(s);} |
---|
| 67 | int Vgetc() {return delegate->Vgetc();} |
---|
| 68 | int Vseek(long offset, int whence) {return delegate->Vseek(offset,whence);} |
---|
| 69 | long Vtell() {return delegate->Vtell();} |
---|
| 70 | void Vrewind() {delegate->Vrewind();} |
---|
| 71 | int Vflush() {return delegate->Vflush();} |
---|
| 72 | char *Vgets(char *s, int size) {return delegate->Vgets(s,size);} |
---|
| 73 | int Vprintf(const char *format, va_list args) {return delegate->Vprintf(format,args);} |
---|
| 74 | int getSize() {return delegate->getSize();} |
---|
| 75 | // not overriden: VgetPath() |
---|
| 76 | |
---|
| 77 | DelegatedFILE(const char* _path,VirtFILE *_delegate):VirtFILE(_path),delegate(_delegate) {} |
---|
| 78 | virtual ~DelegatedFILE() {if (delegate) delete delegate; delegate=NULL;} |
---|
| 79 | }; |
---|
| 80 | |
---|
[109] | 81 | class DLLEXP VirtDIR |
---|
| 82 | { |
---|
| 83 | public: |
---|
| 84 | virtual ~VirtDIR() {} |
---|
| 85 | virtual dirent* Vreaddir() {return 0;} |
---|
| 86 | }; |
---|
| 87 | |
---|
| 88 | class DLLEXP VirtFileSystem |
---|
| 89 | { |
---|
| 90 | public: |
---|
| 91 | virtual VirtFILE *Vfopen(const char* path,const char*mode); |
---|
[295] | 92 | virtual bool Vfexists(const char* path); |
---|
[109] | 93 | virtual VirtDIR *Vopendir(const char* path); |
---|
[295] | 94 | virtual bool Vmkdir(const char* path); |
---|
| 95 | virtual bool Vmkdirs(const char* path); |
---|
| 96 | virtual bool Vdirexists(const char* path,bool is_writable); |
---|
[109] | 97 | }; |
---|
| 98 | |
---|
[295] | 99 | /// base class for chained filesystems - redirect unimplemented calls -> chain |
---|
| 100 | class DLLEXP ChainFileSystem : public VirtFileSystem |
---|
| 101 | { |
---|
| 102 | public: |
---|
| 103 | VirtFileSystem *chain; |
---|
| 104 | ChainFileSystem(VirtFileSystem *_chain = NULL) :chain(_chain) {} |
---|
| 105 | VirtFILE *Vfopen(const char* path, const char*mode); |
---|
| 106 | bool Vfexists(const char* path); |
---|
| 107 | VirtDIR *Vopendir(const char* path); |
---|
| 108 | bool Vmkdir(const char* path); |
---|
| 109 | bool Vmkdirs(const char* path); |
---|
| 110 | bool Vdirexists(const char* path,bool is_writable); |
---|
| 111 | }; |
---|
| 112 | |
---|
| 113 | |
---|
[109] | 114 | DLLEXP VirtFILE *Vfopen(const char* path,const char*mode); |
---|
| 115 | DLLEXP VirtDIR *Vopendir(const char* path); |
---|
[295] | 116 | DLLEXP bool Vfexists(const char* path); |
---|
| 117 | DLLEXP bool Vmkdir(const char* path); |
---|
| 118 | DLLEXP bool Vmkdirs(const char* path); |
---|
| 119 | DLLEXP bool Vdirexists(const char* path,bool is_writable); |
---|
[109] | 120 | |
---|
| 121 | DLLEXP int fread(void *ptr, size_t size, size_t nmemb, VirtFILE* f); |
---|
| 122 | DLLEXP int fwrite(const void *ptr, size_t size, size_t nmemb, VirtFILE* f); |
---|
[123] | 123 | |
---|
| 124 | |
---|
[131] | 125 | //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.cpp |
---|
[225] | 126 | #if defined _MSC_VER || defined __CYGWIN__ || defined SHP || defined __ANDROID__ |
---|
[131] | 127 | #pragma push_macro("feof") |
---|
[123] | 128 | #undef feof |
---|
| 129 | #endif |
---|
[131] | 130 | #if defined __BORLANDC__ //does not support #pragma push_macro/pop_macro |
---|
| 131 | #undef feof |
---|
| 132 | #endif |
---|
[123] | 133 | |
---|
| 134 | DLLEXP int feof(VirtFILE* f);// {return f->Veof();} |
---|
| 135 | |
---|
| 136 | //...and then restore the original macro: |
---|
[225] | 137 | #if defined _MSC_VER || defined __CYGWIN__ || defined SHP || defined __ANDROID__ |
---|
[131] | 138 | #pragma pop_macro("feof") |
---|
| 139 | #endif |
---|
[225] | 140 | #if defined __BORLANDC__ |
---|
[123] | 141 | #define feof(__f) ((__f)->flags & _F_EOF) |
---|
[109] | 142 | #endif |
---|
| 143 | |
---|
[123] | 144 | |
---|
[109] | 145 | DLLEXP int fputc(int c,VirtFILE* f); |
---|
| 146 | DLLEXP int fputs(const char *s,VirtFILE* f); |
---|
| 147 | DLLEXP int fgetc(VirtFILE* f); |
---|
| 148 | DLLEXP int fseek(VirtFILE* f,long offset, int whence); |
---|
| 149 | DLLEXP int ftell(VirtFILE* f); |
---|
| 150 | DLLEXP void rewind(VirtFILE* f); |
---|
| 151 | DLLEXP int fflush(VirtFILE* f); |
---|
| 152 | DLLEXP char *fgets(char *s, int size, VirtFILE* f); |
---|
| 153 | DLLEXP int fprintf(VirtFILE* f,const char *format, ...); |
---|
| 154 | DLLEXP int fclose(VirtFILE* f); |
---|
| 155 | |
---|
| 156 | DLLEXP dirent* readdir(VirtDIR* d); |
---|
| 157 | DLLEXP int closedir(VirtDIR* d); |
---|
| 158 | |
---|
| 159 | #endif |
---|
| 160 | |
---|