MWCC/command_line/CmdLine/Src/OSLib/FileHandles.c

129 lines
2.8 KiB
C
Raw Normal View History

#include "oslib.h"
2022-10-12 17:07:57 +00:00
#include <errno.h>
static int OS_LoadFileHandle(OSFileHandle *hand) {
2022-10-12 17:07:57 +00:00
int err;
int ref;
UInt32 sz;
void *buffer;
2022-10-12 17:07:57 +00:00
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) {
2022-10-12 17:07:57 +00:00
int err;
int ref;
UInt32 sz;
void *buffer;
if (!hand->loaded && !hand->changed)
return 0;
2022-10-12 17:07:57 +00:00
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) {
2022-10-12 17:07:57 +00:00
int err;
if (!writeable && src)
return EACCES;
hand->spec = *spec;
hand->writeable = writeable;
2022-10-12 17:07:57 +00:00
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) {
2022-10-12 17:07:57 +00:00
*size = 0;
if (!OS_ValidHandle(&hand->hand))
return ENOMEM;
2022-10-12 17:07:57 +00:00
*ptr = OS_LockHandle(&hand->hand);
OS_GetHandleSize(&hand->hand, size);
return 0;
}
int OS_UnlockFileHandle(OSFileHandle *hand) {
2022-10-12 17:07:57 +00:00
if (!OS_ValidHandle(&hand->hand))
return ENOMEM;
2022-10-12 17:07:57 +00:00
OS_UnlockHandle(&hand->hand);
return 0;
}
int OS_FreeFileHandle(OSFileHandle *hand) {
2022-10-12 17:07:57 +00:00
int err;
if (hand->writeable && hand->changed) {
err = OS_WriteFileHandle(hand);
if (err)
return err;
}
2022-10-12 17:07:57 +00:00
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) {
2022-10-12 17:07:57 +00:00
*spec = hand->spec;
}