From 115ffcb10a45bca233fa6e22f674d8db8982b7df Mon Sep 17 00:00:00 2001 From: Miodrag Milanovic Date: Sat, 12 Sep 2015 10:12:14 +0200 Subject: Moved netlist from emu to lib (nw) --- src/lib/netlist/plib/pstring.c | 705 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 705 insertions(+) create mode 100644 src/lib/netlist/plib/pstring.c (limited to 'src/lib/netlist/plib/pstring.c') diff --git a/src/lib/netlist/plib/pstring.c b/src/lib/netlist/plib/pstring.c new file mode 100644 index 00000000000..a493707c247 --- /dev/null +++ b/src/lib/netlist/plib/pstring.c @@ -0,0 +1,705 @@ +// license:GPL-2.0+ +// copyright-holders:Couriersud +/* + * nl_string.c + * + */ + +#include +//FIXME:: pstring should be locale free +#include +#include +#include +#include + +#include "pstring.h" +#include "palloc.h" +#include "plists.h" + +template<> +pstr_t pstring_t::m_zero = pstr_t(0); +template<> +pstr_t pstring_t::m_zero = pstr_t(0); + + +/* + * Uncomment the following to override defaults + */ + +#define IMMEDIATE_MODE (1) +//#define DEBUG_MODE (0) + +#ifdef MAME_DEBUG + #ifndef IMMEDIATE_MODE + #define IMMEDIATE_MODE (1) + #endif + #ifndef DEBUG_MODE + #define DEBUG_MODE (0) + #endif +#else + #ifndef IMMEDIATE_MODE + #define IMMEDIATE_MODE (1) + #endif + #ifndef DEBUG_MODE + #define DEBUG_MODE (0) + #endif +#endif + +template +pstring_t::~pstring_t() +{ + sfree(m_ptr); +} + +template +void pstring_t::pcat(const mem_t *s) +{ + int slen = strlen(s); + pstr_t *n = salloc(m_ptr->len() + slen); + if (m_ptr->len() > 0) + std::memcpy(n->str(), m_ptr->str(), m_ptr->len()); + if (slen > 0) + std::memcpy(n->str() + m_ptr->len(), s, slen); + *(n->str() + n->len()) = 0; + sfree(m_ptr); + m_ptr = n; +} + +template +void pstring_t::pcat(const pstring_t &s) +{ + int slen = s.blen(); + pstr_t *n = salloc(m_ptr->len() + slen); + if (m_ptr->len() > 0) + std::memcpy(n->str(), m_ptr->str(), m_ptr->len()); + if (slen > 0) + std::memcpy(n->str() + m_ptr->len(), s.cstr(), slen); + *(n->str() + n->len()) = 0; + sfree(m_ptr); + m_ptr = n; +} + +template +int pstring_t::pcmp(const pstring_t &right) const +{ + long l = std::min(blen(), right.blen()); + if (l == 0) + { + if (blen() == 0 && right.blen() == 0) + return 0; + else if (right.blen() == 0) + return 1; + else + return -1; + } + int ret = memcmp(m_ptr->str(), right.cstr(), l); + if (ret == 0) + ret = this->blen() - right.blen(); + if (ret < 0) + return -1; + else if (ret > 0) + return 1; + else + return 0; +} + + +template +void pstring_t::pcopy(const mem_t *from, int size) +{ + pstr_t *n = salloc(size); + if (size > 0) + std::memcpy(n->str(), from, size); + *(n->str() + size) = 0; + sfree(m_ptr); + m_ptr = n; +} + +template +const pstring_t pstring_t::substr(int start, int count) const +{ + pstring_t ret; + int alen = (int) len(); + if (start < 0) + start = 0; + if (start >= alen) + return ret; + if (count <0 || start + count > alen) + count = alen - start; + const char *p = cstr(); + if (count <= 0) + ret.pcopy(p, 0); + else + { + //FIXME: Trait to tell which one + //ret.pcopy(cstr() + start, count); + // find start + for (int i=0; i +const pstring_t pstring_t::ucase() const +{ + pstring_t ret = *this; + ret.pcopy(cstr(), blen()); + for (unsigned i=0; istr()[i] = toupper((unsigned) ret.m_ptr->str()[i]); + return ret; +} + +template +int pstring_t::find_first_not_of(const pstring_t &no) const +{ + char *t = m_ptr->str(); + unsigned nolen = no.len(); + unsigned tlen = len(); + for (unsigned i=0; i < tlen; i++) + { + char *n = no.m_ptr->str(); + bool f = true; + for (unsigned j=0; j < nolen; j++) + { + if (F::code(t) == F::code(n)) + f = false; + n += F::codelen(t); + } + if (f) + return i; + t += F::codelen(t); + } + return -1; +} + +template +int pstring_t::find_last_not_of(const pstring_t &no) const +{ + char *t = m_ptr->str(); + unsigned nolen = no.len(); + unsigned tlen = len(); + int last_found = -1; + for (unsigned i=0; i < tlen; i++) + { + char *n = no.m_ptr->str(); + bool f = true; + for (unsigned j=0; j < nolen; j++) + { + if (F::code(t) == F::code(n)) + f = false; + n += F::codelen(t); + } + if (f) + last_found = i; + t += F::codelen(t); + } + return last_found; +} + +template +pstring_t pstring_t::replace(const pstring_t &search, const pstring_t &replace) const +{ + // FIXME: use this pstringbuffer ret = ""; + pstring_t ret = ""; + const int slen = search.blen(); + const int tlen = blen(); + + if (slen == 0 || tlen < slen ) + return *this; + int i = 0; + while (i < tlen - slen + 1) + { + if (memcmp(cstr()+i,search.cstr(),slen) == 0) + { + ret += replace; + i += slen; + } + else + { + /* avoid adding a code, cat a string ... */ + mem_t buf[2] = { *(cstr() + i), 0 }; + ret = ret.cat(buf); + i++; + } + } + ret = ret.cat(cstr() + i); + return ret; +} + +template +const pstring_t pstring_t::ltrim(const pstring_t &ws) const +{ + int f = find_first_not_of(ws); + if (f>=0) + return substr(f); + else + return ""; +} + +template +const pstring_t pstring_t::rtrim(const pstring_t &ws) const +{ + int f = find_last_not_of(ws); + if (f>=0) + return left(f+1); + else + return ""; +} + +template +const pstring_t pstring_t::rpad(const pstring_t &ws, const unsigned cnt) const +{ + // FIXME: pstringbuffer ret(*this); + + pstring_t ret(*this); + while (ret.len() < cnt) + ret += ws; + return pstring_t(ret).substr(0, cnt); +} + + +template +void pstring_t::pcopy(const mem_t *from) +{ + pcopy(from, strlen(from)); +} + +template +double pstring_t::as_double(bool *error) const +{ + double ret; + char *e = NULL; + + if (error != NULL) + *error = false; + ret = strtod(cstr(), &e); + if (*e != 0) + if (error != NULL) + *error = true; + return ret; +} + +template +long pstring_t::as_long(bool *error) const +{ + long ret; + char *e = NULL; + + if (error != NULL) + *error = false; + if (startsWith("0x")) + ret = strtol(substr(2).cstr(), &e, 16); + else + ret = strtol(cstr(), &e, 10); + if (*e != 0) + if (error != NULL) + *error = true; + return ret; +} + +// ---------------------------------------------------------------------------------------- +// static stuff ... +// ---------------------------------------------------------------------------------------- + +/* + * Cached allocation of string memory + * + * This improves startup performance by 30%. + */ + +#if 1 + +static pstack_t *stk = NULL; + +static inline unsigned countleadbits(unsigned x) +{ +#ifndef count_leading_zeros + unsigned msk; + unsigned ret; + if (x < 0x100) + { + msk = 0x80; + ret = 24; + } + else if (x < 0x10000) + { + msk = 0x8000; + ret = 16; + } + else if (x < 0x1000000) + { + msk = 0x800000; + ret = 8; + } + else + { + msk = 0x80000000; + ret = 0; + } + while ((msk & x) == 0 && ret < 31) + { + msk = msk >> 1; + ret++; + } + return ret; +#else + return count_leading_zeros(x); +#endif +} + +template +void pstring_t::sfree(pstr_t *s) +{ + s->m_ref_count--; + if (s->m_ref_count == 0 && s != &m_zero) + { + if (stk != NULL) + { + unsigned sn= ((32 - countleadbits(s->len())) + 1) / 2; + stk[sn].push(s); + } + else + pfree_array(((char *)s)); + //_mm_free(((char *)s)); + } +} + +template +pstr_t *pstring_t::salloc(int n) +{ + if (stk == NULL) + stk = palloc_array(pstack_t, 17); + pstr_t *p; + unsigned sn= ((32 - countleadbits(n)) + 1) / 2; + unsigned size = sizeof(pstr_t) + (1<<(sn * 2)) + 1; + if (stk[sn].empty()) + p = (pstr_t *) palloc_array(char, size); + else + { + //printf("%u %u\n", sn, (unsigned) stk[sn].count()); + p = stk[sn].pop(); + } + + // str_t *p = (str_t *) _mm_malloc(size, 8); + p->init(n); + return p; +} +template +void pstring_t::resetmem() +{ + if (stk != NULL) + { + for (unsigned i=0; i<=16; i++) + { + for (; stk[i].count() > 0; ) + pfree_array(stk[i].pop()); + } + pfree_array(stk); + stk = NULL; + } +} + + +#else +template +void pstring_t::sfree(pstr_t *s) +{ + s->m_ref_count--; + if (s->m_ref_count == 0 && s != &m_zero) + { + pfree_array(((char *)s)); + //_mm_free(((char *)s)); + } +} + +template +pstr_t *pstring_t::salloc(int n) +{ + int size = sizeof(pstr_t) + n + 1; + pstr_t *p = (pstr_t *) palloc_array(char, size); + // str_t *p = (str_t *) _mm_malloc(size, 8); + p->init(n); + return p; +} + +template +void pstring_t::resetmem() +{ + // Release the 0 string +} +#endif + + +// ---------------------------------------------------------------------------------------- +// pstring ... +// ---------------------------------------------------------------------------------------- + +const pstring::type_t pstring::vprintf(va_list args) const +{ + // sprintf into the temporary buffer + char tempbuf[4096]; + std::vsprintf(tempbuf, cstr(), args); + + return type_t(tempbuf); +} + + +const pstring::type_t pstring::sprintf(const char *format, ...) +{ + va_list ap; + va_start(ap, format); + type_t ret = pstring(format).vprintf(ap); + va_end(ap); + return ret; +} + +template +int pstring_t::find(const pstring_t &search, unsigned start) const +{ + const unsigned tlen = len(); + const unsigned slen = search.len(); + const char *s = search.cstr(); + const unsigned startt = std::min(start, tlen); + const char *t = cstr(); + for (unsigned i=0; i +int pstring_t::find(const mem_t *search, unsigned start) const +{ + const unsigned tlen = len(); + unsigned slen = 0; + unsigned sblen = 0; + const mem_t *x = search; + while (*x != 0) + { + slen++; + const unsigned sl = F::codelen(x); + x += sl; + sblen += sl; + } + const char *s = search; + const unsigned startt = std::min(start, tlen); + const char *t = cstr(); + for (unsigned i=0; i +bool pstring_t::startsWith(const pstring_t &arg) const +{ + if (arg.blen() > blen()) + return false; + else + return (memcmp(arg.cstr(), cstr(), arg.len()) == 0); +} + +template +bool pstring_t::endsWith(const pstring_t &arg) const +{ + if (arg.blen() > blen()) + return false; + else + return (memcmp(cstr()+this->len()-arg.len(), arg.cstr(), arg.len()) == 0); +} + + +template +bool pstring_t::startsWith(const mem_t *arg) const +{ + unsigned alen = strlen(arg); + if (alen > blen()) + return false; + else + return (memcmp(arg, cstr(), alen) == 0); +} + +template +int pstring_t::pcmp(const mem_t *right) const +{ + return std::strcmp(m_ptr->str(), right); +} + +// ---------------------------------------------------------------------------------------- +// pstringbuffer +// ---------------------------------------------------------------------------------------- + +pstringbuffer::~pstringbuffer() +{ + if (m_ptr != NULL) + pfree_array(m_ptr); +} + +void pstringbuffer::resize(const std::size_t size) +{ + if (m_ptr == NULL) + { + m_size = DEFAULT_SIZE; + while (m_size <= size) + m_size *= 2; + m_ptr = palloc_array(char, m_size); + *m_ptr = 0; + m_len = 0; + } + else if (m_size < size) + { + while (m_size < size) + m_size *= 2; + char *new_buf = palloc_array(char, m_size); + std::memcpy(new_buf, m_ptr, m_len + 1); + pfree_array(m_ptr); + m_ptr = new_buf; + } +} + +void pstringbuffer::pcopy(const char *from) +{ + std::size_t nl = strlen(from) + 1; + resize(nl); + std::memcpy(m_ptr, from, nl); +} + +void pstringbuffer::pcopy(const pstring &from) +{ + std::size_t nl = from.blen() + 1; + resize(nl); + std::memcpy(m_ptr, from.cstr(), nl); +} + +void pstringbuffer::pcat(const char *s) +{ + const std::size_t slen = std::strlen(s); + const std::size_t nl = m_len + slen + 1; + resize(nl); + std::memcpy(m_ptr + m_len, s, slen + 1); + m_len += slen; +} + +void pstringbuffer::pcat(const void *m, unsigned l) +{ + const std::size_t nl = m_len + l + 1; + resize(nl); + std::memcpy(m_ptr + m_len, m, l); + m_len += l; + *(m_ptr + m_len) = 0; +} + +void pstringbuffer::pcat(const pstring &s) +{ + const std::size_t slen = s.blen(); + const std::size_t nl = m_len + slen + 1; + resize(nl); + std::memcpy(m_ptr + m_len, s.cstr(), slen); + m_len += slen; + m_ptr[m_len] = 0; +} + +pfmt::pfmt(const pstring &fmt) +: m_arg(0) +{ + memcpy(m_str, fmt.cstr(), fmt.blen() + 1); +} + +pfmt::pfmt(const char *fmt) +: m_arg(0) +{ + strncpy(m_str, fmt, sizeof(m_str) - 1); + m_str[sizeof(m_str) - 1] = 0; +} + +#if 0 +void pformat::format_element(const char *f, const char *l, const char *fmt_spec, ...) +{ + va_list ap; + va_start(ap, fmt_spec); + char fmt[30] = "%"; + char search[10] = ""; + char buf[1024]; + strcat(fmt, f); + strcat(fmt, l); + strcat(fmt, fmt_spec); + int nl = vsprintf(buf, fmt, ap); + m_arg++; + int sl = sprintf(search, "%%%d", m_arg); + char *p = strstr(m_str, search); + if (p != NULL) + { + // Make room + memmove(p+nl, p+sl, strlen(p) + 1 - sl); + memcpy(p, buf, nl); + } + va_end(ap); +} +#else +void pfmt::format_element(const char *f, const char *l, const char *fmt_spec, ...) +{ + va_list ap; + va_start(ap, fmt_spec); + char fmt[30] = "%"; + char search[10] = ""; + char buf[1024]; + m_arg++; + int sl = sprintf(search, "{%d:", m_arg); + char *p = strstr(m_str, search); + if (p == NULL) + { + sl = sprintf(search, "{%d}", m_arg); + p = strstr(m_str, search); + if (p == NULL) + { + sl = 2; + p = strstr(m_str, "{}"); + } + strcat(fmt, f); + } + else + { + char *p1 = strstr(p, "}"); + if (p1 != NULL) + { + sl = p1 - p + 1; + if (m_arg>=10) + strncat(fmt, p+4, p1 - p - 4); + else + strncat(fmt, p+3, p1 - p - 3); + } + else + strcat(fmt, f); + } + strcat(fmt, l); + strcat(fmt, fmt_spec); + int nl = vsprintf(buf, fmt, ap); + if (p != NULL) + { + // Make room + memmove(p+nl, p+sl, strlen(p) + 1 - sl); + memcpy(p, buf, nl); + } + va_end(ap); +} +#endif + +template struct pstring_t; +template struct pstring_t; -- cgit v1.2.3-70-g09d2 From 954e900b78f42c0b7307682e50db928df0489ba1 Mon Sep 17 00:00:00 2001 From: Miodrag Milanovic Date: Sun, 13 Sep 2015 12:17:50 +0200 Subject: Fix VS x64 compile (nw) --- src/lib/netlist/plib/pstring.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/lib/netlist/plib/pstring.c') diff --git a/src/lib/netlist/plib/pstring.c b/src/lib/netlist/plib/pstring.c index a493707c247..fb952f6fe91 100644 --- a/src/lib/netlist/plib/pstring.c +++ b/src/lib/netlist/plib/pstring.c @@ -376,7 +376,7 @@ pstr_t *pstring_t::salloc(int n) stk = palloc_array(pstack_t, 17); pstr_t *p; unsigned sn= ((32 - countleadbits(n)) + 1) / 2; - unsigned size = sizeof(pstr_t) + (1<<(sn * 2)) + 1; + unsigned size = sizeof(pstr_t) + (1ULL<<(sn * 2)) + 1; if (stk[sn].empty()) p = (pstr_t *) palloc_array(char, size); else -- cgit v1.2.3-70-g09d2 From 9fca8bdc44faa35e14ed868115a9d01f6635fd84 Mon Sep 17 00:00:00 2001 From: couriersud Date: Mon, 14 Sep 2015 12:34:07 +0200 Subject: Remove last traces of printf from plib. Reorganized some code. (nw) --- scripts/src/netlist.lua | 3 + src/lib/netlist/nl_base.c | 1 - src/lib/netlist/nl_base.h | 1 + src/lib/netlist/nl_parser.c | 1 - src/lib/netlist/plib/palloc.c | 2 - src/lib/netlist/plib/pfmtlog.c | 166 +++++++++++++++++ src/lib/netlist/plib/pfmtlog.h | 292 ++++++++++++++++++++++++++++++ src/lib/netlist/plib/pstream.c | 114 +++++++++++- src/lib/netlist/plib/pstream.h | 100 ++++++---- src/lib/netlist/plib/pstring.c | 107 +---------- src/lib/netlist/plib/pstring.h | 277 ---------------------------- src/lib/netlist/plib/ptypes.h | 97 ++++++++++ src/lib/netlist/prg/nltool.c | 127 ++++++------- src/lib/netlist/solver/nld_ms_direct_lu.h | 2 +- src/lib/netlist/solver/nld_solver.h | 3 - 15 files changed, 781 insertions(+), 512 deletions(-) create mode 100644 src/lib/netlist/plib/pfmtlog.c create mode 100644 src/lib/netlist/plib/pfmtlog.h create mode 100644 src/lib/netlist/plib/ptypes.h (limited to 'src/lib/netlist/plib/pstring.c') diff --git a/scripts/src/netlist.lua b/scripts/src/netlist.lua index 34c64261095..7cf8ddc9991 100644 --- a/scripts/src/netlist.lua +++ b/scripts/src/netlist.lua @@ -38,6 +38,8 @@ project "netlist" MAME_DIR .. "src/lib/netlist/plib/pconfig.h", MAME_DIR .. "src/lib/netlist/plib/palloc.c", MAME_DIR .. "src/lib/netlist/plib/palloc.h", + MAME_DIR .. "src/lib/netlist/plib/pfmtlog.c", + MAME_DIR .. "src/lib/netlist/plib/pfmtlog.h", MAME_DIR .. "src/lib/netlist/plib/plists.h", MAME_DIR .. "src/lib/netlist/plib/poptions.h", MAME_DIR .. "src/lib/netlist/plib/pparser.c", @@ -50,6 +52,7 @@ project "netlist" MAME_DIR .. "src/lib/netlist/plib/pstring.h", MAME_DIR .. "src/lib/netlist/plib/pstream.c", MAME_DIR .. "src/lib/netlist/plib/pstream.h", + MAME_DIR .. "src/lib/netlist/plib/ptypes.h", MAME_DIR .. "src/lib/netlist/tools/nl_convert.c", MAME_DIR .. "src/lib/netlist/tools/nl_convert.h", MAME_DIR .. "src/lib/netlist/analog/nld_bjt.c", diff --git a/src/lib/netlist/nl_base.c b/src/lib/netlist/nl_base.c index 6c1dab54289..ce818e0d94f 100644 --- a/src/lib/netlist/nl_base.c +++ b/src/lib/netlist/nl_base.c @@ -271,7 +271,6 @@ ATTR_COLD net_t *netlist_t::find_net(const pstring &name) ATTR_COLD void netlist_t::rebuild_lists() { - //printf("Rebuild Lists\n"); for (std::size_t i = 0; i < m_nets.size(); i++) m_nets[i]->rebuild_list(); } diff --git a/src/lib/netlist/nl_base.h b/src/lib/netlist/nl_base.h index e472031baa6..4bc152e253f 100644 --- a/src/lib/netlist/nl_base.h +++ b/src/lib/netlist/nl_base.h @@ -159,6 +159,7 @@ #include "nl_time.h" #include "nl_util.h" #include "plib/pstate.h" +#include "plib/pfmtlog.h" // ---------------------------------------------------------------------------------------- // Type definitions diff --git a/src/lib/netlist/nl_parser.c b/src/lib/netlist/nl_parser.c index 32609079ba6..312410fafc6 100644 --- a/src/lib/netlist/nl_parser.c +++ b/src/lib/netlist/nl_parser.c @@ -425,7 +425,6 @@ nl_double parser_t::eval_param(const token_t tok) nl_double ret; pstring val; - //printf("param {1}\n", tok.m_token); for (i=1; i<6;i++) if (tok.str().equals(macs[i])) f = i; diff --git a/src/lib/netlist/plib/palloc.c b/src/lib/netlist/plib/palloc.c index 9b0ee555cbb..22dbc1d4e93 100644 --- a/src/lib/netlist/plib/palloc.c +++ b/src/lib/netlist/plib/palloc.c @@ -35,13 +35,11 @@ pmemory_pool *ppool = &sppool; void* operator new(std::size_t size, pmemory_pool *pool) throw (std::bad_alloc) { - //printf("here new\n"); return palloc_raw(size);; } void operator delete(void *ptr, pmemory_pool *pool) { - //printf("here delete\n"); if (ptr != NULL) pfree_raw(ptr); } diff --git a/src/lib/netlist/plib/pfmtlog.c b/src/lib/netlist/plib/pfmtlog.c new file mode 100644 index 00000000000..2b08bb2fa98 --- /dev/null +++ b/src/lib/netlist/plib/pfmtlog.c @@ -0,0 +1,166 @@ +// license:GPL-2.0+ +// copyright-holders:Couriersud +/* + * nl_string.c + * + */ + +#if 0 +#include +//FIXME:: pstring should be locale free +#include +#include +#include +#endif + +#include + +#include "pfmtlog.h" +#include "palloc.h" + +pfmt::pfmt(const pstring &fmt) +: m_str(m_str_buf), m_allocated(0), m_arg(0) +{ + unsigned l = fmt.blen() + 1; + if (l>sizeof(m_str_buf)) + { + m_allocated = 2 * l; + m_str = palloc_array(char, 2 * l); + } + memcpy(m_str, fmt.cstr(), l); +} + +pfmt::pfmt(const char *fmt) +: m_str(m_str_buf), m_allocated(0), m_arg(0) +{ + unsigned l = strlen(fmt) + 1; + if (l>sizeof(m_str_buf)) + { + m_allocated = 2 * l; + m_str = palloc_array(char, 2 * l); + } + memcpy(m_str, fmt, l); +} + +pfmt::~pfmt() +{ + if (m_allocated > 0) + pfree_array(m_str); +} + +#if 0 +void pformat::format_element(const char *f, const char *l, const char *fmt_spec, ...) +{ + va_list ap; + va_start(ap, fmt_spec); + char fmt[30] = "%"; + char search[10] = ""; + char buf[1024]; + strcat(fmt, f); + strcat(fmt, l); + strcat(fmt, fmt_spec); + int nl = vsprintf(buf, fmt, ap); + m_arg++; + int sl = sprintf(search, "%%%d", m_arg); + char *p = strstr(m_str, search); + if (p != NULL) + { + // Make room + memmove(p+nl, p+sl, strlen(p) + 1 - sl); + memcpy(p, buf, nl); + } + va_end(ap); +} +#else +void pfmt::format_element(const char *f, const char *l, const char *fmt_spec, ...) +{ + va_list ap; + va_start(ap, fmt_spec); + char fmt[30] = "%"; + char search[10] = ""; + char buf[2048]; + m_arg++; + int sl = sprintf(search, "{%d:", m_arg); + char *p = strstr(m_str, search); + if (p == NULL) + { + sl = sprintf(search, "{%d}", m_arg); + p = strstr(m_str, search); + if (p == NULL) + { + sl = 2; + p = strstr(m_str, "{}"); + } + if (p==NULL) + { + sl=1; + p = strstr(m_str, "{"); + if (p != NULL) + { + char *p1 = strstr(p, "}"); + if (p1 != NULL) + { + sl = p1 - p + 1; + strncat(fmt, p+1, p1 - p - 2); + } + else + strcat(fmt, f); + } + else + strcat(fmt, f); + } + } + else + { + char *p1 = strstr(p, "}"); + if (p1 != NULL) + { + sl = p1 - p + 1; + if (m_arg>=10) + strncat(fmt, p+4, p1 - p - 4); + else + strncat(fmt, p+3, p1 - p - 3); + } + else + strcat(fmt, f); + } + strcat(fmt, l); + char *pend = fmt + strlen(fmt) - 1; + if (strchr("fge", *fmt_spec) != NULL) + { + if (strchr("fge", *pend) == NULL) + strcat(fmt, fmt_spec); + } + else if (strchr("duxo", *fmt_spec) != NULL) + { + if (strchr("duxo", *pend) == NULL) + strcat(fmt, fmt_spec); + } + else + strcat(fmt, fmt_spec); + int nl = vsprintf(buf, fmt, ap); + if (p != NULL) + { + // check room + unsigned new_size = (p - m_str) + nl + strlen(p) + 1 - sl; + if (new_size > m_allocated) + { + unsigned old_alloc = std::max(m_allocated, (unsigned) sizeof(m_str_buf)); + if (m_allocated < old_alloc) + m_allocated = old_alloc; + while (new_size > m_allocated) + m_allocated *= 2; + char *np = palloc_array(char, m_allocated); + memcpy(np, m_str, old_alloc); + p = np + (p - m_str); + if (m_str != m_str_buf) + pfree_array(m_str); + m_str = np; + } + // Make room + memmove(p+nl, p+sl, strlen(p) + 1 - sl); + memcpy(p, buf, nl); + } + va_end(ap); +} +#endif diff --git a/src/lib/netlist/plib/pfmtlog.h b/src/lib/netlist/plib/pfmtlog.h new file mode 100644 index 00000000000..058152a91cc --- /dev/null +++ b/src/lib/netlist/plib/pfmtlog.h @@ -0,0 +1,292 @@ +// license:GPL-2.0+ +// copyright-holders:Couriersud +/* + * pfmtlog.h + */ + +#ifndef _PFMT_H_ +#define _PFMT_H_ + +//#include +//#include + +#include "pconfig.h" +#include "pstring.h" +#include "ptypes.h" + +template +struct ptype_treats +{ +}; + +template<> +struct ptype_treats +{ + static short cast(char x) { return x; } + static const bool is_signed = true; + static const char *size_specifier() { return "h"; } +}; + +template<> +struct ptype_treats +{ + static short cast(short x) { return x; } + static const bool is_signed = true; + static const char *size_specifier() { return "h"; } +}; + +template<> +struct ptype_treats +{ + static int cast(int x) { return x; } + static const bool is_signed = true; + static const char *size_specifier() { return ""; } +}; + +template<> +struct ptype_treats +{ + static long cast(long x) { return x; } + static const bool is_signed = true; + static const char *size_specifier() { return "l"; } +}; + +template<> +struct ptype_treats +{ + static long long cast(long long x) { return x; } + static const bool is_signed = true; + static const char *size_specifier() { return "ll"; } +}; + +template<> +struct ptype_treats +{ + static unsigned short cast(unsigned char x) { return x; } + static const bool is_signed = false; + static const char *size_specifier() { return "h"; } +}; + +template<> +struct ptype_treats +{ + static unsigned short cast(unsigned short x) { return x; } + static const bool is_signed = false; + static const char *size_specifier() { return "h"; } +}; + +template<> +struct ptype_treats +{ + static unsigned int cast(unsigned int x) { return x; } + static const bool is_signed = false; + static const char *size_specifier() { return ""; } +}; + +template<> +struct ptype_treats +{ + static unsigned long cast(unsigned long x) { return x; } + static const bool is_signed = false; + static const char *size_specifier() { return "l"; } +}; + +template<> +struct ptype_treats +{ + static unsigned long long cast(unsigned long long x) { return x; } + static const bool is_signed = false; + static const char *size_specifier() { return "ll"; } +}; + +template +class pformat_base +{ +public: + + virtual ~pformat_base() { } + + ATTR_COLD P &operator ()(const double x, const char *f = "") { format_element(f, "", "f", x); return static_cast

