[121] | 1 | // This file is a part of the Framsticks GDK. |
---|
| 2 | // Copyright (C) 2002-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> |
---|
| 10 | #include <stdio.h> |
---|
| 11 | #include <common/nonstd_dir.h> |
---|
| 12 | |
---|
| 13 | class StdioFileSystem: public VirtFileSystem |
---|
| 14 | { |
---|
| 15 | public: |
---|
| 16 | VirtFILE *Vfopen(const char* path,const char*mode); |
---|
| 17 | int Vfexists(const char* path); |
---|
| 18 | VirtDIR *Vopendir(const char* path); |
---|
| 19 | }; |
---|
| 20 | |
---|
| 21 | class StdioFILE: public VirtFILE |
---|
| 22 | { |
---|
| 23 | protected: |
---|
| 24 | FILE *file; |
---|
| 25 | SString path; |
---|
| 26 | public: |
---|
| 27 | StdioFILE(FILE *f) {file=f;} |
---|
| 28 | StdioFILE(FILE *f,const SString& p) {file=f;path=p;} |
---|
| 29 | static void setStdio(); |
---|
| 30 | int Vread(void *ptr, size_t size, size_t nmemb) {return fread(ptr,size,nmemb,file);} |
---|
| 31 | int Vwrite(const void *ptr, size_t size, size_t nmemb) {return fwrite(ptr,size,nmemb,file);} |
---|
| 32 | int Veof() {return feof(file);} |
---|
| 33 | int Vputc(int c) {return fputc(c,file);} |
---|
| 34 | int Vputs(const char *s) {return fputs(s,file);} |
---|
| 35 | int Vgetc() {return fgetc(file);} |
---|
| 36 | char *Vgets(char *s, int size) {return fgets(s,size,file);} |
---|
| 37 | int Vprintf(const char *format, va_list args) { return vfprintf(file,format,args); } |
---|
| 38 | int Vseek(long offset, int whence) {return fseek(file,offset,whence);} |
---|
| 39 | int Vtell() {return ftell(file);} |
---|
| 40 | void Vrewind() {rewind(file);} |
---|
| 41 | int Vflush() {return fflush(file);} |
---|
| 42 | const char* VgetPath() {return path;} |
---|
| 43 | |
---|
| 44 | ~StdioFILE() {if (file) fclose(file);} |
---|
| 45 | }; |
---|
| 46 | |
---|
| 47 | class StdioDIR: public VirtDIR |
---|
| 48 | { |
---|
| 49 | DIR *dir; |
---|
| 50 | public: |
---|
| 51 | StdioDIR(DIR* d):dir(d) {} |
---|
| 52 | ~StdioDIR() {if (dir) closedir(dir);} |
---|
| 53 | dirent* Vreaddir(); |
---|
| 54 | }; |
---|
| 55 | |
---|
| 56 | class StdioFILEDontClose: public StdioFILE |
---|
| 57 | { |
---|
| 58 | public: |
---|
| 59 | StdioFILEDontClose(FILE *f):StdioFILE(f) {} |
---|
| 60 | ~StdioFILEDontClose() {file=0;} |
---|
| 61 | }; |
---|
| 62 | |
---|
| 63 | #endif |
---|