#include"mbasic.h"

static char * InitOutPtr,* OutPtr;
static int out_bytes;

void io_init_output(void)
{
 if(!(InitOutPtr=OutPtr=malloc(0x100000))) Error("Unable to allocate memory for output");
}

void io_finalize_output(char * name)
{
 FILE * f=fopen(name,"wb");
 if(!f) Error("Unable to create output");
 fwrite(InitOutPtr,1,out_bytes,f);
 fclose(f);
 free(InitOutPtr);
}

void emitch(char c)
{
 out_bytes++;
 *OutPtr=c;
 OutPtr++;
}

void emit(const char * fmt,...)
{
 va_list args;
 char * buf,buf1[128];
 va_start(args,fmt);
 emitch(EMIT_PREFIX);
 buf=buf1+0;
 memset(buf,0,sizeof(buf1));
 vsprintf(buf,fmt,args);
 va_end(args);
 for(;*buf;buf++) emitch(*buf);
}

void _emit(const char * fmt,...)
{
 va_list args;
 char * buf,buf1[128];
 va_start(args,fmt);
 buf=buf1+0;
 memset(buf,0,sizeof(buf1));
 vsprintf(buf,fmt,args);
 va_end(args);
 for(;*buf;buf++) emitch(*buf);
}
