1 | // This file is a part of Framsticks GDK library. |
---|
2 | // Copyright (C) 2002-2006 Szymon Ulatowski. See LICENSE.txt for details. |
---|
3 | // Refer to http://www.frams.alife.pl/ for further information. |
---|
4 | |
---|
5 | #ifndef _STDIOFILE_H_ |
---|
6 | #define _STDIOFILE_H_ |
---|
7 | |
---|
8 | #include "virtfile.h" |
---|
9 | #include "sstring.h" |
---|
10 | #include <stdio.h> |
---|
11 | #ifndef _MSC_VER |
---|
12 | #include <dirent.h> |
---|
13 | #endif |
---|
14 | |
---|
15 | class StdioFileSystem: public VirtFileSystem |
---|
16 | { |
---|
17 | public: |
---|
18 | VirtFILE *Vfopen(const char* path,const char*mode); |
---|
19 | int Vfexists(const char* path); |
---|
20 | VirtDIR *Vopendir(const char* path); |
---|
21 | }; |
---|
22 | |
---|
23 | class StdioFILE: public VirtFILE |
---|
24 | { |
---|
25 | protected: |
---|
26 | FILE *file; |
---|
27 | SString path; |
---|
28 | public: |
---|
29 | StdioFILE(FILE *f) {file=f;} |
---|
30 | StdioFILE(FILE *f,const SString& p) {file=f;path=p;} |
---|
31 | static void setStdio(); |
---|
32 | int Vread(void *ptr, size_t size, size_t nmemb) {return fread(ptr,size,nmemb,file);} |
---|
33 | int Vwrite(const void *ptr, size_t size, size_t nmemb) {return fwrite(ptr,size,nmemb,file);} |
---|
34 | int Veof() {return feof(file);} |
---|
35 | int Vputc(int c) {return fputc(c,file);} |
---|
36 | int Vputs(const char *s) {return fputs(s,file);} |
---|
37 | int Vgetc() {return fgetc(file);} |
---|
38 | char *Vgets(char *s, int size) {return fgets(s,size,file);} |
---|
39 | int Vprintf(const char *format, va_list args) { return vfprintf(file,format,args); } |
---|
40 | int Vseek(long offset, int whence) {return fseek(file,offset,whence);} |
---|
41 | int Vtell() {return ftell(file);} |
---|
42 | void Vrewind() {rewind(file);} |
---|
43 | int Vflush() {return fflush(file);} |
---|
44 | const char* VgetPath() {return path;} |
---|
45 | |
---|
46 | ~StdioFILE() {if (file) fclose(file);} |
---|
47 | }; |
---|
48 | |
---|
49 | class StdioDIR: public VirtDIR |
---|
50 | { |
---|
51 | #ifndef _MSC_VER |
---|
52 | DIR *dir; |
---|
53 | public: |
---|
54 | StdioDIR(DIR* d):dir(d) {} |
---|
55 | ~StdioDIR() {if (dir) closedir(dir);} |
---|
56 | dirent* Vreaddir(); |
---|
57 | #endif |
---|
58 | }; |
---|
59 | |
---|
60 | class StdioFILEDontClose: public StdioFILE |
---|
61 | { |
---|
62 | public: |
---|
63 | StdioFILEDontClose(FILE *f):StdioFILE(f) {} |
---|
64 | ~StdioFILEDontClose() {file=0;} |
---|
65 | }; |
---|
66 | |
---|
67 | #endif |
---|