omi-eikyo/src/golan5.c

86 lines
1.8 KiB
C

// Copyright © 2017 Project Golan, all rights reserved.
#include "m_tokbuf.h"
#include "m_binio.h"
#include <setjmp.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
// Static Objects ------------------------------------------------------------|
static jmp_buf ejmp;
// Static Functions ----------------------------------------------------------|
//
// Expect
//
static M_token *Expect(M_tkbuf *tb, M_tokty t)
{
M_token *tok = M_TokBuf_Get(tb);
if(tok->type != t) {
fprintf(stderr, "unexpected token\n");
longjmp(ejmp, 1);
}
return tok;
}
// Extern Functions ----------------------------------------------------------|
//
// main
//
int main(int argc, char **argv)
{
if(argc < 2) {
fprintf(stderr, "not enough arguments\n");
return 1;
}
M_tkbuf tb = {.fp = fopen(argv[1], "r"), .bbeg = 1, .bend = 8};
if(!tb.fp) {
fprintf(stderr, "couldn't open input file\n");
return 1;
}
FILE *out = fopen(argv[2], "wb");
if(!out) {
fclose(tb.fp);
fprintf(stderr, "couldn't open output file\n");
return 1;
}
M_TokBuf_Ctor(&tb);
if(setjmp(ejmp) == 1)
goto done;
fwrite("Gmf0\r\n\xF7\0", 1, 8, out);
do {
if(M_TokBuf_Drop(&tb, tok_braceo))
{
M_IO_WriteLE4u(out, strtoul(Expect(&tb, tok_number)->textV, NULL, 0));
M_token *tok = Expect(&tb, tok_identi);
fwrite(tok->textV, 1, tok->textC, out);
if(strcmp(tok->textV, "Sector") == 0)
for(int i = 0; i < 4; i++)
M_IO_WriteLE4k(out, strtof(Expect(&tb, tok_number)->textV, NULL));
Expect(&tb, tok_bracec);
}
}
while(!M_TokBuf_Drop(&tb, tok_eof));
done:
fclose(out);
M_TokBuf_Dtor(&tb);
fclose(tb.fp);
return 0;
}
// EOF