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

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

0 -> NULL or false

  • Property svn:eol-style set to native
File size: 4.9 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#include "virtfile.h"
6#include <common/stl-util.h>
7
8VirtFILE *VirtFILE::Vstdin = NULL;
9VirtFILE *VirtFILE::Vstdout = NULL;
10VirtFILE *VirtFILE::Vstderr = NULL;
11
12VirtFileSystem *VirtFILE::vfs = NULL;
13
14VirtFILE *Vfopen(const char* path,const char*mode)
15{
16return VirtFILE::vfs ? VirtFILE::vfs->Vfopen(path,mode) : NULL;
17}
18
19VirtDIR *Vopendir(const char* path)
20{
21return VirtFILE::vfs ? VirtFILE::vfs->Vopendir(path) : NULL;
22}
23
24bool Vfexists(const char* path)
25{
26return VirtFILE::vfs ? VirtFILE::vfs->Vfexists(path) : false;
27}
28
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
44VirtFILE::~VirtFILE()
45{}
46
47void VirtFILE::selectFileSystem(VirtFileSystem *s) {vfs=s;}
48
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
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
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
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
81// base class only returns NULL/false/not supported - implementations perform the actual work
82VirtFILE* VirtFileSystem::Vfopen(const char* path,const char*mode) {return NULL;}
83bool VirtFileSystem::Vfexists(const char* path) {return false;}
84VirtDIR* VirtFileSystem::Vopendir(const char* path) {return NULL;}
85bool VirtFileSystem::Vmkdir(const char* path) {return false;}
86bool VirtFileSystem::Vdirexists(const char* path,bool is_writable) {return false;}
87
88//////////////////////////////////////////////////////////////////////////
89
90
91
92int fread(void *ptr, size_t size, size_t nmemb, VirtFILE* f) {return f->Vread(ptr,size,nmemb);}
93int fwrite(const void *ptr, size_t size, size_t nmemb, VirtFILE* f) {return f->Vwrite(ptr,size,nmemb);}
94
95
96//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
97#if defined _MSC_VER || defined __CYGWIN__ || defined SHP || defined __ANDROID__
98 #pragma push_macro("feof")
99 #undef feof
100#endif
101#if defined __BORLANDC__ //does not support #pragma push_macro/pop_macro
102 #undef feof
103#endif
104
105int feof(VirtFILE* f) {return f->Veof();}
106
107//...and then restore the original macro:
108#if defined _MSC_VER || defined __CYGWIN__ || defined SHP || defined __ANDROID__
109 #pragma pop_macro("feof")
110#endif
111#if defined __BORLANDC__
112 #define feof(__f)     ((__f)->flags & _F_EOF)
113#endif
114
115
116int fputc(int c,VirtFILE* f) {return f->Vputc(c);}
117int fputs(const char *s,VirtFILE* f) {return f->Vputs(s);}
118int fgetc(VirtFILE* f) {return f->Vgetc();}
119int fseek(VirtFILE* f,long offset, int whence) {return f->Vseek(offset,whence);}
120int ftell(VirtFILE* f) {return f->Vtell();}
121void rewind(VirtFILE* f) {f->Vrewind();}
122int fflush(VirtFILE* f) {return f->Vflush();}
123char *fgets(char *s, int size, VirtFILE* f) {return f->Vgets(s,size);}
124int fprintf(VirtFILE* f,const char *format, ...)
125        {
126        int ret; va_list argptr;
127        va_start(argptr,format);
128        ret=f->Vprintf(format,argptr);
129        va_end(argptr);
130        return ret;
131        }
132int fclose(VirtFILE* f) {delete f; return 0;}
133
134int closedir(VirtDIR* d) {delete d; return 0;}
135dirent* readdir(VirtDIR* d) {return d->Vreaddir();}
136
137/////////
138
139bool VirtFileSystem::Vmkdirs(const char* path)
140{
141if (Vdirexists(path,true)) return true;
142string parentdir = getFileDir(path);
143if (!Vmkdirs(parentdir.c_str())) return false;
144return Vmkdir(path);
145}
146
147//////////
148
149VirtFILE *ChainFileSystem::Vfopen(const char* path, const char*mode)
150{
151        return (chain != NULL) ? chain->Vfopen(path, mode) : NULL;
152}
153
154bool ChainFileSystem::Vfexists(const char* path)
155{
156        return (chain != NULL) ? chain->Vfexists(path) : false;
157}
158
159VirtDIR *ChainFileSystem::Vopendir(const char* path)
160{
161        return (chain != NULL) ? chain->Vopendir(path) : NULL;
162}
163
164bool ChainFileSystem::Vmkdir(const char* path)
165{
166        return (chain != NULL) ? chain->Vmkdir(path) : false;
167}
168
169bool ChainFileSystem::Vmkdirs(const char* path)
170{
171        return (chain != NULL) ? chain->Vmkdirs(path) : false;
172}
173
174bool ChainFileSystem::Vdirexists(const char* path,bool is_writable)
175{
176        return (chain != NULL) ? chain->Vdirexists(path,is_writable) : false;
177}
Note: See TracBrowser for help on using the repository browser.