MWCC/unsorted/uLibImporter.c

104 lines
2.6 KiB
C

#include "cw_common.h"
#include "cos.h"
static CWPluginContext compiler_context;
static void pstrcpy(StringPtr dst, ConstStringPtr src) {
short len = src[0];
do {
*(dst++) = *(src++);
} while (len-- > 0);
}
static void ReportError(int index, int osErr) {
char buf[256];
COS_GetString(buf, 911, index);
if (osErr)
CWOSErrorMessage(compiler_context, buf, osErr);
else
CWReportMessage(compiler_context, NULL, buf, NULL, messagetypeError, 0);
}
static CWMemHandle ImportObjectLibrary(CWFileSpec *spec, SInt32 *codesize, SInt32 *udatasize, SInt32 *idatasize) {
int err;
short ref;
CWMemHandle mh;
UInt32 *ptr;
SInt32 fsize;
err = COS_FileOpen(spec, &ref);
if (err) {
ReportError(1, err);
return NULL;
}
err = COS_FileGetSize(ref, &fsize);
if (err) {
COS_FileClose(ref);
ReportError(1, err);
return NULL;
}
err = CWAllocMemHandle(compiler_context, 8, 1, &mh);
if (err) {
COS_FileClose(ref);
ReportError(1, err);
return NULL;
}
err = CWLockMemHandle(compiler_context, mh, 0, (void **) &ptr);
if (err) {
COS_FileClose(ref);
CWFreeMemHandle(compiler_context, mh);
ReportError(1, err);
return NULL;
}
err = COS_FileRead(ref, ptr, 8);
if (err != (short) err) {
ReportError(1, err);
return NULL;
}
COS_FileClose(ref);
if (ptr[0] != 0xFEEDFACE && ptr[0] != 0xCAFEBABE && ptr[0] != CWFOURCHAR('!','<','a','r') && ptr[1] != CWFOURCHAR('c','h','>','\n')) {
CWFreeMemHandle(compiler_context, mh);
ReportError(2, 0);
return NULL;
}
CWUnlockMemHandle(compiler_context, mh);
*codesize = fsize;
*udatasize = 0;
*idatasize = 0;
return mh;
}
CWPLUGIN_ENTRY(PPCLibImport_main)(CWPluginContext context) {
SInt32 request;
CWResult result;
CWFileSpec fss;
SInt32 fileNumber;
CWObjectData od;
result = cwNoErr;
CWGetPluginRequest(context, &request);
switch (request) {
case reqInitialize:
case reqTerminate:
break;
case 0:
compiler_context = context;
CWGetMainFileSpec(context, &fss);
CWGetMainFileNumber(context, &fileNumber);
memset(&od, 0, sizeof(od));
od.objectdata = ImportObjectLibrary(&fss, &od.codesize, &od.udatasize, &od.idatasize);
result = CWDonePluginRequest(context, od.objectdata ? CWStoreObjectData(context, fileNumber, &od) : cwErrRequestFailed);
break;
}
return result;
}