mirror of
https://github.com/holub/mame
synced 2025-06-06 04:43:45 +03:00
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
This commit is contained in:
parent
4495c35113
commit
500ca5b8fc
@ -184,7 +184,7 @@ namespace devices
|
||||
{
|
||||
// FIXME: use pstonum_ne
|
||||
//pati[i] = plib::pstonum<decltype(pati[i])>(pat[i]);
|
||||
pati[i] = plib::pstonum<std::int64_t, true>(pat[i]);
|
||||
pati[i] = plib::pstonum<std::int64_t>(pat[i]);
|
||||
total += pati[i];
|
||||
}
|
||||
netlist_time ttotal = netlist_time::zero();
|
||||
|
@ -401,7 +401,7 @@ void truthtable_parser::parse(const std::vector<pstring> &truthtable)
|
||||
else
|
||||
nl_assert_always(outs == "0", "Unknown value (not 0 or 1");
|
||||
// FIXME: error handling
|
||||
netlist_time t = netlist_time::from_nsec(plib::pstonum<std::int64_t, true>(plib::trim(times[j])));
|
||||
netlist_time t = netlist_time::from_nsec(plib::pstonum<std::int64_t>(plib::trim(times[j])));
|
||||
uint_least8_t k=0;
|
||||
while (m_timing_nt[k] != netlist_time::zero() && m_timing_nt[k] != t)
|
||||
k++;
|
||||
|
@ -20,7 +20,7 @@
|
||||
#include "nl_errstr.h"
|
||||
|
||||
#include <cmath>
|
||||
#include <cstring>
|
||||
//#include <cstring>
|
||||
#include <limits>
|
||||
|
||||
namespace netlist
|
||||
|
@ -1139,7 +1139,7 @@ namespace netlist
|
||||
protected:
|
||||
void changed() override
|
||||
{
|
||||
stream()->read(reinterpret_cast<plib::pistream::value_type *>(&m_data[0]),1<<AW);
|
||||
stream()->read(reinterpret_cast<plib::pistream::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::value_type *>(&m_data[0]),1<<AW);
|
||||
f->read(reinterpret_cast<plib::pistream::char_type *>(&m_data[0]),1<<AW);
|
||||
else
|
||||
device.state().log().warning(MW_ROM_NOT_FOUND(str()));
|
||||
}
|
||||
|
@ -1199,13 +1199,13 @@ bool source_t::parse(nlparse_t &setup, const pstring &name)
|
||||
plib::unique_ptr<plib::pistream> source_string_t::stream(const pstring &name)
|
||||
{
|
||||
plib::unused_var(name);
|
||||
return plib::make_unique<plib::pimemstream>(m_str.c_str(), std::strlen(m_str.c_str()));
|
||||
return plib::make_unique<plib::pistringstream>(m_str);
|
||||
}
|
||||
|
||||
plib::unique_ptr<plib::pistream> source_mem_t::stream(const pstring &name)
|
||||
{
|
||||
plib::unused_var(name);
|
||||
return plib::make_unique<plib::pimemstream>(m_str.c_str(), std::strlen(m_str.c_str()));
|
||||
return plib::make_unique<plib::pistringstream>(m_str);
|
||||
}
|
||||
|
||||
plib::unique_ptr<plib::pistream> source_file_t::stream(const pstring &name)
|
||||
|
@ -22,7 +22,7 @@
|
||||
#include "plib/ptime.h"
|
||||
#include "plib/putil.h"
|
||||
|
||||
#include <cstdint>
|
||||
//#include <cstdint>
|
||||
#include <unordered_map>
|
||||
|
||||
namespace netlist
|
||||
|
@ -21,7 +21,6 @@
|
||||
#include <algorithm>
|
||||
#include <array>
|
||||
#include <cmath>
|
||||
#include <cstdlib>
|
||||
#include <type_traits>
|
||||
#include <vector>
|
||||
|
||||
|
@ -12,8 +12,8 @@
|
||||
#include "pstring.h"
|
||||
#include "ptypes.h"
|
||||
|
||||
#include <cstddef>
|
||||
#include <cstdlib>
|
||||
#include <cstddef> // for std::max_align_t (usually long long)
|
||||
//#include <cstdlib>
|
||||
#include <memory>
|
||||
#include <type_traits>
|
||||
#include <utility>
|
||||
|
@ -3,8 +3,6 @@
|
||||
|
||||
#include "pchrono.h"
|
||||
|
||||
#include <chrono>
|
||||
|
||||
namespace plib {
|
||||
namespace chrono {
|
||||
#if defined(__x86_64__) && !defined(_clang__) && !defined(_MSC_VER) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ > 6))
|
||||
|
@ -12,7 +12,7 @@
|
||||
#include "ptypes.h"
|
||||
|
||||
#include <chrono>
|
||||
#include <cstdint>
|
||||
//#include <cstdint>
|
||||
|
||||
namespace plib {
|
||||
namespace chrono {
|
||||
|
@ -10,100 +10,133 @@
|
||||
|
||||
#include <algorithm>
|
||||
#include <array>
|
||||
#include <cstdarg>
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
#include <iostream>
|
||||
#include <clocale>
|
||||
#include <iomanip>
|
||||
#include <cmath>
|
||||
|
||||
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<char, 2048> 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++;
|
||||
pfmt::rtype pfmt::setfmt(std::stringstream &strm, unsigned cfmt_spec)
|
||||
{
|
||||
pstring fmt;
|
||||
pstring search("{");
|
||||
search += plib::to_string(m_arg);
|
||||
rtype r;
|
||||
|
||||
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();
|
||||
|
||||
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
|
||||
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("{}");
|
||||
}
|
||||
else
|
||||
// found absolute positional place holder
|
||||
found_abs = true;
|
||||
if (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<pstring::value_type>(cfmt_spec));
|
||||
r.pend = fmt.at(fmt.size() - 1);
|
||||
if (pstring("duxofge").find(r.pend) == pstring::npos)
|
||||
r.pend = static_cast<pstring::value_type>(cfmt_spec);
|
||||
else
|
||||
fmt = plib::left(fmt, fmt.size() - 1) + pstring(l) + plib::right(fmt, 1);
|
||||
}
|
||||
else if (pstring("fge").find(cfmt_spec) != pstring::npos)
|
||||
{
|
||||
if (pstring("fge").find(pend) == pstring::npos)
|
||||
fmt += cfmt_spec;
|
||||
fmt = plib::left(fmt, fmt.size() - 1);
|
||||
}
|
||||
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;
|
||||
// FIXME: Error
|
||||
r.pend = cfmt_spec;
|
||||
|
||||
int pdot(fmt.find('.'));
|
||||
|
||||
if (pdot==0)
|
||||
strm << std::setprecision(pstonum<int>(fmt.substr(1)));
|
||||
else if (r.p != pstring::npos)
|
||||
{
|
||||
strm << std::setprecision(pstonum<int>(fmt.substr(pdot + 1))) << std::setw(pstonum<int>(left(fmt,pdot)));
|
||||
}
|
||||
else if (fmt != "")
|
||||
strm << std::setw(pstonum<int>(fmt));
|
||||
|
||||
switch (r.pend)
|
||||
{
|
||||
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
|
||||
r.ret = -1;
|
||||
return r;
|
||||
|
||||
}
|
||||
|
||||
} // namespace plib
|
||||
|
@ -12,6 +12,8 @@
|
||||
#include "putil.h"
|
||||
|
||||
#include <limits>
|
||||
#include <locale>
|
||||
#include <sstream>
|
||||
|
||||
namespace plib {
|
||||
|
||||
@ -28,7 +30,6 @@ struct ptype_traits_base
|
||||
{
|
||||
static const T cast(const T &x) { return x; }
|
||||
static const bool is_signed = std::numeric_limits<T>::is_signed;
|
||||
static const char *size_spec() { return ""; }
|
||||
static char32_t fmt_spec() { return 'u'; }
|
||||
};
|
||||
|
||||
@ -37,7 +38,6 @@ struct ptype_traits_base<bool>
|
||||
{
|
||||
static unsigned int cast(const bool &x) { return static_cast<unsigned int>(x); }
|
||||
static const bool is_signed = std::numeric_limits<bool>::is_signed;
|
||||
static const char *size_spec() { return ""; }
|
||||
static char32_t fmt_spec() { return 'u'; }
|
||||
};
|
||||
|
||||
@ -52,14 +52,12 @@ struct ptype_traits<bool> : ptype_traits_base<bool>
|
||||
template<>
|
||||
struct ptype_traits<char> : ptype_traits_base<char>
|
||||
{
|
||||
static const char *size_spec() { return "h"; }
|
||||
static char32_t fmt_spec() { return is_signed ? 'd' : 'u'; }
|
||||
};
|
||||
|
||||
template<>
|
||||
struct ptype_traits<short> : ptype_traits_base<short>
|
||||
{
|
||||
static const char *size_spec() { return "h"; }
|
||||
static char32_t fmt_spec() { return 'd'; }
|
||||
};
|
||||
|
||||
@ -72,35 +70,30 @@ struct ptype_traits<int> : ptype_traits_base<int>
|
||||
template<>
|
||||
struct ptype_traits<long> : ptype_traits_base<long>
|
||||
{
|
||||
static const char *size_spec() { return "l"; }
|
||||
static char32_t fmt_spec() { return 'd'; }
|
||||
};
|
||||
|
||||
template<>
|
||||
struct ptype_traits<long long> : ptype_traits_base<long long>
|
||||
{
|
||||
static const char *size_spec() { return "ll"; }
|
||||
static char32_t fmt_spec() { return 'd'; }
|
||||
};
|
||||
|
||||
template<>
|
||||
struct ptype_traits<signed char> : ptype_traits_base<signed char>
|
||||
{
|
||||
static const char *size_spec() { return "h"; }
|
||||
static char32_t fmt_spec() { return 'd'; }
|
||||
};
|
||||
|
||||
template<>
|
||||
struct ptype_traits<unsigned char> : ptype_traits_base<unsigned char>
|
||||
{
|
||||
static const char *size_spec() { return "h"; }
|
||||
static char32_t fmt_spec() { return 'u'; }
|
||||
};
|
||||
|
||||
template<>
|
||||
struct ptype_traits<unsigned short> : ptype_traits_base<unsigned short>
|
||||
{
|
||||
static const char *size_spec() { return "h"; }
|
||||
static char32_t fmt_spec() { return 'u'; }
|
||||
};
|
||||
|
||||
@ -113,14 +106,12 @@ struct ptype_traits<unsigned int> : ptype_traits_base<unsigned int>
|
||||
template<>
|
||||
struct ptype_traits<unsigned long> : ptype_traits_base<unsigned long>
|
||||
{
|
||||
static const char *size_spec() { return "l"; }
|
||||
static char32_t fmt_spec() { return 'u'; }
|
||||
};
|
||||
|
||||
template<>
|
||||
struct ptype_traits<unsigned long long> : ptype_traits_base<unsigned long long>
|
||||
{
|
||||
static const char *size_spec() { return "ll"; }
|
||||
static char32_t fmt_spec() { return 'u'; }
|
||||
};
|
||||
|
||||
@ -154,41 +145,45 @@ class pfmt
|
||||
{
|
||||
public:
|
||||
explicit pfmt(const pstring &fmt)
|
||||
: m_str(fmt), m_arg(0)
|
||||
: m_str(fmt), m_locale(std::locale::classic()), m_arg(0)
|
||||
{
|
||||
}
|
||||
explicit pfmt(std::locale loc, const pstring &fmt)
|
||||
: m_str(fmt), m_locale(loc), m_arg(0)
|
||||
{
|
||||
}
|
||||
|
||||
COPYASSIGNMOVE(pfmt, default)
|
||||
|
||||
~pfmt() noexcept = default;
|
||||
|
||||
operator pstring() const { return m_str; }
|
||||
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
|
||||
pfmt & e(const double &x) {return format_element("", 'e', x); }
|
||||
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
|
||||
pfmt & g(const double &x) {return format_element("", 'g', x); }
|
||||
|
||||
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
|
||||
pfmt & e(const float &x) {return format_element("", 'e', static_cast<double>(x)); }
|
||||
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
|
||||
pfmt & g(const float &x) {return format_element("", 'g', static_cast<double>(x)); }
|
||||
template <typename T>
|
||||
typename std::enable_if<std::is_floating_point<T>::value, pfmt &>::type
|
||||
f(const T &x) {return format_element('f', x); }
|
||||
|
||||
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
|
||||
pfmt &operator ()(const void *x) {return format_element("", 'p', x); }
|
||||
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
|
||||
pfmt &operator ()(const pstring &x) {return format_element("", 's', x.c_str() ); }
|
||||
template <typename T>
|
||||
typename std::enable_if<std::is_floating_point<T>::value, pfmt &>::type
|
||||
e(const T &x) {return format_element('e', x); }
|
||||
|
||||
template <typename T>
|
||||
typename std::enable_if<std::is_floating_point<T>::value, pfmt &>::type
|
||||
g(const T &x) {return format_element('g', x); }
|
||||
|
||||
pfmt &operator ()(const void *x) {return format_element('p', x); }
|
||||
pfmt &operator ()(const pstring &x) {return format_element('s', x.c_str() ); }
|
||||
|
||||
template<typename T>
|
||||
pfmt &operator ()(const T &x)
|
||||
{
|
||||
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
|
||||
return format_element(ptype_traits<T>::size_spec(), ptype_traits<T>::fmt_spec(), ptype_traits<T>::cast(x));
|
||||
return format_element(ptype_traits<T>::fmt_spec(), ptype_traits<T>::cast(x));
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
pfmt &operator ()(const T *x)
|
||||
{
|
||||
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
|
||||
return format_element(ptype_traits<T *>::size_spec(), ptype_traits<T *>::fmt_spec(), ptype_traits<T *>::cast(x));
|
||||
return format_element(ptype_traits<T *>::fmt_spec(), ptype_traits<T *>::cast(x));
|
||||
}
|
||||
|
||||
pfmt &operator ()()
|
||||
@ -204,26 +199,58 @@ public:
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
pfmt &x(const T &x)
|
||||
typename std::enable_if<std::is_integral<T>::value, pfmt &>::type
|
||||
x(const T &x)
|
||||
{
|
||||
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
|
||||
return format_element(ptype_traits<T>::size_spec(), 'x', x);
|
||||
return format_element('x', x);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
pfmt &o(const T &x)
|
||||
typename std::enable_if<std::is_integral<T>::value, pfmt &>::type
|
||||
o(const T &x)
|
||||
{
|
||||
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
|
||||
return format_element(ptype_traits<T>::size_spec(), 'o', x);
|
||||
return format_element('o', x);
|
||||
}
|
||||
|
||||
protected:
|
||||
|
||||
pfmt &format_element(const char *l, const unsigned fmt_spec, ...);
|
||||
struct rtype
|
||||
{
|
||||
rtype() : ret(0), p(0), sl(0), pend(0) {}
|
||||
int ret;
|
||||
pstring::size_type p;
|
||||
pstring::size_type sl;
|
||||
pstring::value_type pend;
|
||||
|
||||
};
|
||||
rtype setfmt(std::stringstream &strm, unsigned cfmt_spec);
|
||||
|
||||
template <typename T>
|
||||
pfmt &format_element(const unsigned cfmt_spec, T &&val)
|
||||
{
|
||||
rtype ret;
|
||||
|
||||
m_arg++;
|
||||
|
||||
do {
|
||||
std::stringstream strm;
|
||||
strm.imbue(m_locale);
|
||||
ret = setfmt(strm, cfmt_spec);
|
||||
if (ret.ret>=0)
|
||||
{
|
||||
strm << std::forward<T>(val);
|
||||
m_str = m_str.substr(0, ret.p) + pstring(strm.str()) + m_str.substr(ret.p + ret.sl);
|
||||
}
|
||||
} while (ret.ret == 1);
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
private:
|
||||
|
||||
pstring m_str;
|
||||
std::locale m_locale;
|
||||
unsigned m_arg;
|
||||
};
|
||||
|
||||
|
@ -16,10 +16,10 @@
|
||||
#include "pstring.h"
|
||||
#include "putil.h"
|
||||
|
||||
#include <cwchar>
|
||||
#include <memory>
|
||||
|
||||
#ifdef _WIN32
|
||||
#include <cwchar>
|
||||
#define PMAIN(appclass) \
|
||||
extern "C" int wmain(int argc, wchar_t *argv[]) { return plib::app::mainrun<appclass, wchar_t>(argc, argv); }
|
||||
#else
|
||||
|
@ -14,7 +14,7 @@
|
||||
|
||||
#include <algorithm>
|
||||
#include <cmath>
|
||||
#include <cstdlib>
|
||||
//#include <cstdlib>
|
||||
#include <type_traits>
|
||||
#include <vector>
|
||||
|
||||
|
@ -15,7 +15,7 @@
|
||||
#include "putil.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <cstddef>
|
||||
//#include <cstddef>
|
||||
#include <memory>
|
||||
#include <unordered_map>
|
||||
#include <utility>
|
||||
|
@ -11,7 +11,7 @@
|
||||
|
||||
#include "pconfig.h"
|
||||
|
||||
#include <cstddef>
|
||||
//#include <cstddef>
|
||||
|
||||
#if HAS_OPENMP
|
||||
#include "omp.h"
|
||||
|
@ -9,7 +9,7 @@
|
||||
#include "palloc.h"
|
||||
#include "putil.h"
|
||||
|
||||
#include <cstdarg>
|
||||
//#include <cstdarg>
|
||||
|
||||
namespace plib {
|
||||
// ----------------------------------------------------------------------------------------
|
||||
@ -296,14 +296,14 @@ void ppreprocessor::error(const pstring &err)
|
||||
throw pexception("PREPRO ERROR: " + err);
|
||||
}
|
||||
|
||||
pstream::size_type ppreprocessor::vread(value_type *buf, const pstream::size_type n)
|
||||
pstream::size_type ppreprocessor::vread(char_type *buf, const pstream::size_type n)
|
||||
{
|
||||
size_type bytes = std::min(m_buf.size() - m_pos, n);
|
||||
|
||||
if (bytes==0)
|
||||
return 0;
|
||||
|
||||
std::memcpy(buf, m_buf.c_str() + m_pos, bytes);
|
||||
std::copy(m_buf.c_str() + m_pos, m_buf.c_str() + m_pos + bytes, buf);
|
||||
m_pos += bytes;
|
||||
return bytes;
|
||||
}
|
||||
@ -357,7 +357,7 @@ int ppreprocessor::expr(const std::vector<pstring> &sexpr, std::size_t &start, i
|
||||
else
|
||||
{
|
||||
// FIXME: error handling
|
||||
val = plib::pstonum<decltype(val), true>(tok);
|
||||
val = plib::pstonum<decltype(val)>(tok);
|
||||
start++;
|
||||
}
|
||||
}
|
||||
|
@ -12,7 +12,7 @@
|
||||
#include "pstream.h"
|
||||
#include "pstring.h"
|
||||
|
||||
#include <cstdint>
|
||||
//#include <cstdint>
|
||||
#include <unordered_map>
|
||||
|
||||
|
||||
@ -204,7 +204,7 @@ public:
|
||||
|
||||
protected:
|
||||
|
||||
size_type vread(value_type *buf, const size_type n) override;
|
||||
size_type vread(char_type *buf, const size_type n) override;
|
||||
void vseek(const pos_type n) override
|
||||
{
|
||||
plib::unused_var(n);
|
||||
|
@ -10,7 +10,7 @@
|
||||
|
||||
#include "pconfig.h"
|
||||
|
||||
#include <cstdint>
|
||||
//#include <cstdint>
|
||||
#include <utility>
|
||||
|
||||
/*
|
||||
|
@ -10,7 +10,7 @@
|
||||
|
||||
#include <algorithm>
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
//#include <cstdlib>
|
||||
|
||||
// VS2015 prefers _dup
|
||||
#ifdef _WIN32
|
||||
@ -71,7 +71,7 @@ pifilestream::~pifilestream()
|
||||
}
|
||||
}
|
||||
|
||||
pifilestream::pos_type pifilestream::vread(value_type *buf, const pos_type n)
|
||||
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)
|
||||
@ -124,6 +124,8 @@ pstdin::pstdin()
|
||||
// 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)
|
||||
{
|
||||
@ -156,7 +158,7 @@ pofilestream::~pofilestream()
|
||||
}
|
||||
}
|
||||
|
||||
void pofilestream::vwrite(const value_type *buf, const pos_type n)
|
||||
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)
|
||||
@ -190,10 +192,44 @@ pstream::pos_type pofilestream::vtell() const
|
||||
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;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
// pstderr: write to stderr
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
#if 1
|
||||
pstderr::pstderr()
|
||||
#ifdef _WIN32
|
||||
: pofilestream(fdopen(_dup(fileno(stderr)), "wb"), "<stderr>", true)
|
||||
@ -215,7 +251,7 @@ pstdout::pstdout()
|
||||
#endif
|
||||
{
|
||||
}
|
||||
|
||||
#endif
|
||||
// -----------------------------------------------------------------------------
|
||||
// Memory stream
|
||||
// -----------------------------------------------------------------------------
|
||||
@ -229,13 +265,14 @@ 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(value_type *buf, const pos_type n)
|
||||
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;
|
||||
|
||||
@ -263,36 +300,6 @@ pimemstream::pos_type pimemstream::vtell() const
|
||||
return m_pos;
|
||||
}
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
// Output memory stream
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
pomemstream::pomemstream()
|
||||
: postream(FLAG_SEEKABLE), m_pos(0), m_mem(1024)
|
||||
{
|
||||
m_mem.clear();
|
||||
}
|
||||
|
||||
void pomemstream::vwrite(const value_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;
|
||||
}
|
||||
|
||||
bool putf8_reader::readline(pstring &line)
|
||||
{
|
||||
@ -308,7 +315,7 @@ bool putf8_reader::readline(pstring &line)
|
||||
if (c == 10)
|
||||
break;
|
||||
else if (c != 13) /* ignore CR */
|
||||
m_linebuf += putf8string(c);
|
||||
m_linebuf += putf8string(1, c);
|
||||
if (!this->readcode(c))
|
||||
break;
|
||||
}
|
||||
|
@ -104,7 +104,7 @@ class pistream_base : public pstream
|
||||
{
|
||||
public:
|
||||
|
||||
using value_type = T;
|
||||
using char_type = T;
|
||||
|
||||
~pistream_base() noexcept override = default;
|
||||
|
||||
@ -139,7 +139,7 @@ class postream_base : public pstream
|
||||
{
|
||||
public:
|
||||
|
||||
using value_type = T;
|
||||
using char_type = T;
|
||||
|
||||
postream_base() = default;
|
||||
~postream_base() noexcept override = default;
|
||||
@ -152,6 +152,14 @@ public:
|
||||
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)) {}
|
||||
@ -191,7 +199,7 @@ public:
|
||||
|
||||
protected:
|
||||
/* write n bytes to stream */
|
||||
void vwrite(const value_type *buf, const pos_type) override;
|
||||
void vwrite(const char_type *buf, const pos_type) override;
|
||||
void vseek(const pos_type n) override;
|
||||
pos_type vtell() const override;
|
||||
|
||||
@ -219,9 +227,10 @@ public:
|
||||
|
||||
protected:
|
||||
/* write n bytes to stream */
|
||||
void vwrite(const value_type *buf, const pos_type n) override
|
||||
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(reinterpret_cast<const pstring::mem_t *>(buf), n);
|
||||
m_buf += pstring(buf, n);
|
||||
}
|
||||
void vseek(const pos_type n) override { unused_var(n); }
|
||||
pos_type vtell() const override { return m_buf.size(); }
|
||||
@ -257,7 +266,7 @@ public:
|
||||
protected:
|
||||
pofilestream(void *file, const pstring &name, const bool do_close);
|
||||
/* write n bytes to stream */
|
||||
void vwrite(const value_type *buf, const pos_type n) override;
|
||||
void vwrite(const char_type *buf, const pos_type n) override;
|
||||
void vseek(const pos_type n) override;
|
||||
pos_type vtell() const override;
|
||||
|
||||
@ -329,7 +338,7 @@ protected:
|
||||
pifilestream(void *file, const pstring &name, const bool do_close);
|
||||
|
||||
/* read up to n bytes from stream */
|
||||
pos_type vread(value_type *buf, const pos_type n) override;
|
||||
pos_type vread(char_type *buf, const pos_type n) override;
|
||||
void vseek(const pos_type n) override;
|
||||
pos_type vtell() const override;
|
||||
|
||||
@ -379,7 +388,7 @@ public:
|
||||
COPYASSIGN(pimemstream, delete)
|
||||
pimemstream &operator=(pimemstream &&src) = delete;
|
||||
|
||||
explicit pimemstream(const pomemstream &ostrm);
|
||||
//explicit pimemstream(const pomemstream &ostrm);
|
||||
|
||||
~pimemstream() override = default;
|
||||
|
||||
@ -393,7 +402,7 @@ protected:
|
||||
}
|
||||
|
||||
/* read up to n bytes from stream */
|
||||
pos_type vread(value_type *buf, const pos_type n) override;
|
||||
pos_type vread(char_type *buf, const pos_type n) override;
|
||||
void vseek(const pos_type n) override;
|
||||
pos_type vtell() const override;
|
||||
|
||||
@ -414,12 +423,12 @@ public:
|
||||
: pimemstream()
|
||||
, m_str(str)
|
||||
{
|
||||
set_mem(m_str.c_str(), std::strlen(m_str.c_str()));
|
||||
set_mem(m_str.c_str(), 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(), std::strlen(m_str.c_str()));
|
||||
set_mem(m_str.c_str(), pstring_mem_t_size(m_str));
|
||||
}
|
||||
COPYASSIGN(pistringstream, delete)
|
||||
pistringstream &operator=(pistringstream &&src) = delete;
|
||||
@ -464,14 +473,14 @@ public:
|
||||
bool eof() const { return m_strm->eof(); }
|
||||
bool readline(pstring &line);
|
||||
|
||||
bool readbyte1(pistream::value_type &b)
|
||||
bool readbyte1(pistream::char_type &b)
|
||||
{
|
||||
return (m_strm->read(&b, 1) == 1);
|
||||
}
|
||||
|
||||
bool readcode(putf8string::traits_type::code_t &c)
|
||||
{
|
||||
std::array<pistream::value_type, 4> b{0};
|
||||
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));
|
||||
@ -526,7 +535,7 @@ public:
|
||||
{
|
||||
// NOLINTNEXTLINE(performance-unnecessary-copy-initialization)
|
||||
const putf8string conv_utf8(text);
|
||||
m_strm->write(reinterpret_cast<const pistream::value_type *>(conv_utf8.c_str()), conv_utf8.mem_t_size());
|
||||
m_strm->write(conv_utf8.c_str(), plib::strlen(conv_utf8.c_str()));
|
||||
}
|
||||
|
||||
void write(const pstring::value_type c) const
|
||||
@ -577,13 +586,13 @@ public:
|
||||
template <typename T>
|
||||
void write(const T &val)
|
||||
{
|
||||
m_strm.write(reinterpret_cast<const postream::value_type *>(&val), sizeof(T));
|
||||
m_strm.write(reinterpret_cast<const postream::char_type *>(&val), sizeof(T));
|
||||
}
|
||||
|
||||
void write(const pstring &s)
|
||||
{
|
||||
const auto sm = reinterpret_cast<const postream::value_type *>(s.c_str());
|
||||
const std::size_t sl = std::strlen(s.c_str());
|
||||
const auto sm = reinterpret_cast<const postream::char_type *>(s.c_str());
|
||||
const std::size_t sl(pstring_mem_t_size(s));
|
||||
write(sl);
|
||||
m_strm.write(sm, sl);
|
||||
}
|
||||
@ -593,7 +602,7 @@ public:
|
||||
{
|
||||
std::size_t sz = val.size();
|
||||
write(sz);
|
||||
m_strm.write(reinterpret_cast<const postream::value_type *>(val.data()), sizeof(T) * sz);
|
||||
m_strm.write(reinterpret_cast<const postream::char_type *>(val.data()), sizeof(T) * sz);
|
||||
}
|
||||
|
||||
private:
|
||||
@ -614,7 +623,7 @@ public:
|
||||
template <typename T>
|
||||
void read(T &val)
|
||||
{
|
||||
m_strm.read(reinterpret_cast<pistream::value_type *>(&val), sizeof(T));
|
||||
m_strm.read(reinterpret_cast<pistream::char_type *>(&val), sizeof(T));
|
||||
}
|
||||
|
||||
void read( pstring &s)
|
||||
@ -633,7 +642,7 @@ public:
|
||||
std::size_t sz = 0;
|
||||
read(sz);
|
||||
val.resize(sz);
|
||||
m_strm.read(reinterpret_cast<pistream::value_type *>(val.data()), sizeof(T) * sz);
|
||||
m_strm.read(reinterpret_cast<pistream::char_type *>(val.data()), sizeof(T) * sz);
|
||||
}
|
||||
|
||||
private:
|
||||
@ -642,7 +651,7 @@ private:
|
||||
|
||||
inline void copystream(postream &dest, pistream &src)
|
||||
{
|
||||
std::array<postream::value_type, 1024> buf; // NOLINT(cppcoreguidelines-pro-type-member-init)
|
||||
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);
|
||||
|
@ -9,7 +9,6 @@
|
||||
|
||||
#include "ptypes.h"
|
||||
|
||||
#include <cstring>
|
||||
#include <exception>
|
||||
#include <iterator>
|
||||
#include <limits>
|
||||
@ -126,9 +125,10 @@ public:
|
||||
pstring_t &operator=(const pstring_t &string) = default;
|
||||
pstring_t &operator=(pstring_t &&string) noexcept = default;
|
||||
|
||||
explicit pstring_t(code_t code)
|
||||
explicit pstring_t(size_type n, code_t code)
|
||||
{
|
||||
*this += code;
|
||||
while (n--)
|
||||
*this += code;
|
||||
}
|
||||
|
||||
template <typename T,
|
||||
@ -198,14 +198,13 @@ public:
|
||||
|
||||
const_reference at(const size_type pos) const { return *reinterpret_cast<const ref_value_type *>(F::nthcode(m_str.c_str(),pos)); }
|
||||
|
||||
static constexpr const size_type npos = static_cast<size_type>(-1);
|
||||
|
||||
/* the following are extensions to <string> */
|
||||
|
||||
// FIXME: remove those
|
||||
size_type mem_t_size() const { return m_str.size(); }
|
||||
|
||||
const string_type &cpp_string() const { return m_str; }
|
||||
|
||||
static constexpr const size_type npos = static_cast<size_type>(-1);
|
||||
|
||||
private:
|
||||
string_type m_str;
|
||||
};
|
||||
@ -477,8 +476,10 @@ extern template struct pstring_t<pwchar_traits>;
|
||||
|
||||
#if (PSTRING_USE_STD_STRING)
|
||||
typedef std::string pstring;
|
||||
static inline pstring::size_type pstring_mem_t_size(const pstring &s) { return s.size(); }
|
||||
#else
|
||||
using pstring = pstring_t<putf8_traits>;
|
||||
static inline pstring::size_type pstring_mem_t_size(const pstring &s) { return s.mem_t_size(); }
|
||||
#endif
|
||||
using putf8string = pstring_t<putf8_traits>;
|
||||
using pu16string = pstring_t<putf16_traits>;
|
||||
@ -602,13 +603,22 @@ namespace plib
|
||||
template<typename T>
|
||||
bool startsWith(const T &str, const char *arg)
|
||||
{
|
||||
return (left(str, std::strlen(arg)) == arg);
|
||||
return startsWith(str, static_cast<pstring>(arg));
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
bool endsWith(const T &str, const char *arg)
|
||||
{
|
||||
return (right(str, std::strlen(arg)) == arg);
|
||||
return endsWith(str, static_cast<pstring>(arg));
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
std::size_t strlen(const T *str)
|
||||
{
|
||||
const T *p = str;
|
||||
while (*p)
|
||||
p++;
|
||||
return p - str;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
@ -670,7 +680,7 @@ namespace std
|
||||
{
|
||||
using argument_type = pstring_t<T>;
|
||||
using result_type = std::size_t;
|
||||
result_type operator()(argument_type const& s) const
|
||||
result_type operator()(const argument_type & s) const
|
||||
{
|
||||
const typename argument_type::mem_t *string = s.c_str();
|
||||
result_type result = 5381;
|
||||
|
@ -11,7 +11,7 @@
|
||||
#include "ptypes.h"
|
||||
|
||||
#include <cmath> // std::floor
|
||||
#include <cstdint>
|
||||
//#include <cstdint>
|
||||
|
||||
// ----------------------------------------------------------------------------------------
|
||||
// netlist_time
|
||||
|
@ -6,8 +6,8 @@
|
||||
#include "ptypes.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
//#include <cstdlib>
|
||||
//#include <cstring>
|
||||
#include <initializer_list>
|
||||
|
||||
namespace plib
|
||||
@ -137,30 +137,15 @@ namespace plib
|
||||
int penum_base::from_string_int(const char *str, const char *x)
|
||||
{
|
||||
int cnt = 0;
|
||||
const char *cur = str;
|
||||
std::size_t lx = strlen(x);
|
||||
while (*str)
|
||||
for (auto s : psplit(str, ",", false))
|
||||
{
|
||||
if (*str == ',')
|
||||
{
|
||||
std::ptrdiff_t l = str-cur;
|
||||
if (static_cast<std::size_t>(l) == lx)
|
||||
if (strncmp(cur, x, lx) == 0)
|
||||
return cnt;
|
||||
}
|
||||
else if (*str == ' ')
|
||||
{
|
||||
cur = str + 1;
|
||||
cnt++;
|
||||
}
|
||||
str++;
|
||||
}
|
||||
std::ptrdiff_t l = str-cur;
|
||||
if (static_cast<std::size_t>(l) == lx)
|
||||
if (strncmp(cur, x, lx) == 0)
|
||||
if (s == x)
|
||||
return cnt;
|
||||
cnt++;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
std::string penum_base::nthstr(int n, const char *str)
|
||||
{
|
||||
return psplit(str, ",", false)[static_cast<std::size_t>(n)];
|
||||
|
@ -14,6 +14,7 @@
|
||||
#include <initializer_list>
|
||||
#include <sstream>
|
||||
#include <vector>
|
||||
#include <locale>
|
||||
|
||||
#define PSTRINGIFY_HELP(y) # y
|
||||
#define PSTRINGIFY(x) PSTRINGIFY_HELP(x)
|
||||
@ -129,66 +130,57 @@ namespace plib
|
||||
// number conversions
|
||||
// ----------------------------------------------------------------------------------------
|
||||
|
||||
template <typename T, bool CLOCALE, typename E = void>
|
||||
template <typename T, typename S>
|
||||
T pstonum_locale(std::locale loc, const S &arg, std::size_t *idx)
|
||||
{
|
||||
std::stringstream ss;
|
||||
ss.imbue(loc);
|
||||
ss << arg;
|
||||
long int len(ss.tellp());
|
||||
auto x(constants<long double>::zero());
|
||||
if (ss >> x)
|
||||
{
|
||||
long int pos(ss.tellg());
|
||||
if (pos == -1)
|
||||
pos = len;
|
||||
*idx = static_cast<std::size_t>(pos);
|
||||
}
|
||||
else
|
||||
*idx = constants<std::size_t>::zero();
|
||||
//printf("%s, %f, %lu %ld\n", arg, (double)x, *idx, (long int) ss.tellg());
|
||||
return x;
|
||||
}
|
||||
|
||||
template <typename T, typename E = void>
|
||||
struct pstonum_helper;
|
||||
|
||||
template<typename T, bool CLOCALE>
|
||||
struct pstonum_helper<T, CLOCALE, typename std::enable_if<std::is_integral<T>::value
|
||||
&& std::is_signed<T>::value>::type>
|
||||
template<typename T>
|
||||
struct pstonum_helper<T, typename std::enable_if<std::is_integral<T>::value>::type>
|
||||
{
|
||||
template <typename S>
|
||||
long long operator()(const S &arg, std::size_t *idx)
|
||||
long long operator()(std::locale loc, const S &arg, std::size_t *idx)
|
||||
{
|
||||
return std::stoll(arg, idx);
|
||||
//return std::stoll(arg, idx);
|
||||
return pstonum_locale<long long>(loc, arg, idx);
|
||||
}
|
||||
};
|
||||
|
||||
template<typename T, bool CLOCALE>
|
||||
struct pstonum_helper<T, CLOCALE, typename std::enable_if<std::is_integral<T>::value
|
||||
&& !std::is_signed<T>::value>::type>
|
||||
template<typename T>
|
||||
struct pstonum_helper<T, typename std::enable_if<std::is_floating_point<T>::value>::type>
|
||||
{
|
||||
template <typename S>
|
||||
unsigned long long operator()(const S &arg, std::size_t *idx)
|
||||
long double operator()(std::locale loc, const S &arg, std::size_t *idx)
|
||||
{
|
||||
return std::stoull(arg, idx);
|
||||
return pstonum_locale<long double>(loc, arg, idx);
|
||||
}
|
||||
};
|
||||
|
||||
template<typename T, bool CLOCALE>
|
||||
struct pstonum_helper<T, CLOCALE, typename std::enable_if<std::is_floating_point<T>::value>::type>
|
||||
{
|
||||
template <typename S>
|
||||
long double operator()(const S &arg, std::size_t *idx)
|
||||
{
|
||||
if (CLOCALE)
|
||||
{
|
||||
std::stringstream ss;
|
||||
ss.imbue(std::locale::classic());
|
||||
ss << arg;
|
||||
long int len(ss.tellp());
|
||||
auto x(constants<long double>::zero());
|
||||
long int pos(0);
|
||||
if (ss >> x)
|
||||
{
|
||||
pos = static_cast<long int>(ss.tellg());
|
||||
if (pos == -1)
|
||||
pos = len;
|
||||
}
|
||||
*idx = static_cast<std::size_t>(pos);
|
||||
//printf("%s, %f, %lu %ld\n", arg, (double)x, *idx, (long int) ss.tellg());
|
||||
return x;
|
||||
}
|
||||
else
|
||||
return std::stold(arg, idx);
|
||||
}
|
||||
};
|
||||
|
||||
template<typename T, bool CLOCALE, typename S>
|
||||
T pstonum(const S &arg)
|
||||
template<typename T, typename S>
|
||||
T pstonum(const S &arg, std::locale loc = std::locale::classic())
|
||||
{
|
||||
decltype(arg.c_str()) cstr = arg.c_str();
|
||||
std::size_t idx(0);
|
||||
auto ret = pstonum_helper<T, CLOCALE>()(cstr, &idx);
|
||||
auto ret = pstonum_helper<T>()(loc, cstr, &idx);
|
||||
using ret_type = decltype(ret);
|
||||
if (ret >= static_cast<ret_type>(std::numeric_limits<T>::lowest())
|
||||
&& ret <= static_cast<ret_type>(std::numeric_limits<T>::max()))
|
||||
@ -205,12 +197,12 @@ namespace plib
|
||||
}
|
||||
|
||||
template<typename R, bool CLOCALE, typename T>
|
||||
R pstonum_ne(const T &str, bool &err) noexcept
|
||||
R pstonum_ne(const T &str, bool &err, std::locale loc = std::locale::classic()) noexcept
|
||||
{
|
||||
try
|
||||
{
|
||||
err = false;
|
||||
return pstonum<R, CLOCALE>(str);
|
||||
return pstonum<R>(str, loc);
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
|
@ -15,7 +15,7 @@
|
||||
#include "netlist/solver/nld_solver.h"
|
||||
#include "netlist/tools/nl_convert.h"
|
||||
|
||||
#include <cstring>
|
||||
#include <cstdio> // scanf
|
||||
|
||||
#define NLTOOL_VERSION 20190420
|
||||
|
||||
@ -292,7 +292,7 @@ struct input_t
|
||||
std::array<char, 400> buf; // NOLINT(cppcoreguidelines-pro-type-member-init)
|
||||
double t;
|
||||
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
|
||||
int e = sscanf(line.c_str(), "%lf,%[^,],%lf", &t, buf.data(), &m_value);
|
||||
int e = std::sscanf(line.c_str(), "%lf,%[^,],%lf", &t, buf.data(), &m_value);
|
||||
if (e != 3)
|
||||
throw netlist::nl_exception(plib::pfmt("error {1} scanning line {2}\n")(e)(line));
|
||||
m_time = netlist::netlist_time::from_double(t);
|
||||
@ -794,6 +794,8 @@ int tool_app_t::execute()
|
||||
|
||||
if (opt_help())
|
||||
{
|
||||
pout(plib::pfmt("{:10.3}\n").f(20.0));
|
||||
//pout(plib::pfmt("{10.3}\n").f(20));
|
||||
pout(usage());
|
||||
return 0;
|
||||
}
|
||||
|
@ -7,7 +7,7 @@
|
||||
#include "plib/ppmf.h"
|
||||
#include "plib/pstream.h"
|
||||
|
||||
#include <cstring>
|
||||
#include <cstdio>
|
||||
|
||||
/* From: https://ffmpeg.org/pipermail/ffmpeg-devel/2007-October/038122.html
|
||||
* The most compatible way to make a wav header for unknown length is to put
|
||||
@ -58,7 +58,7 @@ public:
|
||||
template <typename T>
|
||||
void write(const T &val)
|
||||
{
|
||||
m_f.write(reinterpret_cast<const plib::postream::value_type *>(&val), sizeof(T));
|
||||
m_f.write(reinterpret_cast<const plib::postream::char_type *>(&val), sizeof(T));
|
||||
}
|
||||
|
||||
void write_sample(int *sample)
|
||||
@ -146,7 +146,7 @@ public:
|
||||
{
|
||||
// sscanf is very fast ...
|
||||
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
|
||||
sscanf(line.c_str(), "%lf %lf", &m_e[i].t, &m_e[i].v);
|
||||
std::sscanf(line.c_str(), "%lf %lf", &m_e[i].t, &m_e[i].v);
|
||||
m_e[i].need_more = false;
|
||||
}
|
||||
}
|
||||
@ -304,7 +304,7 @@ public:
|
||||
, m_format(format)
|
||||
{
|
||||
for (pstring::value_type c = 64; c < 64+26; c++)
|
||||
m_ids.emplace_back(pstring(c));
|
||||
m_ids.emplace_back(pstring(1, c));
|
||||
write("$date Sat Jan 19 14:14:17 2019\n");
|
||||
write("$end\n");
|
||||
write("$version Netlist nlwav 0.1\n");
|
||||
@ -355,9 +355,7 @@ public:
|
||||
private:
|
||||
void write(const pstring &line)
|
||||
{
|
||||
auto p = static_cast<const char *>(line.c_str());
|
||||
std::size_t len = std::strlen(p);
|
||||
m_fo.write(p, len);
|
||||
m_fo.write(line.c_str(), plib::strlen(line.c_str()));
|
||||
}
|
||||
|
||||
std::size_t m_channels;
|
||||
|
@ -265,7 +265,7 @@ namespace devices
|
||||
plib::postringstream t;
|
||||
plib::putf8_fmt_writer w(&t);
|
||||
csc_private(w);
|
||||
std::hash<pstring> h;
|
||||
std::hash<typename std::remove_const<std::remove_reference<decltype(t.str())>::type>::type> h;
|
||||
|
||||
return plib::pfmt("nl_gcr_{1:x}_{2}")(h( t.str() ))(mat.nz_num);
|
||||
}
|
||||
|
@ -386,7 +386,7 @@ namespace devices
|
||||
ms->setup(grp);
|
||||
|
||||
log().verbose("Solver {1}", ms->name());
|
||||
log().verbose(" ==> {2} nets", grp.size());
|
||||
log().verbose(" ==> {1} nets", grp.size());
|
||||
log().verbose(" has {1} elements", ms->has_dynamic_devices() ? "dynamic" : "no dynamic");
|
||||
log().verbose(" has {1} elements", ms->has_timestep_devices() ? "timestep" : "no timestep");
|
||||
for (auto &n : grp)
|
||||
|
@ -213,7 +213,7 @@ double nl_convert_base_t::get_sp_val(const pstring &sin)
|
||||
++p;
|
||||
pstring val = plib::left(sin, p);
|
||||
pstring unit = sin.substr(p);
|
||||
double ret = get_sp_unit(unit) * plib::pstonum<double, true>(val);
|
||||
double ret = get_sp_unit(unit) * plib::pstonum<double>(val);
|
||||
return ret;
|
||||
}
|
||||
|
||||
@ -532,7 +532,7 @@ void nl_convert_eagle_t::convert(const pstring &contents)
|
||||
else if (plib::ucase(sval) == "LOW")
|
||||
add_device("TTL_INPUT", name, 0);
|
||||
else
|
||||
add_device("ANALOG_INPUT", name, plib::pstonum<double, true>(sval));
|
||||
add_device("ANALOG_INPUT", name, plib::pstonum<double>(sval));
|
||||
add_pin_alias(name, "1", "Q");
|
||||
break;
|
||||
case 'D':
|
||||
|
@ -29,7 +29,7 @@ public:
|
||||
|
||||
virtual ~nl_convert_base_t();
|
||||
|
||||
const pstring &result() { return m_buf.str(); }
|
||||
pstring result() { return pstring(m_buf.str()); }
|
||||
|
||||
virtual void convert(const pstring &contents) = 0;
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user