Implement a workaround for writing resources. Write the resources I want to a .rez file and use a script in the build itself to turn that into resources and attach it to the output file. This seems to result in a styled teach file for my test input that loads and looks pretty close to right.

This commit is contained in:
Jeremy Rand 2021-04-29 00:24:27 -04:00
parent 8b97df852c
commit 9e3562dd5f
8 changed files with 180 additions and 51 deletions

View File

@ -68,6 +68,7 @@
9DDFC7C4262FD50C006D6E71 /* createDiskImage */ = {isa = PBXFileReference; lastKnownFileType = text.script.sh; path = createDiskImage; sourceTree = "<group>"; };
9DDFC7C5262FD50D006D6E71 /* config.txt */ = {isa = PBXFileReference; lastKnownFileType = text; path = config.txt; sourceTree = "<group>"; };
9DDFC7CA262FD7DD006D6E71 /* tar */ = {isa = PBXFileReference; lastKnownFileType = file; path = tar; sourceTree = "<group>"; };
9DEEBB90263A673200DAA063 /* teachRez */ = {isa = PBXFileReference; lastKnownFileType = text; path = teachRez; sourceTree = "<group>"; };
/* End PBXFileReference section */
/* Begin PBXFrameworksBuildPhase section */
@ -129,6 +130,7 @@
9D6532F52626240800105D50 /* orca-cc */,
9D6532F72626240800105D50 /* orca-rez */,
9D6532F92626240800105D50 /* tail.mk */,
9DEEBB90263A673200DAA063 /* teachRez */,
9DDFC7C5262FD50D006D6E71 /* config.txt */,
9DDFC7C4262FD50C006D6E71 /* createDiskImage */,
9DDFC7C3262FD50C006D6E71 /* empty.2mg */,

View File

@ -17,6 +17,7 @@
#include "io.h"
#include "main.h"
#include "style.h"
// Defines
@ -26,16 +27,44 @@
// but this will let me test it without that capability.
#define RESOURCE_WORKAROUND
#define TEACH_FILE_TYPE 0x50
#define TEACH_AUX_TYPE 0x5445
#define R_WINDOW_POSITION 0x7001
#define WINDOW_POSITION_NUM 1
#define STYLE_BLOCK_NUM 1
// Typedefs
typedef struct tWindowPos
{
int16_t height;
int16_t width;
int16_t top;
int16_t left;
int32_t version;
} tWindowPos;
// Globals
static GSString255 outputFileName;
static Word writeResId;
static IORecGS writeRec;
static char writeBuffer[4096];
static int32_t writeBufferOffset = 0;
static MD_SIZE writePos = 0;
static tWindowPos windowPos = {
0xad, // height
0x27c, // width
0x1a, // top
0x02, // left
0x0 // version
};
// Implementation
@ -71,8 +100,8 @@ int openOutputFile(const char * filename)
createRec.pCount = 5;
createRec.pathname = &outputFileName;
createRec.access = destroyEnable | renameEnable | readWriteEnable;
createRec.fileType = 0x50; // Type for Teach file
createRec.auxType = 0x5445; // Aux type for Teach file
createRec.fileType = TEACH_FILE_TYPE;
createRec.auxType = TEACH_AUX_TYPE;
createRec.storageType = extendedFile;
CreateGS(&createRec);
if (toolerror()) {
@ -126,10 +155,106 @@ MD_SIZE outputPos(void)
}
void closeOutputFile(void)
static int writeResources(void)
{
#ifndef RESOURCE_WORKAROUND
int shutdownResources = 0;
Word writeResId;
if (!ResourceStatus()) {
ResourceStartUp(userid());
shutdownResources = 1;
}
CreateResourceFile(TEACH_AUX_TYPE, TEACH_FILE_TYPE, destroyEnable | renameEnable | readWriteEnable, (Pointer)outputFileName);
if (toolerror()) {
fprintf(stderr, "%s: Unable to create resources of file %s, toolerror=0x%x\n", commandName, outputFileName.text, toolerror());
return 1;
}
writeResId = OpenResourceFile(0x8000 | readWriteEnable, NULL, (Pointer)outputFileName);
if (toolerror()) {
fprintf(stderr, "%s: Unable to open resources of file %s, toolerror=0x%x\n", commandName, outputFileName.text, toolerror());
return 1;
}
CloseResourceFile(writeResId);
// Implement more of this...
if (shutdownResources)
ResourceShutDown();
#else
FILE * rezFile;
uint8_t * ptr;
uint32_t size;
uint32_t i;
strcat(outputFileName.text, ".rez");
rezFile = fopen(outputFileName.text, "w");
if (rezFile == NULL) {
fprintf(stderr, "%s: Unable to open resource file %s, %s\n", commandName, outputFileName.text, strerror(errno));
return 1;
}
fprintf(rezFile,
"#define rStyleBlock 0x%x\n"
"#define rWindowPosition 0x%x\n"
"\n"
"type rStyleBlock {\n"
" hex string;\n"
"};\n"
"\n"
"type rWindowPosition {\n"
" hex string;\n"
"};\n"
"\n"
"resource rStyleBlock (%u) {\n"
" $\"",
rStyleBlock, R_WINDOW_POSITION, STYLE_BLOCK_NUM
);
ptr = stylePtr();
size = styleSize();
for (i = 0; i < size; i++) {
if ((i > 0) &&
((i % 32) == 0)) {
fprintf(rezFile, "\"\n $\"");
}
fprintf(rezFile, "%02x", (uint16_t)*ptr);
ptr++;
}
fprintf(rezFile, "\"\n"
"};\n"
"\n"
"resource rWindowPosition (%u) {\n"
" $\"",
WINDOW_POSITION_NUM);
ptr = (uint8_t *)(&windowPos);
size = sizeof(windowPos);
for (i = 0; i < size; i++) {
if ((i > 0) &&
((i % 32) == 0)) {
fprintf(rezFile, "\"\n $\"");
}
fprintf(rezFile, "%02x", (uint16_t)*ptr);
ptr++;
}
fprintf(rezFile, "\"\n"
"};\n"
"\n");
fclose(rezFile);
#endif
return 0;
}
int closeOutputFile(void)
{
RefNumRecGS closeRec;
int shutdownResources = 0;
if (writeBufferOffset > 0)
flushBuffer();
@ -137,30 +262,7 @@ void closeOutputFile(void)
closeRec.refNum = writeRec.refNum;
CloseGS(&closeRec);
if (!ResourceStatus()) {
ResourceStartUp(userid());
shutdownResources = 1;
}
#ifdef RESOURCE_WORKAROUND
CreateResourceFile(0x5445, 0x50, destroyEnable | renameEnable | readWriteEnable, (Pointer)outputFileName);
if (toolerror()) {
fprintf(stderr, "%s: Unable to create resources of file %s, toolerror=0x%x\n", commandName, outputFileName.text, toolerror());
}
writeResId = OpenResourceFile(0x8000 | readWriteEnable, NULL, (Pointer)outputFileName);
if (toolerror()) {
fprintf(stderr, "%s: Unable to open resources of file %s, toolerror=0x%x\n", commandName, outputFileName.text, toolerror());
} else {
CloseResourceFile(writeResId);
}
// Implement more of this...
if (shutdownResources)
ResourceShutDown();
#else
// Implement the workaround here...
#endif
return writeResources();
}

