Move VFile to vfs.h and add VDirs

This commit is contained in:
Jeffrey Pfau 2014-07-16 23:55:09 -07:00
parent 80c61379a0
commit 73425e80b5
8 changed files with 106 additions and 9 deletions

View File

@ -3,7 +3,7 @@
#include "gba.h"
#include "util/memory.h"
#include "util/vfile.h"
#include "util/vfs.h"
#include <errno.h>
#include <fcntl.h>

View File

@ -5,7 +5,7 @@
#include "gba-thread.h"
#include "util/memory.h"
#include "util/vfile.h"
#include "util/vfs.h"
#include <fcntl.h>

View File

@ -7,7 +7,7 @@
#include "debugger/debugger.h"
#include "util/patch.h"
#include "util/vfile.h"
#include "util/vfs.h"
#include <signal.h>

View File

@ -7,7 +7,7 @@
#include "util/memory.h"
#include "util/patch.h"
#include "util/vfile.h"
#include "util/vfs.h"
const uint32_t GBA_ARM7TDMI_FREQUENCY = 0x1000000;
const uint32_t GBA_COMPONENT_MAGIC = 0x1000000;

View File

@ -1,7 +1,7 @@
#include "util/patch-ips.h"
#include "util/patch.h"
#include "util/vfile.h"
#include "util/vfs.h"
static size_t _IPSOutputSize(struct Patch* patch, size_t inSize);
static bool _IPSApplyPatch(struct Patch* patch, void* out, size_t outSize);

View File

@ -2,7 +2,7 @@
#include "util/crc32.h"
#include "util/patch.h"
#include "util/vfile.h"
#include "util/vfs.h"
enum {
IN_CHECKSUM = -12,

View File

@ -1,12 +1,15 @@
#include "util/vfile.h"
#include "util/vfs.h"
#include <fcntl.h>
#include <dirent.h>
#ifndef _WIN32
#include <sys/mman.h>
#define PATH_SEP '/'
#else
#include <io.h>
#include <Windows.h>
#define PATH_SEP '\\'
#endif
struct VFileFD {
@ -26,6 +29,12 @@ static void* _vfdMap(struct VFile* vf, size_t size, int flags);
static void _vfdUnmap(struct VFile* vf, void* memory, size_t size);
static void _vfdTruncate(struct VFile* vf, size_t size);
static bool _vdClose(struct VDir* vd);
static struct VDirEntry* _vdListNext(struct VDir* vd);
static struct VFile* _vdOpenFile(struct VDir* vd, const char* path, int mode);
static const char* _vdeName(struct VDirEntry* vde);
struct VFile* VFileOpen(const char* path, int flags) {
int fd = open(path, flags, 0666);
return VFileFromFD(fd);
@ -137,3 +146,79 @@ static void _vfdTruncate(struct VFile* vf, size_t size) {
struct VFileFD* vfd = (struct VFileFD*) vf;
ftruncate(vfd->fd, size);
}
struct VDirEntryDE {
struct VDirEntry d;
struct dirent* ent;
};
struct VDirDE {
struct VDir d;
DIR* de;
struct VDirEntryDE vde;
char* path;
};
struct VDir* VDirOpen(const char* path) {
DIR* de = opendir(path);
if (!de) {
return 0;
}
struct VDirDE* vd = malloc(sizeof(struct VDirDE));
if (!vd) {
return 0;
}
vd->d.close = _vdClose;
vd->d.listNext = _vdListNext;
vd->d.openFile = _vdOpenFile;
vd->path = strdup(path);
vd->de = de;
vd->vde.d.name = _vdeName;
return &vd->d;
}
bool _vdClose(struct VDir* vd) {
struct VDirDE* vdde = (struct VDirDE*) vd;
if (closedir(vdde->de) < 0) {
return false;
}
free(vdde->path);
free(vdde);
return true;
}
struct VDirEntry* _vdListNext(struct VDir* vd) {
struct VDirDE* vdde = (struct VDirDE*) vd;
vdde->vde.ent = readdir(vdde->de);
if (vdde->vde.ent) {
return &vdde->vde.d;
}
return 0;
}
struct VFile* _vdOpenFile(struct VDir* vd, const char* path, int mode) {
struct VDirDE* vdde = (struct VDirDE*) vd;
if (!path) {
return 0;
}
const char* dir = vdde->path;
char* combined = malloc(sizeof(char) * (strlen(path) + strlen(dir) + 2));
sprintf(combined, "%s%c%s", dir, PATH_SEP, path);
struct VFile* file = VFileOpen(combined, mode);
free(combined);
return file;
}
const char* _vdeName(struct VDirEntry* vde) {
struct VDirEntryDE* vdede = (struct VDirEntryDE*) vde;
if (vdede->ent) {
return vdede->ent->d_name;
}
return 0;
}

View File

@ -1,5 +1,5 @@
#ifndef VFILE_H
#define VFILE_H
#ifndef VFS_H
#define VFS_H
#include "common.h"
@ -16,7 +16,19 @@ struct VFile {
void (*truncate)(struct VFile* vf, size_t size);
};
struct VDirEntry {
const char* (*name)(struct VDirEntry* vde);
};
struct VDir {
bool (*close)(struct VDir* vd);
struct VDirEntry* (*listNext)(struct VDir* vd);
struct VFile* (*openFile)(struct VDir* vd, const char* name, int mode);
};
struct VFile* VFileOpen(const char* path, int flags);
struct VFile* VFileFromFD(int fd);
struct VDir* VDirOpen(const char* path);
#endif