From 58aa97913fa966b03d3b5bed77c6670e49821f1e Mon Sep 17 00:00:00 2001 From: couriersud Date: Fri, 31 Mar 2017 18:07:35 +0200 Subject: pstring, pdynlib, pfmtlog refactoring : pstring: - added support for UTF16LE to pstring. - renamed size() to mem_t_size() - renmaed len() to length() - added size() == length() - added empty() - added simple compare() pfmtlog: - Simplified pfmtlog, added more c++ pdynlib: - add a dynproc type to dynlib to wrap dynamic library calls. various: - fix two coverty scan issue. - various clang warnings fixed. (nw) --- src/lib/netlist/plib/pfmtlog.cpp | 128 ++++++++++++++------------------------- 1 file changed, 44 insertions(+), 84 deletions(-) mode change 100644 => 100755 src/lib/netlist/plib/pfmtlog.cpp (limited to 'src/lib/netlist/plib/pfmtlog.cpp') diff --git a/src/lib/netlist/plib/pfmtlog.cpp b/src/lib/netlist/plib/pfmtlog.cpp old mode 100644 new mode 100755 index 86d059d9247..ac4f0f6f046 --- a/src/lib/netlist/plib/pfmtlog.cpp +++ b/src/lib/netlist/plib/pfmtlog.cpp @@ -12,8 +12,8 @@ #include #include #include - -//* FIXME: remove cstring, replace with pstring */ +#include +#include namespace plib { @@ -21,115 +21,75 @@ plog_dispatch_intf::~plog_dispatch_intf() { } -pfmt::pfmt(const pstring &fmt) -: m_str(m_str_buf), m_allocated(0), m_arg(0) -{ - std::size_t l = fmt.size() + 1; - if (l>sizeof(m_str_buf)) - { - m_allocated = 2 * l; - m_str = palloc_array(2 * l); - } - std::copy(fmt.c_str(), fmt.c_str() + l, m_str); -} -pfmt::~pfmt() -{ - if (m_allocated > 0) - pfree_array(m_str); -} - -void pfmt::format_element(const char *f, const char *l, const char *fmt_spec, ...) +pfmt &pfmt::format_element(const char *l, const unsigned cfmt_spec, ...) { va_list ap; - va_start(ap, fmt_spec); - char fmt[30] = "%"; - char search[10] = ""; - char buf[2048]; + va_start(ap, cfmt_spec); + pstring fmt("%"); + char buf[2048]; // FIXME + std::size_t sl; + m_arg++; - std::size_t sl = static_cast(sprintf(search, "{%d:", m_arg)); - char *p = strstr(m_str, search); - if (p == nullptr) + + pstring search("{"); + search += plib::to_string(m_arg); + sl = search.length(); + + auto p = m_str.find(search + ":"); + sl++; // ":" + if (p == pstring::npos) // no further specifiers { - sl = static_cast(sprintf(search, "{%d}", m_arg)); - p = strstr(m_str, search); - if (p == nullptr) + p = m_str.find(search + "}"); + if (p == pstring::npos) // not found try default { sl = 2; - p = strstr(m_str, "{}"); + p = m_str.find("{}"); } - if (p==nullptr) + if (p == pstring::npos) { sl=1; - p = strstr(m_str, "{"); - if (p != nullptr) + p = m_str.find("{"); + if (p != pstring:: npos) { - char *p1 = strstr(p, "}"); - if (p1 != nullptr) + auto p1 = m_str.find("}", p); + if (p1 != pstring::npos) { - sl = static_cast(p1 - p + 1); - strncat(fmt, p+1, static_cast(p1 - p - 2)); + sl = p1 - p + 1; + fmt += m_str.substr(p+1, p1 - p - 2); } - else - strcat(fmt, f); } - else - strcat(fmt, f); } } else { - char *p1 = strstr(p, "}"); - if (p1 != nullptr) + auto p1 = m_str.find("}", p); + if (p1 != pstring::npos) { - sl = static_cast(p1 - p + 1); - if (m_arg>=10) - strncat(fmt, p+4, static_cast(p1 - p - 4)); - else - strncat(fmt, p+3, static_cast(p1 - p - 3)); + sl = p1 - p + 1; + fmt += ((m_arg>=10) ? m_str.substr(p+4, p1 - p - 4) : m_str.substr(p+3, p1 - p - 3)); } - else - strcat(fmt, f); } - strcat(fmt, l); - char *pend = fmt + strlen(fmt) - 1; - if (strchr("fge", *fmt_spec) != nullptr) + pstring::code_t pend = fmt.at(fmt.length() - 1); + if (pstring("duxo").find(cfmt_spec) != pstring::npos) { - if (strchr("fge", *pend) == nullptr) - strcat(fmt, fmt_spec); + if (pstring("duxo").find(pend) == pstring::npos) + fmt += (pstring(l, pstring::UTF8) + cfmt_spec); + else + fmt = fmt.left(fmt.length() - 1) + pstring(l, pstring::UTF8) + fmt.right(1); } - else if (strchr("duxo", *fmt_spec) != nullptr) + else if (pstring("fge").find(cfmt_spec) != pstring::npos) { - if (strchr("duxo", *pend) == nullptr) - strcat(fmt, fmt_spec); + if (pstring("fge").find(pend) == pstring::npos) + fmt += cfmt_spec; } else - strcat(fmt, fmt_spec); - std::size_t nl = static_cast(vsprintf(buf, fmt, ap)); - if (p != nullptr) - { - // check room - std::size_t new_size = static_cast(p - m_str) + nl + strlen(p) + 1 - sl; - if (new_size > m_allocated) - { - std::size_t old_alloc = std::max(m_allocated, 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(m_allocated); - std::copy(m_str, m_str + old_alloc, np); - 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); - std::copy_backward(p + sl, p + strlen(p) + 1, p + nl + strlen(p) + 1 - sl); - std::copy(buf, buf + nl, p); - } + fmt += cfmt_spec; + vsprintf(buf, fmt.c_str(), ap); + if (p != pstring::npos) + m_str = m_str.substr(0, p) + pstring(buf, pstring::UTF8) + m_str.substr(p + sl); va_end(ap); + return *this; } } -- cgit v1.2.3-70-g09d2