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

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

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

  • Property svn:eol-style set to native
File size: 3.3 KB
RevLine 
[286]1// This file is a part of Framsticks SDK.  http://www.framsticks.com/
[1328]2// Copyright (C) 1999-2024  Maciej Komosinski and Szymon Ulatowski.
[286]3// See LICENSE.txt for details.
[109]4
5#ifndef _STDIOFILE_H_
6#define _STDIOFILE_H_
7
8#include "virtfile.h"
[206]9#include <common/nonstd_stdio.h>
[109]10#include <common/nonstd_dir.h>
11
[281]12class StdioFileSystem : public VirtFileSystem
[109]13{
14public:
[281]15        VirtFILE *Vfopen(const char *path, const char *mode);
[295]16        bool Vfexists(const char* path);
[281]17        VirtDIR *Vopendir(const char* path);
[295]18        bool Vmkdir(const char* path) { return makeDirectory(path); }
[820]19        bool Vdirexists(const char* path, bool is_writable);
[1328]20        bool Vdelete(const char* path);
21        bool Vstat(const char* path, Stat* stat);
22        bool Vsettime(const char* path, double timestamp);
[109]23};
24
[206]25#ifdef USE_MFILE
[281]26class StdioFILE : public VirtFILE
[109]27{
[281]28protected:
29        MFILE *file;
30public:
[820]31        StdioFILE(MFILE *f) :VirtFILE("") { file = f; }
32        StdioFILE(MFILE *f, const char* p) :VirtFILE(p) { file = f; }
[425]33#ifndef NO_STD_IN_OUT_ERR
[281]34        static void setStdio();
[425]35#endif
[281]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!*/ }
[206]44
[281]45        ~StdioFILE() { if (file) mfclose(file); }
[206]46};
47#else
[281]48class StdioFILE : public VirtFILE
[206]49{
[281]50protected:
51        FILE *file;
52public:
[820]53        StdioFILE(FILE *f) :VirtFILE("") { file = f; }
54        StdioFILE(FILE *f, const char* p) :VirtFILE(p) { file = f; }
[425]55#ifndef NO_STD_IN_OUT_ERR
[281]56        static void setStdio();
[425]57#endif
[281]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); }
[109]70
[281]71        ~StdioFILE() { if (file) fclose(file); }
[109]72};
[206]73#endif
[109]74
[281]75
76#ifdef _WIN32
77#ifdef __BORLANDC__
[820]78typedef wDIR DIRTYPE;
[281]79#else
[820]80typedef WDIR DIRTYPE;
[281]81#endif
82#else
[820]83typedef DIR DIRTYPE;
[281]84#endif
85
86class StdioDIR : public VirtDIR
[109]87{
[281]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();
[109]103};
104
[281]105class StdioFILEDontClose : public StdioFILE
[109]106{
[281]107public:
[206]108#ifdef USE_MFILE
[281]109        StdioFILEDontClose(MFILE *f) : StdioFILE(f) {}
[206]110#else
[281]111        StdioFILEDontClose(FILE *f) : StdioFILE(f) {}
[206]112#endif
[281]113        ~StdioFILEDontClose() { file = 0; }
[109]114};
115
[820]116class StdioFileSystem_autoselect : public StdioFileSystem
[299]117{
118public:
[820]119        StdioFileSystem_autoselect()
[299]120        {
[820]121                VirtFILE::selectFileSystem(this);
[299]122        }
123};
124
[109]125#endif
Note: See TracBrowser for help on using the repository browser.