SgObject Sg_MakeCustomFile(void *data, SgFileTable *vtbl) { SgFile *z = SG_NEW(SgFile); SG_SET_CLASS(z, SG_CLASS_FILE); z->osdependance = data; SG_FILE_VTABLE(z) = vtbl; return SG_OBJ(z); }
SgObject Sg_MakeFileFromFD(uintptr_t handle) { SgFile *f = SG_NEW(SgFile); init_file(f, (int)handle); f->name = UC("fd"); SG_FILE_VTABLE(f) = &vtable; return SG_OBJ(f); }
SgObject Sg_OpenFile(SgString *file, int flags) { SgObject z = Sg_MakeFile(); if (!SG_FILE_VTABLE(z)->open(z, file, flags)){ SgObject err = Sg_FileErrorMessage(z); return err; } return z; }
static int posix_close(SgObject self) { if (SG_FD(self)->fd == 0 || SG_FD(self)->fd == 1 || SG_FD(self)->fd == 2) { /* we never close standard fd */ return TRUE; } if (SG_FILE_VTABLE(self)->isOpen(self)) { const int isOK = close(SG_FD(self)->fd) != 0; setLastError(self); SG_FD(self)->fd = INVALID_HANDLE_VALUE; return isOK; } return FALSE; }
static int posix_open(SgObject self, SgString *path, int flags) { int mode = 0; SG_FILE(self)->name = path->value; if ((flags & SG_READ) && (flags & SG_WRITE)) { mode |= O_RDWR; } else { if (flags & SG_WRITE) { mode |= O_WRONLY; } if (flags & SG_READ) { mode |= O_RDONLY; } } if (flags & SG_CREATE) { mode |= O_CREAT; } if (flags & SG_TRUNCATE) { mode |= O_TRUNC; } SG_FD(self)->fd = open(Sg_Utf32sToUtf8s(path), mode, 0644); setLastError(self); return SG_FILE_VTABLE(self)->isOpen(self); }
SgObject Sg_InitFile(SgFile *file) { init_file(file, INVALID_HANDLE_VALUE); SG_FILE_VTABLE(file) = &vtable; return SG_OBJ(file); }
int64_t Sg_FileSeek(SgObject file, int64_t off, Whence whence) { return SG_FILE_VTABLE(file)->seek(file, off, whence); }
int Sg_CloseFile(SgObject file) { return SG_FILE_VTABLE(file)->close(file); }