diff options
author | Ash Wolf <ninji@wuffs.org> | 2022-10-12 18:07:57 +0100 |
---|---|---|
committer | Ash Wolf <ninji@wuffs.org> | 2022-10-12 18:07:57 +0100 |
commit | 945f73751630db0420522b5d6af0629d90376a45 (patch) | |
tree | 4b374c13d61dd0f54abefa49c6c348d6a56c6a52 /command_line/CmdLine/Src/OSLib/FileHandles.c | |
parent | f080c0e765878ae91949d91a0bc10b87e6c8269b (diff) | |
download | MWCC-945f73751630db0420522b5d6af0629d90376a45.tar.gz MWCC-945f73751630db0420522b5d6af0629d90376a45.zip |
almost finished OSLib
Diffstat (limited to 'command_line/CmdLine/Src/OSLib/FileHandles.c')
-rw-r--r-- | command_line/CmdLine/Src/OSLib/FileHandles.c | 101 |
1 files changed, 100 insertions, 1 deletions
diff --git a/command_line/CmdLine/Src/OSLib/FileHandles.c b/command_line/CmdLine/Src/OSLib/FileHandles.c index 4609d77..69b4b11 100644 --- a/command_line/CmdLine/Src/OSLib/FileHandles.c +++ b/command_line/CmdLine/Src/OSLib/FileHandles.c @@ -1,29 +1,128 @@ #include "oslib.h" +#include <errno.h> static int OS_LoadFileHandle(OSFileHandle *hand) { + int err; + int ref; + UInt32 sz; + void *buffer; + hand->loaded = 0; + + err = OS_Open(&hand->spec, OSReadOnly, &ref); + if (!err) { + err = OS_GetSize(ref, &sz); + if (!err) { + err = OS_ResizeHandle(&hand->hand, sz); + if (!err) { + buffer = OS_LockHandle(&hand->hand); + err = OS_Read(ref, buffer, &sz); + if (!err) { + hand->loaded = 1; + hand->changed = 0; + } + OS_UnlockHandle(&hand->hand); + } + } + OS_Close(ref); + } + + return err; } static int OS_WriteFileHandle(OSFileHandle *hand) { + int err; + int ref; + UInt32 sz; + void *buffer; + + if (!hand->loaded && !hand->changed) + return 0; + OS_Delete(&hand->spec); + err = OS_Create(&hand->spec, &OS_TEXTTYPE); + if (!err) { + err = OS_Open(&hand->spec, OSReadWrite, &ref); + if (!err) { + err = OS_GetHandleSize(&hand->hand, &sz); + if (!err) { + buffer = OS_LockHandle(&hand->hand); + err = OS_Write(ref, buffer, &sz); + if (!err) + hand->changed = 0; + OS_UnlockHandle(&hand->hand); + OS_Close(ref); + } + } + } + + return err; } int OS_NewFileHandle(const OSSpec *spec, OSHandle *src, Boolean writeable, OSFileHandle *hand) { + int err; + + if (!writeable && src) + return EACCES; + + hand->spec = *spec; + hand->writeable = writeable; + if (!src) { + err = OS_NewHandle(0, &hand->hand); + if (err) + return err; + err = OS_LoadFileHandle(hand); + } else { + err = OS_CopyHandle(src, &hand->hand); + if (err) + return err; + hand->changed = 1; + hand->loaded = 1; + } + + return err; } int OS_LockFileHandle(OSFileHandle *hand, Ptr *ptr, UInt32 *size) { + *size = 0; + + if (!OS_ValidHandle(&hand->hand)) + return ENOMEM; + *ptr = OS_LockHandle(&hand->hand); + OS_GetHandleSize(&hand->hand, size); + return 0; } int OS_UnlockFileHandle(OSFileHandle *hand) { + if (!OS_ValidHandle(&hand->hand)) + return ENOMEM; + OS_UnlockHandle(&hand->hand); + return 0; } int OS_FreeFileHandle(OSFileHandle *hand) { + int err; + + if (hand->writeable && hand->changed) { + err = OS_WriteFileHandle(hand); + if (err) + return err; + } + if (!OS_ValidHandle(&hand->hand)) + return ENOMEM; + + err = OS_FreeHandle(&hand->hand); + if (err) + return err; + + hand->loaded = 0; + return 0; } void OS_GetFileHandleSpec(const OSFileHandle *hand, OSSpec *spec) { - + *spec = hand->spec; } |