source: cpp/frams/virtfile/virtfile.cpp @ 295

Last change on this file since 295 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: 4.8 KB
RevLine 
[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
8VirtFILE *VirtFILE::Vstdin=0;
9VirtFILE *VirtFILE::Vstdout=0;
10VirtFILE *VirtFILE::Vstderr=0;
11
12VirtFileSystem *VirtFILE::vfs=0;
13
14VirtFILE *Vfopen(const char* path,const char*mode)
15{
16return VirtFILE::vfs ? VirtFILE::vfs->Vfopen(path,mode) : 0;
17}
18
19VirtDIR *Vopendir(const char* path)
20{
21return VirtFILE::vfs ? VirtFILE::vfs->Vopendir(path) : 0;
22}
23
[295]24bool Vfexists(const char* path)
[109]25{
[295]26return VirtFILE::vfs ? VirtFILE::vfs->Vfexists(path) : false;
[109]27}
28
[295]29bool Vdirexists(const char* path,bool is_writable)
30{
31return VirtFILE::vfs ? VirtFILE::vfs->Vdirexists(path,is_writable) : false;
32}
33
34bool Vmkdir(const char* path)
35{
36return VirtFILE::vfs ? VirtFILE::vfs->Vmkdir(path) : false;
37}
38
39bool Vmkdirs(const char* path)
40{
41return VirtFILE::vfs ? VirtFILE::vfs->Vmkdirs(path) : false;
42}
43
[109]44VirtFILE::~VirtFILE()
45{}
46
47void VirtFILE::selectFileSystem(VirtFileSystem *s) {vfs=s;}
48
[206]49int VirtFILE::Vprintf(const char *format, va_list args)
50{
51string s=ssprintf_va(format,args);
52return Vwrite(s.c_str(),1,s.size());
53}
54
[109]55int VirtFILE::printf(const char *format, ...)
56{
57int ret; va_list argptr;
58va_start(argptr,format);
59ret=Vprintf(format,argptr);
60va_end(argptr);
61return ret;
62}
63
[247]64int 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]73void VirtFILE::setVstdin(VirtFILE *f) {Vstdin=f;}
74void VirtFILE::setVstdout(VirtFILE *f) {Vstdout=f;}
75void VirtFILE::setVstderr(VirtFILE *f) {Vstderr=f;}
76VirtFILE* VirtFILE::getVstdin() {return Vstdin;}
77VirtFILE* VirtFILE::getVstdout() {return Vstdout;}
78VirtFILE* VirtFILE::getVstderr() {return Vstderr;}
79//////////////////////////////////////////////////////////////////////////
80
81VirtFILE* VirtFileSystem::Vfopen(const char* path,const char*mode) {return 0;}
[295]82bool VirtFileSystem::Vfexists(const char* path) {return 0;}
[109]83VirtDIR* VirtFileSystem::Vopendir(const char* path) {return 0;}
[295]84bool VirtFileSystem::Vmkdir(const char* path) {return false;} //error - not supported
85bool VirtFileSystem::Vdirexists(const char* path,bool is_writable) {return false;}
[109]86
87//////////////////////////////////////////////////////////////////////////
88
89
90
91int fread(void *ptr, size_t size, size_t nmemb, VirtFILE* f) {return f->Vread(ptr,size,nmemb);}
92int fwrite(const void *ptr, size_t size, size_t nmemb, VirtFILE* f) {return f->Vwrite(ptr,size,nmemb);}
[123]93
[131]94
95//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]96#if defined _MSC_VER || defined __CYGWIN__ || defined SHP || defined __ANDROID__
[131]97 #pragma push_macro("feof")
[123]98 #undef feof
99#endif
[131]100#if defined __BORLANDC__ //does not support #pragma push_macro/pop_macro
101 #undef feof
102#endif
[123]103
104int feof(VirtFILE* f) {return f->Veof();}
105
106//...and then restore the original macro:
[225]107#if defined _MSC_VER || defined __CYGWIN__ || defined SHP || defined __ANDROID__
[131]108 #pragma pop_macro("feof")
109#endif
[225]110#if defined __BORLANDC__
[123]111 #define feof(__f)     ((__f)->flags & _F_EOF)
[109]112#endif
113
[123]114
[109]115int fputc(int c,VirtFILE* f) {return f->Vputc(c);}
116int fputs(const char *s,VirtFILE* f) {return f->Vputs(s);}
117int fgetc(VirtFILE* f) {return f->Vgetc();}
118int fseek(VirtFILE* f,long offset, int whence) {return f->Vseek(offset,whence);}
119int ftell(VirtFILE* f) {return f->Vtell();}
120void rewind(VirtFILE* f) {f->Vrewind();}
121int fflush(VirtFILE* f) {return f->Vflush();}
122char *fgets(char *s, int size, VirtFILE* f) {return f->Vgets(s,size);}
123int fprintf(VirtFILE* f,const char *format, ...)
124        {
125        int ret; va_list argptr;
126        va_start(argptr,format);
127        ret=f->Vprintf(format,argptr);
128        va_end(argptr);
129        return ret;
130        }
131int fclose(VirtFILE* f) {delete f; return 0;}
132
133int closedir(VirtDIR* d) {delete d; return 0;}
134dirent* readdir(VirtDIR* d) {return d->Vreaddir();}
[123]135
[295]136/////////
137
138bool VirtFileSystem::Vmkdirs(const char* path)
139{
140if (Vdirexists(path,true)) return true;
141string parentdir = getFileDir(path);
142if (!Vmkdirs(parentdir.c_str())) return false;
143return Vmkdir(path);
144}
145
146//////////
147
148VirtFILE *ChainFileSystem::Vfopen(const char* path, const char*mode)
149{
150        return (chain != NULL) ? chain->Vfopen(path, mode) : NULL;
151}
152
153bool ChainFileSystem::Vfexists(const char* path)
154{
155        return (chain != NULL) ? chain->Vfexists(path) : false;
156}
157
158VirtDIR *ChainFileSystem::Vopendir(const char* path)
159{
160        return (chain != NULL) ? chain->Vopendir(path) : NULL;
161}
162
163bool ChainFileSystem::Vmkdir(const char* path)
164{
165        return (chain != NULL) ? chain->Vmkdir(path) : false;
166}
167
168bool ChainFileSystem::Vmkdirs(const char* path)
169{
170        return (chain != NULL) ? chain->Vmkdirs(path) : false;
171}
172
173bool ChainFileSystem::Vdirexists(const char* path,bool is_writable)
174{
175        return (chain != NULL) ? chain->Vdirexists(path,is_writable) : false;
176}
Note: See TracBrowser for help on using the repository browser.