From 3f9bb5d1eae63463ee7b193d8544a98f36a2fe55 Mon Sep 17 00:00:00 2001 From: couriersud Date: Sat, 6 Jun 2020 20:41:11 +0200 Subject: [PATCH] netlist: Enable utf7 filenames on windows. (nw) --- src/lib/netlist/devices/nld_log.cpp | 2 +- src/lib/netlist/nl_setup.cpp | 2 +- src/lib/netlist/plib/pconfig.h | 6 ++-- src/lib/netlist/plib/pmain.h | 5 ++-- src/lib/netlist/plib/pstream.h | 44 +++++++++++++++++++++++++++++ src/lib/netlist/plib/putil.cpp | 7 +++++ src/lib/netlist/plib/putil.h | 1 + src/lib/netlist/prg/nltool.cpp | 21 ++++++++------ src/lib/netlist/prg/nlwav.cpp | 4 +-- 9 files changed, 75 insertions(+), 17 deletions(-) diff --git a/src/lib/netlist/devices/nld_log.cpp b/src/lib/netlist/devices/nld_log.cpp index cf9ed418fe7..f12229edaa3 100644 --- a/src/lib/netlist/devices/nld_log.cpp +++ b/src/lib/netlist/devices/nld_log.cpp @@ -44,7 +44,7 @@ namespace netlist NETLIB_RESETI() { m_reset = true; } protected: analog_input_t m_I; - std::ofstream m_strm; + plib::ofstream m_strm; plib::putf8_writer m_writer; bool m_reset; }; diff --git a/src/lib/netlist/nl_setup.cpp b/src/lib/netlist/nl_setup.cpp index 5960cae62d6..10c70b109a1 100644 --- a/src/lib/netlist/nl_setup.cpp +++ b/src/lib/netlist/nl_setup.cpp @@ -1611,7 +1611,7 @@ source_mem_t::stream_ptr source_mem_t::stream(const pstring &name) source_file_t::stream_ptr source_file_t::stream(const pstring &name) { plib::unused_var(name); - auto ret(plib::make_unique(plib::filesystem::u8path(m_filename))); + auto ret(plib::make_unique(plib::filesystem::u8path(m_filename))); return (ret->is_open()) ? std::move(ret) : stream_ptr(nullptr); } diff --git a/src/lib/netlist/plib/pconfig.h b/src/lib/netlist/plib/pconfig.h index 8b169260360..4e116b90344 100644 --- a/src/lib/netlist/plib/pconfig.h +++ b/src/lib/netlist/plib/pconfig.h @@ -42,7 +42,7 @@ /// OpenMP adds about 10% to 20% performance for analog netlists. /// #ifndef PUSE_OPENMP -#define PUSE_OPENMP (0) +#define PUSE_OPENMP (1) #endif /// \brief Use aligned optimizations. @@ -173,7 +173,9 @@ typedef __float128 FLOAT128; #if (PUSE_OPENMP) #if (!(PHAS_OPENMP)) -#error To use openmp compile and link with "-fopenmp" +//#error To use openmp compile and link with "-fopenmp" +#undef PUSE_OPENMP +#define PUSE_OPENMP (0) #endif #endif diff --git a/src/lib/netlist/plib/pmain.h b/src/lib/netlist/plib/pmain.h index fa57363926d..756bcf49f18 100755 --- a/src/lib/netlist/plib/pmain.h +++ b/src/lib/netlist/plib/pmain.h @@ -15,6 +15,7 @@ #include "putil.h" #include +#include #ifdef _WIN32 #include @@ -52,8 +53,8 @@ namespace plib { template static int mainrun(int argc, T **argv) { - auto a = plib::make_unique(); - return a->main_utfX(argc, argv); + C application;; + return application.main_utfX(argc, argv); } private: diff --git a/src/lib/netlist/plib/pstream.h b/src/lib/netlist/plib/pstream.h index 04ca9e4344c..e5fc0e24b80 100644 --- a/src/lib/netlist/plib/pstream.h +++ b/src/lib/netlist/plib/pstream.h @@ -21,6 +21,7 @@ #include #include #include +#include #include #include @@ -261,6 +262,49 @@ inline void copystream(std::ostream &dest, std::istream &src) } } +/// +/// \brief utf8 filename aware ifstream wrapper +/// +class ifstream : public std::ifstream +{ +public: +#ifdef _WIN32 + template + explicit ifstream(const pstring_t name, ios_base::openmode mode = ios_base::in) + : std::ifstream(reinterpret_cast(pstring_t(name).c_str()), mode) + { + } +#else + template + explicit ifstream(const pstring_t name, ios_base::openmode mode = ios_base::in) + : std::ifstream(pstring_t(name).c_str(), mode) + { + } +#endif +}; + +/// +/// \brief utf8 filename aware ofstream wrapper +/// +class ofstream : public std::ofstream +{ +public: +#ifdef _WIN32 + template + explicit ofstream(const pstring_t name, ios_base::openmode mode = ios_base::in) + : std::ofstream(reinterpret_cast(pstring_t(name).c_str()), mode) + { + } +#else + template + explicit ofstream(const pstring_t name, ios_base::openmode mode = ios_base::in) + : std::ofstream(pstring_t(name).c_str(), mode) + { + } +#endif +}; + + struct perrlogger { template diff --git a/src/lib/netlist/plib/putil.cpp b/src/lib/netlist/plib/putil.cpp index 56ada5c2dd9..44e8141f10a 100644 --- a/src/lib/netlist/plib/putil.cpp +++ b/src/lib/netlist/plib/putil.cpp @@ -5,6 +5,7 @@ #include "penum.h" #include "pstrutil.h" #include "ptypes.h" +#include "pstream.h" #include #include // needed for getenv ... @@ -42,6 +43,12 @@ namespace plib return filename.substr(0, p); } + bool exists (const pstring &filename) + { + plib::ifstream f(filename); + return f.good(); + } + pstring buildpath(std::initializer_list list ) { pstring ret = ""; diff --git a/src/lib/netlist/plib/putil.h b/src/lib/netlist/plib/putil.h index 502aa433f06..401cc35e262 100644 --- a/src/lib/netlist/plib/putil.h +++ b/src/lib/netlist/plib/putil.h @@ -253,6 +253,7 @@ namespace plib { pstring basename(const pstring &filename, const pstring &suffix = ""); pstring path(const pstring &filename); + bool exists(const pstring &filename); pstring buildpath(std::initializer_list list ); pstring environment(const pstring &var, const pstring &default_val); } // namespace util diff --git a/src/lib/netlist/prg/nltool.cpp b/src/lib/netlist/prg/nltool.cpp index 1f024ce99f3..938fe4b32ed 100644 --- a/src/lib/netlist/prg/nltool.cpp +++ b/src/lib/netlist/prg/nltool.cpp @@ -201,7 +201,7 @@ public: stream_ptr stream(const pstring &file) override { pstring name = m_folder + "/" + file; - auto strm(plib::make_unique(plib::filesystem::u8path(name))); + auto strm(plib::make_unique(plib::filesystem::u8path(name))); if (strm->fail()) return stream_ptr(nullptr); @@ -385,7 +385,7 @@ static std::vector read_input(const netlist::setup_t &setup, const pstr std::vector ret; if (fname != "") { - plib::putf8_reader r = plib::putf8_reader(plib::make_unique(plib::filesystem::u8path(fname))); + plib::putf8_reader r = plib::putf8_reader(plib::make_unique(plib::filesystem::u8path(fname))); if (r.stream().fail()) throw netlist::nl_exception(netlist::MF_FILE_OPEN_ERROR(fname)); r.stream().imbue(std::locale::classic()); @@ -411,6 +411,9 @@ void tool_app_t::run() if (opt_files().size() != 1) throw netlist::nl_exception("nltool: run needs exactly one file"); + if (!plib::util::exists(opt_files()[0])) + throw netlist::nl_exception("nltool: file doesn't exists: {}", opt_files()[0]); + netlist_tool_t nt(*this, "netlist", opt_boostlib()); { @@ -441,7 +444,7 @@ void tool_app_t::run() // FIXME: error handling if (opt_loadstate.was_specified()) { - std::ifstream strm(plib::filesystem::u8path(opt_loadstate())); + plib::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()); @@ -484,7 +487,7 @@ void tool_app_t::run() if (opt_savestate.was_specified()) { auto savestate = nt.save_state(); - std::ofstream strm(plib::filesystem::u8path(opt_savestate()), std::ios_base::binary); + plib::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()); @@ -608,7 +611,7 @@ void tool_app_t::static_compile() for (auto &e : mp) { - std::ofstream sout(opt_dir() + "/" + e.first + ".c" ); + plib::ofstream sout(opt_dir() + "/" + e.first + ".c" ); sout << e.second.m_code; } } @@ -623,7 +626,7 @@ void tool_app_t::static_compile() names.push_back(opt_name()); else { - plib::putf8_reader r = plib::putf8_reader(plib::make_unique(plib::filesystem::u8path(f))); + plib::putf8_reader r = plib::putf8_reader(plib::make_unique(plib::filesystem::u8path(f))); if (r.stream().fail()) throw netlist::nl_exception(netlist::MF_FILE_OPEN_ERROR(f)); r.stream().imbue(std::locale::classic()); @@ -655,7 +658,7 @@ void tool_app_t::static_compile() compile_one_and_add_to_map(f, name, target, map); } } - std::ofstream sout = std::ofstream(opt_out()); + plib::ofstream sout(opt_out()); sout << "#include \"plib/pdynlib.h\"\n\n"; for (auto &e : map) @@ -703,7 +706,7 @@ struct doc_ext static doc_ext read_docsrc(const pstring &fname, const pstring &id) { - plib::putf8_reader r = plib::putf8_reader(plib::make_unique(plib::filesystem::u8path(fname))); + plib::putf8_reader r = plib::putf8_reader(plib::make_unique(plib::filesystem::u8path(fname))); if (r.stream().fail()) throw netlist::nl_exception(netlist::MF_FILE_OPEN_ERROR(fname)); r.stream().imbue(std::locale::classic()); @@ -1109,7 +1112,7 @@ void tool_app_t::convert() } else { - std::ifstream strm(plib::filesystem::u8path(opt_files()[0])); + plib::ifstream strm(plib::filesystem::u8path(opt_files()[0])); if (strm.fail()) throw netlist::nl_exception(netlist::MF_FILE_OPEN_ERROR(opt_files()[0])); strm.imbue(std::locale::classic()); diff --git a/src/lib/netlist/prg/nlwav.cpp b/src/lib/netlist/prg/nlwav.cpp index 288e14ae028..291aa5b2ff7 100755 --- a/src/lib/netlist/prg/nlwav.cpp +++ b/src/lib/netlist/prg/nlwav.cpp @@ -723,7 +723,7 @@ static void open_ostream_and_exec(const pstring &fname, bool binary, F func) if (fname != "-") { // FIXME: binary depends on format! - std::ofstream outstrm(plib::filesystem::u8path(fname), + plib::ofstream outstrm(plib::filesystem::u8path(fname), binary ? (std::ios::out | std::ios::binary) : std::ios::out); if (outstrm.fail()) throw plib::file_open_e(fname); @@ -802,7 +802,7 @@ int nlwav_app::execute() } else { - fin = plib::make_unique(plib::filesystem::u8path(oi), std::ios::in); + fin = plib::make_unique(plib::filesystem::u8path(oi), std::ios::in); if (fin->fail()) throw plib::file_open_e(oi); }