(*this); } + ATTR_COLD P & e(const double x, const char *f = "") { format_element(f, "", "e", x); return static_cast

(*this); } + ATTR_COLD P & g(const double x, const char *f = "") { format_element(f, "", "g", x); return static_cast

(*this); } + + ATTR_COLD P &operator ()(const char *x, const char *f = "") { format_element(f, "", "s", x); return static_cast

(*this); } + ATTR_COLD P &operator ()(char *x, const char *f = "") { format_element(f, "", "s", x); return static_cast

(*this); } + ATTR_COLD P &operator ()(const void *x, const char *f = "") { format_element(f, "", "p", x); return static_cast

(*this); } + ATTR_COLD P &operator ()(const pstring &x, const char *f = "") { format_element(f, "", "s", x.cstr() ); return static_cast

(*this); } + + template + ATTR_COLD P &operator ()(const T x, const char *f = "") + { + if (ptype_treats::is_signed) + format_element(f, ptype_treats::size_specifier(), "d", ptype_treats::cast(x)); + else + format_element(f, ptype_treats::size_specifier(), "u", ptype_treats::cast(x)); + return static_cast

(*this); + } + + template + ATTR_COLD P &x(const T x, const char *f = "") + { + format_element(f, ptype_treats::size_specifier(), "x", x); + return static_cast

(*this); + } + + template + ATTR_COLD P &o(const T x, const char *f = "") + { + format_element(f, ptype_treats::size_specifier(), "o", x); + return static_cast

(*this); + } + +protected: + + virtual void format_element(const char *f, const char *l, const char *fmt_spec, ...) = 0; + +}; + +class pfmt : public pformat_base +{ +public: + pfmt(const pstring &fmt); + pfmt(const char *fmt); + virtual ~pfmt(); + + operator pstring() const { return m_str; } + + const char *cstr() { return m_str; } + + +protected: + void format_element(const char *f, const char *l, const char *fmt_spec, ...); + +private: + + char *m_str; + char m_str_buf[256]; + unsigned m_allocated; + unsigned m_arg; +}; + +P_ENUM(plog_level, + DEBUG, + INFO, + VERBOSE, + WARNING, + ERROR, + FATAL) + +class plog_dispatch_intf; + +template +class pfmt_writer_t +{ +public: + pfmt_writer_t() : m_enabled(true) { } + virtual ~pfmt_writer_t() { } + + ATTR_COLD void operator ()(const char *fmt) const + { + if (build_enabled && m_enabled) vdowrite(fmt); + } + + template + ATTR_COLD void operator ()(const char *fmt, const T1 &v1) const + { + if (build_enabled && m_enabled) vdowrite(pfmt(fmt)(v1)); + } + + template + ATTR_COLD void operator ()(const char *fmt, const T1 &v1, const T2 &v2) const + { + if (build_enabled && m_enabled) vdowrite(pfmt(fmt)(v1)(v2)); + } + + template + ATTR_COLD void operator ()(const char *fmt, const T1 &v1, const T2 &v2, const T3 &v3) const + { + if (build_enabled && m_enabled) vdowrite(pfmt(fmt)(v1)(v2)(v3)); + } + + template + ATTR_COLD void operator ()(const char *fmt, const T1 &v1, const T2 &v2, const T3 &v3, const T4 &v4) const + { + if (build_enabled && m_enabled) vdowrite(pfmt(fmt)(v1)(v2)(v3)(v4)); + } + + template + ATTR_COLD void operator ()(const char *fmt, const T1 &v1, const T2 &v2, const T3 &v3, const T4 &v4, const T5 &v5) const + { + if (build_enabled && m_enabled) vdowrite(pfmt(fmt)(v1)(v2)(v3)(v4)(v5)); + } + + void set_enabled(const bool v) + { + m_enabled = v; + } + + bool is_enabled() const { return m_enabled; } + +protected: + virtual void vdowrite(const pstring &ls) const {} + +private: + bool m_enabled; + +}; + +template +class plog_channel : public pfmt_writer_t +{ +public: + plog_channel(plog_dispatch_intf *b) : pfmt_writer_t(), m_base(b) { } + virtual ~plog_channel() { } + +protected: + virtual void vdowrite(const pstring &ls) const; + +private: + plog_dispatch_intf *m_base; +}; + +class plog_dispatch_intf +{ + template friend class plog_channel; + +public: + virtual ~plog_dispatch_intf() { } +protected: + virtual void vlog(const plog_level &l, const pstring &ls) const = 0; +}; + +template +class plog_base +{ +public: + + plog_base(plog_dispatch_intf *proxy) + : debug(proxy), + info(proxy), + verbose(proxy), + warning(proxy), + error(proxy), + fatal(proxy) + {} + virtual ~plog_base() {}; + + plog_channel debug; + plog_channel info; + plog_channel verbose; + plog_channel warning; + plog_channel error; + plog_channel fatal; +}; + + +template +void plog_channel::vdowrite(const pstring &ls) const +{ + m_base->vlog(L, ls); +} + +#endif /* _PSTRING_H_ */ diff --git a/src/lib/netlist/plib/pstream.c b/src/lib/netlist/plib/pstream.c index ae2f5b580a1..ba7c5c7eda2 100644 --- a/src/lib/netlist/plib/pstream.c +++ b/src/lib/netlist/plib/pstream.c @@ -13,13 +13,63 @@ #include "pstream.h" #include "palloc.h" +// ----------------------------------------------------------------------------- +// pistream: input stream +// ----------------------------------------------------------------------------- + +bool pistream::readline(pstring &line) +{ + UINT8 c = 0; + pstringbuffer buf; + if (!this->read(c)) + { + line = ""; + return false; + } + while (true) + { + if (c == 10) + break; + else if (c != 13) /* ignore CR */ + buf += c; + if (!this->read(c)) + break; + } + line = buf; + return true; +} + +// ----------------------------------------------------------------------------- +// postream: output stream +// ----------------------------------------------------------------------------- + +void postream::write(pistream &strm) +{ + char buf[1024]; + unsigned r; + while ( !bad() && ((r=strm.read(buf, 1024)) > 0)) + write(buf, r); +} + // ----------------------------------------------------------------------------- // Input file stream // ----------------------------------------------------------------------------- -pifilestream::pifilestream(const pstring &fname) : pistream(0), m_pos(0) +pifilestream::pifilestream(const pstring &fname) +: pistream(0), m_pos(0), m_actually_close(true) { - m_file = fopen(fname.cstr(), "rb"); + init(fopen(fname.cstr(), "rb")); +} + +pifilestream::pifilestream(void *file, const bool do_close) +: pistream(0), m_pos(0), m_actually_close(do_close) +{ + init(file); +} + +void pifilestream::init(void *file) +{ + m_file = file; if (m_file == NULL) { set_flag(FLAG_ERROR); @@ -44,8 +94,11 @@ pifilestream::~pifilestream() void pifilestream::close() { - fclose((FILE *) m_file); - set_flag(FLAG_CLOSED); + if (m_actually_close) + { + fclose((FILE *) m_file); + set_flag(FLAG_CLOSED); + } } unsigned pifilestream::vread(void *buf, unsigned n) @@ -88,13 +141,35 @@ pifilestream::pos_type pifilestream::vtell() return ret; } +// ----------------------------------------------------------------------------- +// pstdin: reads from stdin +// ----------------------------------------------------------------------------- + +pstdin::pstdin() +: pifilestream(stdin, false) +{ + /* nothing to do */ +} + // ----------------------------------------------------------------------------- // Output file stream // ----------------------------------------------------------------------------- -pofilestream::pofilestream(const pstring &fname) : postream(0), m_pos(0) +pofilestream::pofilestream(const pstring &fname) +: postream(0), m_pos(0), m_actually_close(true) { - m_file = fopen(fname.cstr(), "wb"); + init(fopen(fname.cstr(), "wb")); +} + +pofilestream::pofilestream(void *file, const bool do_close) +: postream(0), m_pos(0), m_actually_close(do_close) +{ + init(file); +} + +void pofilestream::init(void *file) +{ + m_file = file; if (m_file == NULL) { set_flag(FLAG_ERROR); @@ -118,8 +193,11 @@ pofilestream::~pofilestream() void pofilestream::close() { - fclose((FILE *) m_file); - set_flag(FLAG_CLOSED); + if (m_actually_close) + { + fclose((FILE *) m_file); + set_flag(FLAG_CLOSED); + } } void pofilestream::vwrite(const void *buf, unsigned n) @@ -146,7 +224,7 @@ void pofilestream::vseek(pos_type n) } } -pifilestream::pos_type pofilestream::vtell() +pstream::pos_type pofilestream::vtell() { long ret = ftell((FILE *) m_file); if (ret < 0) @@ -157,6 +235,24 @@ pifilestream::pos_type pofilestream::vtell() return ret; } +// ----------------------------------------------------------------------------- +// pstderr: write to stderr +// ----------------------------------------------------------------------------- + +pstderr::pstderr() +: pofilestream(stderr, false) +{ +} + +// ----------------------------------------------------------------------------- +// pstdout: write to stdout +// ----------------------------------------------------------------------------- + +pstdout::pstdout() +: pofilestream(stdout, false) +{ +} + // ----------------------------------------------------------------------------- // Memory stream // ----------------------------------------------------------------------------- diff --git a/src/lib/netlist/plib/pstream.h b/src/lib/netlist/plib/pstream.h index af9f939cc6c..32e7558fabe 100644 --- a/src/lib/netlist/plib/pstream.h +++ b/src/lib/netlist/plib/pstream.h @@ -14,6 +14,7 @@ #include "pconfig.h" #include "pstring.h" #include "palloc.h" +#include "pfmtlog.h" // ----------------------------------------------------------------------------- // pstream: things common to all streams @@ -28,7 +29,7 @@ public: static const pos_type SEEK_EOF = (pos_type) -1; - pstream(unsigned flags) : m_flags(flags) + pstream(const unsigned flags) : m_flags(flags) { } virtual ~pstream() @@ -38,7 +39,7 @@ public: bool bad() const { return ((m_flags & FLAG_ERROR) != 0); } bool seekable() const { return ((m_flags & FLAG_SEEKABLE) != 0); } - void seek(pos_type n) + void seek(const pos_type n) { check_seekable(); return vseek(n); @@ -50,7 +51,7 @@ public: } protected: - virtual void vseek(pos_type n) = 0; + virtual void vseek(const pos_type n) = 0; virtual pos_type vtell() = 0; static const unsigned FLAG_EOF = 0x01; @@ -60,11 +61,11 @@ protected: bool closed() { return ((m_flags & FLAG_CLOSED) != 0); } - void set_flag(unsigned flag) + void set_flag(const unsigned flag) { m_flags |= flag; } - void clear_flag(unsigned flag) + void clear_flag(const unsigned flag) { m_flags &= ~flag; } @@ -96,48 +97,28 @@ class pistream : public pstream P_PREVENT_COPYING(pistream) public: - pistream(unsigned flags) : pstream(flags) {} + pistream(const unsigned flags) : pstream(flags) {} virtual ~pistream() {} bool eof() const { return ((flags() & FLAG_EOF) != 0) || bad(); } /* this digests linux & dos/windows text files */ - bool readline(pstring &line) - { - UINT8 c = 0; - pstringbuffer buf; - if (!this->read(c)) - { - line = ""; - return false; - } - while (true) - { - if (c == 10) - break; - else if (c != 13) /* ignore CR */ - buf += c; - if (!this->read(c)) - break; - } - line = buf; - return true; - } + bool readline(pstring &line); bool read(UINT8 &c) { return (read(&c, 1) == 1); } - unsigned read(void *buf, unsigned n) + unsigned read(void *buf, const unsigned n) { return vread(buf, n); } protected: /* read up to n bytes from stream */ - virtual unsigned vread(void *buf, unsigned n) = 0; + virtual unsigned vread(void *buf, const unsigned n) = 0; private: }; @@ -172,14 +153,16 @@ public: write(&c, 1); } - void write(const void *buf, unsigned n) + void write(const void *buf, const unsigned n) { vwrite(buf, n); } + void write(pistream &strm); + protected: /* write n bytes to stream */ - virtual void vwrite(const void *buf, unsigned n) = 0; + virtual void vwrite(const void *buf, const unsigned n) = 0; private: }; @@ -201,8 +184,8 @@ public: protected: /* write n bytes to stream */ - virtual void vwrite(const void *buf, unsigned n); - virtual void vseek(pos_type n); + virtual void vwrite(const void *buf, const unsigned n); + virtual void vseek(const pos_type n); virtual pos_type vtell(); private: @@ -225,11 +208,11 @@ public: protected: /* write n bytes to stream */ - virtual void vwrite(const void *buf, unsigned n) + virtual void vwrite(const void *buf, const unsigned n) { m_buf.cat(buf, n); } - virtual void vseek(pos_type n) { } + virtual void vseek(const pos_type n) { } virtual pos_type vtell() { return m_buf.len(); } private: @@ -251,14 +234,40 @@ public: void close(); protected: + pofilestream(void *file, const bool do_close); /* write n bytes to stream */ - virtual void vwrite(const void *buf, unsigned n); - virtual void vseek(pos_type n); + virtual void vwrite(const void *buf, const unsigned n); + virtual void vseek(const pos_type n); virtual pos_type vtell(); private: void *m_file; pos_type m_pos; + bool m_actually_close; + + void init(void *file); +}; + +// ----------------------------------------------------------------------------- +// pstderr: write to stderr +// ----------------------------------------------------------------------------- + +class pstderr : public pofilestream +{ + P_PREVENT_COPYING(pstderr) +public: + pstderr(); +}; + +// ----------------------------------------------------------------------------- +// pstdout: write to stdout +// ----------------------------------------------------------------------------- + +class pstdout : public pofilestream +{ + P_PREVENT_COPYING(pstdout) +public: + pstdout(); }; // ----------------------------------------------------------------------------- @@ -276,6 +285,8 @@ public: void close(); protected: + pifilestream(void *file, const bool do_close); + /* read up to n bytes from stream */ virtual unsigned vread(void *buf, unsigned n); virtual void vseek(pos_type n); @@ -284,6 +295,21 @@ protected: private: void *m_file; pos_type m_pos; + bool m_actually_close; + + void init(void *file); +}; + +// ----------------------------------------------------------------------------- +// pstdin: reads from stdin +// ----------------------------------------------------------------------------- + +class pstdin : public pifilestream +{ + P_PREVENT_COPYING(pstdin) +public: + + pstdin(); }; // ----------------------------------------------------------------------------- diff --git a/src/lib/netlist/plib/pstring.c b/src/lib/netlist/plib/pstring.c index fb952f6fe91..682f819269b 100644 --- a/src/lib/netlist/plib/pstring.c +++ b/src/lib/netlist/plib/pstring.c @@ -376,12 +376,11 @@ pstr_t *pstring_t::salloc(int n) stk = palloc_array(pstack_t, 17); pstr_t *p; unsigned sn= ((32 - countleadbits(n)) + 1) / 2; - unsigned size = sizeof(pstr_t) + (1ULL<<(sn * 2)) + 1; + unsigned size = sizeof(pstr_t) + (1<<(sn * 2)) + 1; if (stk[sn].empty()) p = (pstr_t *) palloc_array(char, size); else { - //printf("%u %u\n", sn, (unsigned) stk[sn].count()); p = stk[sn].pop(); } @@ -439,25 +438,6 @@ void pstring_t::resetmem() // pstring ... // ---------------------------------------------------------------------------------------- -const pstring::type_t pstring::vprintf(va_list args) const -{ - // sprintf into the temporary buffer - char tempbuf[4096]; - std::vsprintf(tempbuf, cstr(), args); - - return type_t(tempbuf); -} - - -const pstring::type_t pstring::sprintf(const char *format, ...) -{ - va_list ap; - va_start(ap, format); - type_t ret = pstring(format).vprintf(ap); - va_end(ap); - return ret; -} - template int pstring_t::find(const pstring_t &search, unsigned start) const { @@ -616,90 +596,5 @@ void pstringbuffer::pcat(const pstring &s) m_ptr[m_len] = 0; } -pfmt::pfmt(const pstring &fmt) -: m_arg(0) -{ - memcpy(m_str, fmt.cstr(), fmt.blen() + 1); -} - -pfmt::pfmt(const char *fmt) -: m_arg(0) -{ - strncpy(m_str, fmt, sizeof(m_str) - 1); - m_str[sizeof(m_str) - 1] = 0; -} - -#if 0 -void pformat::format_element(const char *f, const char *l, const char *fmt_spec, ...) -{ - va_list ap; - va_start(ap, fmt_spec); - char fmt[30] = "%"; - char search[10] = ""; - char buf[1024]; - strcat(fmt, f); - strcat(fmt, l); - strcat(fmt, fmt_spec); - int nl = vsprintf(buf, fmt, ap); - m_arg++; - int sl = sprintf(search, "%%%d", m_arg); - char *p = strstr(m_str, search); - if (p != NULL) - { - // Make room - memmove(p+nl, p+sl, strlen(p) + 1 - sl); - memcpy(p, buf, nl); - } - va_end(ap); -} -#else -void pfmt::format_element(const char *f, const char *l, const char *fmt_spec, ...) -{ - va_list ap; - va_start(ap, fmt_spec); - char fmt[30] = "%"; - char search[10] = ""; - char buf[1024]; - m_arg++; - int sl = sprintf(search, "{%d:", m_arg); - char *p = strstr(m_str, search); - if (p == NULL) - { - sl = sprintf(search, "{%d}", m_arg); - p = strstr(m_str, search); - if (p == NULL) - { - sl = 2; - p = strstr(m_str, "{}"); - } - strcat(fmt, f); - } - else - { - char *p1 = strstr(p, "}"); - if (p1 != NULL) - { - sl = p1 - p + 1; - if (m_arg>=10) - strncat(fmt, p+4, p1 - p - 4); - else - strncat(fmt, p+3, p1 - p - 3); - } - else - strcat(fmt, f); - } - strcat(fmt, l); - strcat(fmt, fmt_spec); - int nl = vsprintf(buf, fmt, ap); - if (p != NULL) - { - // Make room - memmove(p+nl, p+sl, strlen(p) + 1 - sl); - memcpy(p, buf, nl); - } - va_end(ap); -} -#endif - template struct pstring_t; template struct pstring_t; diff --git a/src/lib/netlist/plib/pstring.h b/src/lib/netlist/plib/pstring.h index 973ca02ad0f..b101ffc47a5 100644 --- a/src/lib/netlist/plib/pstring.h +++ b/src/lib/netlist/plib/pstring.h @@ -307,9 +307,6 @@ public: pstring(const mem_t *string) : type_t(string) { } pstring(const type_t &string) : type_t(string) { } - const type_t vprintf(va_list args) const; - static const type_t sprintf(const char *format, ...) ATTR_PRINTF(1,2); - }; // ---------------------------------------------------------------------------------------- @@ -381,278 +378,4 @@ private: }; -template -struct ptype_treats -{ -}; - -template<> -struct ptype_treats -{ - static short cast(char x) { return x; } - static const bool is_signed = true; - static const char *size_specifier() { return "h"; } -}; - -template<> -struct ptype_treats -{ - static short cast(short x) { return x; } - static const bool is_signed = true; - static const char *size_specifier() { return "h"; } -}; - -template<> -struct ptype_treats -{ - static int cast(int x) { return x; } - static const bool is_signed = true; - static const char *size_specifier() { return ""; } -}; - -template<> -struct ptype_treats -{ - static long cast(long x) { return x; } - static const bool is_signed = true; - static const char *size_specifier() { return "l"; } -}; - -template<> -struct ptype_treats -{ - static long long cast(long long x) { return x; } - static const bool is_signed = true; - static const char *size_specifier() { return "ll"; } -}; - -template<> -struct ptype_treats -{ - static unsigned short cast(unsigned char x) { return x; } - static const bool is_signed = false; - static const char *size_specifier() { return "h"; } -}; - -template<> -struct ptype_treats -{ - static unsigned short cast(unsigned short x) { return x; } - static const bool is_signed = false; - static const char *size_specifier() { return "h"; } -}; - -template<> -struct ptype_treats -{ - static unsigned int cast(unsigned int x) { return x; } - static const bool is_signed = false; - static const char *size_specifier() { return ""; } -}; - -template<> -struct ptype_treats -{ - static unsigned long cast(unsigned long x) { return x; } - static const bool is_signed = false; - static const char *size_specifier() { return "l"; } -}; - -template<> -struct ptype_treats -{ - static unsigned long long cast(unsigned long long x) { return x; } - static const bool is_signed = false; - static const char *size_specifier() { return "ll"; } -}; - -template -class pformat_base -{ -public: - - virtual ~pformat_base() { } - - ATTR_COLD P &operator ()(const double x, const char *f = "") { format_element(f, "", "f", x); return static_cast

