diff options
author | 2020-09-05 19:43:54 +0200 | |
---|---|---|
committer | 2020-09-05 21:31:49 +0200 | |
commit | f3eb6324652fea263972087146fbdde9e32f9a0f (patch) | |
tree | f63d352948c3a4fcf62857839cc5c5359804b0f5 /src/lib/netlist/plib/pstream.h | |
parent | 4dd7e21f565b12e8487b884089d769f0b30147c6 (diff) |
netlist: code maintenance and performance optimizations.
* rename some misleading type names
* remove callback_t and replace by better scalable approach
* hide implementations details
* move sources classes from putil.h to psources.h
* reduce code complexity
* improve parsing performance, parsing netlists now is twice as fast.
* fix issues around multi-byte string support
* moved psplit into pstrutil.h
Diffstat (limited to 'src/lib/netlist/plib/pstream.h')
-rw-r--r-- | src/lib/netlist/plib/pstream.h | 73 |
1 files changed, 58 insertions, 15 deletions
diff --git a/src/lib/netlist/plib/pstream.h b/src/lib/netlist/plib/pstream.h index ebaacfcfb73..a636d325541 100644 --- a/src/lib/netlist/plib/pstream.h +++ b/src/lib/netlist/plib/pstream.h @@ -47,6 +47,44 @@ namespace plib { return os.write(reinterpret_cast<const ct *>(data), gsl::narrow<std::streamsize>(len * sizeof(T))); } + /// \brief a named istream pointer container + /// + /// This moveable object allows to pass istream unique pointers with + /// information about the origin (filename). This is useful in error + /// reporting where the source of the stream has to be logged. + /// + struct istream_uptr + { + explicit istream_uptr() = default; + + istream_uptr(std::unique_ptr<std::istream> &&strm, const pstring &filename) + : m_strm(std::move(strm)) + , m_filename(filename) + { + } + istream_uptr(const istream_uptr &) = delete; + istream_uptr &operator=(const istream_uptr &) = delete; + istream_uptr(istream_uptr &&rhs) /*noexcept*/ + { + m_strm = std::move(rhs.m_strm); + m_filename = rhs.m_filename; + } + istream_uptr &operator=(istream_uptr &&) /*noexcept*/ = delete; + + std::istream * operator ->() noexcept { return m_strm.get(); } + std::istream & operator *() noexcept { return *m_strm; } + pstring filename() { return m_filename; } + + bool empty() { return m_strm == nullptr; } + + // FIXME: workaround input context should accept stream_ptr + + std::unique_ptr<std::istream> release_stream() { return std::move(m_strm); } + private: + std::unique_ptr<std::istream> m_strm; + pstring m_filename; + }; + /// /// /// putf8reader_t digests linux & dos/windows text files @@ -61,7 +99,6 @@ public: putf8_reader(putf8_reader &&rhs) noexcept : m_strm(std::move(rhs.m_strm)) - , m_linebuf(std::move(rhs.m_linebuf)) { } @@ -81,13 +118,12 @@ public: /// \param line pstring reference to the result /// \returns Returns false if at end of file /// - bool readline(pstring &line) + bool readline(putf8string &line) { putf8string::code_t c = 0; - m_linebuf = putf8string(""); + line = ""; if (!this->readcode(c)) { - line = ""; return false; } while (true) @@ -95,11 +131,10 @@ public: if (c == 10) break; if (c != 13) // ignore CR - m_linebuf += putf8string(1, c); + line += putf8string(1, c); if (!this->readcode(c)) break; } - line = m_linebuf; return true; } @@ -110,25 +145,23 @@ public: /// \param line pstring reference to the result /// \returns Returns false if at end of file /// - bool readline_lf(pstring &line) + bool readline_lf(putf8string &line) { putf8string::code_t c = 0; - m_linebuf = putf8string(""); + line = ""; if (!this->readcode(c)) { - line = ""; return false; } while (true) { if (c != 13) // ignore CR - m_linebuf += putf8string(1, c); + line += putf8string(1, c); if (c == 10) break; if (!this->readcode(c)) break; } - line = m_linebuf; return true; } @@ -164,7 +197,6 @@ public: std::istream &stream() { return *m_strm; } private: std::unique_ptr<std::istream> m_strm; - putf8string m_linebuf; }; // ----------------------------------------------------------------------------- @@ -194,7 +226,7 @@ public: // NOLINTNEXTLINE(performance-unnecessary-copy-initialization) const putf8string conv_utf8(text); //m_strm->write(conv_utf8.c_str(), static_cast<std::streamsize>(plib::strlen(conv_utf8.c_str() ))); - ostream_write(*m_strm, conv_utf8.c_str(), string_info<pstring>::mem_size(conv_utf8)); + ostream_write(*m_strm, conv_utf8.c_str(), string_info<putf8string>::mem_size(conv_utf8)); } void write(const pstring::value_type c) const @@ -255,7 +287,8 @@ public: void write(const pstring &s) { const auto *sm = s.c_str(); - const auto sl(std::char_traits<pstring::mem_t>::length(sm)); + //const auto sl(std::char_traits<pstring::mem_t>::length(sm)); + const auto sl(string_info<pstring>::mem_size(s)); write(sl); ostream_write(m_strm, sm, sl); } @@ -293,7 +326,7 @@ public: { std::size_t sz = 0; read(sz); - std::vector<plib::string_info<pstring>::mem_t> buf(sz+1); + std::vector<plib::string_info<putf8string>::mem_t> buf(sz+1); m_strm.read(buf.data(), static_cast<std::streamsize>(sz)); buf[sz] = 0; s = pstring(buf.data()); @@ -338,6 +371,11 @@ public: : std::ifstream(filename_type(name).c_str(), mode) { } + + explicit ifstream(const std::string name, ios_base::openmode mode = ios_base::in) + : std::ifstream(filename_type(name).c_str(), mode) + { + } }; /// @@ -354,6 +392,11 @@ public: : std::ofstream(filename_type(name).c_str(), mode) { } + + explicit ofstream(const std::string name, ios_base::openmode mode = ios_base::out | ios_base::trunc) + : std::ofstream(filename_type(name).c_str(), mode) + { + } }; |