13 #define GFILE_NEED_STDIO_MUST_BE_OFF
15 #include "../../gfx.h"
17 #if GFX_USE_GFILE && GFILE_NEED_NATIVEFS
22 #include <sys/types.h>
25 static gBool NativeDel(
const char *fname);
26 static gBool NativeExists(
const char *fname);
27 static gFileSize NativeFilesize(
const char *fname);
28 static gBool NativeRen(
const char *oldname,
const char *newname);
29 static gBool NativeOpen(
GFILE *f,
const char *fname);
30 static void NativeClose(
GFILE *f);
31 static int NativeRead(
GFILE *f,
void *buf,
int size);
32 static int NativeWrite(
GFILE *f,
const void *buf,
int size);
33 static gBool NativeSetpos(
GFILE *f, gFileSize pos);
34 static gFileSize NativeGetsize(
GFILE *f);
35 static gBool NativeEof(
GFILE *f);
36 #if GFILE_NEED_FILELISTS
37 static gfileList *NativeFlOpen(
const char *path, gBool dirs);
38 static const char *NativeFlRead(gfileList *pfl);
39 static void NativeFlClose(gfileList *pfl);
42 const GFILEVMT FsNativeVMT = {
43 #if defined(WIN32) || GFX_USE_OS_WIN32
48 GFSFLG_WRITEABLE|GFSFLG_SEEKABLE|GFSFLG_FAST,
50 NativeDel, NativeExists, NativeFilesize, NativeRen,
51 NativeOpen, NativeClose, NativeRead, NativeWrite,
52 NativeSetpos, NativeGetsize, NativeEof,
54 #if GFILE_NEED_FILELISTS
55 NativeFlOpen, NativeFlRead, NativeFlClose
59 void _gfileNativeAssignStdio(
void) {
60 static GFILE NativeStdIn;
61 static GFILE NativeStdOut;
62 static GFILE NativeStdErr;
64 NativeStdIn.flags = GFILEFLG_OPEN|GFILEFLG_READ;
65 NativeStdIn.vmt = &FsNativeVMT;
66 NativeStdIn.obj = (
void *)stdin;
68 gfileStdIn = &NativeStdIn;
69 NativeStdOut.flags = GFILEFLG_OPEN|GFILEFLG_WRITE|GFILEFLG_APPEND;
70 NativeStdOut.vmt = &FsNativeVMT;
71 NativeStdOut.obj = (
void *)stdout;
73 gfileStdOut = &NativeStdOut;
74 NativeStdErr.flags = GFILEFLG_OPEN|GFILEFLG_WRITE|GFILEFLG_APPEND;
75 NativeStdErr.vmt = &FsNativeVMT;
76 NativeStdErr.obj = (
void *)stderr;
78 gfileStdErr = &NativeStdErr;
81 static void Native_flags2mode(
char *buf, gU16 flags) {
82 if (flags & GFILEFLG_MUSTEXIST)
84 else if (flags & GFILEFLG_APPEND)
89 if ((flags & (GFILEFLG_READ|GFILEFLG_WRITE)) == (GFILEFLG_READ|GFILEFLG_WRITE))
91 if (flags & GFILEFLG_BINARY)
93 if (flags & GFILEFLG_MUSTNOTEXIST)
98 static gBool NativeDel(
const char *fname) {
return remove(fname) ? gFalse : gTrue; }
99 static void NativeClose(
GFILE *f) { fclose((FILE *)f->obj); }
100 static int NativeRead(
GFILE *f,
void *buf,
int size) {
return fread(buf, 1, size, (FILE *)f->obj); }
101 static int NativeWrite(
GFILE *f,
const void *buf,
int size) {
return fwrite(buf, 1, size, (FILE *)f->obj); }
102 static gBool NativeSetpos(
GFILE *f, gFileSize pos) {
return fseek((FILE *)f->obj, pos, SEEK_SET) ? gFalse : gTrue; }
103 static gBool NativeEof(
GFILE *f) {
return feof((FILE *)f->obj) ? gTrue : gFalse; }
104 static gBool NativeRen(
const char *oldname,
const char *newname) {
return rename(oldname, newname) ? gFalse : gTrue; }
105 static gBool NativeExists(
const char *fname) {
108 extern int access(
const char *pathname,
int mode);
109 return access(fname, 0) ? gFalse : gTrue;
111 static gFileSize NativeFilesize(
const char *fname) {
113 if (stat(fname, &st))
return (gFileSize)-1;
114 return (gFileSize)st.st_size;
116 static gBool NativeOpen(
GFILE *f,
const char *fname) {
120 Native_flags2mode(mode, f->flags);
121 if (!(fd = fopen(fname, mode)))
126 static gFileSize NativeGetsize(
GFILE *f) {
128 if (fstat(fileno((FILE *)f->obj), &st))
return (gFileSize)-1;
129 return (gFileSize)st.st_size;
132 #if GFILE_NEED_FILELISTS
133 #if defined(WIN32) || GFX_USE_OS_WIN32
134 typedef struct NativeFileList {
141 static gfileList *NativeFlOpen(
const char *path, gBool dirs) {
145 if (!(p =
gfxAlloc(
sizeof(NativeFileList))))
147 if ((p->d = FindFirstFile(path, &p->f)) == INVALID_HANDLE_VALUE) {
155 static const char *NativeFlRead(gfileList *pfl) {
156 #define nfl ((NativeFileList *)pfl)
158 if (!nfl->first && !FindNextFile(nfl->d, &nfl->f))
161 if (nfl->f.cFileName[0] ==
'.')
164 if ((nfl->f.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
167 if (!(nfl->f.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
171 return nfl->f.cFileName;
175 static void NativeFlClose(gfileList *pfl) {
176 CloseHandle(((NativeFileList *)pfl)->d);
183 typedef struct NativeFileList {
189 static gfileList *NativeFlOpen(
const char *path, gBool dirs) {
193 if (!(p =
gfxAlloc(
sizeof(NativeFileList))))
195 if (!(p->d = opendir(path))) {
202 static const char *NativeFlRead(gfileList *pfl) {
203 #define nfl ((NativeFileList *)pfl)
205 if (!(nfl->f = readdir(nfl->d)))
207 if (nfl->f->d_name[0] ==
'.')
210 #ifdef _DIRENT_HAVE_D_TYPE
212 if (nfl->f->d_type == DT_DIR)
215 if (nfl->f->d_type == DT_REG)
226 return nfl->f->d_name;
230 static void NativeFlClose(gfileList *pfl) {
231 closedir(((NativeFileList *)pfl)->d);
GFILE file system header.
struct GFILE GFILE
A file pointer.
void * gfxAlloc(gMemSize sz)
Allocate memory.
void gfxFree(void *ptr)
Free memory.