diff --git a/main/gbc/gbc_dump.c b/main/gbc/gbc_dump.c index b39b41539..5a1f424ba 100644 --- a/main/gbc/gbc_dump.c +++ b/main/gbc/gbc_dump.c @@ -43,7 +43,8 @@ static FILE *_finfo; - +static char *_buffer = NULL; +static int _buffer_ptr = 0; static const char *get_name(int index) { @@ -321,32 +322,6 @@ static void create_file(FILE **fw, const char *file) } } -static char *read_line(FILE *f, int *plen) -{ - int len; - char *line; - - if (!f) - return NULL; - - line = fgets(COMMON_buffer, COMMON_BUF_MAX, f); - - if (!line) - return NULL; - - len = strlen(line); - if (line[len - 1] == '\n') - { - line[len - 1] = 0; - len--; - } - - if (plen) - *plen = len; - - return line; -} - static void close_file_and_rename(FILE *f, const char *file, const char *dest) { if (f) @@ -371,24 +346,72 @@ static bool exist_bytecode_file(char *name) return exist; } +static void read_line(char **line, int *len) +{ + int l; + char c; + int lmax; + + *line = NULL; + *len = 0; + + if (!_buffer) + return; + + lmax = BUFFER_length(_buffer); + if (_buffer_ptr >= lmax) + return; + + *line = &_buffer[_buffer_ptr]; + + l = 0; + for(;;) + { + if (_buffer_ptr >= lmax) + break; + c = _buffer[_buffer_ptr++]; + if (c == '\n') + { + _buffer[_buffer_ptr - 1] = 0; + break; + } + l++; + } + + *len = l; +} + +static bool load_file(const char *name) +{ + _buffer_ptr = 0; + BUFFER_create(&_buffer); + if (BUFFER_load_file(&_buffer, name)) + { + BUFFER_delete(&_buffer); + return TRUE; + } + else + return FALSE; +} + static void class_update_exported(CLASS *class) { FILE *fw = NULL; - FILE *fr; char *name; int len; bool inserted = FALSE; bool optional; int cmp; - fr = fopen(".list", "r"); - - if (!fr && !class->exported) + if (load_file(".list") && !class->exported) return; + //if (!fr && !class->exported) + // return; + for(;;) { - name = read_line(fr, &len); + read_line(&name, &len); optional = FALSE; if (!name) @@ -442,8 +465,8 @@ static void class_update_exported(CLASS *class) } } - if (fr) - fclose(fr); + if (_buffer) + BUFFER_delete(&_buffer); close_file_and_rename(fw, ".list#", ".list"); } @@ -672,7 +695,6 @@ static void output_help(void) void CLASS_export(void) { FILE *fw = NULL; - FILE *fr; char *line; int len; bool inserted = FALSE; @@ -687,10 +709,10 @@ void CLASS_export(void) } class_update_exported(class); - - fr = fopen(".info", "r"); - line = read_line(fr, &len); + load_file(".info"); + + read_line(&line, &len); for(;;) { @@ -706,7 +728,7 @@ void CLASS_export(void) for(;;) { - line = read_line(fr, &len); + read_line(&line, &len); if (!line || *line == '#') break; } @@ -735,7 +757,7 @@ void CLASS_export(void) create_file(&fw, ".info#"); fputs(line, fw); fputc('\n', fw); - line = read_line(fr, NULL); + read_line(&line, &len); if (!line || *line == '#') break; } @@ -746,15 +768,15 @@ void CLASS_export(void) printf("Remove '%s' information from .info file\n", &line[1]); for(;;) { - line = read_line(fr, NULL); + read_line(&line, &len); if (!line || *line == '#') break; } } } - if (fr) - fclose(fr); + if (_buffer) + BUFFER_delete(&_buffer); close_file_and_rename(fw, ".info#", ".info"); return; diff --git a/main/gbc/gbc_output.c b/main/gbc/gbc_output.c index 552b14293..b92f8fbe9 100644 --- a/main/gbc/gbc_output.c +++ b/main/gbc/gbc_output.c @@ -179,8 +179,8 @@ static void flush_buffer(void) if (len <= 0) return; - - if (UNLIKELY(fwrite(_buffer, sizeof(char), len, _file) != len)) + + if (fwrite(_buffer, sizeof(char), len, _file) != len) THROW("Write error"); _pbuffer = _buffer; @@ -193,7 +193,7 @@ static void write_byte(unsigned char val) printf("%ld : b %u 0x%X\n", get_pos(), val, val); #endif - if (UNLIKELY(_pbuffer >= _mbuffer)) + if (_pbuffer >= _mbuffer) flush_buffer(); *_pbuffer++ = val; @@ -210,10 +210,10 @@ static void write_short(ushort val) printf("%ld : i %u 0x%X\n", get_pos(), val, val); #endif - if (UNLIKELY(_swap)) + if (_swap) SWAP_short((short *)&val); - if (UNLIKELY(_pbuffer >= _mbuffer)) + if (_pbuffer >= _mbuffer) flush_buffer(); *((ushort *)_pbuffer) = val; @@ -228,10 +228,10 @@ static void write_int(uint val) printf("%ld : l %lu 0x%lX\n", get_pos(), val, val); #endif - if (UNLIKELY(_swap)) + if (_swap) SWAP_int((int *)&val); - if (UNLIKELY(_pbuffer >= _mbuffer)) + if (_pbuffer >= _mbuffer) flush_buffer(); *((uint *)_pbuffer) = val; @@ -246,10 +246,10 @@ static void write_int64(uint64_t val) printf("%ld : l %llu 0x%llX\n", get_pos(), val, val); #endif - if (UNLIKELY(_swap)) + if (_swap) SWAP_int64((int64_t *)&val); - if (UNLIKELY(_pbuffer >= _mbuffer)) + if (_pbuffer >= _mbuffer) flush_buffer(); *((uint64_t *)_pbuffer) = val; @@ -263,7 +263,7 @@ static void write_string(const char *str, int len) printf("%ld : s \"%.*s\"\n", get_pos(), len, str); #endif - if (UNLIKELY(&_pbuffer[len] > _mbuffer)) + if (&_pbuffer[len] > _mbuffer) flush_buffer(); if (&_pbuffer[len] <= _mbuffer) @@ -275,7 +275,7 @@ static void write_string(const char *str, int len) return; } - if (UNLIKELY(fwrite(str, sizeof(char), len, _file) != len)) + if (fwrite(str, sizeof(char), len, _file) != len) THROW("Write error"); _pos += len; @@ -305,7 +305,7 @@ static void write_buffer(void *str, int len) flush_buffer(); - if (UNLIKELY(fwrite(str, sizeof(char), len, _file) != len)) + if (fwrite(str, sizeof(char), len, _file) != len) THROW("Write error"); _pos += len;