mirror of
https://github.com/holub/mame
synced 2025-04-23 17:00:53 +03:00
Merge branch 'master' of https://github.com/mamedev/mame
This commit is contained in:
commit
5e47e9e62c
@ -289,7 +289,7 @@ public:
|
||||
{
|
||||
}
|
||||
|
||||
virtual plib::unique_ptr<plib::pistream> stream(const pstring &name) override;
|
||||
virtual plib::unique_ptr<std::istream> stream(const pstring &name) override;
|
||||
private:
|
||||
device_t &m_dev;
|
||||
pstring m_name;
|
||||
@ -300,7 +300,7 @@ class netlist_data_memregions_t : public netlist::source_t
|
||||
public:
|
||||
netlist_data_memregions_t(const device_t &dev);
|
||||
|
||||
virtual plib::unique_ptr<plib::pistream> stream(const pstring &name) override;
|
||||
virtual plib::unique_ptr<std::istream> stream(const pstring &name) override;
|
||||
|
||||
private:
|
||||
const device_t &m_dev;
|
||||
@ -311,12 +311,14 @@ private:
|
||||
// memregion source support
|
||||
// ----------------------------------------------------------------------------------------
|
||||
|
||||
plib::unique_ptr<plib::pistream> netlist_source_memregion_t::stream(const pstring &name)
|
||||
plib::unique_ptr<std::istream> netlist_source_memregion_t::stream(const pstring &name)
|
||||
{
|
||||
if (m_dev.has_running_machine())
|
||||
{
|
||||
memory_region *mem = m_dev.memregion(m_name.c_str());
|
||||
return plib::make_unique<plib::pistringstream>(pstring(reinterpret_cast<char *>(mem->base()), mem->bytes()));
|
||||
auto ret(plib::make_unique<std::istringstream>(pstring(reinterpret_cast<char *>(mem->base()), mem->bytes())));
|
||||
ret->imbue(std::locale::classic());
|
||||
return ret;
|
||||
}
|
||||
else
|
||||
throw memregion_not_set("memregion unavailable for {1} in source {2}", name, m_name);
|
||||
@ -347,24 +349,32 @@ static bool rom_exists(device_t &root, pstring name)
|
||||
return false;
|
||||
}
|
||||
|
||||
plib::unique_ptr<plib::pistream> netlist_data_memregions_t::stream(const pstring &name)
|
||||
plib::unique_ptr<std::istream> netlist_data_memregions_t::stream(const pstring &name)
|
||||
{
|
||||
//memory_region *mem = static_cast<netlist_mame_device::netlist_mame_t &>(setup().setup().exec()).parent().memregion(name.c_str());
|
||||
if (m_dev.has_running_machine())
|
||||
{
|
||||
memory_region *mem = m_dev.memregion(name.c_str());
|
||||
if (mem != nullptr)
|
||||
return plib::make_unique<plib::pistringstream>(pstring(reinterpret_cast<char *>(mem->base()), mem->bytes()));
|
||||
{
|
||||
auto ret(plib::make_unique<std::istringstream>(std::string(reinterpret_cast<char *>(mem->base()), mem->bytes()), std::ios_base::binary));
|
||||
ret->imbue(std::locale::classic());
|
||||
return ret;
|
||||
}
|
||||
else
|
||||
return plib::unique_ptr<plib::pistream>(nullptr);
|
||||
return plib::unique_ptr<std::istream>(nullptr);
|
||||
}
|
||||
else
|
||||
{
|
||||
/* validation */
|
||||
if (rom_exists(m_dev.mconfig().root_device(), pstring(m_dev.tag()) + ":" + name))
|
||||
return plib::make_unique<plib::pimemstream>();
|
||||
{
|
||||
auto ret(plib::make_unique<std::istringstream>(std::ios_base::binary));
|
||||
ret->imbue(std::locale::classic());
|
||||
return ret;
|
||||
}
|
||||
else
|
||||
return plib::unique_ptr<plib::pistream>(nullptr);
|
||||
return plib::unique_ptr<std::istream>(nullptr);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -19,9 +19,13 @@ namespace netlist
|
||||
{
|
||||
NETLIB_CONSTRUCTOR(log)
|
||||
, m_I(*this, "I")
|
||||
, m_strm(pstring(plib::pfmt("{1}.log")(this->name())))
|
||||
, m_strm(plib::filesystem::u8path(plib::pfmt("{1}.log")(this->name())))
|
||||
, m_writer(&m_strm)
|
||||
{
|
||||
if (m_strm.fail())
|
||||
throw plib::file_open_e(plib::pfmt("{1}.log")(this->name()));
|
||||
|
||||
m_strm.imbue(std::locale::classic());
|
||||
}
|
||||
|
||||
NETLIB_UPDATEI()
|
||||
@ -33,7 +37,7 @@ namespace netlist
|
||||
NETLIB_RESETI() { }
|
||||
protected:
|
||||
analog_input_t m_I;
|
||||
plib::pofilestream m_strm;
|
||||
std::ofstream m_strm;
|
||||
plib::putf8_writer m_writer;
|
||||
};
|
||||
|
||||
|
@ -1033,7 +1033,7 @@ nl_double param_model_t::value(const pstring &entity)
|
||||
}
|
||||
|
||||
|
||||
plib::unique_ptr<plib::pistream> param_data_t::stream()
|
||||
plib::unique_ptr<std::istream> param_data_t::stream()
|
||||
{
|
||||
return device().setup().get_data_stream(str());
|
||||
}
|
||||
|
@ -1119,7 +1119,7 @@ namespace netlist
|
||||
{
|
||||
}
|
||||
|
||||
plib::unique_ptr<plib::pistream> stream();
|
||||
plib::unique_ptr<std::istream> stream();
|
||||
protected:
|
||||
void changed() override { }
|
||||
};
|
||||
@ -1139,7 +1139,7 @@ namespace netlist
|
||||
protected:
|
||||
void changed() override
|
||||
{
|
||||
stream()->read(reinterpret_cast<plib::pistream::char_type *>(&m_data[0]),1<<AW);
|
||||
stream()->read(reinterpret_cast<std::istream::char_type *>(&m_data[0]),1<<AW);
|
||||
}
|
||||
|
||||
private:
|
||||
@ -1634,7 +1634,7 @@ namespace netlist
|
||||
{
|
||||
auto f = stream();
|
||||
if (f != nullptr)
|
||||
f->read(reinterpret_cast<plib::pistream::char_type *>(&m_data[0]),1<<AW);
|
||||
f->read(reinterpret_cast<std::istream::char_type *>(&m_data[0]),1<<AW);
|
||||
else
|
||||
device.state().log().warning(MW_ROM_NOT_FOUND(str()));
|
||||
}
|
||||
|
@ -142,6 +142,12 @@ namespace netlist
|
||||
|
||||
PERRMSGV(MW_MOSFET_THRESHOLD_VOLTAGE, 1, "Mosfet: Threshold voltage not specified for {1}")
|
||||
|
||||
// nl_tool.cpp
|
||||
|
||||
PERRMSGV(MF_FILE_OPEN_ERROR, 1, "Error opening file: {1}")
|
||||
|
||||
|
||||
|
||||
} // namespace netlist
|
||||
|
||||
|
||||
|
@ -206,7 +206,7 @@ namespace netlist
|
||||
return false;
|
||||
}
|
||||
|
||||
bool nlparse_t::parse_stream(plib::unique_ptr<plib::pistream> &&istrm, const pstring &name)
|
||||
bool nlparse_t::parse_stream(plib::unique_ptr<std::istream> &&istrm, const pstring &name)
|
||||
{
|
||||
plib::ppreprocessor y(&m_defines);
|
||||
plib::ppreprocessor &x(y.process(std::move(istrm)));
|
||||
@ -1018,7 +1018,7 @@ const logic_family_desc_t *setup_t::family_from_model(const pstring &model)
|
||||
// Sources
|
||||
// ----------------------------------------------------------------------------------------
|
||||
|
||||
plib::unique_ptr<plib::pistream> setup_t::get_data_stream(const pstring &name)
|
||||
plib::unique_ptr<std::istream> setup_t::get_data_stream(const pstring &name)
|
||||
{
|
||||
for (auto &source : m_sources)
|
||||
{
|
||||
@ -1030,7 +1030,7 @@ plib::unique_ptr<plib::pistream> setup_t::get_data_stream(const pstring &name)
|
||||
}
|
||||
}
|
||||
log().warning(MW_DATA_1_NOT_FOUND(name));
|
||||
return plib::unique_ptr<plib::pistream>(nullptr);
|
||||
return plib::unique_ptr<std::istream>(nullptr);
|
||||
}
|
||||
|
||||
|
||||
@ -1199,22 +1199,27 @@ bool source_t::parse(nlparse_t &setup, const pstring &name)
|
||||
}
|
||||
}
|
||||
|
||||
plib::unique_ptr<plib::pistream> source_string_t::stream(const pstring &name)
|
||||
plib::unique_ptr<std::istream> source_string_t::stream(const pstring &name)
|
||||
{
|
||||
plib::unused_var(name);
|
||||
return plib::make_unique<plib::pistringstream>(m_str);
|
||||
auto ret(plib::make_unique<std::istringstream>(m_str));
|
||||
ret->imbue(std::locale::classic());
|
||||
return std::move(ret);
|
||||
}
|
||||
|
||||
plib::unique_ptr<plib::pistream> source_mem_t::stream(const pstring &name)
|
||||
plib::unique_ptr<std::istream> source_mem_t::stream(const pstring &name)
|
||||
{
|
||||
plib::unused_var(name);
|
||||
return plib::make_unique<plib::pistringstream>(m_str);
|
||||
auto ret(plib::make_unique<std::istringstream>(m_str, std::ios_base::binary));
|
||||
ret->imbue(std::locale::classic());
|
||||
return std::move(ret);
|
||||
}
|
||||
|
||||
plib::unique_ptr<plib::pistream> source_file_t::stream(const pstring &name)
|
||||
plib::unique_ptr<std::istream> source_file_t::stream(const pstring &name)
|
||||
{
|
||||
plib::unused_var(name);
|
||||
return plib::make_unique<plib::pifilestream>(m_filename);
|
||||
auto ret(plib::make_unique<std::ifstream>(plib::filesystem::u8path(m_filename)));
|
||||
return std::move(ret);
|
||||
}
|
||||
|
||||
bool source_proc_t::parse(nlparse_t &setup, const pstring &name)
|
||||
@ -1228,10 +1233,10 @@ bool source_proc_t::parse(nlparse_t &setup, const pstring &name)
|
||||
return false;
|
||||
}
|
||||
|
||||
plib::unique_ptr<plib::pistream> source_proc_t::stream(const pstring &name)
|
||||
plib::unique_ptr<std::istream> source_proc_t::stream(const pstring &name)
|
||||
{
|
||||
plib::unused_var(name);
|
||||
plib::unique_ptr<plib::pistream> p(nullptr);
|
||||
plib::unique_ptr<std::istream> p(nullptr);
|
||||
return p;
|
||||
}
|
||||
|
||||
|
@ -196,7 +196,7 @@ namespace netlist
|
||||
type_t type() const { return m_type; }
|
||||
|
||||
protected:
|
||||
virtual plib::unique_ptr<plib::pistream> stream(const pstring &name) = 0;
|
||||
virtual plib::unique_ptr<std::istream> stream(const pstring &name) = 0;
|
||||
|
||||
private:
|
||||
const type_t m_type;
|
||||
@ -278,7 +278,7 @@ namespace netlist
|
||||
bool device_exists(const pstring &name) const;
|
||||
|
||||
/* FIXME: used by source_t - need a different approach at some time */
|
||||
bool parse_stream(plib::unique_ptr<plib::pistream> &&istrm, const pstring &name);
|
||||
bool parse_stream(plib::unique_ptr<std::istream> &&istrm, const pstring &name);
|
||||
|
||||
void add_define(const pstring &def, const pstring &val)
|
||||
{
|
||||
@ -363,7 +363,7 @@ namespace netlist
|
||||
void register_dynamic_log_devices();
|
||||
void resolve_inputs();
|
||||
|
||||
plib::unique_ptr<plib::pistream> get_data_stream(const pstring &name);
|
||||
plib::unique_ptr<std::istream> get_data_stream(const pstring &name);
|
||||
|
||||
factory::list_t &factory() { return m_factory; }
|
||||
const factory::list_t &factory() const { return m_factory; }
|
||||
@ -434,7 +434,7 @@ namespace netlist
|
||||
}
|
||||
|
||||
protected:
|
||||
plib::unique_ptr<plib::pistream> stream(const pstring &name) override;
|
||||
plib::unique_ptr<std::istream> stream(const pstring &name) override;
|
||||
|
||||
private:
|
||||
pstring m_str;
|
||||
@ -450,7 +450,7 @@ namespace netlist
|
||||
}
|
||||
|
||||
protected:
|
||||
plib::unique_ptr<plib::pistream> stream(const pstring &name) override;
|
||||
plib::unique_ptr<std::istream> stream(const pstring &name) override;
|
||||
|
||||
private:
|
||||
pstring m_filename;
|
||||
@ -465,7 +465,7 @@ namespace netlist
|
||||
}
|
||||
|
||||
protected:
|
||||
plib::unique_ptr<plib::pistream> stream(const pstring &name) override;
|
||||
plib::unique_ptr<std::istream> stream(const pstring &name) override;
|
||||
|
||||
private:
|
||||
pstring m_str;
|
||||
@ -484,7 +484,7 @@ namespace netlist
|
||||
bool parse(nlparse_t &setup, const pstring &name) override;
|
||||
|
||||
protected:
|
||||
plib::unique_ptr<plib::pistream> stream(const pstring &name) override;
|
||||
plib::unique_ptr<std::istream> stream(const pstring &name) override;
|
||||
|
||||
private:
|
||||
void (*m_setup_func)(nlparse_t &);
|
||||
|
@ -35,15 +35,8 @@ namespace plib {
|
||||
|
||||
app::app()
|
||||
: options()
|
||||
#if !USE_CSTREAM
|
||||
, pout_strm()
|
||||
, perr_strm()
|
||||
, pout(&pout_strm)
|
||||
, perr(&perr_strm)
|
||||
#else
|
||||
, pout(&std::cout)
|
||||
, perr(&std::cerr)
|
||||
#endif
|
||||
{
|
||||
|
||||
}
|
||||
|
@ -44,10 +44,7 @@ namespace plib {
|
||||
|
||||
virtual pstring usage() = 0;
|
||||
virtual int execute() = 0;
|
||||
#if !USE_CSTREAM
|
||||
plib::pstdout pout_strm;
|
||||
plib::pstderr perr_strm;
|
||||
#endif
|
||||
|
||||
plib::putf8_fmt_writer pout;
|
||||
plib::putf8_fmt_writer perr;
|
||||
|
||||
|
@ -264,11 +264,7 @@ void ptokenizer::error(const pstring &errs)
|
||||
// ----------------------------------------------------------------------------------------
|
||||
|
||||
ppreprocessor::ppreprocessor(defines_map_type *defines)
|
||||
#if !USE_CSTREAM
|
||||
: pistream()
|
||||
#else
|
||||
: pistream(new st(this))
|
||||
#endif
|
||||
: std::istream(new st(this))
|
||||
, m_ifflag(0)
|
||||
, m_level(0)
|
||||
, m_lineno(0)
|
||||
@ -300,20 +296,6 @@ void ppreprocessor::error(const pstring &err)
|
||||
throw pexception("PREPRO ERROR: " + err);
|
||||
}
|
||||
|
||||
#if !USE_CSTREAM
|
||||
pstream::size_type ppreprocessor::vread(char_type *buf, const pstream::size_type n)
|
||||
{
|
||||
size_type bytes = std::min(m_buf.size() - m_pos, static_cast<std::size_t>(n));
|
||||
|
||||
if (bytes==0)
|
||||
return 0;
|
||||
|
||||
std::copy(m_buf.c_str() + m_pos, m_buf.c_str() + m_pos + bytes, buf);
|
||||
m_pos += bytes;
|
||||
return bytes;
|
||||
}
|
||||
#endif
|
||||
|
||||
#define CHECKTOK2(p_op, p_prio) \
|
||||
else if (tok == # p_op) \
|
||||
{ \
|
||||
|
@ -125,6 +125,7 @@ public:
|
||||
token_t get_token_internal();
|
||||
void error(const pstring &errs);
|
||||
|
||||
putf8_reader &stream() { return m_strm; }
|
||||
protected:
|
||||
virtual void verror(const pstring &msg, int line_num, const pstring &line) = 0;
|
||||
|
||||
@ -158,11 +159,7 @@ private:
|
||||
};
|
||||
|
||||
|
||||
#if !USE_CSTREAM
|
||||
class ppreprocessor : public pistream
|
||||
#else
|
||||
class ppreprocessor : public /*std::streambuf,*/ pistream
|
||||
#endif
|
||||
class ppreprocessor : public std::istream
|
||||
{
|
||||
public:
|
||||
|
||||
@ -178,14 +175,11 @@ public:
|
||||
using defines_map_type = std::unordered_map<pstring, define_t>;
|
||||
|
||||
explicit ppreprocessor(defines_map_type *defines = nullptr);
|
||||
#if !USE_CSTREAM
|
||||
~ppreprocessor() override = default;
|
||||
#else
|
||||
~ppreprocessor() override
|
||||
{
|
||||
delete rdbuf();
|
||||
}
|
||||
#endif
|
||||
|
||||
template <typename T>
|
||||
ppreprocessor & process(T &&istrm)
|
||||
{
|
||||
@ -205,12 +199,7 @@ public:
|
||||
|
||||
|
||||
ppreprocessor(ppreprocessor &&s) noexcept
|
||||
//: pistream(s.rdbuf())
|
||||
#if !USE_CSTREAM
|
||||
: pistream()
|
||||
#else
|
||||
: pistream(new st(this))
|
||||
#endif
|
||||
: std::istream(new st(this))
|
||||
, m_defines(std::move(s.m_defines))
|
||||
, m_expr_sep(std::move(s.m_expr_sep))
|
||||
, m_ifflag(s.m_ifflag)
|
||||
@ -225,15 +214,6 @@ public:
|
||||
|
||||
protected:
|
||||
|
||||
#if !USE_CSTREAM
|
||||
size_type vread(char_type *buf, const pos_type n) override;
|
||||
void vseek(const pos_type n) override
|
||||
{
|
||||
plib::unused_var(n);
|
||||
/* FIXME throw exception - should be done in base unless implemented */
|
||||
}
|
||||
pos_type vtell() const override { return m_pos; }
|
||||
#else
|
||||
class st : public std::streambuf
|
||||
{
|
||||
public:
|
||||
@ -264,7 +244,7 @@ protected:
|
||||
std::array<char_type, 1024> m_buf;
|
||||
};
|
||||
//friend class st;
|
||||
#endif
|
||||
|
||||
int expr(const std::vector<pstring> &sexpr, std::size_t &start, int prio);
|
||||
define_t *get_define(const pstring &name);
|
||||
pstring replace_macros(const pstring &line);
|
||||
@ -288,7 +268,7 @@ private:
|
||||
int m_level;
|
||||
int m_lineno;
|
||||
pstring_t<pu8_traits> m_buf;
|
||||
pistream::pos_type m_pos;
|
||||
std::istream::pos_type m_pos;
|
||||
state_e m_state;
|
||||
pstring m_line;
|
||||
bool m_comment;
|
||||
|
@ -21,289 +21,6 @@
|
||||
|
||||
namespace plib {
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
// pistream: input stream
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
// postream: output stream
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
// Input file stream
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
#if !USE_CSTREAM
|
||||
|
||||
pifilestream::pifilestream(const pstring &fname)
|
||||
: pistream(0)
|
||||
, m_file(fopen(fname.c_str(), "rb"))
|
||||
, m_pos(0)
|
||||
, m_actually_close(true)
|
||||
, m_filename(fname)
|
||||
{
|
||||
if (m_file == nullptr)
|
||||
throw file_open_e(fname);
|
||||
else
|
||||
init();
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
if (m_file == nullptr)
|
||||
throw null_argument_e(m_filename);
|
||||
init();
|
||||
}
|
||||
|
||||
void pifilestream::init()
|
||||
{
|
||||
if (ftell(static_cast<FILE *>(m_file)) >= 0)
|
||||
{
|
||||
if (fseek(static_cast<FILE *>(m_file), 0, SEEK_SET) >= 0)
|
||||
set_flag(FLAG_SEEKABLE);
|
||||
}
|
||||
}
|
||||
|
||||
pifilestream::~pifilestream()
|
||||
{
|
||||
if (m_actually_close)
|
||||
{
|
||||
fclose(static_cast<FILE *>(m_file));
|
||||
}
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
if (feof(static_cast<FILE *>(m_file)))
|
||||
set_flag(FLAG_EOF);
|
||||
if (ferror(static_cast<FILE *>(m_file)))
|
||||
throw file_read_e(m_filename);
|
||||
}
|
||||
m_pos += r;
|
||||
return r;
|
||||
}
|
||||
|
||||
void pifilestream::vseek(const pos_type n)
|
||||
{
|
||||
if (fseek(static_cast<FILE *>(m_file), static_cast<long>(n), SEEK_SET) < 0)
|
||||
throw file_e("File seek failed: {}", m_filename);
|
||||
else
|
||||
m_pos = n;
|
||||
if (feof(static_cast<FILE *>(m_file)))
|
||||
set_flag(FLAG_EOF);
|
||||
else
|
||||
clear_flag(FLAG_EOF);
|
||||
if (ferror(static_cast<FILE *>(m_file)))
|
||||
throw file_e("Generic file operation failed: {}", m_filename);
|
||||
}
|
||||
|
||||
pifilestream::pos_type pifilestream::vtell() const
|
||||
{
|
||||
long ret = ftell(static_cast<FILE *>(m_file));
|
||||
if (ret < 0)
|
||||
{
|
||||
return m_pos;
|
||||
}
|
||||
else
|
||||
return static_cast<pos_type>(ret);
|
||||
}
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
// pstdin: reads from stdin
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
pstdin::pstdin()
|
||||
: pifilestream(stdin, "<stdin>", false)
|
||||
{
|
||||
/* nothing to do */
|
||||
}
|
||||
#endif
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
// 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)
|
||||
{
|
||||
if (m_file == nullptr)
|
||||
throw file_open_e(m_filename);
|
||||
init();
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
if (m_file == nullptr)
|
||||
throw null_argument_e(m_filename);
|
||||
init();
|
||||
}
|
||||
|
||||
void pofilestream::init()
|
||||
{
|
||||
if (ftell(static_cast<FILE *>(m_file)) >= 0)
|
||||
if (fseek(static_cast<FILE *>(m_file), 0, SEEK_SET) >= 0)
|
||||
set_flag(FLAG_SEEKABLE);
|
||||
}
|
||||
|
||||
pofilestream::~pofilestream()
|
||||
{
|
||||
if (m_actually_close)
|
||||
{
|
||||
fflush(static_cast<FILE *>(m_file));
|
||||
fclose(static_cast<FILE *>(m_file));
|
||||
}
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
if (ferror(static_cast<FILE *>(m_file)))
|
||||
throw file_write_e(m_filename);
|
||||
}
|
||||
m_pos += r;
|
||||
}
|
||||
|
||||
void pofilestream::vseek(const pos_type n)
|
||||
{
|
||||
if (fseek(static_cast<FILE *>(m_file), static_cast<long>(n), SEEK_SET) < 0)
|
||||
throw file_e("File seek failed: {}", m_filename);
|
||||
else
|
||||
{
|
||||
m_pos = n;
|
||||
if (ferror(static_cast<FILE *>(m_file)))
|
||||
throw file_e("Generic file operation failed: {}", m_filename);
|
||||
}
|
||||
}
|
||||
|
||||
pstream::pos_type pofilestream::vtell() const
|
||||
{
|
||||
std::ptrdiff_t ret = ftell(static_cast<FILE *>(m_file));
|
||||
if (ret < 0)
|
||||
{
|
||||
return m_pos;
|
||||
}
|
||||
else
|
||||
return static_cast<pos_type>(ret);
|
||||
}
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
// 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;
|
||||
}
|
||||
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
// pstderr: write to stderr
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
pstderr::pstderr()
|
||||
#ifdef _WIN32
|
||||
: pofilestream(fdopen(_dup(fileno(stderr)), "wb"), "<stderr>", true)
|
||||
#else
|
||||
: pofilestream(fdopen(dup(fileno(stderr)), "wb"), "<stderr>", true)
|
||||
#endif
|
||||
{
|
||||
}
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
// pstdout: write to stdout
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
pstdout::pstdout()
|
||||
#ifdef _WIN32
|
||||
: pofilestream(fdopen(_dup(fileno(stdout)), "wb"), "<stdout>", true)
|
||||
#else
|
||||
: pofilestream(fdopen(dup(fileno(stdout)), "wb"), "<stdout>", true)
|
||||
#endif
|
||||
{
|
||||
}
|
||||
#endif
|
||||
|
||||
#if !USE_CSTREAM
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
// 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))
|
||||
{
|
||||
}
|
||||
|
||||
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(char_type *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));
|
||||
m_pos += ret;
|
||||
}
|
||||
|
||||
if (ret < n)
|
||||
set_flag(FLAG_EOF);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
void pimemstream::vseek(const pos_type n)
|
||||
{
|
||||
m_pos = (n>=m_len) ? m_len : n;
|
||||
clear_flag(FLAG_EOF);
|
||||
|
||||
}
|
||||
|
||||
pimemstream::pos_type pimemstream::vtell() const
|
||||
{
|
||||
return m_pos;
|
||||
}
|
||||
#endif
|
||||
|
||||
bool putf8_reader::readline(pstring &line)
|
||||
{
|
||||
|
@ -14,441 +14,16 @@
|
||||
#include "pfmtlog.h"
|
||||
#include "pstring.h"
|
||||
|
||||
#define USE_CSTREAM (0)
|
||||
|
||||
#include <array>
|
||||
#include <type_traits>
|
||||
#include <vector>
|
||||
#include <ios>
|
||||
|
||||
#if USE_CSTREAM
|
||||
#include <fstream>
|
||||
//#include <strstream>
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
#endif
|
||||
|
||||
namespace plib {
|
||||
|
||||
#if USE_CSTREAM
|
||||
using postream = std::ostream;
|
||||
using pofilestream = std::ofstream;
|
||||
using postringstream = std::ostringstream;
|
||||
using pomemstream = std::ostringstream;
|
||||
|
||||
using pistream = std::istream;
|
||||
using pifilestream = std::ifstream;
|
||||
using pistringstream = std::istringstream;
|
||||
using pimemstream = std::istringstream;
|
||||
|
||||
#endif
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
// pstream: things common to all streams
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
class pstream
|
||||
{
|
||||
public:
|
||||
|
||||
using pos_type = std::streamsize;
|
||||
using size_type = std::streamsize;
|
||||
|
||||
static constexpr pos_type SEEK_EOF = static_cast<pos_type>(-1);
|
||||
|
||||
COPYASSIGN(pstream, delete)
|
||||
pstream &operator=(pstream &&) noexcept = delete;
|
||||
|
||||
bool seekable() const { return ((m_flags & FLAG_SEEKABLE) != 0); }
|
||||
|
||||
void seekp(const pos_type n)
|
||||
{
|
||||
vseek(n);
|
||||
}
|
||||
|
||||
pos_type tellp() const
|
||||
{
|
||||
return vtell();
|
||||
}
|
||||
|
||||
protected:
|
||||
pstream() : m_flags(0)
|
||||
{
|
||||
}
|
||||
explicit pstream(const unsigned flags) : m_flags(flags)
|
||||
{
|
||||
}
|
||||
pstream(pstream &&src) noexcept = default;
|
||||
|
||||
virtual ~pstream() = default;
|
||||
|
||||
virtual void vseek(const pos_type n) = 0;
|
||||
virtual pos_type vtell() const = 0;
|
||||
|
||||
static constexpr unsigned FLAG_EOF = 0x01;
|
||||
static constexpr unsigned FLAG_SEEKABLE = 0x04;
|
||||
|
||||
void set_flag(const unsigned flag)
|
||||
{
|
||||
m_flags |= flag;
|
||||
}
|
||||
void clear_flag(const unsigned flag)
|
||||
{
|
||||
m_flags &= ~flag;
|
||||
}
|
||||
unsigned flags() const { return m_flags; }
|
||||
private:
|
||||
|
||||
unsigned m_flags;
|
||||
};
|
||||
|
||||
#if !USE_CSTREAM
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
// pistream: input stream
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
template <typename T>
|
||||
class pistream_base : public pstream
|
||||
{
|
||||
public:
|
||||
|
||||
using char_type = T;
|
||||
|
||||
~pistream_base() noexcept override = default;
|
||||
|
||||
COPYASSIGN(pistream_base, delete)
|
||||
pistream_base &operator=(pistream_base &&src) noexcept = delete;
|
||||
|
||||
bool eof() const { return ((flags() & FLAG_EOF) != 0); }
|
||||
|
||||
pos_type read(T *buf, const pos_type n)
|
||||
{
|
||||
return vread(buf, n);
|
||||
}
|
||||
|
||||
protected:
|
||||
pistream_base() : pstream(0) {}
|
||||
explicit pistream_base(const unsigned flags) : pstream(flags) {}
|
||||
pistream_base(pistream_base &&src) noexcept : pstream(std::move(src)) {}
|
||||
|
||||
/* read up to n bytes from stream */
|
||||
virtual size_type vread(T *buf, const size_type n) = 0;
|
||||
};
|
||||
|
||||
using pistream = pistream_base<char>;
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
// postream: output stream
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
template <typename T>
|
||||
class postream_base : public pstream
|
||||
{
|
||||
public:
|
||||
|
||||
using char_type = T;
|
||||
|
||||
postream_base() = default;
|
||||
~postream_base() noexcept override = default;
|
||||
|
||||
COPYASSIGN(postream_base, delete)
|
||||
postream_base &operator=(postream_base &&src) noexcept = delete;
|
||||
|
||||
void write(const T *buf, const size_type n)
|
||||
{
|
||||
vwrite(buf, n);
|
||||
}
|
||||
|
||||
void write_c_str(const T *buf)
|
||||
{
|
||||
size_type n(0);
|
||||
while (buf[n])
|
||||
n++;
|
||||
vwrite(buf, n);
|
||||
}
|
||||
|
||||
protected:
|
||||
explicit postream_base(unsigned flags) : pstream(flags) {}
|
||||
postream_base(postream_base &&src) noexcept : pstream(std::move(src)) {}
|
||||
|
||||
/* write n bytes to stream */
|
||||
virtual void vwrite(const T *buf, const size_type n) = 0;
|
||||
|
||||
private:
|
||||
};
|
||||
|
||||
using postream = postream_base<char>;
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
// pomemstream: output string stream
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
class pomemstream : public postream
|
||||
{
|
||||
public:
|
||||
|
||||
pomemstream();
|
||||
|
||||
COPYASSIGN(pomemstream, delete)
|
||||
|
||||
pomemstream(pomemstream &&src) noexcept
|
||||
: postream(std::move(src))
|
||||
, m_pos(src.m_pos)
|
||||
, m_mem(std::move(src.m_mem))
|
||||
{
|
||||
}
|
||||
pomemstream &operator=(pomemstream &&src) = delete;
|
||||
|
||||
~pomemstream() override = default;
|
||||
|
||||
const char *memory() const { return m_mem.data(); }
|
||||
pos_type size() const { return static_cast<pos_type>(m_mem.size()); }
|
||||
|
||||
protected:
|
||||
/* write n bytes to stream */
|
||||
void vwrite(const char_type *buf, const pos_type) override;
|
||||
void vseek(const pos_type n) override;
|
||||
pos_type vtell() const override;
|
||||
|
||||
private:
|
||||
pos_type m_pos;
|
||||
std::vector<char> m_mem;
|
||||
};
|
||||
|
||||
class postringstream : public postream
|
||||
{
|
||||
public:
|
||||
|
||||
postringstream() : postream(0) { }
|
||||
postringstream(postringstream &&src) noexcept
|
||||
: postream(std::move(src))
|
||||
, m_buf(std::move(src.m_buf))
|
||||
{ src.m_buf = ""; }
|
||||
|
||||
COPYASSIGN(postringstream, delete)
|
||||
postringstream &operator=(postringstream &&src) = delete;
|
||||
|
||||
~postringstream() override = default;
|
||||
|
||||
const pstring &str() { return m_buf; }
|
||||
|
||||
protected:
|
||||
/* write n bytes to stream */
|
||||
void vwrite(const char_type *buf, const pos_type n) override
|
||||
{
|
||||
//m_buf += pstring(reinterpret_cast<const pstring::mem_t *>(buf), n);
|
||||
m_buf += pstring(buf, static_cast<pstring::size_type>(n));
|
||||
}
|
||||
void vseek(const pos_type n) override { unused_var(n); }
|
||||
pos_type vtell() const override { return static_cast<pos_type>(m_buf.size()); }
|
||||
|
||||
private:
|
||||
pstring m_buf;
|
||||
};
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
// pofilestream: file output stream
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
class pofilestream : public postream
|
||||
{
|
||||
public:
|
||||
|
||||
pofilestream(const pstring &fname);
|
||||
pofilestream(pofilestream &&src) noexcept
|
||||
: postream(std::move(src))
|
||||
, m_file(src.m_file)
|
||||
, m_pos(src.m_pos)
|
||||
, m_actually_close(src.m_actually_close)
|
||||
, m_filename(std::move(src.m_filename))
|
||||
{
|
||||
src.m_file = nullptr;
|
||||
src.m_actually_close = false;
|
||||
}
|
||||
COPYASSIGN(pofilestream, delete)
|
||||
pofilestream &operator=(pofilestream &&src) = delete;
|
||||
|
||||
~pofilestream() override;
|
||||
|
||||
protected:
|
||||
pofilestream(void *file, const pstring &name, const bool do_close);
|
||||
/* write n bytes to stream */
|
||||
void vwrite(const char_type *buf, const pos_type n) override;
|
||||
void vseek(const pos_type n) override;
|
||||
pos_type vtell() const override;
|
||||
|
||||
private:
|
||||
void *m_file;
|
||||
pos_type m_pos;
|
||||
bool m_actually_close;
|
||||
pstring m_filename;
|
||||
|
||||
void init();
|
||||
};
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
// pstderr: write to stderr
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
class pstderr : public pofilestream
|
||||
{
|
||||
public:
|
||||
pstderr();
|
||||
pstderr(pstderr &&src) noexcept = default;
|
||||
pstderr &operator=(pstderr &&src) = delete;
|
||||
COPYASSIGN(pstderr, delete)
|
||||
|
||||
~pstderr() noexcept override= default;
|
||||
};
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
// pstdout: write to stdout
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
class pstdout : public pofilestream
|
||||
{
|
||||
public:
|
||||
pstdout();
|
||||
pstdout(pstdout &&src) noexcept = default;
|
||||
pstdout &operator=(pstdout &&src) = delete;
|
||||
COPYASSIGN(pstdout, delete)
|
||||
|
||||
~pstdout() noexcept override = default;
|
||||
};
|
||||
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
// pifilestream: file input stream
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
class pifilestream : public pistream
|
||||
{
|
||||
public:
|
||||
|
||||
pifilestream(const pstring &fname);
|
||||
~pifilestream() override;
|
||||
|
||||
pifilestream(pifilestream &&src) noexcept
|
||||
: pistream(std::move(src))
|
||||
, m_file(src.m_file)
|
||||
, m_pos(src.m_pos)
|
||||
, m_actually_close(src.m_actually_close)
|
||||
, m_filename(std::move(src.m_filename))
|
||||
{
|
||||
src.m_actually_close = false;
|
||||
src.m_file = nullptr;
|
||||
}
|
||||
COPYASSIGN(pifilestream, delete)
|
||||
pifilestream &operator=(pifilestream &&src) = delete;
|
||||
|
||||
protected:
|
||||
pifilestream(void *file, const pstring &name, const bool do_close);
|
||||
|
||||
/* read up to n bytes from stream */
|
||||
pos_type vread(char_type *buf, const pos_type n) override;
|
||||
void vseek(const pos_type n) override;
|
||||
pos_type vtell() const override;
|
||||
|
||||
private:
|
||||
void *m_file;
|
||||
pos_type m_pos;
|
||||
bool m_actually_close;
|
||||
pstring m_filename;
|
||||
|
||||
void init();
|
||||
};
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
// pstdin: reads from stdin
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
class pstdin : public pifilestream
|
||||
{
|
||||
public:
|
||||
|
||||
pstdin();
|
||||
pstdin(pstdin &&src) noexcept = default;
|
||||
pstdin &operator=(pstdin &&src) = delete;
|
||||
COPYASSIGN(pstdin, delete)
|
||||
~pstdin() override = default;
|
||||
};
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
// pimemstream: input memory stream
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
class pimemstream : public pistream
|
||||
{
|
||||
public:
|
||||
|
||||
pimemstream(const void *mem, const pos_type len);
|
||||
pimemstream();
|
||||
|
||||
pimemstream(pimemstream &&src) noexcept
|
||||
: pistream(std::move(src))
|
||||
, m_pos(src.m_pos)
|
||||
, m_len(src.m_len)
|
||||
, m_mem(src.m_mem)
|
||||
{
|
||||
src.m_mem = nullptr;
|
||||
}
|
||||
COPYASSIGN(pimemstream, delete)
|
||||
pimemstream &operator=(pimemstream &&src) = delete;
|
||||
|
||||
//explicit pimemstream(const pomemstream &ostrm);
|
||||
|
||||
~pimemstream() override = default;
|
||||
|
||||
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 */
|
||||
pos_type vread(char_type *buf, const pos_type n) override;
|
||||
void vseek(const pos_type n) override;
|
||||
pos_type vtell() const override;
|
||||
|
||||
private:
|
||||
pos_type m_pos;
|
||||
pos_type m_len;
|
||||
const char *m_mem;
|
||||
};
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
// pistringstream: input string stream
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
class pistringstream : public pimemstream
|
||||
{
|
||||
public:
|
||||
pistringstream(const pstring &str)
|
||||
: pimemstream()
|
||||
, m_str(str)
|
||||
{
|
||||
set_mem(m_str.c_str(), static_cast<pos_type>(pstring_mem_t_size(m_str)));
|
||||
}
|
||||
pistringstream(pistringstream &&src) noexcept
|
||||
: pimemstream(std::move(src)), m_str(src.m_str)
|
||||
{
|
||||
set_mem(m_str.c_str(), static_cast<pos_type>(pstring_mem_t_size(m_str)));
|
||||
}
|
||||
COPYASSIGN(pistringstream, delete)
|
||||
pistringstream &operator=(pistringstream &&src) = delete;
|
||||
|
||||
~pistringstream() override = default;
|
||||
|
||||
private:
|
||||
/* only needed for a reference till destruction */
|
||||
const pstring m_str;
|
||||
};
|
||||
#endif
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
// putf8reader_t: reader on top of istream
|
||||
// -----------------------------------------------------------------------------
|
||||
@ -459,7 +34,7 @@ private:
|
||||
template <typename T>
|
||||
struct constructor_helper
|
||||
{
|
||||
plib::unique_ptr<pistream> operator()(T &&s) { return std::move(plib::make_unique<T>(std::move(s))); }
|
||||
plib::unique_ptr<std::istream> operator()(T &&s) { return std::move(plib::make_unique<T>(std::move(s))); }
|
||||
};
|
||||
|
||||
// NOLINTNEXTLINE(cppcoreguidelines-special-member-functions)
|
||||
@ -482,27 +57,7 @@ public:
|
||||
bool eof() const { return m_strm->eof(); }
|
||||
bool readline(pstring &line);
|
||||
|
||||
|
||||
#if !USE_CSTREAM
|
||||
bool readbyte1(pistream::char_type &b)
|
||||
{
|
||||
return (m_strm->read(&b, 1) == 1);
|
||||
}
|
||||
|
||||
bool readcode(putf8string::traits_type::code_t &c)
|
||||
{
|
||||
std::array<pistream::char_type, 4> b{0};
|
||||
if (m_strm->read(&b[0], 1) != 1)
|
||||
return false;
|
||||
const std::size_t l = putf8string::traits_type::codelen(reinterpret_cast<putf8string::traits_type::mem_t *>(&b));
|
||||
for (std::size_t i = 1; i < l; i++)
|
||||
if (m_strm->read(&b[i], 1) != 1)
|
||||
return false;
|
||||
c = putf8string::traits_type::code(reinterpret_cast<putf8string::traits_type::mem_t *>(&b));
|
||||
return true;
|
||||
}
|
||||
#else
|
||||
bool readbyte1(pistream::char_type &b)
|
||||
bool readbyte1(std::istream::char_type &b)
|
||||
{
|
||||
if (m_strm->eof())
|
||||
return false;
|
||||
@ -511,7 +66,7 @@ public:
|
||||
}
|
||||
bool readcode(putf8string::traits_type::code_t &c)
|
||||
{
|
||||
std::array<pistream::char_type, 4> b{0};
|
||||
std::array<std::istream::char_type, 4> b{0};
|
||||
if (m_strm->eof())
|
||||
return false;
|
||||
m_strm->read(&b[0], 1);
|
||||
@ -526,22 +81,22 @@ public:
|
||||
return true;
|
||||
}
|
||||
|
||||
#endif
|
||||
std::istream &stream() { return *m_strm; }
|
||||
private:
|
||||
plib::unique_ptr<pistream> m_strm;
|
||||
plib::unique_ptr<std::istream> m_strm;
|
||||
putf8string m_linebuf;
|
||||
};
|
||||
|
||||
template <>
|
||||
struct constructor_helper<putf8_reader>
|
||||
{
|
||||
plib::unique_ptr<pistream> operator()(putf8_reader &&s) { return std::move(s.m_strm); }
|
||||
plib::unique_ptr<std::istream> operator()(putf8_reader &&s) { return std::move(s.m_strm); }
|
||||
};
|
||||
|
||||
template <>
|
||||
struct constructor_helper<plib::unique_ptr<pistream>>
|
||||
struct constructor_helper<plib::unique_ptr<std::istream>>
|
||||
{
|
||||
plib::unique_ptr<pistream> operator()(plib::unique_ptr<pistream> &&s) { return std::move(s); }
|
||||
plib::unique_ptr<std::istream> operator()(plib::unique_ptr<std::istream> &&s) { return std::move(s); }
|
||||
};
|
||||
|
||||
|
||||
@ -552,7 +107,7 @@ struct constructor_helper<plib::unique_ptr<pistream>>
|
||||
class putf8_writer
|
||||
{
|
||||
public:
|
||||
explicit putf8_writer(postream *strm) : m_strm(strm) {}
|
||||
explicit putf8_writer(std::ostream *strm) : m_strm(strm) {}
|
||||
|
||||
putf8_writer(putf8_writer &&src) noexcept : m_strm(src.m_strm) {}
|
||||
|
||||
@ -581,14 +136,14 @@ public:
|
||||
}
|
||||
|
||||
private:
|
||||
postream *m_strm;
|
||||
std::ostream *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(std::ostream *strm)
|
||||
: pfmt_writer_t()
|
||||
, putf8_writer(strm)
|
||||
{
|
||||
@ -611,23 +166,23 @@ private:
|
||||
class pbinary_writer
|
||||
{
|
||||
public:
|
||||
explicit pbinary_writer(postream &strm) : m_strm(strm) {}
|
||||
explicit pbinary_writer(std::ostream &strm) : m_strm(strm) {}
|
||||
pbinary_writer(pbinary_writer &&src) noexcept : m_strm(src.m_strm) {}
|
||||
|
||||
COPYASSIGN(pbinary_writer, delete)
|
||||
postringstream &operator=(pbinary_writer &&src) = delete;
|
||||
pbinary_writer &operator=(pbinary_writer &&src) = delete;
|
||||
|
||||
virtual ~pbinary_writer() = default;
|
||||
|
||||
template <typename T>
|
||||
void write(const T &val)
|
||||
{
|
||||
m_strm.write(reinterpret_cast<const postream::char_type *>(&val), sizeof(T));
|
||||
m_strm.write(reinterpret_cast<const std::ostream::char_type *>(&val), sizeof(T));
|
||||
}
|
||||
|
||||
void write(const pstring &s)
|
||||
{
|
||||
const auto sm = reinterpret_cast<const postream::char_type *>(s.c_str());
|
||||
const auto sm = reinterpret_cast<const std::ostream::char_type *>(s.c_str());
|
||||
const std::streamsize sl(static_cast<std::streamsize>(pstring_mem_t_size(s)));
|
||||
write(sl);
|
||||
m_strm.write(sm, sl);
|
||||
@ -638,17 +193,17 @@ public:
|
||||
{
|
||||
const std::streamsize sz(static_cast<std::streamsize>(val.size()));
|
||||
write(sz);
|
||||
m_strm.write(reinterpret_cast<const postream::char_type *>(val.data()), sz * static_cast<std::streamsize>(sizeof(T)));
|
||||
m_strm.write(reinterpret_cast<const std::ostream::char_type *>(val.data()), sz * static_cast<std::streamsize>(sizeof(T)));
|
||||
}
|
||||
|
||||
private:
|
||||
postream &m_strm;
|
||||
std::ostream &m_strm;
|
||||
};
|
||||
|
||||
class pbinary_reader
|
||||
{
|
||||
public:
|
||||
explicit pbinary_reader(pistream &strm) : m_strm(strm) {}
|
||||
explicit pbinary_reader(std::istream &strm) : m_strm(strm) {}
|
||||
pbinary_reader(pbinary_reader &&src) noexcept : m_strm(src.m_strm) { }
|
||||
|
||||
COPYASSIGN(pbinary_reader, delete)
|
||||
@ -659,7 +214,7 @@ public:
|
||||
template <typename T>
|
||||
void read(T &val)
|
||||
{
|
||||
m_strm.read(reinterpret_cast<pistream::char_type *>(&val), sizeof(T));
|
||||
m_strm.read(reinterpret_cast<std::istream::char_type *>(&val), sizeof(T));
|
||||
}
|
||||
|
||||
void read( pstring &s)
|
||||
@ -678,29 +233,22 @@ public:
|
||||
std::size_t sz = 0;
|
||||
read(sz);
|
||||
val.resize(sz);
|
||||
m_strm.read(reinterpret_cast<pistream::char_type *>(val.data()), static_cast<std::streamsize>(sizeof(T) * sz));
|
||||
m_strm.read(reinterpret_cast<std::istream::char_type *>(val.data()), static_cast<std::streamsize>(sizeof(T) * sz));
|
||||
}
|
||||
|
||||
private:
|
||||
pistream &m_strm;
|
||||
std::istream &m_strm;
|
||||
};
|
||||
|
||||
inline void copystream(postream &dest, pistream &src)
|
||||
inline void copystream(std::ostream &dest, std::istream &src)
|
||||
{
|
||||
// FIXME: optimize
|
||||
#if !USE_CSTREAM
|
||||
std::array<postream::char_type, 1024> buf; // NOLINT(cppcoreguidelines-pro-type-member-init)
|
||||
pstream::pos_type r;
|
||||
while ((r=src.read(buf.data(), 1024)) > 0)
|
||||
dest.write(buf.data(), r);
|
||||
#else
|
||||
std::array<postream::char_type, 1024> buf; // NOLINT(cppcoreguidelines-pro-type-member-init)
|
||||
std::array<std::ostream::char_type, 1024> buf; // NOLINT(cppcoreguidelines-pro-type-member-init)
|
||||
while (!src.eof())
|
||||
{
|
||||
src.read(buf.data(), 1);
|
||||
dest.write(buf.data(), 1);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
struct perrlogger
|
||||
@ -713,16 +261,24 @@ struct perrlogger
|
||||
private:
|
||||
static putf8_fmt_writer &h()
|
||||
{
|
||||
#if !USE_CSTREAM
|
||||
static plib::pstderr perr_strm;
|
||||
static plib::putf8_fmt_writer perr(&perr_strm);
|
||||
#else
|
||||
static plib::putf8_fmt_writer perr(&std::cerr);
|
||||
#endif
|
||||
return perr;
|
||||
}
|
||||
};
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
// c++17 preparation
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
namespace filesystem
|
||||
{
|
||||
template< class Source >
|
||||
pstring /*path */ u8path( const Source& source )
|
||||
{
|
||||
return pstring(source);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
} // namespace plib
|
||||
|
||||
|
@ -11,6 +11,7 @@
|
||||
#include "netlist/plib/pmain.h"
|
||||
#include "netlist/devices/net_lib.h"
|
||||
#include "netlist/nl_parser.h"
|
||||
#include "netlist/nl_errstr.h"
|
||||
#include "netlist/nl_setup.h"
|
||||
#include "netlist/solver/nld_solver.h"
|
||||
#include "netlist/tools/nl_convert.h"
|
||||
@ -18,6 +19,7 @@
|
||||
#include <cstdio> // scanf
|
||||
#include <iomanip> // scanf
|
||||
#include <iostream> // scanf
|
||||
#include <ios>
|
||||
|
||||
#define NLTOOL_VERSION 20190420
|
||||
|
||||
@ -137,20 +139,17 @@ public:
|
||||
{
|
||||
}
|
||||
|
||||
plib::unique_ptr<plib::pistream> stream(const pstring &file) override
|
||||
plib::unique_ptr<std::istream> stream(const pstring &file) override
|
||||
{
|
||||
pstring name = m_folder + "/" + file;
|
||||
try
|
||||
auto strm(plib::make_unique<std::ifstream>(plib::filesystem::u8path(name)));
|
||||
if (strm->fail())
|
||||
return plib::unique_ptr<std::istream>(nullptr);
|
||||
else
|
||||
{
|
||||
auto strm = plib::make_unique<plib::pifilestream>(name);
|
||||
strm->imbue(std::locale::classic());
|
||||
return std::move(strm);
|
||||
}
|
||||
catch (const plib::pexception &e)
|
||||
{
|
||||
if (dynamic_cast<const plib::file_open_e *>(&e) == nullptr )
|
||||
throw;
|
||||
}
|
||||
return plib::unique_ptr<plib::pistream>(nullptr);
|
||||
}
|
||||
|
||||
private:
|
||||
@ -330,7 +329,10 @@ static std::vector<input_t> read_input(const netlist::setup_t &setup, const pstr
|
||||
std::vector<input_t> ret;
|
||||
if (fname != "")
|
||||
{
|
||||
plib::putf8_reader r = plib::putf8_reader(plib::pifilestream(fname));
|
||||
plib::putf8_reader r = plib::putf8_reader(std::ifstream(plib::filesystem::u8path(fname)));
|
||||
if (r.stream().fail())
|
||||
throw netlist::nl_exception(netlist::MF_FILE_OPEN_ERROR(fname));
|
||||
r.stream().imbue(std::locale::classic());
|
||||
pstring l;
|
||||
while (r.readline(l))
|
||||
{
|
||||
@ -385,7 +387,10 @@ void tool_app_t::run()
|
||||
// FIXME: error handling
|
||||
if (opt_loadstate.was_specified())
|
||||
{
|
||||
plib::pifilestream strm(opt_loadstate());
|
||||
std::ifstream strm(plib::filesystem::u8path(opt_loadstate()));
|
||||
if (strm.fail())
|
||||
throw netlist::nl_exception(netlist::MF_FILE_OPEN_ERROR(opt_loadstate()));
|
||||
strm.imbue(std::locale::classic());
|
||||
plib::pbinary_reader reader(strm);
|
||||
std::vector<char> loadstate;
|
||||
reader.read(loadstate);
|
||||
@ -420,7 +425,11 @@ void tool_app_t::run()
|
||||
if (opt_savestate.was_specified())
|
||||
{
|
||||
auto savestate = nt.save_state();
|
||||
plib::pofilestream strm(opt_savestate());
|
||||
std::ofstream strm(plib::filesystem::u8path(opt_savestate()), std::ios_base::binary);
|
||||
if (strm.fail())
|
||||
throw plib::file_open_e(opt_savestate());
|
||||
strm.imbue(std::locale::classic());
|
||||
|
||||
plib::pbinary_writer writer(strm);
|
||||
writer.write(savestate);
|
||||
}
|
||||
@ -728,20 +737,19 @@ void tool_app_t::listdevices()
|
||||
void tool_app_t::convert()
|
||||
{
|
||||
pstring contents;
|
||||
plib::postringstream ostrm;
|
||||
std::stringstream ostrm;
|
||||
ostrm.imbue(std::locale::classic());
|
||||
if (opt_file() == "-")
|
||||
{
|
||||
#if !USE_CSTREAM
|
||||
plib::pstdin f;
|
||||
plib::copystream(ostrm, f);
|
||||
#else
|
||||
plib::copystream(ostrm, std::cin);
|
||||
#endif
|
||||
}
|
||||
else
|
||||
{
|
||||
plib::pifilestream f(opt_file());
|
||||
plib::copystream(ostrm, f);
|
||||
std::ifstream strm(plib::filesystem::u8path(opt_file()));
|
||||
if (strm.fail())
|
||||
throw netlist::nl_exception(netlist::MF_FILE_OPEN_ERROR(opt_file()));
|
||||
strm.imbue(std::locale::classic());
|
||||
plib::copystream(ostrm, strm);
|
||||
}
|
||||
contents = pstring(ostrm.str());
|
||||
|
||||
@ -874,6 +882,8 @@ int tool_app_t::execute()
|
||||
std::cout << plib::pfmt("{:20}")("Общая ком") << "|" << "\n";
|
||||
|
||||
//char x = 'a';
|
||||
//auto b= U'щ';
|
||||
|
||||
auto b= U'\U00000449';
|
||||
std::cout << "b: <" << b << ">";
|
||||
#endif
|
||||
|
@ -24,7 +24,7 @@ class wav_t
|
||||
{
|
||||
public:
|
||||
// XXNOLINTNEXTLINE(cppcoreguidelines-pro-type-member-init)
|
||||
wav_t(plib::postream &strm, bool is_seekable, std::size_t sr, std::size_t channels)
|
||||
wav_t(std::ostream &strm, bool is_seekable, std::size_t sr, std::size_t channels)
|
||||
: m_f(strm)
|
||||
, m_stream_is_seekable(is_seekable)
|
||||
/* force "play" to play and warn about eof instead of being silent */
|
||||
@ -59,7 +59,7 @@ public:
|
||||
template <typename T>
|
||||
void write(const T &val)
|
||||
{
|
||||
m_f.write(reinterpret_cast<const plib::postream::char_type *>(&val), sizeof(T));
|
||||
m_f.write(reinterpret_cast<const std::ostream::char_type *>(&val), sizeof(T));
|
||||
}
|
||||
|
||||
void write_sample(int *sample)
|
||||
@ -107,7 +107,7 @@ private:
|
||||
// data follows
|
||||
};
|
||||
|
||||
plib::postream &m_f;
|
||||
std::ostream &m_f;
|
||||
bool m_stream_is_seekable;
|
||||
|
||||
riff_chunk_t m_fh;
|
||||
@ -157,7 +157,7 @@ public:
|
||||
return success;
|
||||
}
|
||||
|
||||
void process(std::vector<plib::unique_ptr<plib::pistream>> &is)
|
||||
void process(std::vector<plib::unique_ptr<std::istream>> &is)
|
||||
{
|
||||
std::vector<plib::putf8_reader> readers;
|
||||
for (auto &i : is)
|
||||
@ -240,7 +240,7 @@ private:
|
||||
class wavwriter
|
||||
{
|
||||
public:
|
||||
wavwriter(plib::postream &fo, bool is_seekable, std::size_t channels, std::size_t sample_rate, double ampa)
|
||||
wavwriter(std::ostream &fo, bool is_seekable, std::size_t channels, std::size_t sample_rate, double ampa)
|
||||
: mean(channels, 0.0)
|
||||
, means(channels, 0.0)
|
||||
, maxsam(channels, -1e9)
|
||||
@ -281,7 +281,7 @@ public:
|
||||
|
||||
private:
|
||||
|
||||
plib::postream &m_fo;
|
||||
std::ostream &m_fo;
|
||||
double m_amp;
|
||||
wav_t m_wo;
|
||||
};
|
||||
@ -296,7 +296,7 @@ public:
|
||||
ANALOG
|
||||
};
|
||||
|
||||
vcdwriter(plib::postream &fo, const std::vector<pstring> &channels,
|
||||
vcdwriter(std::ostream &fo, const std::vector<pstring> &channels,
|
||||
format_e format, double high_level = 2.0, double low_level = 1.0)
|
||||
: m_channels(channels.size())
|
||||
, m_last_time(0)
|
||||
@ -363,7 +363,7 @@ private:
|
||||
std::size_t m_channels;
|
||||
double m_last_time;
|
||||
|
||||
plib::postream &m_fo;
|
||||
std::ostream &m_fo;
|
||||
std::vector<pstring> m_ids;
|
||||
pstring m_buf;
|
||||
double m_high_level;
|
||||
@ -420,11 +420,8 @@ private:
|
||||
plib::option_bool opt_help;
|
||||
plib::option_example opt_ex1;
|
||||
plib::option_example opt_ex2;
|
||||
#if !USE_CSTREAM
|
||||
plib::pstdin pin_strm;
|
||||
#endif
|
||||
std::vector<plib::unique_ptr<plib::pistream>> m_instrms;
|
||||
plib::postream *m_outstrm;
|
||||
std::vector<plib::unique_ptr<std::istream>> m_instrms;
|
||||
std::ostream *m_outstrm;
|
||||
};
|
||||
|
||||
void nlwav_app::convert_wav()
|
||||
@ -502,22 +499,24 @@ int nlwav_app::execute()
|
||||
return 0;
|
||||
}
|
||||
|
||||
#if !USE_CSTREAM
|
||||
m_outstrm = (opt_out() == "-" ? &pout_strm : plib::pnew<plib::pofilestream>(opt_out()));
|
||||
#else
|
||||
m_outstrm = (opt_out() == "-" ? &std::cout : plib::pnew<plib::pofilestream>(opt_out()));
|
||||
#endif
|
||||
m_outstrm = (opt_out() == "-" ? &std::cout : plib::pnew<std::ofstream>(plib::filesystem::u8path(opt_out())));
|
||||
if (m_outstrm->fail())
|
||||
throw plib::file_open_e(opt_out());
|
||||
m_outstrm->imbue(std::locale::classic());
|
||||
|
||||
for (auto &oi: opt_args())
|
||||
{
|
||||
#if !USE_CSTREAM
|
||||
plib::unique_ptr<plib::pistream> fin = (oi == "-" ?
|
||||
plib::make_unique<plib::pstdin>()
|
||||
: plib::make_unique<plib::pifilestream>(oi));
|
||||
#else
|
||||
//FIXME:
|
||||
plib::unique_ptr<plib::pistream> fin = plib::make_unique<plib::pifilestream>(oi);
|
||||
#endif
|
||||
plib::unique_ptr<std::istream> fin;
|
||||
|
||||
if (oi == "-")
|
||||
{
|
||||
auto temp(plib::make_unique<std::stringstream>());
|
||||
plib::copystream(*temp, std::cin);
|
||||
fin = std::move(temp);
|
||||
}
|
||||
else
|
||||
fin = plib::make_unique<std::ifstream>(plib::filesystem::u8path(oi));
|
||||
fin->imbue(std::locale::classic());
|
||||
m_instrms.push_back(std::move(fin));
|
||||
}
|
||||
|
||||
|
@ -262,7 +262,8 @@ namespace devices
|
||||
template <typename FT, int SIZE>
|
||||
pstring matrix_solver_GCR_t<FT, SIZE>::static_compile_name()
|
||||
{
|
||||
plib::postringstream t;
|
||||
std::stringstream t;
|
||||
t.imbue(std::locale::classic());
|
||||
plib::putf8_fmt_writer w(&t);
|
||||
csc_private(w);
|
||||
std::hash<typename std::remove_const<std::remove_reference<decltype(t.str())>::type>::type> h;
|
||||
@ -273,7 +274,8 @@ namespace devices
|
||||
template <typename FT, int SIZE>
|
||||
std::pair<pstring, pstring> matrix_solver_GCR_t<FT, SIZE>::create_solver_code()
|
||||
{
|
||||
plib::postringstream t;
|
||||
std::stringstream t;
|
||||
t.imbue(std::locale::classic());
|
||||
plib::putf8_fmt_writer strm(&t);
|
||||
pstring name = static_compile_name();
|
||||
|
||||
@ -281,7 +283,7 @@ namespace devices
|
||||
strm.writeline("{\n");
|
||||
csc_private(strm);
|
||||
strm.writeline("}\n");
|
||||
return std::pair<pstring, pstring>(name, t.str());
|
||||
return std::pair<pstring, pstring>(name, pstring(t.str()));
|
||||
}
|
||||
|
||||
template <typename FT, int SIZE>
|
||||
|
@ -43,7 +43,8 @@ using lib_map_t = std::unordered_map<pstring, lib_map_entry>;
|
||||
|
||||
static lib_map_t read_lib_map(const pstring &lm)
|
||||
{
|
||||
auto reader = plib::putf8_reader(plib::pistringstream(lm));
|
||||
auto reader = plib::putf8_reader(std::istringstream(lm));
|
||||
reader.stream().imbue(std::locale::classic());
|
||||
lib_map_t m;
|
||||
pstring line;
|
||||
while (reader.readline(line))
|
||||
@ -62,6 +63,7 @@ nl_convert_base_t::nl_convert_base_t()
|
||||
: out(&m_buf)
|
||||
, m_numberchars("0123456789-+e.")
|
||||
{
|
||||
m_buf.imbue(std::locale::classic());
|
||||
m_units = {
|
||||
{"T", "", 1.0e12 },
|
||||
{"G", "", 1.0e9 },
|
||||
@ -484,7 +486,8 @@ void nl_convert_eagle_t::tokenizer::verror(const pstring &msg, int line_num, con
|
||||
void nl_convert_eagle_t::convert(const pstring &contents)
|
||||
{
|
||||
|
||||
tokenizer tok(*this, plib::putf8_reader(plib::pistringstream(contents)));
|
||||
tokenizer tok(*this, plib::putf8_reader(std::istringstream(contents)));
|
||||
tok.stream().stream().imbue(std::locale::classic());
|
||||
|
||||
out("NETLIST_START(dummy)\n");
|
||||
add_term("GND", "GND");
|
||||
@ -631,7 +634,8 @@ void nl_convert_rinf_t::tokenizer::verror(const pstring &msg, int line_num, cons
|
||||
|
||||
void nl_convert_rinf_t::convert(const pstring &contents)
|
||||
{
|
||||
tokenizer tok(*this, plib::putf8_reader(plib::pistringstream(contents)));
|
||||
tokenizer tok(*this, plib::putf8_reader(std::istringstream(contents)));
|
||||
tok.stream().stream().imbue(std::locale::classic());
|
||||
auto lm = read_lib_map(s_lib_map);
|
||||
|
||||
out("NETLIST_START(dummy)\n");
|
||||
|
@ -139,7 +139,7 @@ private:
|
||||
|
||||
void add_device(plib::unique_ptr<dev_t> dev);
|
||||
|
||||
plib::postringstream m_buf;
|
||||
std::stringstream m_buf;
|
||||
|
||||
std::vector<plib::unique_ptr<dev_t>> m_devs;
|
||||
std::unordered_map<pstring, plib::unique_ptr<net_t> > m_nets;
|
||||
|
Loading…
Reference in New Issue
Block a user