From c5eb5e56b8911bb520a987761739bbb9d9328380 Mon Sep 17 00:00:00 2001 From: Clifford Wolf Date: Thu, 23 Oct 2014 10:47:21 +0200 Subject: [PATCH] Re-introduced Yosys::readsome() helper function (f.read() + f.gcount() made problems with lines > 16kB) --- frontends/ilang/ilang_lexer.l | 6 +----- frontends/verilog/preproc.cc | 8 +++----- frontends/verilog/verilog_lexer.l | 6 +----- kernel/yosys.cc | 16 ++++++++++++++++ kernel/yosys.h | 1 + passes/cmds/write_file.cc | 9 +++------ 6 files changed, 25 insertions(+), 21 deletions(-) diff --git a/frontends/ilang/ilang_lexer.l b/frontends/ilang/ilang_lexer.l index dcbc6b6d7..ace992fbd 100644 --- a/frontends/ilang/ilang_lexer.l +++ b/frontends/ilang/ilang_lexer.l @@ -35,11 +35,7 @@ USING_YOSYS_NAMESPACE #define YY_INPUT(buf,result,max_size) \ - do { \ - ILANG_FRONTEND::lexin->read(buf, max_size-1); \ - result = ILANG_FRONTEND::lexin->gcount(); \ - if (result >= 0) buf[result] = '\0'; \ - } while (0) + result = readsome(*ILANG_FRONTEND::lexin, buf, max_size) %} diff --git a/frontends/verilog/preproc.cc b/frontends/verilog/preproc.cc index da658410d..b4e77c31b 100644 --- a/frontends/verilog/preproc.cc +++ b/frontends/verilog/preproc.cc @@ -196,16 +196,14 @@ static std::string next_token(bool pass_newline = false) static void input_file(std::istream &f, std::string filename) { char buffer[513]; + int rc; insert_input(""); auto it = input_buffer.begin(); input_buffer.insert(it, "`file_push " + filename + "\n"); - while (1) { - f.read(buffer, sizeof(buffer)-1); - if (f.gcount() <= 0) - break; - buffer[f.gcount()] = 0; + while ((rc = readsome(f, buffer, sizeof(buffer)-1)) > 0) { + buffer[rc] = 0; input_buffer.insert(it, buffer); } input_buffer.insert(it, "\n`file_pop\n"); diff --git a/frontends/verilog/verilog_lexer.l b/frontends/verilog/verilog_lexer.l index 0d28e2e7f..ae16ebf78 100644 --- a/frontends/verilog/verilog_lexer.l +++ b/frontends/verilog/verilog_lexer.l @@ -64,11 +64,7 @@ YOSYS_NAMESPACE_END return TOK_ID; #define YY_INPUT(buf,result,max_size) \ - do { \ - lexin->read(buf, max_size-1); \ - result = lexin->gcount(); \ - if (result >= 0) buf[result] = '\0'; \ - } while (0) + result = readsome(*VERILOG_FRONTEND::lexin, buf, max_size) %} diff --git a/kernel/yosys.cc b/kernel/yosys.cc index ad0aa5a6d..d4365ee00 100644 --- a/kernel/yosys.cc +++ b/kernel/yosys.cc @@ -97,6 +97,22 @@ std::string vstringf(const char *fmt, va_list ap) return string; } +int readsome(std::istream &f, char *s, int n) +{ + int rc = f.readsome(s, n); + + // f.readsome() sometimes returns 0 on a non-empty stream.. + if (rc == 0) { + int c = f.get(); + if (c != EOF) { + *s = c; + rc = 1; + } + } + + return rc; +} + std::string next_token(std::string &text, const char *sep) { size_t pos_begin = text.find_first_not_of(sep); diff --git a/kernel/yosys.h b/kernel/yosys.h index b9182c1df..11f356adc 100644 --- a/kernel/yosys.h +++ b/kernel/yosys.h @@ -127,6 +127,7 @@ namespace RTLIL { std::string stringf(const char *fmt, ...) __attribute__((format(printf, 1, 2))); std::string vstringf(const char *fmt, va_list ap); +int readsome(std::istream &f, char *s, int n); std::string next_token(std::string &text, const char *sep); bool patmatch(const char *pattern, const char *string); int run_command(const std::string &command, std::function process_line = std::function()); diff --git a/passes/cmds/write_file.cc b/passes/cmds/write_file.cc index 9bf1a75a8..25ec4acc2 100644 --- a/passes/cmds/write_file.cc +++ b/passes/cmds/write_file.cc @@ -68,13 +68,10 @@ struct WriteFileFrontend : public Frontend { FILE *of = fopen(output_filename.c_str(), append_mode ? "a" : "w"); char buffer[64 * 1024]; + int bytes; - while (1) { - f->read(buffer, sizeof(buffer)); - if (f->gcount() <= 0) - break; - fwrite(buffer, f->gcount(), 1, of); - } + while (0 < (bytes = readsome(*f, buffer, sizeof(buffer)))) + fwrite(buffer, bytes, 1, of); fclose(of); } -- 2.30.2