Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor VFS file operations for improved readability and consistency #60

Merged
merged 1 commit into from
Sep 8, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
126 changes: 63 additions & 63 deletions src/vfile/vfsFile.c
Original file line number Diff line number Diff line change
@@ -1,96 +1,96 @@
#include "oslib.h"

/*
SOURCE VFS: file
SOURCE VFS: file
*/

#define FLAG_EOF 1

int VF_FILE = -1;

#define _file_ ((SceUID)f->ioPtr)

int vfsFileOpen(void *param1, int param2, int type, int mode, VIRTUAL_FILE* f) {
int stdMode = PSP_O_RDONLY;
if (mode == VF_O_WRITE)
stdMode = PSP_O_WRONLY | PSP_O_CREAT | PSP_O_TRUNC;
else if (mode == VF_O_READWRITE)
stdMode = PSP_O_RDWR;

f->ioPtr = (void*)sceIoOpen((char*)param1, stdMode, 0777);
return (s32)f->ioPtr >= 0;
#define _file_ ((SceUID)f->ioPtr)

int vfsFileOpen(void *param1, int param2, int type, int mode, VIRTUAL_FILE* f) {
int stdMode = PSP_O_RDONLY;

switch (mode) {
case VF_O_WRITE:
stdMode = PSP_O_WRONLY | PSP_O_CREAT | PSP_O_TRUNC;
break;
case VF_O_READWRITE:
stdMode = PSP_O_RDWR;
break;
}

f->ioPtr = (void*)sceIoOpen((char*)param1, stdMode, 0777);
return (s32)f->ioPtr >= 0; // Return true if file descriptor is valid
}

int vfsFileClose(VIRTUAL_FILE *f) {
sceIoClose(_file_);
return 1;
int vfsFileClose(VIRTUAL_FILE *f) {
return sceIoClose(_file_) >= 0; // Return true if closing was successful
}

int vfsFileWrite(const void *ptr, size_t size, size_t n, VIRTUAL_FILE* f) {
return sceIoWrite(_file_, ptr, size * n);
int vfsFileWrite(const void *ptr, size_t size, size_t n, VIRTUAL_FILE* f) {
return sceIoWrite(_file_, ptr, size * n);
}

int vfsFileRead(void *ptr, size_t size, size_t n, VIRTUAL_FILE* f) {
int readSize = sceIoRead(_file_, ptr, size * n);
if (readSize < size * n)
f->userData |= FLAG_EOF;
return readSize;
int vfsFileRead(void *ptr, size_t size, size_t n, VIRTUAL_FILE* f) {
int readSize = sceIoRead(_file_, ptr, size * n);

if (readSize < size * n) {
f->userData |= FLAG_EOF; // Set EOF flag if less data was read
}

return readSize;
}

int vfsFileGetc(VIRTUAL_FILE *f) {
/* unsigned char car = -1;
sceIoRead(_file_, &car, sizeof(car));
return (int)car;*/
return vfsMemGetc(f);
int vfsFileGetc(VIRTUAL_FILE *f) {
// Fallback to memory-based getchar
return vfsMemGetc(f);
}

int vfsFilePutc(int caractere, VIRTUAL_FILE *f) {
/* unsigned char car = (unsigned char)caractere;
return sceIoWrite(_file_, &car, sizeof(car));*/
return vfsMemPutc(caractere, f);
int vfsFilePutc(int caractere, VIRTUAL_FILE *f) {
// Fallback to memory-based putchar
return vfsMemPutc(caractere, f);
}

char *vfsFileGets(char *str, int maxLen, VIRTUAL_FILE *f) {
return vfsMemGets(str, maxLen, f);
char *vfsFileGets(char *str, int maxLen, VIRTUAL_FILE *f) {
// Fallback to memory-based fgets
return vfsMemGets(str, maxLen, f);
}

void vfsFilePuts(const char *s, VIRTUAL_FILE *f) {
return vfsMemPuts(s, f);
void vfsFilePuts(const char *s, VIRTUAL_FILE *f) {
// Fallback to memory-based puts
vfsMemPuts(s, f);
}

void vfsFileSeek(VIRTUAL_FILE *f, int offset, int whence) {
// int oldOffset = sceIoLseek32(_file_, 0, SEEK_CUR);
// if (!(offset == 0 && whence == SEEK_CUR))
// sceIoLseek32(_file_, offset, whence);
sceIoLseek32(_file_, offset, whence);
f->userData &= ~FLAG_EOF;
void vfsFileSeek(VIRTUAL_FILE *f, int offset, int whence) {
sceIoLseek32(_file_, offset, whence);
f->userData &= ~FLAG_EOF; // Reset EOF flag after seek
}

int vfsFileTell(VIRTUAL_FILE *f) {
return sceIoLseek32(_file_, 0, SEEK_CUR);
int vfsFileTell(VIRTUAL_FILE *f) {
return sceIoLseek32(_file_, 0, SEEK_CUR); // Return current file position
}

int vfsFileEof(VIRTUAL_FILE *f) {
return f->userData & FLAG_EOF;
int vfsFileEof(VIRTUAL_FILE *f) {
return f->userData & FLAG_EOF; // Check EOF flag
}

VIRTUAL_FILE_SOURCE vfsFile = {
vfsFileOpen,
vfsFileClose,
vfsFileRead,
vfsFileWrite,
vfsFileGetc,
vfsFilePutc,
vfsFileGets,
vfsFilePuts,
vfsFileSeek,
vfsFileTell,
vfsFileEof,
VIRTUAL_FILE_SOURCE vfsFile = {
vfsFileOpen,
vfsFileClose,
vfsFileRead,
vfsFileWrite,
vfsFileGetc,
vfsFilePutc,
vfsFileGets,
vfsFilePuts,
vfsFileSeek,
vfsFileTell,
vfsFileEof
};

int oslInitVfsFile() {
VF_FILE = VirtualFileRegisterSource(&vfsFile);
return VF_FILE;
int oslInitVfsFile() {
VF_FILE = VirtualFileRegisterSource(&vfsFile);
return VF_FILE;
}