source: cpp/frams/virtfile/stdiofile.h @ 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: 2.6 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#ifndef _STDIOFILE_H_
6#define _STDIOFILE_H_
7
8#include "virtfile.h"
9#include <frams/util/sstring.h>
10#ifdef USE_MFILE
11#include <common/nonstd_stdio.h>
12#else
13#include <stdio.h>
14#endif
15#include <common/nonstd_dir.h>
16
17class StdioFileSystem: public VirtFileSystem
18{
19public:
20VirtFILE *Vfopen(const char* path,const char*mode);
21int Vfexists(const char* path);
22VirtDIR *Vopendir(const char* path);
23};
24
25#ifdef USE_MFILE
26class StdioFILE: public VirtFILE
27{
28  protected:
29MFILE *file;
30SString path;
31  public:
32StdioFILE(MFILE *f) {file=f;}
33StdioFILE(MFILE *f,const SString& p) {file=f;path=p;}
34static void setStdio();
35int Vread(void *ptr, size_t size, size_t nmemb) {return mfread(ptr,size,nmemb,file);}
36int Vwrite(const void *ptr, size_t size, size_t nmemb) {return mfwrite(ptr,size,nmemb,file);}
37int Veof() {return mfeof(file);}
38int Vputs(const char *s) {return mfputs(s,file);}
39char *Vgets(char *s, int size) {return mfgets(s,size,file);}
40int Vseek(long offset, int whence) {return mfseek(file,offset,whence);}
41int Vtell() {return mftell(file);}
42int Vflush() {return 0;/*NOT IMPLEMENTED!*/;}
43const char* VgetPath() {return path;}
44
45~StdioFILE() {if (file) fclose(file);}
46};
47#else
48class StdioFILE: public VirtFILE
49{
50  protected:
51FILE *file;
52SString path;
53  public:
54StdioFILE(FILE *f) {file=f;}
55StdioFILE(FILE *f,const SString& p) {file=f;path=p;}
56static void setStdio();
57int Vread(void *ptr, size_t size, size_t nmemb) {return fread(ptr,size,nmemb,file);}
58int Vwrite(const void *ptr, size_t size, size_t nmemb) {return fwrite(ptr,size,nmemb,file);}
59int Veof() {return feof(file);}
60int Vputc(int c) {return fputc(c,file);}
61int Vputs(const char *s) {return fputs(s,file);}
62int Vgetc() {return fgetc(file);}
63char *Vgets(char *s, int size) {return fgets(s,size,file);}
64int Vprintf(const char *format, va_list args) { return vfprintf(file,format,args); }
65int Vseek(long offset, int whence) {return fseek(file,offset,whence);}
66int Vtell() {return ftell(file);}
67void Vrewind() {rewind(file);}
68int Vflush() {return fflush(file);}
69const char* VgetPath() {return path;}
70
71~StdioFILE() {if (file) fclose(file);}
72};
73#endif
74
75class StdioDIR: public VirtDIR
76{
77DIR *dir;
78  public:
79StdioDIR(DIR* d):dir(d) {}
80~StdioDIR() {if (dir) closedir(dir);}
81dirent* Vreaddir();
82};
83
84class StdioFILEDontClose: public StdioFILE
85{
86  public:
87#ifdef USE_MFILE
88StdioFILEDontClose(MFILE *f):StdioFILE(f) {}
89#else
90StdioFILEDontClose(FILE *f):StdioFILE(f) {}
91#endif
92~StdioFILEDontClose() {file=0;}
93};
94
95#endif
Note: See TracBrowser for help on using the repository browser.