From a4d2de2120a358f0255e3a794ca73a81d27d0ec2 Mon Sep 17 00:00:00 2001 From: Dietrich Epp Date: Tue, 22 Mar 2022 19:04:29 -0400 Subject: [PATCH] Create replacements for Mac OS toolbox functions --- src/BUILD.bazel | 12 ++++++++ src/defs.h | 43 +++++++++++++++++++++++++++ src/toolbox.c | 79 +++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 134 insertions(+) create mode 100644 src/defs.h create mode 100644 src/toolbox.c diff --git a/src/BUILD.bazel b/src/BUILD.bazel index 4261ca4..ead3393 100644 --- a/src/BUILD.bazel +++ b/src/BUILD.bazel @@ -1,3 +1,6 @@ +load("@rules_cc//cc:defs.bzl", "cc_library") +load("//bazel:copts.bzl", "COPTS") + genrule( name = "data", srcs = [ @@ -24,3 +27,12 @@ genrule( "//gen:macscript", ], ) + +cc_library( + name = "convert", + srcs = [ + "defs.h", + "toolbox.c", + ], + copts = COPTS, +) diff --git a/src/defs.h b/src/defs.h new file mode 100644 index 0000000..814d473 --- /dev/null +++ b/src/defs.h @@ -0,0 +1,43 @@ +#ifndef defs_h +#define defs_h +/* defs.h - common definitions. */ + +#if macintosh + +#include + +#else + +/* Definitions in */ + +/* Include for NULL */ +#include +#include + +typedef uint8_t UInt8; +typedef int8_t SInt8; +typedef uint16_t UInt16; +typedef int16_t SInt16; +typedef uint32_t UInt32; +typedef int32_t SInt32; + +typedef char *Ptr; +typedef Ptr *Handle; +typedef long Size; + +typedef SInt16 OSErr; + +/* Definitions in */ + +Handle NewHandle(Size byteCount); +void HLock(Handle h); +void HUnlock(Handle h); +void DisposeHandle(Handle h); +void SetHandleSize(Handle h, Size newSize); +OSErr MemError(void); + +#endif + +void MemClear(void *ptr, Size size); + +#endif diff --git a/src/toolbox.c b/src/toolbox.c new file mode 100644 index 0000000..4a505bb --- /dev/null +++ b/src/toolbox.c @@ -0,0 +1,79 @@ +/* toolbox.c - replacement functions for Mac OS toolbox functions + + This is used to run conversion tests on non-Mac OS systems to make + development easier. These are not intended to make it possible to port the + converter to non-Mac OS systems. */ +#include "defs.h" + +#include +#include +#include +#include + +static void Dief(const char *msg, ...) + __attribute__((noreturn, format(printf, 1, 2))); + +static void Dief(const char *msg, ...) { + va_list ap; + fputs("Error: ", stderr); + va_start(ap, msg); + vfprintf(stderr, msg, ap); + va_end(ap); + fputc('\n', stderr); + exit(1); +} + +Handle NewHandle(Size byteCount) { + Ptr p; + Handle h; + + if (byteCount < 0) { + Dief("NewHandle: byteCount = %ld", byteCount); + } + p = malloc(byteCount); + if (byteCount > 0 && p == NULL) { + Dief("NewHandle: malloc failed"); + } + h = malloc(sizeof(Ptr)); + if (h == NULL) { + Dief("NewHandle: malloc failed"); + } + *h = p; + return h; +} + +void HLock(Handle h) { + (void)h; +} + +void HUnlock(Handle h) { + (void)h; +} + +void DisposeHandle(Handle h) { + if (h != NULL) { + free(*h); + free(h); + } +} + +void SetHandleSize(Handle h, Size newSize) { + Ptr p; + if (h == NULL) { + Dief("SetHandleSize: h = NULL"); + } + p = realloc(*h, newSize); + if (newSize > 0 && p == NULL) { + Dief("SetHandleSize: realloc failed"); + } + *h = p; +} + +OSErr MemError(void) { + /* Memory allocation failures abort the program. */ + return 0; +} + +void MemClear(void *ptr, Size size) { + memset(ptr, 0, size); +}