mirror of
https://github.com/holub/mame
synced 2025-10-04 16:34:53 +03:00
Improve dealing ownership in pstreams. (nw)
I am not really happy with this. But I am missing some creativity currently.
This commit is contained in:
parent
66de2ce663
commit
633528eb31
@ -19,8 +19,8 @@ namespace netlist
|
||||
{
|
||||
NETLIB_CONSTRUCTOR(log)
|
||||
, m_I(*this, "I")
|
||||
, m_strm(plib::pfmt("{1}.log")(this->name()))
|
||||
, m_writer(m_strm)
|
||||
, m_strm(pstring(plib::pfmt("{1}.log")(this->name())))
|
||||
, m_writer(&m_strm)
|
||||
{
|
||||
}
|
||||
|
||||
|
@ -16,8 +16,8 @@ namespace netlist
|
||||
class parser_t : public plib::ptokenizer
|
||||
{
|
||||
public:
|
||||
parser_t(plib::putf8_reader &strm, setup_t &setup)
|
||||
: plib::ptokenizer(strm), m_setup(setup) {}
|
||||
parser_t(plib::putf8_reader &&strm, setup_t &setup)
|
||||
: plib::ptokenizer(std::move(strm)), m_setup(setup) {}
|
||||
|
||||
bool parse(const pstring &nlname = "");
|
||||
|
||||
|
@ -965,15 +965,15 @@ std::unique_ptr<plib::pistream> setup_t::get_data_stream(const pstring &name)
|
||||
}
|
||||
|
||||
|
||||
bool setup_t::parse_stream(plib::putf8_reader &istrm, const pstring &name)
|
||||
bool setup_t::parse_stream(plib::putf8_reader &&istrm, const pstring &name)
|
||||
{
|
||||
plib::pomemstream ostrm;
|
||||
plib::putf8_writer owrt(ostrm);
|
||||
plib::putf8_writer owrt(&ostrm);
|
||||
|
||||
plib::ppreprocessor(&m_defines).process(istrm, owrt);
|
||||
plib::pimemstream istrm2(ostrm);
|
||||
plib::putf8_reader reader2(istrm2);
|
||||
return parser_t(reader2, *this).parse(name);
|
||||
plib::putf8_reader reader2 = plib::putf8_reader(plib::pimemstream(ostrm));
|
||||
|
||||
return parser_t(std::move(reader2), *this).parse(name);
|
||||
}
|
||||
|
||||
void setup_t::register_define(const pstring &defstr)
|
||||
@ -996,8 +996,8 @@ bool source_t::parse(const pstring &name)
|
||||
else
|
||||
{
|
||||
auto rstream = stream(name);
|
||||
plib::putf8_reader reader(*rstream);
|
||||
return m_setup.parse_stream(reader, name);
|
||||
plib::putf8_reader reader(rstream.get());
|
||||
return m_setup.parse_stream(std::move(reader), name);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -259,7 +259,7 @@ namespace netlist
|
||||
|
||||
std::unique_ptr<plib::pistream> get_data_stream(const pstring &name);
|
||||
|
||||
bool parse_stream(plib::putf8_reader &istrm, const pstring &name);
|
||||
bool parse_stream(plib::putf8_reader &&istrm, const pstring &name);
|
||||
|
||||
/* register a source */
|
||||
|
||||
|
@ -37,8 +37,8 @@ namespace plib {
|
||||
: options()
|
||||
, pout_strm()
|
||||
, perr_strm()
|
||||
, pout(pout_strm)
|
||||
, perr(perr_strm)
|
||||
, pout(&pout_strm)
|
||||
, perr(&perr_strm)
|
||||
{
|
||||
|
||||
}
|
||||
|
@ -16,8 +16,8 @@ namespace plib {
|
||||
// A simple tokenizer
|
||||
// ----------------------------------------------------------------------------------------
|
||||
|
||||
ptokenizer::ptokenizer(plib::putf8_reader &strm)
|
||||
: m_strm(strm), m_lineno(0), m_cur_line(""), m_px(m_cur_line.begin()), m_unget(0), m_string('"')
|
||||
ptokenizer::ptokenizer(plib::putf8_reader &&strm)
|
||||
: m_strm(std::move(strm)), m_lineno(0), m_cur_line(""), m_px(m_cur_line.begin()), m_unget(0), m_string('"')
|
||||
{
|
||||
}
|
||||
|
||||
|
@ -19,7 +19,7 @@ namespace plib {
|
||||
class ptokenizer : nocopyassignmove
|
||||
{
|
||||
public:
|
||||
explicit ptokenizer(plib::putf8_reader &strm);
|
||||
ptokenizer(plib::putf8_reader &&strm);
|
||||
|
||||
virtual ~ptokenizer();
|
||||
|
||||
@ -123,7 +123,7 @@ private:
|
||||
|
||||
bool eof() { return m_strm.eof(); }
|
||||
|
||||
putf8_reader &m_strm;
|
||||
putf8_reader m_strm;
|
||||
|
||||
int m_lineno;
|
||||
pstring m_cur_line;
|
||||
|
@ -41,14 +41,6 @@ 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
|
||||
// -----------------------------------------------------------------------------
|
||||
@ -119,7 +111,7 @@ void pifilestream::vseek(const pos_type n)
|
||||
throw file_e("Generic file operation failed: {}", m_filename);
|
||||
}
|
||||
|
||||
pifilestream::pos_type pifilestream::vtell()
|
||||
pifilestream::pos_type pifilestream::vtell() const
|
||||
{
|
||||
long ret = ftell(static_cast<FILE *>(m_file));
|
||||
if (ret < 0)
|
||||
@ -204,7 +196,7 @@ void pofilestream::vseek(const pos_type n)
|
||||
}
|
||||
}
|
||||
|
||||
pstream::pos_type pofilestream::vtell()
|
||||
pstream::pos_type pofilestream::vtell() const
|
||||
{
|
||||
std::ptrdiff_t ret = ftell(static_cast<FILE *>(m_file));
|
||||
if (ret < 0)
|
||||
@ -263,6 +255,11 @@ pimemstream::pimemstream(const void *mem, const pos_type len)
|
||||
{
|
||||
}
|
||||
|
||||
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<const char *>(ostrm.memory()))
|
||||
{
|
||||
@ -295,7 +292,7 @@ void pimemstream::vseek(const pos_type n)
|
||||
|
||||
}
|
||||
|
||||
pimemstream::pos_type pimemstream::vtell()
|
||||
pimemstream::pos_type pimemstream::vtell() const
|
||||
{
|
||||
return m_pos;
|
||||
}
|
||||
@ -316,7 +313,8 @@ pomemstream::pomemstream()
|
||||
|
||||
pomemstream::~pomemstream()
|
||||
{
|
||||
pfree_array(m_mem);
|
||||
if (m_mem != nullptr)
|
||||
pfree_array(m_mem);
|
||||
}
|
||||
|
||||
void pomemstream::vwrite(const void *buf, const pos_type n)
|
||||
@ -359,7 +357,7 @@ void pomemstream::vseek(const pos_type n)
|
||||
}
|
||||
}
|
||||
|
||||
pstream::pos_type pomemstream::vtell()
|
||||
pstream::pos_type pomemstream::vtell() const
|
||||
{
|
||||
return m_pos;
|
||||
}
|
||||
@ -386,11 +384,6 @@ bool putf8_reader::readline(pstring &line)
|
||||
return true;
|
||||
}
|
||||
|
||||
putf8_fmt_writer::putf8_fmt_writer(postream &strm)
|
||||
: pfmt_writer_t()
|
||||
, putf8_writer(strm)
|
||||
{
|
||||
}
|
||||
|
||||
putf8_fmt_writer::~putf8_fmt_writer()
|
||||
{
|
||||
|
@ -11,30 +11,51 @@
|
||||
#include "pstring.h"
|
||||
#include "pfmtlog.h"
|
||||
#include "pexception.h"
|
||||
#include "palloc.h"
|
||||
|
||||
#define USE_CSTREAM (0)
|
||||
|
||||
#include <vector>
|
||||
#include <type_traits>
|
||||
|
||||
#if USE_CSTREAM
|
||||
#include <fstream>
|
||||
//#include <strstream>
|
||||
#include <sstream>
|
||||
#endif
|
||||
|
||||
|
||||
namespace plib {
|
||||
|
||||
#if USE_CSTREAM
|
||||
typedef std::ostream postream;
|
||||
typedef std::ofstream pofilestream;
|
||||
typedef std::ostringstream postringstream;
|
||||
typedef std::ostringstream pomemstream;
|
||||
|
||||
#endif
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
// pstream: things common to all streams
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
class pstream : nocopyassignmove
|
||||
class pstream : nocopyassign
|
||||
{
|
||||
public:
|
||||
|
||||
using pos_type = std::size_t;
|
||||
using size_type = std::size_t;
|
||||
|
||||
static constexpr pos_type SEEK_EOF = static_cast<pos_type>(-1);
|
||||
|
||||
bool seekable() const { return ((m_flags & FLAG_SEEKABLE) != 0); }
|
||||
|
||||
void seek(const pos_type n)
|
||||
void seekp(const pos_type n)
|
||||
{
|
||||
return vseek(n);
|
||||
vseek(n);
|
||||
}
|
||||
|
||||
pos_type tell()
|
||||
pos_type tellp() const
|
||||
{
|
||||
return vtell();
|
||||
}
|
||||
@ -43,10 +64,13 @@ protected:
|
||||
explicit pstream(const unsigned flags) : m_flags(flags)
|
||||
{
|
||||
}
|
||||
pstream(pstream &&src) : m_flags(src.m_flags)
|
||||
{
|
||||
}
|
||||
~pstream();
|
||||
|
||||
virtual void vseek(const pos_type n) = 0;
|
||||
virtual pos_type vtell() = 0;
|
||||
virtual pos_type vtell() const = 0;
|
||||
|
||||
static constexpr unsigned FLAG_EOF = 0x01;
|
||||
static constexpr unsigned FLAG_SEEKABLE = 0x04;
|
||||
@ -84,8 +108,9 @@ public:
|
||||
|
||||
protected:
|
||||
explicit pistream(const unsigned flags) : pstream(flags) {}
|
||||
explicit pistream(pistream &&src) : pstream(std::move(src)) {}
|
||||
/* read up to n bytes from stream */
|
||||
virtual pos_type vread(void *buf, const pos_type n) = 0;
|
||||
virtual size_type vread(void *buf, const size_type n) = 0;
|
||||
|
||||
};
|
||||
|
||||
@ -93,23 +118,23 @@ protected:
|
||||
// postream: output stream
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
#if !USE_CSTREAM
|
||||
class postream : public pstream
|
||||
{
|
||||
public:
|
||||
|
||||
virtual ~postream();
|
||||
|
||||
void write(const void *buf, const pos_type n)
|
||||
void write(const char *buf, const size_type n)
|
||||
{
|
||||
vwrite(buf, n);
|
||||
}
|
||||
|
||||
void write(pistream &strm);
|
||||
|
||||
protected:
|
||||
explicit postream(unsigned flags) : pstream(flags) {}
|
||||
explicit postream(postream &&src) : pstream(std::move(src)) {}
|
||||
/* write n bytes to stream */
|
||||
virtual void vwrite(const void *buf, const pos_type n) = 0;
|
||||
virtual void vwrite(const void *buf, const size_type n) = 0;
|
||||
|
||||
private:
|
||||
};
|
||||
@ -123,6 +148,17 @@ class pomemstream : public postream
|
||||
public:
|
||||
|
||||
pomemstream();
|
||||
|
||||
pomemstream(pomemstream &&src)
|
||||
: postream(std::move(src))
|
||||
, m_pos(src.m_pos)
|
||||
, m_capacity(src.m_capacity)
|
||||
, m_size(src.m_size)
|
||||
, m_mem(src.m_mem)
|
||||
{
|
||||
src.m_mem = nullptr;
|
||||
}
|
||||
|
||||
virtual ~pomemstream() override;
|
||||
|
||||
char *memory() const { return m_mem; }
|
||||
@ -132,7 +168,7 @@ protected:
|
||||
/* write n bytes to stream */
|
||||
virtual void vwrite(const void *buf, const pos_type) override;
|
||||
virtual void vseek(const pos_type n) override;
|
||||
virtual pos_type vtell() override;
|
||||
virtual pos_type vtell() const override;
|
||||
|
||||
private:
|
||||
pos_type m_pos;
|
||||
@ -146,6 +182,11 @@ class postringstream : public postream
|
||||
public:
|
||||
|
||||
postringstream() : postream(0) { }
|
||||
postringstream(postringstream &&src)
|
||||
: postream(std::move(src))
|
||||
, m_buf(src.m_buf)
|
||||
{ src.m_buf = ""; }
|
||||
|
||||
virtual ~postringstream() override;
|
||||
|
||||
const pstring &str() { return m_buf; }
|
||||
@ -157,7 +198,7 @@ protected:
|
||||
m_buf += pstring(static_cast<const char *>(buf), n);
|
||||
}
|
||||
virtual void vseek(const pos_type n) override { }
|
||||
virtual pos_type vtell() override { return m_buf.size(); }
|
||||
virtual pos_type vtell() const override { return m_buf.size(); }
|
||||
|
||||
private:
|
||||
pstring m_buf;
|
||||
@ -171,7 +212,18 @@ class pofilestream : public postream
|
||||
{
|
||||
public:
|
||||
|
||||
explicit pofilestream(const pstring &fname);
|
||||
pofilestream(const pstring &fname);
|
||||
pofilestream(pofilestream &&src)
|
||||
: postream(std::move(src))
|
||||
, m_file(src.m_file)
|
||||
, m_pos(src.m_pos)
|
||||
, m_actually_close(src.m_actually_close)
|
||||
, m_filename(src.m_filename)
|
||||
{
|
||||
src.m_file = nullptr;
|
||||
src.m_actually_close = false;
|
||||
}
|
||||
|
||||
virtual ~pofilestream() override;
|
||||
|
||||
protected:
|
||||
@ -179,7 +231,7 @@ protected:
|
||||
/* write n bytes to stream */
|
||||
virtual void vwrite(const void *buf, const pos_type n) override;
|
||||
virtual void vseek(const pos_type n) override;
|
||||
virtual pos_type vtell() override;
|
||||
virtual pos_type vtell() const override;
|
||||
|
||||
private:
|
||||
void *m_file;
|
||||
@ -193,6 +245,7 @@ private:
|
||||
// -----------------------------------------------------------------------------
|
||||
// pstderr: write to stderr
|
||||
// -----------------------------------------------------------------------------
|
||||
#endif
|
||||
|
||||
class pstderr : public pofilestream
|
||||
{
|
||||
@ -220,16 +273,27 @@ class pifilestream : public pistream
|
||||
{
|
||||
public:
|
||||
|
||||
explicit pifilestream(const pstring &fname);
|
||||
pifilestream(const pstring &fname);
|
||||
virtual ~pifilestream() override;
|
||||
|
||||
pifilestream(pifilestream &&src)
|
||||
: pistream(std::move(src))
|
||||
, m_file(src.m_file)
|
||||
, m_pos(src.m_pos)
|
||||
, m_actually_close(src.m_actually_close)
|
||||
, m_filename(src.m_filename)
|
||||
{
|
||||
src.m_actually_close = false;
|
||||
src.m_file = nullptr;
|
||||
}
|
||||
|
||||
protected:
|
||||
pifilestream(void *file, const pstring &name, const bool do_close);
|
||||
|
||||
/* read up to n bytes from stream */
|
||||
virtual pos_type vread(void *buf, const pos_type n) override;
|
||||
virtual void vseek(const pos_type n) override;
|
||||
virtual pos_type vtell() override;
|
||||
virtual pos_type vtell() const override;
|
||||
|
||||
private:
|
||||
void *m_file;
|
||||
@ -261,15 +325,34 @@ class pimemstream : public pistream
|
||||
public:
|
||||
|
||||
pimemstream(const void *mem, const pos_type len);
|
||||
pimemstream();
|
||||
|
||||
pimemstream(pimemstream &&src)
|
||||
: pistream(std::move(src))
|
||||
, m_pos(src.m_pos)
|
||||
, m_len(src.m_len)
|
||||
, m_mem(src.m_mem)
|
||||
{
|
||||
src.m_mem = nullptr;
|
||||
}
|
||||
|
||||
explicit pimemstream(const pomemstream &ostrm);
|
||||
|
||||
virtual ~pimemstream() override;
|
||||
|
||||
pos_type size() const { return m_len; }
|
||||
protected:
|
||||
|
||||
void set_mem(const void *mem, const pos_type len)
|
||||
{
|
||||
m_mem = static_cast<const char *>(mem);
|
||||
m_len = len;
|
||||
}
|
||||
|
||||
/* read up to n bytes from stream */
|
||||
virtual pos_type vread(void *buf, const pos_type n) override;
|
||||
virtual void vseek(const pos_type n) override;
|
||||
virtual pos_type vtell() override;
|
||||
virtual pos_type vtell() const override;
|
||||
|
||||
private:
|
||||
pos_type m_pos;
|
||||
@ -284,12 +367,22 @@ private:
|
||||
class pistringstream : public pimemstream
|
||||
{
|
||||
public:
|
||||
explicit pistringstream(const pstring &str) : pimemstream(str.c_str(), std::strlen(str.c_str())), m_str(str) { }
|
||||
pistringstream(const pstring &str)
|
||||
: pimemstream()
|
||||
, m_str(str)
|
||||
{
|
||||
set_mem(m_str.c_str(), std::strlen(m_str.c_str()));
|
||||
}
|
||||
pistringstream(pistringstream &&src)
|
||||
: pimemstream(std::move(src)), m_str(src.m_str)
|
||||
{
|
||||
set_mem(m_str.c_str(), std::strlen(m_str.c_str()));
|
||||
}
|
||||
virtual ~pistringstream() override;
|
||||
|
||||
private:
|
||||
/* only needed for a reference till destruction */
|
||||
pstring m_str;
|
||||
const pstring m_str;
|
||||
};
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
@ -298,35 +391,62 @@ private:
|
||||
|
||||
/* this digests linux & dos/windows text files */
|
||||
|
||||
class putf8_reader : plib::nocopyassignmove
|
||||
class putf8_reader : plib::nocopyassign
|
||||
{
|
||||
public:
|
||||
explicit putf8_reader(pistream &strm) : m_strm(strm) {}
|
||||
virtual ~putf8_reader() {}
|
||||
putf8_reader(pistream *strm) : m_strm(strm), m_stream_owned(false) {}
|
||||
virtual ~putf8_reader()
|
||||
{
|
||||
if (m_stream_owned && m_strm != nullptr)
|
||||
pfree(m_strm);
|
||||
}
|
||||
|
||||
bool eof() const { return m_strm.eof(); }
|
||||
putf8_reader(putf8_reader &&src) : m_strm(src.m_strm), m_stream_owned(src.m_stream_owned)
|
||||
{
|
||||
src.m_strm = nullptr;
|
||||
src.m_stream_owned = false;
|
||||
}
|
||||
|
||||
putf8_reader(pimemstream &&strm)
|
||||
: m_strm(palloc<pimemstream>(std::move(strm))),
|
||||
m_stream_owned(true)
|
||||
{}
|
||||
|
||||
putf8_reader(pifilestream &&strm)
|
||||
: m_strm(palloc<pifilestream>(std::move(strm))),
|
||||
m_stream_owned(true)
|
||||
{}
|
||||
|
||||
putf8_reader(pistringstream &&strm)
|
||||
: m_strm(palloc<pistringstream>(std::move(strm))),
|
||||
m_stream_owned(true)
|
||||
{}
|
||||
|
||||
|
||||
bool eof() const { return m_strm->eof(); }
|
||||
bool readline(pstring &line);
|
||||
|
||||
bool readbyte1(char &b)
|
||||
{
|
||||
return (m_strm.read(&b, 1) == 1);
|
||||
return (m_strm->read(&b, 1) == 1);
|
||||
}
|
||||
|
||||
bool readcode(putf8string::traits_type::code_t &c)
|
||||
{
|
||||
char b[4];
|
||||
if (m_strm.read(&b[0], 1) != 1)
|
||||
if (m_strm->read(&b[0], 1) != 1)
|
||||
return false;
|
||||
const std::size_t l = putf8string::traits_type::codelen(b);
|
||||
for (std::size_t i = 1; i < l; i++)
|
||||
if (m_strm.read(&b[i], 1) != 1)
|
||||
if (m_strm->read(&b[i], 1) != 1)
|
||||
return false;
|
||||
c = putf8string::traits_type::code(b);
|
||||
return true;
|
||||
}
|
||||
|
||||
private:
|
||||
pistream &m_strm;
|
||||
pistream *m_strm;
|
||||
bool m_stream_owned;
|
||||
putf8string m_linebuf;
|
||||
};
|
||||
|
||||
@ -334,10 +454,12 @@ private:
|
||||
// putf8writer_t: writer on top of ostream
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
class putf8_writer : plib::nocopyassignmove
|
||||
class putf8_writer : plib::nocopyassign
|
||||
{
|
||||
public:
|
||||
explicit putf8_writer(postream &strm) : m_strm(strm) {}
|
||||
explicit putf8_writer(postream *strm) : m_strm(strm) {}
|
||||
|
||||
putf8_writer(putf8_writer &&src) : m_strm(src.m_strm) {}
|
||||
virtual ~putf8_writer() {}
|
||||
|
||||
void writeline(const pstring &line) const
|
||||
@ -349,7 +471,7 @@ public:
|
||||
void write(const pstring &text) const
|
||||
{
|
||||
putf8string conv_utf8(text);
|
||||
m_strm.write(conv_utf8.c_str(), conv_utf8.mem_t_size());
|
||||
m_strm->write(conv_utf8.c_str(), conv_utf8.mem_t_size());
|
||||
}
|
||||
|
||||
void write(const pstring::value_type c) const
|
||||
@ -359,14 +481,19 @@ public:
|
||||
}
|
||||
|
||||
private:
|
||||
postream &m_strm;
|
||||
postream *m_strm;
|
||||
};
|
||||
|
||||
class putf8_fmt_writer : public pfmt_writer_t<putf8_fmt_writer>, public putf8_writer
|
||||
{
|
||||
public:
|
||||
|
||||
explicit putf8_fmt_writer(postream &strm);
|
||||
explicit putf8_fmt_writer(postream *strm)
|
||||
: pfmt_writer_t()
|
||||
, putf8_writer(strm)
|
||||
{
|
||||
}
|
||||
|
||||
virtual ~putf8_fmt_writer() override;
|
||||
|
||||
//protected:
|
||||
@ -379,16 +506,17 @@ private:
|
||||
// pbinary_writer_t: writer on top of ostream
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
class pbinary_writer : plib::nocopyassignmove
|
||||
class pbinary_writer : plib::nocopyassign
|
||||
{
|
||||
public:
|
||||
explicit pbinary_writer(postream &strm) : m_strm(strm) {}
|
||||
pbinary_writer(pbinary_writer &&src) : m_strm(src.m_strm) {}
|
||||
virtual ~pbinary_writer() {}
|
||||
|
||||
template <typename T>
|
||||
void write(const T val)
|
||||
void write(const T &val)
|
||||
{
|
||||
m_strm.write(&val, sizeof(T));
|
||||
m_strm.write(reinterpret_cast<const char *>(&val), sizeof(T));
|
||||
}
|
||||
|
||||
void write(const pstring &s)
|
||||
@ -411,10 +539,11 @@ private:
|
||||
postream &m_strm;
|
||||
};
|
||||
|
||||
class pbinary_reader : plib::nocopyassignmove
|
||||
class pbinary_reader : plib::nocopyassign
|
||||
{
|
||||
public:
|
||||
explicit pbinary_reader(pistream &strm) : m_strm(strm) {}
|
||||
pbinary_reader(pbinary_reader &&src) : m_strm(src.m_strm) { }
|
||||
virtual ~pbinary_reader() {}
|
||||
|
||||
template <typename T>
|
||||
@ -447,6 +576,15 @@ private:
|
||||
pistream &m_strm;
|
||||
};
|
||||
|
||||
inline void copystream(postream &dest, pistream &src)
|
||||
{
|
||||
char buf[1024];
|
||||
pstream::pos_type r;
|
||||
while ((r=src.read(buf, 1024)) > 0)
|
||||
dest.write(buf, r);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
#endif /* PSTREAM_H_ */
|
||||
|
@ -147,6 +147,8 @@ public:
|
||||
*this += static_cast<code_t>(c); // FIXME: codepage conversion for u8
|
||||
}
|
||||
|
||||
operator string_type () const { return m_str; }
|
||||
|
||||
pstring_t &operator=(const pstring_t &string) { m_str = string.m_str; return *this; }
|
||||
|
||||
template <typename T,
|
||||
|
@ -292,8 +292,7 @@ static std::vector<input_t> read_input(const netlist::setup_t &setup, pstring fn
|
||||
std::vector<input_t> ret;
|
||||
if (fname != "")
|
||||
{
|
||||
plib::pifilestream f(fname);
|
||||
plib::putf8_reader r(f);
|
||||
plib::putf8_reader r = plib::putf8_reader(plib::pifilestream(fname));
|
||||
pstring l;
|
||||
while (r.readline(l))
|
||||
{
|
||||
@ -402,7 +401,7 @@ void tool_app_t::static_compile()
|
||||
opt_logs(),
|
||||
opt_defines(), opt_rfolders());
|
||||
|
||||
plib::putf8_writer w(pout_strm);
|
||||
plib::putf8_writer w(&pout_strm);
|
||||
std::map<pstring, pstring> mp;
|
||||
|
||||
nt.solver()->create_solver_code(mp);
|
||||
@ -724,12 +723,12 @@ int tool_app_t::execute()
|
||||
if (opt_file() == "-")
|
||||
{
|
||||
plib::pstdin f;
|
||||
ostrm.write(f);
|
||||
plib::copystream(ostrm, f);
|
||||
}
|
||||
else
|
||||
{
|
||||
plib::pifilestream f(opt_file());
|
||||
ostrm.write(f);
|
||||
plib::copystream(ostrm, f);
|
||||
}
|
||||
contents = ostrm.str();
|
||||
|
||||
|
@ -51,38 +51,45 @@ private:
|
||||
* and data chunk length to the file.
|
||||
*/
|
||||
/* http://de.wikipedia.org/wiki/RIFF_WAVE */
|
||||
|
||||
class wav_t
|
||||
{
|
||||
public:
|
||||
wav_t(plib::postream &strm, unsigned sr) : m_f(strm)
|
||||
{
|
||||
initialize(sr);
|
||||
m_f.write(&m_fh, sizeof(m_fh));
|
||||
m_f.write(&m_fmt, sizeof(m_fmt));
|
||||
m_f.write(&m_data, sizeof(m_data));
|
||||
write(m_fh);
|
||||
write(m_fmt);
|
||||
write(m_data);
|
||||
}
|
||||
~wav_t()
|
||||
{
|
||||
if (m_f.seekable())
|
||||
{
|
||||
m_fh.filelen = m_data.len + sizeof(m_data) + sizeof(m_fh) + sizeof(m_fmt) - 8;
|
||||
m_f.seek(0);
|
||||
m_f.write(&m_fh, sizeof(m_fh));
|
||||
m_f.write(&m_fmt, sizeof(m_fmt));
|
||||
m_f.seekp(0);
|
||||
write(m_fh);
|
||||
write(m_fmt);
|
||||
|
||||
//data.len = fmt.block_align * n;
|
||||
m_f.write(&m_data, sizeof(m_data));
|
||||
write(m_data);
|
||||
}
|
||||
}
|
||||
|
||||
unsigned channels() { return m_fmt.channels; }
|
||||
unsigned sample_rate() { return m_fmt.sample_rate; }
|
||||
|
||||
template <typename T>
|
||||
void write(const T &val)
|
||||
{
|
||||
m_f.write(reinterpret_cast<const char *>(&val), sizeof(T));
|
||||
}
|
||||
|
||||
void write_sample(int sample)
|
||||
{
|
||||
m_data.len += m_fmt.block_align;
|
||||
int16_t ps = static_cast<int16_t>(sample); /* 16 bit sample, FIXME: Endianess? */
|
||||
m_f.write(&ps, sizeof(ps));
|
||||
write(ps);
|
||||
}
|
||||
|
||||
private:
|
||||
@ -150,7 +157,7 @@ public:
|
||||
|
||||
void process()
|
||||
{
|
||||
plib::putf8_reader reader(m_is);
|
||||
plib::putf8_reader reader(&m_is);
|
||||
pstring line;
|
||||
|
||||
while(reader.readline(line))
|
||||
@ -248,7 +255,7 @@ void nlwav_app::convert(long sample_rate)
|
||||
{
|
||||
plib::postream *fo = (opt_out() == "-" ? &pout_strm : plib::palloc<plib::pofilestream>(opt_out()));
|
||||
plib::pistream *fin = (opt_inp() == "-" ? &pin_strm : plib::palloc<plib::pifilestream>(opt_inp()));
|
||||
plib::putf8_reader reader(*fin);
|
||||
plib::putf8_reader reader(fin);
|
||||
wav_t *wo = plib::palloc<wav_t>(*fo, static_cast<unsigned>(sample_rate));
|
||||
|
||||
double dt = 1.0 / static_cast<double>(wo->sample_rate());
|
||||
|
@ -298,7 +298,7 @@ template <std::size_t m_N, std::size_t storage_N>
|
||||
pstring matrix_solver_GCR_t<m_N, storage_N>::static_compile_name()
|
||||
{
|
||||
plib::postringstream t;
|
||||
plib::putf8_fmt_writer w(t);
|
||||
plib::putf8_fmt_writer w(&t);
|
||||
csc_private(w);
|
||||
std::hash<pstring> h;
|
||||
|
||||
@ -309,7 +309,7 @@ template <std::size_t m_N, std::size_t storage_N>
|
||||
std::pair<pstring, pstring> matrix_solver_GCR_t<m_N, storage_N>::create_solver_code()
|
||||
{
|
||||
plib::postringstream t;
|
||||
plib::putf8_fmt_writer strm(t);
|
||||
plib::putf8_fmt_writer strm(&t);
|
||||
pstring name = static_compile_name();
|
||||
|
||||
strm.writeline(plib::pfmt("extern \"C\" void {1}(double * __restrict m_A, double * __restrict RHS, double * __restrict V)\n")(name));
|
||||
|
@ -42,8 +42,7 @@ using lib_map_t = std::unordered_map<pstring, lib_map_entry>;
|
||||
|
||||
static lib_map_t read_lib_map(const pstring &lm)
|
||||
{
|
||||
plib::pistringstream istrm(lm);
|
||||
plib::putf8_reader reader(istrm);
|
||||
auto reader = plib::putf8_reader(plib::pistringstream(lm));
|
||||
lib_map_t m;
|
||||
pstring line;
|
||||
while (reader.readline(line))
|
||||
@ -59,7 +58,7 @@ static lib_map_t read_lib_map(const pstring &lm)
|
||||
-------------------------------------------------*/
|
||||
|
||||
nl_convert_base_t::nl_convert_base_t()
|
||||
: out(m_buf)
|
||||
: out(&m_buf)
|
||||
, m_numberchars("0123456789-+e.")
|
||||
{
|
||||
}
|
||||
@ -401,8 +400,8 @@ void nl_convert_spice_t::process_line(const pstring &line)
|
||||
Eagle converter
|
||||
-------------------------------------------------*/
|
||||
|
||||
nl_convert_eagle_t::tokenizer::tokenizer(nl_convert_eagle_t &convert, plib::putf8_reader &strm)
|
||||
: plib::ptokenizer(strm)
|
||||
nl_convert_eagle_t::tokenizer::tokenizer(nl_convert_eagle_t &convert, plib::putf8_reader &&strm)
|
||||
: plib::ptokenizer(std::move(strm))
|
||||
, m_convert(convert)
|
||||
{
|
||||
set_identifier_chars("abcdefghijklmnopqrstuvwvxyzABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890_.-");
|
||||
@ -429,9 +428,8 @@ void nl_convert_eagle_t::tokenizer::verror(const pstring &msg, int line_num, con
|
||||
//FIXME: should accept a stream as well
|
||||
void nl_convert_eagle_t::convert(const pstring &contents)
|
||||
{
|
||||
plib::pistringstream istrm(contents);
|
||||
plib::putf8_reader reader(istrm);
|
||||
tokenizer tok(*this, reader);
|
||||
|
||||
tokenizer tok(*this, plib::putf8_reader(plib::pistringstream(contents)));
|
||||
|
||||
out("NETLIST_START(dummy)\n");
|
||||
add_term("GND", "GND");
|
||||
@ -539,8 +537,8 @@ void nl_convert_eagle_t::convert(const pstring &contents)
|
||||
RINF converter
|
||||
-------------------------------------------------*/
|
||||
|
||||
nl_convert_rinf_t::tokenizer::tokenizer(nl_convert_rinf_t &convert, plib::putf8_reader &strm)
|
||||
: plib::ptokenizer(strm)
|
||||
nl_convert_rinf_t::tokenizer::tokenizer(nl_convert_rinf_t &convert, plib::putf8_reader &&strm)
|
||||
: plib::ptokenizer(std::move(strm))
|
||||
, m_convert(convert)
|
||||
{
|
||||
set_identifier_chars(".abcdefghijklmnopqrstuvwvxyzABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890_-");
|
||||
@ -579,9 +577,7 @@ void nl_convert_rinf_t::tokenizer::verror(const pstring &msg, int line_num, cons
|
||||
|
||||
void nl_convert_rinf_t::convert(const pstring &contents)
|
||||
{
|
||||
plib::pistringstream istrm(contents);
|
||||
plib::putf8_reader reader(istrm);
|
||||
tokenizer tok(*this, reader);
|
||||
tokenizer tok(*this, plib::putf8_reader(plib::pistringstream(contents)));
|
||||
auto lm = read_lib_map(s_lib_map);
|
||||
|
||||
out("NETLIST_START(dummy)\n");
|
||||
|
@ -166,7 +166,7 @@ public:
|
||||
class tokenizer : public plib::ptokenizer
|
||||
{
|
||||
public:
|
||||
tokenizer(nl_convert_eagle_t &convert, plib::putf8_reader &strm);
|
||||
tokenizer(nl_convert_eagle_t &convert, plib::putf8_reader &&strm);
|
||||
|
||||
token_id_t m_tok_ADD;
|
||||
token_id_t m_tok_VALUE;
|
||||
@ -202,7 +202,7 @@ public:
|
||||
class tokenizer : public plib::ptokenizer
|
||||
{
|
||||
public:
|
||||
tokenizer(nl_convert_rinf_t &convert, plib::putf8_reader &strm);
|
||||
tokenizer(nl_convert_rinf_t &convert, plib::putf8_reader &&strm);
|
||||
|
||||
token_id_t m_tok_HEA;
|
||||
token_id_t m_tok_APP;
|
||||
|
Loading…
Reference in New Issue
Block a user