diff options
Diffstat (limited to 'src/lib/netlist/plib/pstream.cpp')
-rw-r--r-- | src/lib/netlist/plib/pstream.cpp | 131 |
1 files changed, 105 insertions, 26 deletions
diff --git a/src/lib/netlist/plib/pstream.cpp b/src/lib/netlist/plib/pstream.cpp index a7acdaafb52..420f63c9e5e 100644 --- a/src/lib/netlist/plib/pstream.cpp +++ b/src/lib/netlist/plib/pstream.cpp @@ -8,9 +8,9 @@ #include "pstream.h" #include "palloc.h" -#include <algorithm> #include <cstdio> #include <cstdlib> +#include <algorithm> // VS2015 prefers _dup #ifdef _WIN32 @@ -21,14 +21,34 @@ namespace plib { +pstream::~pstream() +{ +} + // ----------------------------------------------------------------------------- // pistream: input stream // ----------------------------------------------------------------------------- +pistream::~pistream() +{ +} + // ----------------------------------------------------------------------------- // postream: output stream // ----------------------------------------------------------------------------- +postream::~postream() +{ +} + +void postream::write(pistream &strm) +{ + char buf[1024]; + pos_type r; + while ((r=strm.read(buf, 1024)) > 0) + write(buf, r); +} + // ----------------------------------------------------------------------------- // Input file stream // ----------------------------------------------------------------------------- @@ -71,7 +91,7 @@ pifilestream::~pifilestream() } } -pifilestream::pos_type pifilestream::vread(value_type *buf, const pos_type n) +pifilestream::pos_type pifilestream::vread(void *buf, const pos_type n) { pos_type r = fread(buf, 1, n, static_cast<FILE *>(m_file)); if (r < n) @@ -99,7 +119,7 @@ void pifilestream::vseek(const pos_type n) throw file_e("Generic file operation failed: {}", m_filename); } -pifilestream::pos_type pifilestream::vtell() const +pifilestream::pos_type pifilestream::vtell() { long ret = ftell(static_cast<FILE *>(m_file)); if (ret < 0) @@ -120,6 +140,10 @@ pstdin::pstdin() /* nothing to do */ } +pstdin::~pstdin() +{ +} + // ----------------------------------------------------------------------------- // Output file stream // ----------------------------------------------------------------------------- @@ -156,11 +180,12 @@ pofilestream::~pofilestream() } } -void pofilestream::vwrite(const value_type *buf, const pos_type n) +void pofilestream::vwrite(const void *buf, const pos_type n) { std::size_t r = fwrite(buf, 1, n, static_cast<FILE *>(m_file)); if (r < n) { + //printf("%ld %ld %s\n", r, n, strerror(errno)); if (ferror(static_cast<FILE *>(m_file))) throw file_write_e(m_filename); } @@ -179,7 +204,7 @@ void pofilestream::vseek(const pos_type n) } } -pstream::pos_type pofilestream::vtell() const +pstream::pos_type pofilestream::vtell() { std::ptrdiff_t ret = ftell(static_cast<FILE *>(m_file)); if (ret < 0) @@ -190,6 +215,11 @@ pstream::pos_type pofilestream::vtell() const return static_cast<pos_type>(ret); } +postringstream::~postringstream() +{ +} + + // ----------------------------------------------------------------------------- // pstderr: write to stderr // ----------------------------------------------------------------------------- @@ -203,6 +233,10 @@ pstderr::pstderr() { } +pstderr::~pstderr() +{ +} + // ----------------------------------------------------------------------------- // pstdout: write to stdout // ----------------------------------------------------------------------------- @@ -216,32 +250,35 @@ pstdout::pstdout() { } +pstdout::~pstdout() +{ +} + // ----------------------------------------------------------------------------- // Memory stream // ----------------------------------------------------------------------------- pimemstream::pimemstream(const void *mem, const pos_type len) - : pistream(FLAG_SEEKABLE), m_pos(0), m_len(len), m_mem(static_cast<const char *>(mem)) + : pistream(FLAG_SEEKABLE), m_pos(0), m_len(len), m_mem(static_cast<const pstring::mem_t *>(mem)) { } -pimemstream::pimemstream() - : pistream(FLAG_SEEKABLE), m_pos(0), m_len(0), m_mem(static_cast<const char *>(nullptr)) +pimemstream::pimemstream(const pomemstream &ostrm) +: pistream(FLAG_SEEKABLE), m_pos(0), m_len(ostrm.size()), m_mem(reinterpret_cast<pstring::mem_t *>(ostrm.memory())) { } -pimemstream::pimemstream(const pomemstream &ostrm) -: pistream(FLAG_SEEKABLE), m_pos(0), m_len(ostrm.size()), m_mem(reinterpret_cast<const char *>(ostrm.memory())) +pimemstream::~pimemstream() { } -pimemstream::pos_type pimemstream::vread(value_type *buf, const pos_type n) +pimemstream::pos_type pimemstream::vread(void *buf, const pos_type n) { pos_type ret = (m_pos + n <= m_len) ? n : m_len - m_pos; if (ret > 0) { - std::copy(m_mem + m_pos, m_mem + m_pos + ret, reinterpret_cast<char *>(buf)); + std::copy(m_mem + m_pos, m_mem + m_pos + ret, static_cast<char *>(buf)); m_pos += ret; } @@ -258,45 +295,78 @@ void pimemstream::vseek(const pos_type n) } -pimemstream::pos_type pimemstream::vtell() const +pimemstream::pos_type pimemstream::vtell() { return m_pos; } +pistringstream::~pistringstream() +{ +} + // ----------------------------------------------------------------------------- // Output memory stream // ----------------------------------------------------------------------------- pomemstream::pomemstream() -: postream(FLAG_SEEKABLE), m_pos(0), m_mem(1024) +: postream(FLAG_SEEKABLE), m_pos(0), m_capacity(1024), m_size(0) +{ + m_mem = palloc_array<char>(m_capacity); +} + +pomemstream::~pomemstream() { - m_mem.clear(); + pfree_array(m_mem); } -void pomemstream::vwrite(const value_type *buf, const pos_type n) +void pomemstream::vwrite(const void *buf, const pos_type n) { - if (m_pos + n >= m_mem.size()) - m_mem.resize(m_pos + n); + if (m_pos + n >= m_capacity) + { + while (m_pos + n >= m_capacity) + m_capacity *= 2; + char *o = m_mem; + m_mem = palloc_array<char>(m_capacity); + if (m_mem == nullptr) + { + throw out_of_mem_e("pomemstream::vwrite"); + } + std::copy(o, o + m_pos, m_mem); + pfree_array(o); + } - std::copy(buf, buf + n, &m_mem[0] + m_pos); + std::copy(static_cast<const char *>(buf), static_cast<const char *>(buf) + n, m_mem + m_pos); m_pos += n; + m_size = std::max(m_pos, m_size); } void pomemstream::vseek(const pos_type n) { m_pos = n; - if (m_pos>=m_mem.size()) - m_mem.resize(m_pos); + m_size = std::max(m_pos, m_size); + if (m_size >= m_capacity) + { + while (m_size >= m_capacity) + m_capacity *= 2; + char *o = m_mem; + m_mem = palloc_array<char>(m_capacity); + if (m_mem == nullptr) + { + throw out_of_mem_e("pomemstream::vseek"); + } + std::copy(o, o + m_pos, m_mem); + pfree_array(o); + } } -pstream::pos_type pomemstream::vtell() const +pstream::pos_type pomemstream::vtell() { return m_pos; } bool putf8_reader::readline(pstring &line) { - putf8string::code_t c = 0; + pstring::code_t c = 0; m_linebuf = ""; if (!this->readcode(c)) { @@ -308,14 +378,23 @@ bool putf8_reader::readline(pstring &line) if (c == 10) break; else if (c != 13) /* ignore CR */ - m_linebuf += putf8string(c); + m_linebuf += pstring(c); if (!this->readcode(c)) break; } - line = m_linebuf.c_str(); + line = m_linebuf; return true; } +putf8_fmt_writer::putf8_fmt_writer(postream &strm) +: pfmt_writer_t() +, putf8_writer(strm) +{ +} + +putf8_fmt_writer::~putf8_fmt_writer() +{ +} void putf8_fmt_writer::vdowrite(const pstring &ls) const { @@ -324,4 +403,4 @@ void putf8_fmt_writer::vdowrite(const pstring &ls) const -} // namespace plib +} |