From 500ca5b8fc6cd17e224f154de0914e95f080f51e Mon Sep 17 00:00:00 2001 From: couriersud Date: Tue, 17 Sep 2019 21:05:01 +0200 Subject: netlist code maintenance. (nw) - remove a lot of c library use and instead use c++ - improved pstring compatibility to std::string - prepare removal of pstream --- src/lib/netlist/plib/pfmtlog.cpp | 179 +++++++++++++++++++++++---------------- 1 file changed, 106 insertions(+), 73 deletions(-) (limited to 'src/lib/netlist/plib/pfmtlog.cpp') diff --git a/src/lib/netlist/plib/pfmtlog.cpp b/src/lib/netlist/plib/pfmtlog.cpp index a42546629cf..2d2ee40562c 100644 --- a/src/lib/netlist/plib/pfmtlog.cpp +++ b/src/lib/netlist/plib/pfmtlog.cpp @@ -10,100 +10,133 @@ #include #include -#include -#include -#include -#include #include -#include +#include +#include namespace plib { -pfmt &pfmt::format_element(const char *l, const unsigned cfmt_spec, ...) +#if 0 +struct ptemporary_locale { - va_list ap; - // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg) - std::array buf = {0}; - std::size_t sl; - bool found_abs = false; - pstring old_locale(std::setlocale(LC_ALL, nullptr)); + ptemporary_locale(std::locale tlocale) + : new_locale(tlocale), old_clocale(std::setlocale(LC_ALL, nullptr)) + { + if (old_locale != tlocale) + std::locale::global(tlocale); + if (old_clocale != tlocale.name().c_str()) + std::setlocale(LC_ALL, tlocale.name().c_str()); + } - if (old_locale != "C") - std::setlocale(LC_ALL, "C"); + ~ptemporary_locale() + { + if (old_clocale != new_locale.name().c_str()) + std::setlocale(LC_ALL, old_clocale.c_str()); + if (old_locale != new_locale) + std::locale::global(old_locale); + } +private: + std::locale new_locale; + std::locale old_locale; + pstring old_clocale; +}; +#endif - m_arg++; - - do { - pstring fmt("%"); - va_start(ap, cfmt_spec); - found_abs = false; - buf[0] = 0; - pstring search("{"); - search += plib::to_string(m_arg); - sl = search.size(); +pfmt::rtype pfmt::setfmt(std::stringstream &strm, unsigned cfmt_spec) +{ + pstring fmt; + pstring search("{"); + search += plib::to_string(m_arg); + rtype r; - auto p = m_str.find(search + ":"); - sl++; // ":" - if (p == pstring::npos) // no further specifiers + r.sl = search.size(); + r.p = m_str.find(search + ":"); + r.sl++; // ":" + if (r.p == pstring::npos) // no further specifiers + { + r.p = m_str.find(search + "}"); + if (r.p == pstring::npos) // not found try default { - p = m_str.find(search + "}"); - if (p == pstring::npos) // not found try default - { - sl = 2; - p = m_str.find("{}"); - } - else - // found absolute positional place holder - found_abs = true; - if (p == pstring::npos) + r.sl = 2; + r.p = m_str.find("{}"); + } + else + // found absolute positional place holder + r.ret = 1; + if (r.p == pstring::npos) + { + r.sl=2; + r.p = m_str.find("{:"); + if (r.p != pstring:: npos) { - sl=2; - p = m_str.find("{:"); - if (p != pstring:: npos) + auto p1 = m_str.find("}", r.p); + if (p1 != pstring::npos) { - auto p1 = m_str.find("}", p); - if (p1 != pstring::npos) - { - sl = p1 - p + 1; - fmt += m_str.substr(p+1, p1 - p - 1); - } + r.sl = p1 - r.p + 1; + fmt += m_str.substr(r.p+2, p1 - r.p - 2); } } } - else + } + else + { + // found absolute positional place holder + auto p1 = m_str.find("}", r.p); + if (p1 != pstring::npos) { - // found absolute positional place holder - auto p1 = m_str.find("}", p); - if (p1 != pstring::npos) - { - sl = p1 - p + 1; - fmt += ((m_arg>=10) ? m_str.substr(p+4, p1 - p - 4) : m_str.substr(p+3, p1 - p - 3)); - found_abs = true; - } + r.sl = p1 - r.p + 1; + fmt += ((m_arg>=10) ? m_str.substr(r.p+4, p1 - r.p - 4) : m_str.substr(r.p+3, p1 - r.p - 3)); + r.ret = 1; } - pstring::value_type pend = fmt.at(fmt.size() - 1); - if (pstring("duxo").find(cfmt_spec) != pstring::npos) + } + if (r.p != pstring::npos) + { + // a.b format here ... + if (fmt != "" && pstring("duxofge").find(cfmt_spec) != pstring::npos) { - if (pstring("duxo").find(pend) == pstring::npos) - fmt += (pstring(l) + static_cast(cfmt_spec)); + r.pend = fmt.at(fmt.size() - 1); + if (pstring("duxofge").find(r.pend) == pstring::npos) + r.pend = static_cast(cfmt_spec); else - fmt = plib::left(fmt, fmt.size() - 1) + pstring(l) + plib::right(fmt, 1); + fmt = plib::left(fmt, fmt.size() - 1); + } + else + // FIXME: Error + r.pend = cfmt_spec; + + int pdot(fmt.find('.')); + + if (pdot==0) + strm << std::setprecision(pstonum(fmt.substr(1))); + else if (r.p != pstring::npos) + { + strm << std::setprecision(pstonum(fmt.substr(pdot + 1))) << std::setw(pstonum(left(fmt,pdot))); } - else if (pstring("fge").find(cfmt_spec) != pstring::npos) + else if (fmt != "") + strm << std::setw(pstonum(fmt)); + + switch (r.pend) { - if (pstring("fge").find(pend) == pstring::npos) - fmt += cfmt_spec; + case 'x': + strm << std::hex; + break; + case 'o': + strm << std::oct; + break; + case 'f': + strm << std::fixed; + break; + case 'e': + strm << std::scientific; + break; + default: + break; } - else - fmt += cfmt_spec; - std::vsnprintf(buf.data(), buf.size(), fmt.c_str(), ap); - if (p != pstring::npos) - m_str = m_str.substr(0, p) + pstring(buf.data()) + m_str.substr(p + sl); - va_end(ap); - } while (found_abs); - if (old_locale != "C") - std::setlocale(LC_ALL, old_locale.c_str()); - return *this; + } + else + r.ret = -1; + return r; + } } // namespace plib -- cgit v1.2.3-70-g09d2