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

Last change on this file since 207 was 206, checked in by Maciej Komosinski, 10 years ago

VirtFILE base class provides Vprintf, Vgetc, Vputc, Vrewind, so that subclasses do not have to implement these functions

  • Property svn:eol-style set to native
File size: 3.2 KB
Line 
1// This file is a part of the Framsticks GDK.
2// Copyright (C) 1999-2014  Maciej Komosinski and Szymon Ulatowski.  See LICENSE.txt for details.
3// Refer to http://www.framsticks.com/ for further information.
4
5#include "virtfile.h"
6#include <common/stl-util.h>
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
24int Vfexists(const char* path)
25{
26return VirtFILE::vfs ? VirtFILE::vfs->Vfexists(path) : 0;
27}
28
29VirtFILE::~VirtFILE()
30{}
31
32void VirtFILE::selectFileSystem(VirtFileSystem *s) {vfs=s;}
33
34int VirtFILE::Vprintf(const char *format, va_list args)
35{
36string s=ssprintf_va(format,args);
37return Vwrite(s.c_str(),1,s.size());
38}
39
40int VirtFILE::printf(const char *format, ...)
41{
42int ret; va_list argptr;
43va_start(argptr,format);
44ret=Vprintf(format,argptr);
45va_end(argptr);
46return ret;
47}
48
49void VirtFILE::setVstdin(VirtFILE *f) {Vstdin=f;}
50void VirtFILE::setVstdout(VirtFILE *f) {Vstdout=f;}
51void VirtFILE::setVstderr(VirtFILE *f) {Vstderr=f;}
52VirtFILE* VirtFILE::getVstdin() {return Vstdin;}
53VirtFILE* VirtFILE::getVstdout() {return Vstdout;}
54VirtFILE* VirtFILE::getVstderr() {return Vstderr;}
55//////////////////////////////////////////////////////////////////////////
56
57VirtFILE* VirtFileSystem::Vfopen(const char* path,const char*mode) {return 0;}
58int VirtFileSystem::Vfexists(const char* path) {return 0;}
59VirtDIR* VirtFileSystem::Vopendir(const char* path) {return 0;}
60
61//////////////////////////////////////////////////////////////////////////
62
63
64
65int fread(void *ptr, size_t size, size_t nmemb, VirtFILE* f) {return f->Vread(ptr,size,nmemb);}
66int fwrite(const void *ptr, size_t size, size_t nmemb, VirtFILE* f) {return f->Vwrite(ptr,size,nmemb);}
67
68
69//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
70#if defined _MSC_VER || defined __CYGWIN__ || defined SHP
71 #pragma push_macro("feof")
72 #undef feof
73#endif
74#if defined __BORLANDC__ //does not support #pragma push_macro/pop_macro
75 #undef feof
76#endif
77
78int feof(VirtFILE* f) {return f->Veof();}
79
80//...and then restore the original macro:
81#if defined _MSC_VER || defined __CYGWIN__ || defined SHP
82 #pragma pop_macro("feof")
83#endif
84#ifdef __BORLANDC__
85 #define feof(__f)     ((__f)->flags & _F_EOF)
86#endif
87
88
89int fputc(int c,VirtFILE* f) {return f->Vputc(c);}
90int fputs(const char *s,VirtFILE* f) {return f->Vputs(s);}
91int fgetc(VirtFILE* f) {return f->Vgetc();}
92int fseek(VirtFILE* f,long offset, int whence) {return f->Vseek(offset,whence);}
93int ftell(VirtFILE* f) {return f->Vtell();}
94void rewind(VirtFILE* f) {f->Vrewind();}
95int fflush(VirtFILE* f) {return f->Vflush();}
96char *fgets(char *s, int size, VirtFILE* f) {return f->Vgets(s,size);}
97int fprintf(VirtFILE* f,const char *format, ...)
98        {
99        int ret; va_list argptr;
100        va_start(argptr,format);
101        ret=f->Vprintf(format,argptr);
102        va_end(argptr);
103        return ret;
104        }
105int fclose(VirtFILE* f) {delete f; return 0;}
106
107int closedir(VirtDIR* d) {delete d; return 0;}
108dirent* readdir(VirtDIR* d) {return d->Vreaddir();}
109
Note: See TracBrowser for help on using the repository browser.