Skip to content

Commit e531569

Browse files
committed
foundation: reduce IO calls for incrmtfile() and lastrites()
1 parent 9de9349 commit e531569

1 file changed

Lines changed: 57 additions & 44 deletions

File tree

statements.c

Lines changed: 57 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -6859,57 +6859,66 @@ void incrmtfile (char **statement)
68596859

68606860
printf("%s\n",datalabelname);
68616861

6862-
// we handle rmta different than rmt, so we need to open the file
6863-
// first and see which it is.
6864-
68656862
backupthisfile(statement[2]);
68666863

6867-
char magic[6];
6868-
FILE *fp = fopen (statement[2], "rb");
6869-
if(fp==NULL)
6870-
prerror ("couldn't open %s",statement[2]);
6871-
fread(magic,1,5,fp);
6872-
magic[5]=0;
6873-
if(!strcmp(magic,";RMTA"))
6874-
{
6875-
// this is an RMTA, so we can just do a regular include and be done
6876-
fclose(fp);
6877-
printf(" include \"%s\"\n",statement[2]);
6878-
return;
6879-
}
6864+
FILE *fp = fopen(statement[2], "rb");
6865+
if (fp == NULL)
6866+
prerror("couldn't open %s", statement[2]);
6867+
6868+
// 1. Get file size and read entire file into memory
6869+
fseek(fp, 0, SEEK_END);
6870+
long file_size = ftell(fp);
68806871
rewind(fp);
68816872

6882-
memset(magic,0,6);
6883-
int c;
6884-
while ((c = fgetc(fp)) != EOF)
6873+
unsigned char *file_buffer = malloc(file_size);
6874+
if (file_buffer == NULL)
6875+
prerror("couldn't allocate memory to read %s", statement[2]);
6876+
6877+
if (fread(file_buffer, 1, file_size, fp) != file_size)
6878+
prerror("couldn't read %s into memory", statement[2]);
6879+
fclose(fp);
6880+
6881+
// 2. Check for RMTA header in memory
6882+
if (file_size >= 5 && memcmp(file_buffer, ";RMTA", 5) == 0)
68856883
{
6886-
magic[0]=magic[1];
6887-
magic[1]=magic[2];
6888-
magic[2]=magic[3];
6889-
magic[3]=c;
6890-
if (!strncmp(magic,"RMT4",4))
6884+
// This is an RMTA, so we can just do a regular include and be done
6885+
printf(" include \"%s\"\n", statement[2]);
6886+
free(file_buffer);
6887+
return;
6888+
}
6889+
6890+
// 3. Scan for 'RMT4' signature in memory
6891+
long start_offset = -1;
6892+
for (long i = 0; i < file_size - 4; i++) {
6893+
if (memcmp(file_buffer + i, "RMT4", 4) == 0) {
6894+
start_offset = i;
68916895
break;
6896+
}
68926897
}
6893-
if (c == EOF)
6894-
prerror ("file doesn't appear to contain RMT data");
6895-
printf(" .byte \"RMT4\"\n");
6896-
t=0;
6897-
size=0;
6898-
while ((c = fgetc(fp)) != EOF)
6898+
6899+
if (start_offset == -1) {
6900+
free(file_buffer);
6901+
prerror("file doesn't appear to contain RMT data");
6902+
}
6903+
6904+
// 4. Generate output from memory buffer
6905+
printf(" .byte \"RMT4\"\n");
6906+
size = 0;
6907+
for (long i = start_offset + 4; i < file_size; i++)
68996908
{
69006909
size++;
6901-
if (t%16>0)
6910+
if ((i - (start_offset + 4)) % 16 > 0)
69026911
printf(",");
6903-
if (t%16==0)
6912+
if ((i - (start_offset + 4)) % 16 == 0)
69046913
printf("\n .byte ");
6905-
printf("$%02x",c);
6906-
t++;
6907-
if (t%16==0)
6908-
printf("\n");
6914+
printf("$%02x", file_buffer[i]);
69096915
}
6916+
69106917
printf("\n");
6911-
prout ("RMT %s imported, %ld bytes\n",datalabelname,size);
6912-
fclose(fp);
6918+
prout ("RMT %s imported, %ld bytes\n", datalabelname, size);
6919+
6920+
// 5. Cleanup
6921+
free(file_buffer);
69136922
}
69146923

69156924
void decompress (char **statement)
@@ -12268,15 +12277,19 @@ void lastrites()
1226812277
}
1226912278
prinit();
1227012279
fclose(stderrfilepointer);
12280+
1227112281
stderrfilepointer = fopen ("message.log","rb");
12272-
int logchar;
12273-
logchar = fgetc (stderrfilepointer);
12274-
while (logchar != EOF)
12282+
if (stderrfilepointer != NULL)
1227512283
{
12276-
fputc(logchar,stderr);
12277-
logchar = fgetc (stderrfilepointer);
12284+
char buffer[4096];
12285+
size_t bytes_read;
12286+
while ((bytes_read = fread(buffer, 1, sizeof(buffer), stderrfilepointer)) > 0)
12287+
{
12288+
fwrite(buffer, 1, bytes_read, stderr);
12289+
}
12290+
fclose(stderrfilepointer);
1227812291
}
12279-
fclose(stderrfilepointer);
12292+
1228012293
remove("message.log");
1228112294
remove("7800hole.0.asm");
1228212295
remove("7800hole.1.asm");

0 commit comments

Comments
 (0)