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)
%}
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");
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)
%}
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);
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<void(const std::string&)> process_line = std::function<void(const std::string&)>());
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);
}