source: cpp/common/virtfile/stdiofile.h @ 1333

Last change on this file since 1333 was 1328, checked in by Maciej Komosinski, 12 days ago

Added Vstat(), Vdelete(), Vsettime() to VirtFILE

  • Property svn:eol-style set to native
File size: 3.3 KB
Line 
1// This file is a part of Framsticks SDK.  http://www.framsticks.com/
2// Copyright (C) 1999-2024  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);
20        bool Vdelete(const char* path);
21        bool Vstat(const char* path, Stat* stat);
22        bool Vsettime(const char* path, double timestamp);
23};
24
25#ifdef USE_MFILE
26class StdioFILE : public VirtFILE
27{
28protected:
29        MFILE *file;
30public:
31        StdioFILE(MFILE *f) :VirtFILE("") { file = f; }
32        StdioFILE(MFILE *f, const char* p) :VirtFILE(p) { file = f; }
33#ifndef NO_STD_IN_OUT_ERR
34        static void setStdio();
35#endif
36        size_t Vread(void *ptr, size_t size, size_t nmemb) { return mfread(ptr, size, nmemb, file); }
37        size_t Vwrite(const void *ptr, size_t size, size_t nmemb) { return mfwrite(ptr, size, nmemb, file); }
38        int Veof() { return mfeof(file); }
39        int Vputs(const char *s) { return mfputs(s, file); }
40        char *Vgets(char *s, int size) { return mfgets(s, size, file); }
41        int Vseek(long offset, int whence) { return mfseek(file, offset, whence); }
42        long Vtell() { return mftell(file); }
43        int Vflush() { return 0; /*NOT IMPLEMENTED!*/ }
44
45        ~StdioFILE() { if (file) mfclose(file); }
46};
47#else
48class StdioFILE : public VirtFILE
49{
50protected:
51        FILE *file;
52public:
53        StdioFILE(FILE *f) :VirtFILE("") { file = f; }
54        StdioFILE(FILE *f, const char* p) :VirtFILE(p) { file = f; }
55#ifndef NO_STD_IN_OUT_ERR
56        static void setStdio();
57#endif
58        size_t Vread(void *ptr, size_t size, size_t nmemb) { return fread(ptr, size, nmemb, file); }
59        size_t Vwrite(const void *ptr, size_t size, size_t nmemb) { return fwrite(ptr, size, nmemb, file); }
60        int Veof() { return feof(file); }
61        int Vputc(int c) { return fputc(c, file); }
62        int Vputs(const char *s) { return fputs(s, file); }
63        int Vgetc() { return fgetc(file); }
64        char *Vgets(char *s, int size) { return fgets(s, size, file); }
65        int Vprintf(const char *format, va_list args) { return vfprintf(file, format, args); }
66        int Vseek(long offset, int whence) { return fseek(file, offset, whence); }
67        long Vtell() { return ftell(file); }
68        void Vrewind() { rewind(file); }
69        int Vflush() { return fflush(file); }
70
71        ~StdioFILE() { if (file) fclose(file); }
72};
73#endif
74
75
76#ifdef _WIN32
77#ifdef __BORLANDC__
78typedef wDIR DIRTYPE;
79#else
80typedef WDIR DIRTYPE;
81#endif
82#else
83typedef DIR DIRTYPE;
84#endif
85
86class StdioDIR : public VirtDIR
87{
88        DIRTYPE *dir;
89#ifdef _WIN32
90        dirent de; //only used to convert wide string names (wdirent) to utf8 (dirent)
91#endif
92public:
93        StdioDIR(DIRTYPE *d) : dir(d) {}
94        ~StdioDIR()
95        {
96#ifdef _WIN32
97                if (dir) wclosedir(dir);
98#else
99                if (dir) closedir(dir);
100#endif
101        }
102        dirent* Vreaddir();
103};
104
105class StdioFILEDontClose : public StdioFILE
106{
107public:
108#ifdef USE_MFILE
109        StdioFILEDontClose(MFILE *f) : StdioFILE(f) {}
110#else
111        StdioFILEDontClose(FILE *f) : StdioFILE(f) {}
112#endif
113        ~StdioFILEDontClose() { file = 0; }
114};
115
116class StdioFileSystem_autoselect : public StdioFileSystem
117{
118public:
119        StdioFileSystem_autoselect()
120        {
121                VirtFILE::selectFileSystem(this);
122        }
123};
124
125#endif
Note: See TracBrowser for help on using the repository browser.