source: cpp/frams/virtfile/virtfile.h @ 299

Last change on this file since 299 was 295, checked in by Maciej Komosinski, 9 years ago

Reorganizations and extensions of directory/file/filesystem IO classes

  • Property svn:eol-style set to native
File size: 5.4 KB
Line 
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.
4
5#ifndef _VIRTFILE_H_
6#define _VIRTFILE_H_
7
8#include <stdio.h>
9#include <stdarg.h>
10#include <common/nonstd_dir.h>
11#include <string>
12using std::string;
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
22class DLLEXP VirtFileSystem;
23
24class DLLEXP VirtFILE
25{
26  protected:
27string path;
28  public:
29virtual size_t Vread(void *ptr, size_t size, size_t nmemb)=0;
30virtual size_t Vwrite(const void *ptr, size_t size, size_t nmemb)=0;
31virtual int Veof()=0;
32virtual int Vputc(int c) {unsigned char data=(unsigned char)c; return (Vwrite(&data,1,1)==1)?data:EOF;}
33virtual int Vputs(const char *s)=0;
34virtual int Vgetc() {unsigned char data; if (Vread(&data,1,1)==1) return data; else return EOF;}
35virtual int Vseek(long offset, int whence)=0;
36virtual long Vtell()=0;
37virtual void Vrewind() {Vseek(0,SEEK_SET);}
38virtual int Vflush()=0;
39virtual char *Vgets(char *s, int size)=0;
40virtual int Vprintf(const char *format, va_list args);
41int printf(const char *format, ...);
42virtual const char *VgetPath() {return path.c_str();}
43virtual int getSize();
44VirtFILE(const char* _path):path(_path) {}
45virtual ~VirtFILE();
46static VirtFILE *Vstdin,*Vstdout,*Vstderr;
47static void setVstdin(VirtFILE *);
48static void setVstdout(VirtFILE *);
49static void setVstderr(VirtFILE *);
50static VirtFILE* getVstdin();
51static VirtFILE* getVstdout();
52static VirtFILE* getVstderr();
53static VirtFileSystem *vfs;
54static void selectFileSystem(VirtFileSystem *s);
55};
56
57/** can be used directly or as a base class for implementations delegating VirtFILE calls to another VirtFILE object */
58class DLLEXP DelegatedFILE: public VirtFILE
59{
60VirtFILE *delegate;
61  public:
62size_t Vread(void *ptr, size_t size, size_t nmemb) {return delegate->Vread(ptr,size,nmemb);}
63size_t Vwrite(const void *ptr, size_t size, size_t nmemb) {return delegate->Vwrite(ptr,size,nmemb);}
64int Veof() {return delegate->Veof();}
65int Vputc(int c) {return delegate->Vputc(c);}
66int Vputs(const char *s) {return delegate->Vputs(s);}
67int Vgetc() {return delegate->Vgetc();}
68int Vseek(long offset, int whence) {return delegate->Vseek(offset,whence);}
69long Vtell() {return delegate->Vtell();}
70void Vrewind() {delegate->Vrewind();}
71int Vflush() {return delegate->Vflush();}
72char *Vgets(char *s, int size) {return delegate->Vgets(s,size);}
73int Vprintf(const char *format, va_list args) {return delegate->Vprintf(format,args);}
74int getSize() {return delegate->getSize();}
75// not overriden: VgetPath()
76
77DelegatedFILE(const char* _path,VirtFILE *_delegate):VirtFILE(_path),delegate(_delegate) {}
78virtual ~DelegatedFILE() {if (delegate) delete delegate; delegate=NULL;}
79};
80
81class DLLEXP VirtDIR
82{
83  public:
84virtual ~VirtDIR() {}
85virtual dirent* Vreaddir() {return 0;}
86};
87
88class DLLEXP VirtFileSystem
89{
90public:
91virtual VirtFILE *Vfopen(const char* path,const char*mode);
92virtual bool Vfexists(const char* path);
93virtual VirtDIR *Vopendir(const char* path);
94virtual bool Vmkdir(const char* path);
95virtual bool Vmkdirs(const char* path);
96virtual bool Vdirexists(const char* path,bool is_writable);
97};
98
99/// base class for chained filesystems - redirect unimplemented calls -> chain
100class DLLEXP ChainFileSystem : public VirtFileSystem
101{
102public:
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
114DLLEXP VirtFILE *Vfopen(const char* path,const char*mode);
115DLLEXP VirtDIR *Vopendir(const char* path);
116DLLEXP bool Vfexists(const char* path);
117DLLEXP bool Vmkdir(const char* path);
118DLLEXP bool Vmkdirs(const char* path);
119DLLEXP bool Vdirexists(const char* path,bool is_writable);
120
121DLLEXP int fread(void *ptr, size_t size, size_t nmemb, VirtFILE* f);
122DLLEXP int fwrite(const void *ptr, size_t size, size_t nmemb, VirtFILE* f);
123
124
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
126#if defined _MSC_VER || defined __CYGWIN__ || defined SHP || defined __ANDROID__
127 #pragma push_macro("feof")
128 #undef feof
129#endif
130#if defined __BORLANDC__ //does not support #pragma push_macro/pop_macro
131 #undef feof
132#endif
133 
134DLLEXP int feof(VirtFILE* f);// {return f->Veof();}
135
136//...and then restore the original macro:
137#if defined _MSC_VER || defined __CYGWIN__ || defined SHP || defined __ANDROID__
138 #pragma pop_macro("feof")
139#endif
140#if defined __BORLANDC__
141 #define feof(__f)     ((__f)->flags & _F_EOF)
142#endif
143
144
145DLLEXP int fputc(int c,VirtFILE* f);
146DLLEXP int fputs(const char *s,VirtFILE* f);
147DLLEXP int fgetc(VirtFILE* f);
148DLLEXP int fseek(VirtFILE* f,long offset, int whence);
149DLLEXP int ftell(VirtFILE* f);
150DLLEXP void rewind(VirtFILE* f);
151DLLEXP int fflush(VirtFILE* f);
152DLLEXP char *fgets(char *s, int size, VirtFILE* f);
153DLLEXP int fprintf(VirtFILE* f,const char *format, ...);
154DLLEXP int fclose(VirtFILE* f);
155
156DLLEXP dirent* readdir(VirtDIR* d);
157DLLEXP int closedir(VirtDIR* d);
158
159#endif
160
Note: See TracBrowser for help on using the repository browser.