[121] | 1 | // This file is a part of the Framsticks GDK. |
---|
[197] | 2 | // Copyright (C) 1999-2014 Maciej Komosinski and Szymon Ulatowski. See LICENSE.txt for details. |
---|
[109] | 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> |
---|
[206] | 10 | #ifdef USE_MFILE |
---|
| 11 | #include <common/nonstd_stdio.h> |
---|
| 12 | #else |
---|
[109] | 13 | #include <stdio.h> |
---|
[206] | 14 | #endif |
---|
[109] | 15 | #include <common/nonstd_dir.h> |
---|
| 16 | |
---|
| 17 | class StdioFileSystem: public VirtFileSystem |
---|
| 18 | { |
---|
| 19 | public: |
---|
| 20 | VirtFILE *Vfopen(const char* path,const char*mode); |
---|
| 21 | int Vfexists(const char* path); |
---|
| 22 | VirtDIR *Vopendir(const char* path); |
---|
| 23 | }; |
---|
| 24 | |
---|
[206] | 25 | #ifdef USE_MFILE |
---|
[109] | 26 | class StdioFILE: public VirtFILE |
---|
| 27 | { |
---|
| 28 | protected: |
---|
[206] | 29 | MFILE *file; |
---|
| 30 | SString path; |
---|
| 31 | public: |
---|
| 32 | StdioFILE(MFILE *f) {file=f;} |
---|
| 33 | StdioFILE(MFILE *f,const SString& p) {file=f;path=p;} |
---|
| 34 | static void setStdio(); |
---|
[247] | 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);} |
---|
[206] | 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);} |
---|
[247] | 41 | long Vtell() {return mftell(file);} |
---|
[206] | 42 | int Vflush() {return 0;/*NOT IMPLEMENTED!*/;} |
---|
| 43 | const char* VgetPath() {return path;} |
---|
| 44 | |
---|
[227] | 45 | ~StdioFILE() {if (file) mfclose(file);} |
---|
[206] | 46 | }; |
---|
| 47 | #else |
---|
| 48 | class StdioFILE: public VirtFILE |
---|
| 49 | { |
---|
| 50 | protected: |
---|
[109] | 51 | FILE *file; |
---|
| 52 | SString path; |
---|
| 53 | public: |
---|
| 54 | StdioFILE(FILE *f) {file=f;} |
---|
| 55 | StdioFILE(FILE *f,const SString& p) {file=f;path=p;} |
---|
| 56 | static void setStdio(); |
---|
[247] | 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);} |
---|
[109] | 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);} |
---|
[247] | 66 | long Vtell() {return ftell(file);} |
---|
[109] | 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 | }; |
---|
[206] | 73 | #endif |
---|
[109] | 74 | |
---|
| 75 | class StdioDIR: public VirtDIR |
---|
| 76 | { |
---|
| 77 | DIR *dir; |
---|
| 78 | public: |
---|
| 79 | StdioDIR(DIR* d):dir(d) {} |
---|
| 80 | ~StdioDIR() {if (dir) closedir(dir);} |
---|
| 81 | dirent* Vreaddir(); |
---|
| 82 | }; |
---|
| 83 | |
---|
| 84 | class StdioFILEDontClose: public StdioFILE |
---|
| 85 | { |
---|
| 86 | public: |
---|
[206] | 87 | #ifdef USE_MFILE |
---|
| 88 | StdioFILEDontClose(MFILE *f):StdioFILE(f) {} |
---|
| 89 | #else |
---|
[109] | 90 | StdioFILEDontClose(FILE *f):StdioFILE(f) {} |
---|
[206] | 91 | #endif |
---|
[109] | 92 | ~StdioFILEDontClose() {file=0;} |
---|
| 93 | }; |
---|
| 94 | |
---|
| 95 | #endif |
---|