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

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

Support for wide char (unicode) names of files and directories under Windows, internally encoded as char* utf-8

  • Property svn:eol-style set to native
File size: 3.1 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:
20        VirtFILE *Vfopen(const char *path, const char *mode);
21        int Vfexists(const char* path);
22        VirtDIR *Vopendir(const char* path);
23};
24
25#ifdef USE_MFILE
26class StdioFILE : public VirtFILE
27{
28protected:
29        MFILE *file;
30        SString path;
31public:
32        StdioFILE(MFILE *f) { file = f; }
33        StdioFILE(MFILE *f, const SString& p) { file = f; path = p; }
34        static void setStdio();
35        size_t Vread(void *ptr, size_t size, size_t nmemb) { return mfread(ptr, size, nmemb, file); }
36        size_t Vwrite(const void *ptr, size_t size, size_t nmemb) { return mfwrite(ptr, size, nmemb, file); }
37        int Veof() { return mfeof(file); }
38        int Vputs(const char *s) { return mfputs(s, file); }
39        char *Vgets(char *s, int size) { return mfgets(s, size, file); }
40        int Vseek(long offset, int whence) { return mfseek(file, offset, whence); }
41        long Vtell() { return mftell(file); }
42        int Vflush() { return 0; /*NOT IMPLEMENTED!*/ }
43        const char* VgetPath() { return path; }
44
45        ~StdioFILE() { if (file) mfclose(file); }
46};
47#else
48class StdioFILE : public VirtFILE
49{
50protected:
51        FILE *file;
52        SString path;
53public:
54        StdioFILE(FILE *f) { file = f; }
55        StdioFILE(FILE *f, const SString& p) { file = f; path = p; }
56        static void setStdio();
57        size_t Vread(void *ptr, size_t size, size_t nmemb) { return fread(ptr, size, nmemb, file); }
58        size_t Vwrite(const void *ptr, size_t size, size_t nmemb) { return fwrite(ptr, size, nmemb, file); }
59        int Veof() { return feof(file); }
60        int Vputc(int c) { return fputc(c, file); }
61        int Vputs(const char *s) { return fputs(s, file); }
62        int Vgetc() { return fgetc(file); }
63        char *Vgets(char *s, int size) { return fgets(s, size, file); }
64        int Vprintf(const char *format, va_list args) { return vfprintf(file, format, args); }
65        int Vseek(long offset, int whence) { return fseek(file, offset, whence); }
66        long Vtell() { return ftell(file); }
67        void Vrewind() { rewind(file); }
68        int Vflush() { return fflush(file); }
69        const char* VgetPath() { return path; }
70
71        ~StdioFILE() { if (file) fclose(file); }
72};
73#endif
74
75
76#ifdef _WIN32
77#ifdef __BORLANDC__
78 typedef wDIR DIRTYPE;
79#else
80 typedef WDIR DIRTYPE;
81#endif
82#else
83 typedef 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
116#endif
117
Note: See TracBrowser for help on using the repository browser.