Skip to content

Commit 3c26521

Browse files
committed
rewrote dmahole code redirection to use regular fopen
1 parent 490fd78 commit 3c26521

3 files changed

Lines changed: 41 additions & 23 deletions

File tree

7800bas.c

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010

1111
char stdoutfilename[256];
1212
char backupname[256];
13-
FILE *stdoutfilepointer;
13+
FILE *main_asm_fp = NULL;
14+
FILE *current_output_fp = NULL;
1415
FILE *preprocessedfd = NULL;
1516

1617
extern int currentbank;
@@ -63,7 +64,7 @@ extern int TIGHTPACKBORDER;
6364
extern int changedmaholescalled;
6465
int maxpasses = 2;
6566

66-
#define BASIC_VERSION_INFO "7800basic v0.37"
67+
#define BASIC_VERSION_INFO "7800basic v0.38"
6768

6869
int main (int argc, char *argv[])
6970
{
@@ -221,10 +222,12 @@ int main (int argc, char *argv[])
221222

222223
// redirect STDOUT to 7800.asm, overwriting if it exists...
223224
strcpy (stdoutfilename, "7800.asm");
224-
if ((stdoutfilepointer = freopen (stdoutfilename, "w", stdout)) == NULL)
225+
main_asm_fp = fopen(stdoutfilename, "w");
226+
if (main_asm_fp == NULL)
225227
{
226228
prerror ("couldn't create the 7800.asm file.");
227229
}
230+
current_output_fp = main_asm_fp;
228231

229232
printf ("SPACEOVERFLOW SET 0\n");
230233
printf (" ifnconst SPACEOVERFLOWPASS\n");
@@ -410,14 +413,10 @@ int main (int argc, char *argv[])
410413
printf ("DMAHOLEEND%d SET .\n", currentdmahole);
411414

412415
//if stdout is redirected, change it back to 7800.asm so the gameend label goes in the right spot...
413-
if (strcmp (stdoutfilename, "7800.asm") != 0)
414-
{
415-
strcpy (stdoutfilename, "7800.asm");
416-
if ((stdoutfilepointer = freopen (stdoutfilename, "a", stdout)) == NULL)
417-
{
418-
prerror ("couldn't reopen the 7800.asm file.");
419-
}
420-
}
416+
fflush(current_output_fp);
417+
if (current_output_fp != main_asm_fp)
418+
fclose(current_output_fp);
419+
current_output_fp = main_asm_fp;
421420

422421
printf ("gameend\n");
423422

statements.c

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,9 @@ int savelines[MAXINCBASIC];
3030
char savelinesname[MAXINCBASIC][MAXINCBASIC];
3131

3232
extern char stdoutfilename[256];
33-
extern FILE *stdoutfilepointer;
3433
extern FILE *preprocessedfd;
34+
extern FILE *main_asm_fp;
35+
extern FILE *current_output_fp;
3536
extern char backupname[256];
3637
extern int maxpasses;
3738

@@ -1316,8 +1317,13 @@ void dmahole (char **statement)
13161317
if (requestedhole > 0)
13171318
printf ("DMAHOLEEND%d SET .\n", requestedhole - 1);
13181319

1320+
fflush(current_output_fp);
1321+
if (current_output_fp != main_asm_fp)
1322+
fclose(current_output_fp);
1323+
13191324
sprintf (stdoutfilename, "7800hole.%d.asm", requestedhole);
1320-
if ((stdoutfilepointer = freopen (stdoutfilename, "w", stdout)) == NULL)
1325+
current_output_fp = fopen(stdoutfilename, "w");
1326+
if (current_output_fp == NULL)
13211327
{
13221328
prerror ("couldn't create the %s file", stdoutfilename);
13231329
}
@@ -4964,15 +4970,10 @@ void barf_graphic_file (void)
49644970
printf ("DMAHOLEEND%d SET .\n", currentdmahole);
49654971

49664972
//if stdout is redirected, its time change it back to 7800.asm
4967-
if (strcmp (stdoutfilename, "7800.asm") != 0)
4968-
{
4969-
strcpy (stdoutfilename, "7800.asm");
4970-
if ((stdoutfilepointer = freopen (stdoutfilename, "a", stdout)) == NULL)
4971-
{
4972-
prerror ("couldn't reopen the 7800.asm file");
4973-
}
4974-
}
4975-
4973+
fflush(current_output_fp);
4974+
if (current_output_fp != main_asm_fp)
4975+
fclose(current_output_fp);
4976+
current_output_fp = main_asm_fp;
49764977

49774978
ADDRBASE = BANKSTART - (dmaplain * DMASIZE);
49784979

@@ -5250,7 +5251,7 @@ void barf_graphic_file (void)
52505251
prout (" DMA hole code found and imported\n");
52515252
int c;
52525253
while ((c = getc (holefilepointer)) != EOF)
5253-
putchar (c);
5254+
printf("%c",c);
52545255
fclose (holefilepointer);
52555256
remove (holefilename);
52565257
}
@@ -12268,4 +12269,18 @@ void lastrites()
1226812269
remove("7800hole.0.asm");
1226912270
remove("7800hole.1.asm");
1227012271
remove("7800hole.2.asm");
12272+
12273+
// Close all our managed file pointers
12274+
if (current_output_fp != NULL)
12275+
{
12276+
fflush(current_output_fp);
12277+
if (current_output_fp != main_asm_fp)
12278+
fclose(current_output_fp);
12279+
current_output_fp = NULL;
12280+
}
12281+
if (main_asm_fp != NULL)
12282+
{
12283+
fclose(main_asm_fp);
12284+
main_asm_fp = NULL;
12285+
}
1227112286
}

statements.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@
2323

2424
#include <stdio.h>
2525

26+
// Global stream for all printf output, to avoid freopen performance issues in WASM.
27+
extern FILE *current_output_fp;
28+
#define printf(...) fprintf(current_output_fp, __VA_ARGS__)
29+
2630
int linenum ();
2731
int getcondpart ();
2832
void add_inline (char *myinclude);

0 commit comments

Comments
 (0)