source: cpp/frams/virtfile/stdiofile.h @ 347

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

Instead of linking with stdiofile-autoinit.cpp, create an object StdioFileSystem_autoselect stdiofilesys;

  • Property svn:eol-style set to native
File size: 3.1 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 _STDIOFILE_H_
6#define _STDIOFILE_H_
7
8#include "virtfile.h"
9#include <common/nonstd_stdio.h>
10#include <common/nonstd_dir.h>
11
12class StdioFileSystem : public VirtFileSystem
13{
14public:
15        VirtFILE *Vfopen(const char *path, const char *mode);
16        bool Vfexists(const char* path);
17        VirtDIR *Vopendir(const char* path);
18        bool Vmkdir(const char* path) { return makeDirectory(path); }
19        bool Vdirexists(const char* path,bool is_writable) { return directoryExists(path,is_writable); }
20};
21
22#ifdef USE_MFILE
23class StdioFILE : public VirtFILE
24{
25protected:
26        MFILE *file;
27public:
28        StdioFILE(MFILE *f):VirtFILE("") { file = f; }
29        StdioFILE(MFILE *f, const char* p):VirtFILE(p) { file = f; }
30        static void setStdio();
31        size_t Vread(void *ptr, size_t size, size_t nmemb) { return mfread(ptr, size, nmemb, file); }
32        size_t Vwrite(const void *ptr, size_t size, size_t nmemb) { return mfwrite(ptr, size, nmemb, file); }
33        int Veof() { return mfeof(file); }
34        int Vputs(const char *s) { return mfputs(s, file); }
35        char *Vgets(char *s, int size) { return mfgets(s, size, file); }
36        int Vseek(long offset, int whence) { return mfseek(file, offset, whence); }
37        long Vtell() { return mftell(file); }
38        int Vflush() { return 0; /*NOT IMPLEMENTED!*/ }
39
40        ~StdioFILE() { if (file) mfclose(file); }
41};
42#else
43class StdioFILE : public VirtFILE
44{
45protected:
46        FILE *file;
47public:
48        StdioFILE(FILE *f):VirtFILE("") { file = f; }
49        StdioFILE(FILE *f, const char* p):VirtFILE(p) { file = f; }
50        static void setStdio();
51        size_t Vread(void *ptr, size_t size, size_t nmemb) { return fread(ptr, size, nmemb, file); }
52        size_t Vwrite(const void *ptr, size_t size, size_t nmemb) { return fwrite(ptr, size, nmemb, file); }
53        int Veof() { return feof(file); }
54        int Vputc(int c) { return fputc(c, file); }
55        int Vputs(const char *s) { return fputs(s, file); }
56        int Vgetc() { return fgetc(file); }
57        char *Vgets(char *s, int size) { return fgets(s, size, file); }
58        int Vprintf(const char *format, va_list args) { return vfprintf(file, format, args); }
59        int Vseek(long offset, int whence) { return fseek(file, offset, whence); }
60        long Vtell() { return ftell(file); }
61        void Vrewind() { rewind(file); }
62        int Vflush() { return fflush(file); }
63
64        ~StdioFILE() { if (file) fclose(file); }
65};
66#endif
67
68
69#ifdef _WIN32
70#ifdef __BORLANDC__
71 typedef wDIR DIRTYPE;
72#else
73 typedef WDIR DIRTYPE;
74#endif
75#else
76 typedef DIR DIRTYPE;
77#endif
78
79class StdioDIR : public VirtDIR
80{
81        DIRTYPE *dir;
82#ifdef _WIN32
83        dirent de; //only used to convert wide string names (wdirent) to utf8 (dirent)
84#endif
85public:
86        StdioDIR(DIRTYPE *d) : dir(d) {}
87        ~StdioDIR()
88        {
89#ifdef _WIN32
90                if (dir) wclosedir(dir);
91#else
92                if (dir) closedir(dir);
93#endif
94        }
95        dirent* Vreaddir();
96};
97
98class StdioFILEDontClose : public StdioFILE
99{
100public:
101#ifdef USE_MFILE
102        StdioFILEDontClose(MFILE *f) : StdioFILE(f) {}
103#else
104        StdioFILEDontClose(FILE *f) : StdioFILE(f) {}
105#endif
106        ~StdioFILEDontClose() { file = 0; }
107};
108
109class StdioFileSystem_autoselect: public StdioFileSystem
110{
111public:
112StdioFileSystem_autoselect()
113        {
114        VirtFILE::selectFileSystem(this);
115        }
116};
117
118#endif
Note: See TracBrowser for help on using the repository browser.