diff options
author | 2019-09-17 21:05:01 +0200 | |
---|---|---|
committer | 2019-09-17 21:05:01 +0200 | |
commit | 500ca5b8fc6cd17e224f154de0914e95f080f51e (patch) | |
tree | 8708ae27ba19ab8164bef760577ef269e11e631b /src/lib/netlist/plib/pstream.cpp | |
parent | 4495c351139f13d46e087c448e3b2418491485e2 (diff) |
netlist code maintenance. (nw)
- remove a lot of c library use and instead use c++
- improved pstring compatibility to std::string
- prepare removal of pstream
Diffstat (limited to 'src/lib/netlist/plib/pstream.cpp')
-rw-r--r-- | src/lib/netlist/plib/pstream.cpp | 81 |
1 files changed, 44 insertions, 37 deletions
diff --git a/src/lib/netlist/plib/pstream.cpp b/src/lib/netlist/plib/pstream.cpp index a7acdaafb52..d06e19aff94 100644 --- a/src/lib/netlist/plib/pstream.cpp +++ b/src/lib/netlist/plib/pstream.cpp @@ -10,7 +10,7 @@ #include <algorithm> #include <cstdio> -#include <cstdlib> +//#include <cstdlib> // VS2015 prefers _dup #ifdef _WIN32 @@ -71,7 +71,7 @@ pifilestream::~pifilestream() } } -pifilestream::pos_type pifilestream::vread(value_type *buf, const pos_type n) +pifilestream::pos_type pifilestream::vread(char_type *buf, const pos_type n) { pos_type r = fread(buf, 1, n, static_cast<FILE *>(m_file)); if (r < n) @@ -124,6 +124,8 @@ pstdin::pstdin() // Output file stream // ----------------------------------------------------------------------------- +#if !USE_CSTREAM + pofilestream::pofilestream(const pstring &fname) : postream(0), m_file(fopen(fname.c_str(), "wb")), m_pos(0), m_actually_close(true), m_filename(fname) { @@ -156,7 +158,7 @@ pofilestream::~pofilestream() } } -void pofilestream::vwrite(const value_type *buf, const pos_type n) +void pofilestream::vwrite(const char_type *buf, const pos_type n) { std::size_t r = fwrite(buf, 1, n, static_cast<FILE *>(m_file)); if (r < n) @@ -191,9 +193,43 @@ pstream::pos_type pofilestream::vtell() const } // ----------------------------------------------------------------------------- +// Output memory stream +// ----------------------------------------------------------------------------- + +pomemstream::pomemstream() +: postream(FLAG_SEEKABLE), m_pos(0), m_mem(1024) +{ + m_mem.clear(); +} + +void pomemstream::vwrite(const char_type *buf, const pos_type n) +{ + if (m_pos + n >= m_mem.size()) + m_mem.resize(m_pos + n); + + std::copy(buf, buf + n, &m_mem[0] + m_pos); + m_pos += n; +} + +void pomemstream::vseek(const pos_type n) +{ + m_pos = n; + if (m_pos>=m_mem.size()) + m_mem.resize(m_pos); +} + +pstream::pos_type pomemstream::vtell() const +{ + return m_pos; +} + +#endif + +// ----------------------------------------------------------------------------- // pstderr: write to stderr // ----------------------------------------------------------------------------- +#if 1 pstderr::pstderr() #ifdef _WIN32 : pofilestream(fdopen(_dup(fileno(stderr)), "wb"), "<stderr>", true) @@ -215,7 +251,7 @@ pstdout::pstdout() #endif { } - +#endif // ----------------------------------------------------------------------------- // Memory stream // ----------------------------------------------------------------------------- @@ -229,13 +265,14 @@ pimemstream::pimemstream() : pistream(FLAG_SEEKABLE), m_pos(0), m_len(0), m_mem(static_cast<const char *>(nullptr)) { } - +#if 0 pimemstream::pimemstream(const pomemstream &ostrm) : pistream(FLAG_SEEKABLE), m_pos(0), m_len(ostrm.size()), m_mem(reinterpret_cast<const char *>(ostrm.memory())) { } +#endif -pimemstream::pos_type pimemstream::vread(value_type *buf, const pos_type n) +pimemstream::pos_type pimemstream::vread(char_type *buf, const pos_type n) { pos_type ret = (m_pos + n <= m_len) ? n : m_len - m_pos; @@ -263,36 +300,6 @@ pimemstream::pos_type pimemstream::vtell() const return m_pos; } -// ----------------------------------------------------------------------------- -// Output memory stream -// ----------------------------------------------------------------------------- - -pomemstream::pomemstream() -: postream(FLAG_SEEKABLE), m_pos(0), m_mem(1024) -{ - m_mem.clear(); -} - -void pomemstream::vwrite(const value_type *buf, const pos_type n) -{ - if (m_pos + n >= m_mem.size()) - m_mem.resize(m_pos + n); - - std::copy(buf, buf + n, &m_mem[0] + m_pos); - m_pos += n; -} - -void pomemstream::vseek(const pos_type n) -{ - m_pos = n; - if (m_pos>=m_mem.size()) - m_mem.resize(m_pos); -} - -pstream::pos_type pomemstream::vtell() const -{ - return m_pos; -} bool putf8_reader::readline(pstring &line) { @@ -308,7 +315,7 @@ bool putf8_reader::readline(pstring &line) if (c == 10) break; else if (c != 13) /* ignore CR */ - m_linebuf += putf8string(c); + m_linebuf += putf8string(1, c); if (!this->readcode(c)) break; } |