(*this); } - ATTR_COLD P & e(const double x, const char *f = "") { format_element(f, "", "e", x); return static_cast

(*this); } - ATTR_COLD P & g(const double x, const char *f = "") { format_element(f, "", "g", x); return static_cast

(*this); } - - ATTR_COLD P &operator ()(const char *x, const char *f = "") { format_element(f, "", "s", x); return static_cast

(*this); } - ATTR_COLD P &operator ()(char *x, const char *f = "") { format_element(f, "", "s", x); return static_cast

(*this); } - ATTR_COLD P &operator ()(const void *x, const char *f = "") { format_element(f, "", "p", x); return static_cast

(*this); } - ATTR_COLD P &operator ()(const pstring &x, const char *f = "") { format_element(f, "", "s", x.cstr() ); return static_cast

(*this); } - - template - ATTR_COLD P &operator ()(const T x, const char *f = "") - { - if (ptype_treats::is_signed) - format_element(f, ptype_treats::size_specifier(), "d", ptype_treats::cast(x)); - else - format_element(f, ptype_treats::size_specifier(), "u", ptype_treats::cast(x)); - return static_cast

(*this); - } - - template - ATTR_COLD P &x(const T x, const char *f = "") - { - format_element(f, ptype_treats::size_specifier(), "x", x); - return static_cast

(*this); - } - - template - ATTR_COLD P &o(const T x, const char *f = "") - { - format_element(f, ptype_treats::size_specifier(), "o", x); - return static_cast

(*this); - } - -protected: - - virtual void format_element(const char *f, const char *l, const char *fmt_spec, ...) = 0; - -}; - -class pfmt : public pformat_base -{ -public: - ATTR_COLD pfmt(const pstring &fmt); - ATTR_COLD pfmt(const char *fmt); - - operator pstring() const { return m_str; } - - const char *cstr() { return m_str; } - - -protected: - void format_element(const char *f, const char *l, const char *fmt_spec, ...); - -private: - - char m_str[2048]; - unsigned m_arg; -}; - -enum plog_level -{ - DEBUG, - INFO, - VERBOSE, - WARNING, - ERROR, - FATAL -}; - -class plog_dispatch_intf; - -template -class pfmt_writer_t -{ -public: - pfmt_writer_t() : m_enabled(true) { } - virtual ~pfmt_writer_t() { } - - ATTR_COLD void operator ()(const char *fmt) const - { - if (build_enabled && m_enabled) vdowrite(fmt); - } - - template - ATTR_COLD void operator ()(const char *fmt, const T1 &v1) const - { - if (build_enabled && m_enabled) vdowrite(pfmt(fmt)(v1)); - } - - template - ATTR_COLD void operator ()(const char *fmt, const T1 &v1, const T2 &v2) const - { - if (build_enabled && m_enabled) vdowrite(pfmt(fmt)(v1)(v2)); - } - - template - ATTR_COLD void operator ()(const char *fmt, const T1 &v1, const T2 &v2, const T3 &v3) const - { - if (build_enabled && m_enabled) vdowrite(pfmt(fmt)(v1)(v2)(v3)); - } - - template - ATTR_COLD void operator ()(const char *fmt, const T1 &v1, const T2 &v2, const T3 &v3, const T4 &v4) const - { - if (build_enabled && m_enabled) vdowrite(pfmt(fmt)(v1)(v2)(v3)(v4)); - } - - template - ATTR_COLD void operator ()(const char *fmt, const T1 &v1, const T2 &v2, const T3 &v3, const T4 &v4, const T5 &v5) const - { - if (build_enabled && m_enabled) vdowrite(pfmt(fmt)(v1)(v2)(v3)(v4)(v5)); - } - - void set_enabled(const bool v) - { - m_enabled = v; - } - - bool is_enabled() const { return m_enabled; } - -protected: - virtual void vdowrite(const pstring &ls) const {} - -private: - bool m_enabled; - -}; - -template -class plog_channel : public pfmt_writer_t -{ -public: - plog_channel(plog_dispatch_intf *b) : pfmt_writer_t(), m_base(b) { } - virtual ~plog_channel() { } - -protected: - virtual void vdowrite(const pstring &ls) const; - -private: - plog_dispatch_intf *m_base; -}; - -class plog_dispatch_intf -{ - template friend class plog_channel; - -public: - virtual ~plog_dispatch_intf() { } -protected: - virtual void vlog(const plog_level &l, const pstring &ls) const = 0; -}; - -template -class plog_base -{ -public: - - plog_base(plog_dispatch_intf *proxy) - : debug(proxy), - info(proxy), - verbose(proxy), - warning(proxy), - error(proxy), - fatal(proxy) - {} - virtual ~plog_base() {}; - - plog_channel debug; - plog_channel info; - plog_channel verbose; - plog_channel warning; - plog_channel error; - plog_channel fatal; -}; - - -template -void plog_channel::vdowrite(const pstring &ls) const -{ - m_base->vlog(L, ls); -} - #endif /* _PSTRING_H_ */ diff --git a/src/lib/netlist/plib/ptypes.h b/src/lib/netlist/plib/ptypes.h new file mode 100644 index 00000000000..5ccfe171ed4 --- /dev/null +++ b/src/lib/netlist/plib/ptypes.h @@ -0,0 +1,97 @@ +// license:GPL-2.0+ +// copyright-holders:Couriersud +/* + * ptypes.h + * + */ + +#ifndef PTYPES_H_ +#define PTYPES_H_ + +#include "pconfig.h" +#include "pstring.h" + +//============================================================ +// penum - strongly typed enumeration +//============================================================ + +struct penum_base +{ +protected: + static int from_string_int(const char *str, const char *x) + { + int cnt = 0; + const char *cur = str; + int lx = strlen(x); + while (*str) + { + if (*str == ',') + { + int l = str-cur; + if (l == lx) + if (strncmp(cur, x, lx) == 0) + return cnt; + } + else if (*str == ' ') + { + cur = str + 1; + cnt++; + } + str++; + } + int l = str-cur; + if (l == lx) + if (strncmp(cur, x, lx) == 0) + return cnt; + return -1; + } + static pstring nthstr(int n, const char *str) + { + char buf[64]; + char *bufp = buf; + int cur = 0; + while (*str) + { + if (cur == n) + { + if (*str == ',') + { + *bufp = 0; + return pstring(buf); + } + else if (*str != ' ') + *bufp++ = *str; + } + else + { + if (*str == ',') + cur++; + } + str++; + } + *bufp = 0; + return pstring(buf); + } +}; + +#define P_ENUM(_name, ...) \ + struct _name : public penum_base { \ + enum e { __VA_ARGS__ }; \ + _name (e v) : m_v(v) { } \ + bool set_from_string (const pstring &s) { \ + static const char *strings = # __VA_ARGS__; \ + int f = from_string_int(strings, s.cstr()); \ + if (f>=0) { m_v = (e) f; return true; } else { return false; } \ + } \ + operator e() const {return m_v;} \ + int as_int() const {return (int) m_v;} \ + bool operator==(const _name &rhs) const {return m_v == rhs.m_v;} \ + bool operator==(const e &rhs) const {return m_v == (int) rhs;} \ + const pstring name() const { \ + static const char *strings = # __VA_ARGS__; \ + return nthstr((int) m_v, strings); \ + } \ + private: e m_v; }; + + +#endif /* PTYPES_H_ */ diff --git a/src/lib/netlist/prg/nltool.c b/src/lib/netlist/prg/nltool.c index 1951ec7468f..3510f70e7f2 100644 --- a/src/lib/netlist/prg/nltool.c +++ b/src/lib/netlist/prg/nltool.c @@ -19,6 +19,7 @@ #include "plib/poptions.h" #include "plib/pstring.h" #include "plib/plists.h" +#include "plib/ptypes.h" #include "nl_setup.h" #include "nl_factory.h" #include "nl_parser.h" @@ -122,8 +123,11 @@ public: poption_bool opt_help; }; -//Alternative -//static poption *optlist[] = { &opt_ttr, &opt_logs, &opt_file, &opt_cmd, &opt_verb, &opt_help, NULL }; +pstdout pout_strm; +pstderr perr_strm; + +pstream_fmt_writer_t pout(pout_strm); +pstream_fmt_writer_t perr(perr_strm); NETLIST_START(dummy) /* Standard stuff */ @@ -137,39 +141,6 @@ NETLIST_END() CORE IMPLEMENTATION ***************************************************************************/ -pstring filetobuf(pstring fname) -{ - pstring pbuf = ""; - - if (fname == "-") - { - char lbuf[1024]; - while (!feof(stdin)) - { - fgets(lbuf, 1024, stdin); - pbuf += lbuf; - } - printf("%d\n",*(pbuf.right(1).cstr()+1)); - return pbuf; - } - else - { - FILE *f; - f = fopen(fname.cstr(), "rb"); - fseek(f, 0, SEEK_END); - long fsize = ftell(f); - fseek(f, 0, SEEK_SET); - - char *buf = (char *) malloc(fsize + 1); - fread(buf, fsize, 1, f); - buf[fsize] = 0; - fclose(f); - pbuf = buf; - free(buf); - return pbuf; - } -} - class netlist_tool_t : public netlist::netlist_t { public: @@ -225,30 +196,9 @@ protected: void vlog(const plog_level &l, const pstring &ls) const { - switch (l) - { - case DEBUG: - case INFO: - break; - case VERBOSE: - if (m_opts ? m_opts->opt_verb() : false) - { - printf("%s\n", ls.cstr()); - } - break; - case WARNING: - if (!(m_opts ? m_opts->opt_quiet() : false)) - { - printf("%s\n", ls.cstr()); - } - break; - case ERROR: - printf("%s\n", ls.cstr()); - break; - case FATAL: - printf("%s\n", ls.cstr()); - throw; - } + pout("{}: {}\n", l.name().cstr(), ls.cstr()); + if (l == plog_level::FATAL) + throw; } private: @@ -258,14 +208,14 @@ private: void usage(tool_options_t &opts) { - fprintf(stderr, + perr("{}", "Usage:\n" " nltool -help\n" " nltool [options]\n" "\n" "Where:\n" ); - fprintf(stderr, "%s\n", opts.help().cstr()); + perr("{}\n", opts.help().cstr()); } struct input_t @@ -336,14 +286,20 @@ static void run(tool_options_t &opts) nt.m_opts = &opts; nt.init(); + + if (!opts.opt_verb()) + nt.log().verbose.set_enabled(false); + if (opts.opt_quiet()) + nt.log().warning.set_enabled(false); + nt.read_netlist(opts.opt_file(), opts.opt_name()); plist_t *inps = read_input(&nt, opts.opt_inp()); double ttr = opts.opt_ttr(); - printf("startup time ==> %5.3f\n", (double) (osd_ticks() - t) / (double) osd_ticks_per_second() ); - printf("runnning ...\n"); + pout("startup time ==> {1:5.3f}\n", (double) (osd_ticks() - t) / (double) osd_ticks_per_second() ); + pout("runnning ...\n"); t = osd_ticks(); unsigned pos = 0; @@ -361,7 +317,7 @@ static void run(tool_options_t &opts) pfree(inps); double emutime = (double) (osd_ticks() - t) / (double) osd_ticks_per_second(); - printf("%f seconds emulation took %f real time ==> %5.2f%%\n", ttr, emutime, ttr/emutime*100.0); + pout("{1:f} seconds emulation took {2:f} real time ==> {3:5.2f}%\n", ttr, emutime, ttr/emutime*100.0); } /*------------------------------------------------- @@ -446,11 +402,11 @@ int main(int argc, char *argv[]) tool_options_t opts; int ret; - fprintf(stderr, "%s", "WARNING: This is Work In Progress! - It may fail anytime\n"); - fprintf(stderr, "Update dispatching using method %s\n", pmf_verbose[NL_PMF_TYPE]); + perr("{}", "WARNING: This is Work In Progress! - It may fail anytime\n"); + perr("Update dispatching using method {}\n", pmf_verbose[NL_PMF_TYPE]); if ((ret = opts.parse(argc, argv)) != argc) { - fprintf(stderr, "Error parsing %s\n", argv[ret]); + perr("Error parsing {}\n", argv[ret]); usage(opts); return 1; } @@ -468,23 +424,44 @@ int main(int argc, char *argv[]) run(opts); else if (cmd == "convert") { - pstring contents = filetobuf(opts.opt_file()); - nl_convert_base_t *converter = NULL; + pstring contents; + postringstream ostrm; + if (opts.opt_file() == "-") + { + pstdin f; + ostrm.write(f); + } + else + { + pifilestream f(opts.opt_file()); + ostrm.write(f); + } + contents = ostrm.str(); + + pstring result; if (opts.opt_type().equals("spice")) - converter = palloc(nl_convert_spice_t); + { + nl_convert_spice_t c; + c.convert(contents); + result = c.result(); + } else - converter = palloc(nl_convert_eagle_t); - converter->convert(contents); + { + nl_convert_eagle_t c; + c.convert(contents); + result = c.result(); + } /* present result */ - printf("%s\n", converter->result().cstr()); - pfree(converter); + pout_strm.write(result.cstr()); } else { - fprintf(stderr, "Unknown command %s\n", cmd.cstr()); + perr("Unknown command {}\n", cmd.cstr()); usage(opts); return 1; } + + pstring::resetmem(); #if (!PSTANDALONE) } dump_unfreed_mem(); diff --git a/src/lib/netlist/solver/nld_ms_direct_lu.h b/src/lib/netlist/solver/nld_ms_direct_lu.h index 5d7587c0ef1..d988942058e 100644 --- a/src/lib/netlist/solver/nld_ms_direct_lu.h +++ b/src/lib/netlist/solver/nld_ms_direct_lu.h @@ -237,7 +237,7 @@ template ATTR_COLD void matrix_solver_direct_t::vsetup(analog_net_t::list_t &nets) { if (m_dim < nets.size()) - netlist().error("Dimension {1} less than {2}", m_dim, SIZET_PRINTF(nets.size())); + netlist().error("Dimension {1} less than {2}", m_dim,nets.size()); for (unsigned k = 0; k < N(); k++) { diff --git a/src/lib/netlist/solver/nld_solver.h b/src/lib/netlist/solver/nld_solver.h index 608b744372a..040866c5736 100644 --- a/src/lib/netlist/solver/nld_solver.h +++ b/src/lib/netlist/solver/nld_solver.h @@ -14,9 +14,6 @@ //#define ATTR_ALIGNED(N) __attribute__((aligned(N))) #define ATTR_ALIGNED(N) ATTR_ALIGN -#define SOLVER_VERBOSE_OUT(x) do {} while (0) -//#define SOLVER_VERBOSE_OUT(x) printf x - // ---------------------------------------------------------------------------------------- // Macros // ---------------------------------------------------------------------------------------- -- cgit v1.2.3-70-g09d2 From 804cd541ec0f84c39d09588a44fcdd738efc401e Mon Sep 17 00:00:00 2001 From: Peter Ferrie Date: Mon, 28 Sep 2015 20:20:15 -0700 Subject: pstream: fix the compile for MSVC (nw) --- src/lib/netlist/plib/pstream.h | 8 ++++---- src/lib/netlist/plib/pstring.c | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) (limited to 'src/lib/netlist/plib/pstring.c') diff --git a/src/lib/netlist/plib/pstream.h b/src/lib/netlist/plib/pstream.h index 32e7558fabe..61f0959cadd 100644 --- a/src/lib/netlist/plib/pstream.h +++ b/src/lib/netlist/plib/pstream.h @@ -288,8 +288,8 @@ protected: pifilestream(void *file, const bool do_close); /* read up to n bytes from stream */ - virtual unsigned vread(void *buf, unsigned n); - virtual void vseek(pos_type n); + virtual unsigned vread(void *buf, const unsigned n); + virtual void vseek(const pos_type n); virtual pos_type vtell(); private: @@ -327,8 +327,8 @@ public: protected: /* read up to n bytes from stream */ - virtual unsigned vread(void *buf, unsigned n); - virtual void vseek(pos_type n); + virtual unsigned vread(void *buf, const unsigned n); + virtual void vseek(const pos_type n); virtual pos_type vtell(); private: diff --git a/src/lib/netlist/plib/pstring.c b/src/lib/netlist/plib/pstring.c index 682f819269b..8ad86f41f3d 100644 --- a/src/lib/netlist/plib/pstring.c +++ b/src/lib/netlist/plib/pstring.c @@ -376,7 +376,7 @@ pstr_t *pstring_t::salloc(int n) stk = palloc_array(pstack_t, 17); pstr_t *p; unsigned sn= ((32 - countleadbits(n)) + 1) / 2; - unsigned size = sizeof(pstr_t) + (1<<(sn * 2)) + 1; + unsigned size = sizeof(pstr_t) + ((UINT64) 1<<(sn * 2)) + 1; if (stk[sn].empty()) p = (pstr_t *) palloc_array(char, size); else -- cgit v1.2.3-70-g09d2