Create replacements for Mac OS toolbox functions

This commit is contained in:
Dietrich Epp 2022-03-22 19:04:29 -04:00
parent f2317d0ce7
commit a4d2de2120
3 changed files with 134 additions and 0 deletions

View File

@ -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,
)

43
src/defs.h Normal file
View File

@ -0,0 +1,43 @@
#ifndef defs_h
#define defs_h
/* defs.h - common definitions. */
#if macintosh
#include <MacTypes.h>
#else
/* Definitions in <MacTypes.h> */
/* Include <stddef.h> for NULL */
#include <stddef.h>
#include <stdint.h>
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 <MacMemory.h> */
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

79
src/toolbox.c Normal file
View File

@ -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 <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
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);
}