diff options
author | 2016-07-09 00:13:18 +0200 | |
---|---|---|
committer | 2016-07-09 00:13:18 +0200 | |
commit | 011dbbe713a3faa07538c9c2130befdf7871e389 (patch) | |
tree | 4dd7150d527d6b6d6c2c7dd42e4b449953c305ec /src/lib/netlist/plib/pstream.cpp | |
parent | 3f58aa4548327df4a6223ed7bf84e614c2cb12e7 (diff) |
Moved two macros, added more RAII and improved exceptions. Fixed some
bugs in parser code. (nw)
Diffstat (limited to 'src/lib/netlist/plib/pstream.cpp')
-rw-r--r-- | src/lib/netlist/plib/pstream.cpp | 123 |
1 files changed, 47 insertions, 76 deletions
diff --git a/src/lib/netlist/plib/pstream.cpp b/src/lib/netlist/plib/pstream.cpp index 40152408b0c..9fcede380d5 100644 --- a/src/lib/netlist/plib/pstream.cpp +++ b/src/lib/netlist/plib/pstream.cpp @@ -21,8 +21,8 @@ namespace plib { bool pistream::readline(pstring &line) { char c = 0; - pstringbuffer buf; - if (!this->read(c)) + m_linebuf.clear(); + if (!this->readbyte(c)) { line = ""; return false; @@ -32,11 +32,11 @@ bool pistream::readline(pstring &line) if (c == 10) break; else if (c != 13) /* ignore CR */ - buf += c; - if (!this->read(c)) + m_linebuf += c; + if (!this->readbyte(c)) break; } - line = buf; + line = m_linebuf; return true; } @@ -48,7 +48,7 @@ void postream::write(pistream &strm) { char buf[1024]; unsigned r; - while ( !bad() && ((r=strm.read(buf, 1024)) > 0)) + while ((r=strm.read(buf, 1024)) > 0) write(buf, r); } @@ -57,48 +57,39 @@ void postream::write(pistream &strm) // ----------------------------------------------------------------------------- pifilestream::pifilestream(const pstring &fname) -: pistream(0), m_pos(0), m_actually_close(true) +: pistream(0) +, m_file(fopen(fname.cstr(), "rb")) +, m_pos(0) +, m_actually_close(true) +, m_filename(fname) { - init(fopen(fname.cstr(), "rb")); + if (m_file == nullptr) + throw file_open_e(fname); + init(); } -pifilestream::pifilestream(void *file, const bool do_close) -: pistream(0), m_pos(0), m_actually_close(do_close) +pifilestream::pifilestream(void *file, const pstring name, const bool do_close) +: pistream(0), m_file(file), m_pos(0), m_actually_close(do_close), m_filename(name) { - init(file); + if (m_file == nullptr) + throw null_argument_e(m_filename); + init(); } -void pifilestream::init(void *file) +void pifilestream::init() { - m_file = file; - if (m_file == nullptr) - { - set_flag(FLAG_ERROR); - set_flag(FLAG_EOF); - set_flag(FLAG_CLOSED); - } - else + if (ftell((FILE *) m_file) >= 0) { - if (ftell((FILE *) m_file) >= 0) - { - if (fseek((FILE *) m_file, 0, SEEK_SET) >= 0) - set_flag(FLAG_SEEKABLE); - } + if (fseek((FILE *) m_file, 0, SEEK_SET) >= 0) + set_flag(FLAG_SEEKABLE); } } pifilestream::~pifilestream() { - if (!closed()) - close(); -} - -void pifilestream::close() -{ if (m_actually_close) { fclose((FILE *) m_file); - set_flag(FLAG_CLOSED); } } @@ -110,7 +101,7 @@ unsigned pifilestream::vread(void *buf, const unsigned n) if (feof((FILE *) m_file)) set_flag(FLAG_EOF); if (ferror((FILE *) m_file)) - set_flag(FLAG_ERROR); + throw file_read_e(m_filename); } m_pos += r; return r; @@ -118,9 +109,8 @@ unsigned pifilestream::vread(void *buf, const unsigned n) void pifilestream::vseek(const pos_type n) { - check_seekable(); if (fseek((FILE *) m_file, SEEK_SET, n) < 0) - set_flag(FLAG_ERROR); + throw file_e("File seek failed: {}", m_filename); else m_pos = n; if (feof((FILE *) m_file)) @@ -128,7 +118,7 @@ void pifilestream::vseek(const pos_type n) else clear_flag(FLAG_EOF); if (ferror((FILE *) m_file)) - set_flag(FLAG_ERROR); + throw file_e("Generic file operation failed: {}", m_filename); } pifilestream::pos_type pifilestream::vtell() @@ -147,7 +137,7 @@ pifilestream::pos_type pifilestream::vtell() // ----------------------------------------------------------------------------- pstdin::pstdin() -: pifilestream(stdin, false) +: pifilestream(stdin, "<stdin>", false) { /* nothing to do */ } @@ -157,48 +147,32 @@ pstdin::pstdin() // ----------------------------------------------------------------------------- pofilestream::pofilestream(const pstring &fname) -: postream(0), m_pos(0), m_actually_close(true) +: postream(0), m_file(fopen(fname.cstr(), "wb")), m_pos(0), m_actually_close(true), m_filename(fname) { - init(fopen(fname.cstr(), "wb")); -} - -pofilestream::pofilestream(void *file, const bool do_close) -: postream(0), m_pos(0), m_actually_close(do_close) -{ - init(file); + if (m_file == nullptr) + throw file_open_e(m_filename); + init(); } -void pofilestream::init(void *file) +pofilestream::pofilestream(void *file, const pstring name, const bool do_close) +: postream(0), m_file(file), m_pos(0), m_actually_close(do_close), m_filename(name) { - m_file = file; if (m_file == nullptr) - { - set_flag(FLAG_ERROR); - set_flag(FLAG_CLOSED); - } - else - { - if (ftell((FILE *) m_file) >= 0) - { - if (fseek((FILE *) m_file, 0, SEEK_SET) >= 0) - set_flag(FLAG_SEEKABLE); - } - } + throw null_argument_e(m_filename); + init(); } -pofilestream::~pofilestream() +void pofilestream::init() { - if (!closed()) - close(); + if (ftell((FILE *) m_file) >= 0) + if (fseek((FILE *) m_file, 0, SEEK_SET) >= 0) + set_flag(FLAG_SEEKABLE); } -void pofilestream::close() +pofilestream::~pofilestream() { if (m_actually_close) - { fclose((FILE *) m_file); - set_flag(FLAG_CLOSED); - } } void pofilestream::vwrite(const void *buf, const unsigned n) @@ -207,21 +181,20 @@ void pofilestream::vwrite(const void *buf, const unsigned n) if (r < n) { if (ferror((FILE *) m_file)) - set_flag(FLAG_ERROR); + throw file_write_e(m_filename); } m_pos += r; } void pofilestream::vseek(const pos_type n) { - check_seekable(); if (fseek((FILE *) m_file, SEEK_SET, n) < 0) - set_flag(FLAG_ERROR); + throw file_e("File seek failed: {}", m_filename); else { m_pos = n; if (ferror((FILE *) m_file)) - set_flag(FLAG_ERROR); + throw file_e("Generic file operation failed: {}", m_filename); } } @@ -241,7 +214,7 @@ pstream::pos_type pofilestream::vtell() // ----------------------------------------------------------------------------- pstderr::pstderr() -: pofilestream(stderr, false) +: pofilestream(stderr, "<stderr>", false) { } @@ -250,7 +223,7 @@ pstderr::pstderr() // ----------------------------------------------------------------------------- pstdout::pstdout() -: pofilestream(stdout, false) +: pofilestream(stdout, "<stdout>", false) { } @@ -325,8 +298,7 @@ void pomemstream::vwrite(const void *buf, const unsigned n) m_mem = palloc_array<char>(m_capacity); if (m_mem == nullptr) { - set_flag(FLAG_ERROR); - return; + throw out_of_mem_e("pomemstream::vwrite"); } memcpy(m_mem, o, m_pos); pfree_array(o); @@ -349,8 +321,7 @@ void pomemstream::vseek(const pos_type n) m_mem = palloc_array<char>(m_capacity); if (m_mem == nullptr) { - set_flag(FLAG_ERROR); - return; + throw out_of_mem_e("pomemstream::vseek"); } memcpy(m_mem, o, m_pos); pfree_array(o); |