View File

@ -17,7 +17,7 @@ extern int openOutputFile(const char * filename);
extern void writeChar(MD_CHAR ch);
extern void writeString(const MD_CHAR * str, MD_SIZE size);
extern MD_SIZE outputPos(void);
extern void closeOutputFile(void);
extern int closeOutputFile(void);
extern const MD_CHAR * readInputFile(const char * filename, MD_SIZE * bufferSize);
extern void releaseInputBuffer(const MD_CHAR * inputBuffer);

View File

@ -102,12 +102,9 @@ int main(int argc, char * argv[])
}
result = parse(inputBuffer, inputFileLen);
closeOutputFile();
releaseInputBuffer(inputBuffer);
putchar('\n');
if (debugEnabled) {
fprintf(stderr, "Parser result: %d\n", result);
}
@ -115,5 +112,13 @@ int main(int argc, char * argv[])
if (result != 0)
fprintf(stderr, "%s: Parser failed (%d)\n", commandName, result);
if (closeOutputFile() != 0)
result = 1;
if (result != 0)
remove(argv[index + 1]);
putchar('\n');
return result;
}

View File

@ -192,6 +192,7 @@ executeGUI: all
executeShell: all
$(ORCA) --mem $(TARGETDIR)/$(PGM) -d test.md $(TARGETDIR)/outfile.txt
make/teachRez $(TARGETDIR)/outfile.txt
make/createDiskImage "$(DISKIMAGE)" $(DESTBOOTIMAGE) "$(TARGETDIR)/outfile.txt"
make/launchEmulator "$(DISKIMAGE)" "$(DESTBOOTIMAGE)"

23
md2teach/make/teachRez Executable file
View File

@ -0,0 +1,23 @@
#!/bin/bash
PROJECT_DIR=`pwd`
cd `dirname $1`
TEACH_FILE=`basename $1`
BASEFILE=`echo $TEACH_FILE | sed 's/\.[^\/]*$//'`
REZ_FILE="${BASEFILE}.rez"
R_FILE="${BASEFILE}.r"
mv "${TEACH_FILE}.rez" "$REZ_FILE"
$ORCA chtyp -l rez "$REZ_FILE"
$PROJECT_DIR/make/orca-rez "$REZ_FILE" "$BASEFILE"
if [ $? -ne 0 ]
then
echo Error compiling resource file
exit 1
fi
cp "${R_FILE}"/..namedfork/rsrc "$TEACH_FILE"/..namedfork/rsrc
exit 0

View File

@ -36,15 +36,6 @@
// Typedefs
typedef struct tWindowPos
{
int16_t height;
int16_t width;
int16_t top;
int16_t left;
int32_t version;
} tWindowPos;
// I wish I could use the structure definition from textedit.h but TERuler contains optional
// fields in the definition and Teach isn't expecting them it seems (array of theTabs). So,
// I need my own struct which omits them.
@ -80,14 +71,6 @@ typedef struct tFormat
// Globals
static tWindowPos windowPos = {
0xad, // height
0x27c, // width
0x1a, // top
0x02, // left
0x0 // version
};
// For the 6 header sizes, we are going with:
// 1 -> Helvetica 36
// 2 -> Helvetica 30
@ -268,3 +251,13 @@ void closeStyle(void)
formatPtr->styleItems[lastStyleIndex]. dataLength = currentPos - styleChangedAt;
}
}
uint8_t * stylePtr(void)
{
return (uint8_t *)formatPtr;
}
uint32_t styleSize(void)
{
return sizeof(formatPtr->header) + (sizeof(formatPtr->styleItems) * formatPtr->header.numberOfStyles);
}

View File

@ -35,5 +35,8 @@ extern int styleInit(void);
extern void setStyle(tStyleType styleType, uint16_t textMask, uint16_t headerSize);
extern void closeStyle(void);
uint8_t * stylePtr(void);
uint32_t styleSize(void);
#endif /* define _GUARD_PROJECTmd2teach_FILEstyle_ */