Change from stdio to direct GS/OS calls for writing the file. Need to switch from a text file to a teach file and write the resources next.
This commit is contained in:
parent
beb063b56f
commit
3456321b23
132
md2teach/main.c
132
md2teach/main.c
|
@ -14,6 +14,8 @@
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
|
#include <gsos.h>
|
||||||
|
|
||||||
#include "md4c.h"
|
#include "md4c.h"
|
||||||
|
|
||||||
#pragma memorymodel 1
|
#pragma memorymodel 1
|
||||||
|
@ -87,7 +89,9 @@ int isFirstNonDocumentBlock = 1;
|
||||||
|
|
||||||
tBlockListItem * blockList = NULL;
|
tBlockListItem * blockList = NULL;
|
||||||
|
|
||||||
FILE * outputFile;
|
IORecGS writeRec;
|
||||||
|
char writeBuffer[4096];
|
||||||
|
int writeBufferOffset = 0;
|
||||||
|
|
||||||
tEntity entities[] = {
|
tEntity entities[] = {
|
||||||
{ "	", 0x9, 0x9 },
|
{ "	", 0x9, 0x9 },
|
||||||
|
@ -317,6 +321,34 @@ tEntity entities[] = {
|
||||||
|
|
||||||
// Implementation
|
// Implementation
|
||||||
|
|
||||||
|
void flushBuffer(void)
|
||||||
|
{
|
||||||
|
writeRec.requestCount = writeBufferOffset;
|
||||||
|
WriteGS(&writeRec);
|
||||||
|
if (toolerror()) {
|
||||||
|
fprintf(stderr, "%s: Error writing to output file\n", commandName);
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
writeBufferOffset = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
void writeChar(MD_CHAR ch)
|
||||||
|
{
|
||||||
|
if (writeBufferOffset == sizeof(writeBuffer))
|
||||||
|
flushBuffer();
|
||||||
|
|
||||||
|
writeBuffer[writeBufferOffset] = ch;
|
||||||
|
writeBufferOffset++;
|
||||||
|
}
|
||||||
|
|
||||||
|
void writeString(MD_CHAR * str, MD_SIZE size)
|
||||||
|
{
|
||||||
|
MD_SIZE i;
|
||||||
|
|
||||||
|
for (i = 0; i < size; i++)
|
||||||
|
writeChar(str[i]);
|
||||||
|
}
|
||||||
|
|
||||||
static int enterBlockHook(MD_BLOCKTYPE type, void * detail, void * userdata)
|
static int enterBlockHook(MD_BLOCKTYPE type, void * detail, void * userdata)
|
||||||
{
|
{
|
||||||
tBlockListItem * newBlock = malloc(sizeof(tBlockListItem));
|
tBlockListItem * newBlock = malloc(sizeof(tBlockListItem));
|
||||||
|
@ -359,7 +391,7 @@ static int enterBlockHook(MD_BLOCKTYPE type, void * detail, void * userdata)
|
||||||
newBlock->numTabs++;
|
newBlock->numTabs++;
|
||||||
|
|
||||||
if (!isFirstNonDocumentBlock)
|
if (!isFirstNonDocumentBlock)
|
||||||
fputc('\r', outputFile);
|
writeChar('\r');
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -372,7 +404,7 @@ static int enterBlockHook(MD_BLOCKTYPE type, void * detail, void * userdata)
|
||||||
newBlock->numTabs++;
|
newBlock->numTabs++;
|
||||||
|
|
||||||
if (!isFirstNonDocumentBlock)
|
if (!isFirstNonDocumentBlock)
|
||||||
fputc('\r', outputFile);
|
writeChar('\r');
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -380,6 +412,7 @@ static int enterBlockHook(MD_BLOCKTYPE type, void * detail, void * userdata)
|
||||||
int i;
|
int i;
|
||||||
tBlockListItem * enclosingBlock = newBlock->next;
|
tBlockListItem * enclosingBlock = newBlock->next;
|
||||||
int isNumbered = 0;
|
int isNumbered = 0;
|
||||||
|
static char str[16];
|
||||||
|
|
||||||
if (debugEnabled)
|
if (debugEnabled)
|
||||||
fprintf(stderr, "%*sLI {\n", debugIndentLevel, "");
|
fprintf(stderr, "%*sLI {\n", debugIndentLevel, "");
|
||||||
|
@ -393,22 +426,23 @@ static int enterBlockHook(MD_BLOCKTYPE type, void * detail, void * userdata)
|
||||||
isNumbered = 1;
|
isNumbered = 1;
|
||||||
if ((!enclosingBlock->u.olDetail.is_tight) &&
|
if ((!enclosingBlock->u.olDetail.is_tight) &&
|
||||||
(!isFirstNonDocumentBlock))
|
(!isFirstNonDocumentBlock))
|
||||||
fputc('\r', outputFile);
|
writeChar('\r');
|
||||||
} else if (enclosingBlock->type == MD_BLOCK_UL) {
|
} else if (enclosingBlock->type == MD_BLOCK_UL) {
|
||||||
if ((!enclosingBlock->u.ulDetail.is_tight) &&
|
if ((!enclosingBlock->u.ulDetail.is_tight) &&
|
||||||
(!isFirstNonDocumentBlock))
|
(!isFirstNonDocumentBlock))
|
||||||
fputc('\r', outputFile);
|
writeChar('\r');
|
||||||
}
|
}
|
||||||
|
|
||||||
for (i = 0; i < newBlock->numTabs; i++)
|
for (i = 0; i < newBlock->numTabs; i++)
|
||||||
fputc('\t', outputFile);
|
writeChar('\t');
|
||||||
|
|
||||||
if (isNumbered) {
|
if (isNumbered) {
|
||||||
fprintf(outputFile, "%u%c ", enclosingBlock->u.olDetail.start, enclosingBlock->u.olDetail.mark_delimiter);
|
sprintf(str, "%u%c ", enclosingBlock->u.olDetail.start, enclosingBlock->u.olDetail.mark_delimiter);
|
||||||
enclosingBlock->u.olDetail.start++;
|
enclosingBlock->u.olDetail.start++;
|
||||||
} else {
|
} else {
|
||||||
fprintf(outputFile, "%c ", 0xa5); // 0xa5 is a bullet character
|
sprintf(str, "%c ", 0xa5); // 0xa5 is a bullet character
|
||||||
}
|
}
|
||||||
|
writeString(str, strlen(str));
|
||||||
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
@ -420,10 +454,10 @@ static int enterBlockHook(MD_BLOCKTYPE type, void * detail, void * userdata)
|
||||||
fprintf(stderr, "%*sHR {\n", debugIndentLevel, "");
|
fprintf(stderr, "%*sHR {\n", debugIndentLevel, "");
|
||||||
|
|
||||||
if (!isFirstNonDocumentBlock)
|
if (!isFirstNonDocumentBlock)
|
||||||
fputc('\r', outputFile);
|
writeChar('\r');
|
||||||
|
|
||||||
for (i = 0; i < 30; i++)
|
for (i = 0; i < 30; i++)
|
||||||
fputc('_', outputFile); // 0xd1 is a horizontal line
|
writeChar('_');
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -435,7 +469,7 @@ static int enterBlockHook(MD_BLOCKTYPE type, void * detail, void * userdata)
|
||||||
memcpy(&(newBlock->u.hDetail), hDetail, sizeof(*hDetail));
|
memcpy(&(newBlock->u.hDetail), hDetail, sizeof(*hDetail));
|
||||||
|
|
||||||
if (!isFirstNonDocumentBlock)
|
if (!isFirstNonDocumentBlock)
|
||||||
fputc('\r', outputFile);
|
writeChar('\r');
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -452,7 +486,7 @@ static int enterBlockHook(MD_BLOCKTYPE type, void * detail, void * userdata)
|
||||||
memcpy(&(newBlock->u.codeDetail), codeDetail, sizeof(*codeDetail));
|
memcpy(&(newBlock->u.codeDetail), codeDetail, sizeof(*codeDetail));
|
||||||
|
|
||||||
if (!isFirstNonDocumentBlock)
|
if (!isFirstNonDocumentBlock)
|
||||||
fputc('\r', outputFile);
|
writeChar('\r');
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -461,7 +495,7 @@ static int enterBlockHook(MD_BLOCKTYPE type, void * detail, void * userdata)
|
||||||
fprintf(stderr, "%*sP {\n", debugIndentLevel, "");
|
fprintf(stderr, "%*sP {\n", debugIndentLevel, "");
|
||||||
|
|
||||||
if (!isFirstNonDocumentBlock)
|
if (!isFirstNonDocumentBlock)
|
||||||
fputc('\r', outputFile);
|
writeChar('\r');
|
||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
|
@ -508,31 +542,31 @@ static int leaveBlockHook(MD_BLOCKTYPE type, void * detail, void * userdata)
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case MD_BLOCK_UL:
|
case MD_BLOCK_UL:
|
||||||
fputc('\r', outputFile);
|
writeChar('\r');
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case MD_BLOCK_OL:
|
case MD_BLOCK_OL:
|
||||||
fputc('\r', outputFile);
|
writeChar('\r');
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case MD_BLOCK_LI:
|
case MD_BLOCK_LI:
|
||||||
fputc('\r', outputFile);
|
writeChar('\r');
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case MD_BLOCK_HR:
|
case MD_BLOCK_HR:
|
||||||
fputc('\r', outputFile);
|
writeChar('\r');
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case MD_BLOCK_H:
|
case MD_BLOCK_H:
|
||||||
fputc('\r', outputFile);
|
writeChar('\r');
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case MD_BLOCK_CODE:
|
case MD_BLOCK_CODE:
|
||||||
fputc('\r', outputFile);
|
writeChar('\r');
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case MD_BLOCK_P:
|
case MD_BLOCK_P:
|
||||||
fputc('\r', outputFile);
|
writeChar('\r');
|
||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
|
@ -648,7 +682,7 @@ static void printEntity(const MD_CHAR * text, MD_SIZE size)
|
||||||
unicodeChar = 0;
|
unicodeChar = 0;
|
||||||
if ((unicodeChar > 0) &&
|
if ((unicodeChar > 0) &&
|
||||||
(unicodeChar < 128)) {
|
(unicodeChar < 128)) {
|
||||||
fputc(unicodeChar, outputFile);
|
writeChar(unicodeChar);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -660,7 +694,7 @@ static void printEntity(const MD_CHAR * text, MD_SIZE size)
|
||||||
unicodeChar = 0;
|
unicodeChar = 0;
|
||||||
if ((unicodeChar > 0) &&
|
if ((unicodeChar > 0) &&
|
||||||
(unicodeChar < 128)) {
|
(unicodeChar < 128)) {
|
||||||
fputc(unicodeChar, outputFile);
|
writeChar(unicodeChar);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -668,7 +702,7 @@ static void printEntity(const MD_CHAR * text, MD_SIZE size)
|
||||||
for (entityNum = 0; entityNum < (sizeof(entities) / sizeof(entities[0])); entityNum++) {
|
for (entityNum = 0; entityNum < (sizeof(entities) / sizeof(entities[0])); entityNum++) {
|
||||||
if ((unicodeChar == entities[entityNum].unicodeChar) ||
|
if ((unicodeChar == entities[entityNum].unicodeChar) ||
|
||||||
(strncmp(entities[entityNum].entityString, text, size) == 0)) {
|
(strncmp(entities[entityNum].entityString, text, size) == 0)) {
|
||||||
fputc(entities[entityNum].entityChar, outputFile);
|
writeChar(entities[entityNum].entityChar);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -725,7 +759,7 @@ static int textHook(MD_TEXTTYPE type, const MD_CHAR * text, MD_SIZE size, void *
|
||||||
}
|
}
|
||||||
|
|
||||||
if (size > 0)
|
if (size > 0)
|
||||||
fwrite(text, sizeof(MD_CHAR), size, outputFile);
|
writeString(text, size);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -787,8 +821,11 @@ int main(int argc, char * argv[])
|
||||||
static FILE * inputFile;
|
static FILE * inputFile;
|
||||||
static long inputFileLen;
|
static long inputFileLen;
|
||||||
static char * inputBuffer;
|
static char * inputBuffer;
|
||||||
|
|
||||||
static char * outputFileName;
|
static GSString255 outputFileName;
|
||||||
|
static OpenRecGS openRec;
|
||||||
|
static SetPositionRecGS setEofRec;
|
||||||
|
static RefNumRecGS closeRec;
|
||||||
|
|
||||||
static int index;
|
static int index;
|
||||||
|
|
||||||
|
@ -796,13 +833,41 @@ int main(int argc, char * argv[])
|
||||||
|
|
||||||
index = parseArgs(argc, argv);
|
index = parseArgs(argc, argv);
|
||||||
inputFileName = argv[index];
|
inputFileName = argv[index];
|
||||||
outputFileName = argv[index + 1];
|
|
||||||
|
|
||||||
outputFile = fopen(outputFileName, "w");
|
outputFileName.length = strlen(argv[index + 1]);
|
||||||
if (outputFile == NULL) {
|
if (outputFileName.length >= sizeof(outputFileName.text)) {
|
||||||
fprintf(stderr, "%s: Unable to open output file %s, %s\n", commandName, outputFileName, strerror(errno));
|
fprintf(stderr, "%s: Output file path too long, %s\n", commandName, outputFileName);
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
strcpy(outputFileName.text, argv[index + 1]);
|
||||||
|
|
||||||
|
openRec.pCount = 7;
|
||||||
|
openRec.refNum = 0;
|
||||||
|
openRec.pathname = &outputFileName;
|
||||||
|
openRec.requestAccess = writeEnable;
|
||||||
|
openRec.resourceNumber = 0;
|
||||||
|
openRec.access = destroyEnable | renameEnable | readWriteEnable;
|
||||||
|
openRec.fileType = 0x04; // This is for text, it is 0x50 for Teach
|
||||||
|
openRec.auxType = 0x0000;
|
||||||
|
OpenGS(&openRec);
|
||||||
|
if (toolerror()) {
|
||||||
|
fprintf(stderr, "%s: Unable to open output file %s\n", commandName, outputFileName.text);
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
setEofRec.pCount = 3;
|
||||||
|
setEofRec.refNum = openRec.refNum;
|
||||||
|
setEofRec.base = startPlus;
|
||||||
|
setEofRec.displacement = 0;
|
||||||
|
SetEOFGS(&setEofRec);
|
||||||
|
if (toolerror()) {
|
||||||
|
fprintf(stderr, "%s: Unable to truncate output file %s\n", commandName, outputFileName.text);
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
writeRec.pCount = 4;
|
||||||
|
writeRec.refNum = openRec.refNum;
|
||||||
|
writeRec.dataBuffer = writeBuffer;
|
||||||
|
|
||||||
inputFile = fopen(inputFileName, "r");
|
inputFile = fopen(inputFileName, "r");
|
||||||
if (inputFile == NULL) {
|
if (inputFile == NULL) {
|
||||||
|
@ -846,10 +911,15 @@ int main(int argc, char * argv[])
|
||||||
|
|
||||||
result = md_parse(inputBuffer, inputFileLen, &parser, NULL);
|
result = md_parse(inputBuffer, inputFileLen, &parser, NULL);
|
||||||
|
|
||||||
|
if (writeBufferOffset > 0)
|
||||||
|
flushBuffer();
|
||||||
|
closeRec.pCount = 1;
|
||||||
|
closeRec.refNum = openRec.refNum;
|
||||||
|
CloseGS(&closeRec);
|
||||||
|
|
||||||
putchar('\n');
|
putchar('\n');
|
||||||
|
|
||||||
fclose(inputFile);
|
fclose(inputFile);
|
||||||
fclose(outputFile);
|
|
||||||
|
|
||||||
if (debugEnabled) {
|
if (debugEnabled) {
|
||||||
fprintf(stderr, "Parser result: %d\n", result);
|
fprintf(stderr, "Parser result: %d\n", result);
|
||||||
|
|
Loading…
Reference in New Issue