summaryrefslogtreecommitdiffstatshomepage
path: root/scripts/src/lib.lua
blob: 28fb3b7c9a6864776904dcdec2da2ffd3fb60ac3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
-- license:BSD-3-Clause
-- copyright-holders:MAMEdev Team

---------------------------------------------------------------------------
--
--   lib.lua
--
--   Rules for building lib cores
--
---------------------------------------------------------------------------

project "utils"
	uuid "22489ad0-4cb2-4d91-ad81-24b0d80ca30a"
	kind (LIBTYPE)

	addprojectflags()

	includedirs {
		MAME_DIR .. "src/osd",
		MAME_DIR .. "src/lib/util",
		MAME_DIR .. "3rdparty",
		ext_includedir("expat"),
		ext_includedir("zlib"),
		ext_includedir("flac"),
		ext_includedir("utf8proc"),
	}

	files {
		MAME_DIR .. "src/lib/util/bitstream.h",
		MAME_DIR .. "src/lib/util/coretmpl.h",
		MAME_DIR .. "src/lib/util/avhuff.cpp",
		MAME_DIR .. "src/lib/util/avhuff.h",
		MAME_DIR .. "src/lib/util/aviio.cpp",
		MAME_DIR .. "src/lib/util/aviio.h",
		MAME_DIR .. "src/lib/util/base64.hpp",
		MAME_DIR .. "src/lib/util/bitmap.cpp",
		MAME_DIR .. "src/lib/util/bitmap.h",
		MAME_DIR .. "src/lib/util/cdrom.cpp",
		MAME_DIR .. "src/lib/util/cdrom.h",
		MAME_DIR .. "src/lib/util/chd.cpp",
		MAME_DIR .. "src/lib/util/chd.h",
		MAME_DIR .. "src/lib/util/chdcd.cpp",
		MAME_DIR .. "src/lib/util/chdcd.h",
		MAME_DIR .. "src/lib/util/chdcodec.cpp",
		MAME_DIR .. "src/lib/util/chdcodec.h",
		MAME_DIR .. "src/lib/util/client_http.hpp",
		MAME_DIR .. "src/lib/util/client_https.hpp",
		MAME_DIR .. "src/lib/util/client_ws.hpp",
		MAME_DIR .. "src/lib/util/client_wss.hpp",
		MAME_DIR .. "src/lib/util/corealloc.h",
		MAME_DIR .. "src/lib/util/corefile.cpp",
		MAME_DIR .. "src/lib/util/corefile.h",
		MAME_DIR .. "src/lib/util/corestr.cpp",
		MAME_DIR .. "src/lib/util/corestr.h",
		MAME_DIR .. "src/lib/util/coreutil.cpp",
		MAME_DIR .. "src/lib/util/coreutil.h",
		MAME_DIR .. "src/lib/util/crypto.hpp",
		MAME_DIR .. "src/lib/util/delegate.cpp",
		MAME_DIR .. "src/lib/util/delegate.h",
		MAME_DIR .. "src/lib/util/disasmintf.cpp",
		MAME_DIR .. "src/lib/util/disasmintf.h",
		MAME_DIR .. "src/lib/util/flac.cpp",
		MAME_DIR .. "src/lib/util/flac.h",
		MAME_DIR .. "src/lib/util/harddisk.cpp",
		MAME_DIR .. "src/lib/util/harddisk.h",
		MAME_DIR .. "src/lib/util/hash.cpp",
		MAME_DIR .. "src/lib/util/hash.h",
		MAME_DIR .. "src/lib/util/hashing.cpp",
		MAME_DIR .. "src/lib/util/hashing.h",
		MAME_DIR .. "src/lib/util/huffman.cpp",
		MAME_DIR .. "src/lib/util/huffman.h",
		MAME_DIR .. "src/lib/util/jedparse.cpp",
		MAME_DIR .. "src/lib/util/jedparse.h",
		MAME_DIR .. "src/lib/util/md5.cpp",
		MAME_DIR .. "src/lib/util/md5.h",
		MAME_DIR .. "src/lib/util/nanosvg.cpp",
		MAME_DIR .. "src/lib/util/opresolv.cpp",
		MAME_DIR .. "src/lib/util/opresolv.h",
		MAME_DIR .. "src/lib/util/options.cpp",
		MAME_DIR .. "src/lib/util/options.h",
		MAME_DIR .. "src/lib/util/palette.cpp",
		MAME_DIR .. "src/lib/util/palette.h",
		MAME_DIR .. "src/lib/util/path_to_regex.cpp",
		MAME_DIR .. "src/lib/util/path_to_regex.hpp",
		MAME_DIR .. "src/lib/util/plaparse.cpp",
		MAME_DIR .. "src/lib/util/plaparse.h",
		MAME_DIR .. "src/lib/util/png.cpp",
		MAME_DIR .. "src/lib/util/png.h",
		MAME_DIR .. "src/lib/util/pool.cpp",
		MAME_DIR .. "src/lib/util/pool.h",
		MAME_DIR .. "src/lib/util/server_http.hpp",
		MAME_DIR .. "src/lib/util/server_https.hpp",
		MAME_DIR .. "src/lib/util/server_ws.hpp",
		MAME_DIR .. "src/lib/util/server_wss.hpp",
		MAME_DIR .. "src/lib/util/sha1.cpp",
		MAME_DIR .. "src/lib/util/sha1.h",
		MAME_DIR .. "src/lib/util/sha1.hpp",
		MAME_DIR .. "src/lib/util/strformat.cpp",
		MAME_DIR .. "src/lib/util/strformat.h",
		MAME_DIR .. "src/lib/util/timeconv.cpp",
		MAME_DIR .. "src/lib/util/timeconv.h",
		MAME_DIR .. "src/lib/util/unicode.cpp",
		MAME_DIR .. "src/lib/util/unicode.h",
		MAME_DIR .. "src/lib/util/unzip.cpp",
		MAME_DIR .. "src/lib/util/unzip.h",
		MAME_DIR .. "src/lib/util/un7z.cpp",
		MAME_DIR .. "src/lib/util/vbiparse.cpp",
		MAME_DIR .. "src/lib/util/vbiparse.h",
		MAME_DIR .. "src/lib/util/vecstream.cpp",
		MAME_DIR .. "src/lib/util/vecstream.h",
		MAME_DIR .. "src/lib/util/wavwrite.cpp",
		MAME_DIR .. "src/lib/util/wavwrite.h",
		MAME_DIR .. "src/lib/util/xmlfile.cpp",
		MAME_DIR .. "src/lib/util/xmlfile.h",
		MAME_DIR .. "src/lib/util/zippath.cpp",
		MAME_DIR .. "src/lib/util/zippath.h",
	}
span>m_zero(0); template<typename F> pstring_t<F>::~pstring_t() { if (m_ptr != nullptr) sfree(m_ptr); } template<typename F> void pstring_t<F>::pcat(const mem_t *s) { std::size_t 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<typename F> void pstring_t<F>::pcat(const pstring_t &s) { std::size_t 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.c_str(), slen); *(n->str() + n->len()) = 0; sfree(m_ptr); m_ptr = n; } template<typename F> int pstring_t<F>::pcmp(const pstring_t &right) const { std::size_t 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.c_str(), l); if (ret == 0) { if (this->blen() > right.blen()) ret = 1; else if (this->blen() < right.blen()) ret = -1; } return ret; } template<typename F> void pstring_t<F>::pcopy(const mem_t *from, std::size_t 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<typename F> const pstring_t<F> pstring_t<F>::substr(const iterator start, const iterator end) const { pstring_t ret; //FIXME: throw ? ret.pcopy(start.p, static_cast<std::size_t>(end.p - start.p)); return ret; } template<typename F> const pstring_t<F> pstring_t<F>::ucase() const { pstring_t ret = ""; for (auto c : *this) if (c >= 'a' && c <= 'z') ret += (c - 'a' + 'A'); else ret += c; return ret; } template<typename F> typename pstring_t<F>::iterator pstring_t<F>::find_first_not_of(const pstring_t &no) const { for (auto it = begin(); it != end(); ++it) { bool f = true; for (auto const jt : no) { if (*it == jt) { f = false; break; } } if (f) return it; } return end(); } template<typename F> typename pstring_t<F>::iterator pstring_t<F>::find_last_not_of(const pstring_t &no) const { /* FIXME: reverse iterator */ iterator last_found = end(); for (auto it = begin(); it != end(); ++it) { bool f = true; for (auto const jt : no) { if (*it == jt) { f = false; break; } } if (f) last_found = it; } return last_found; } template<typename F> typename pstring_t<F>::iterator pstring_t<F>::find(const pstring_t search, iterator start) const { for (; start != end(); ++start) { iterator itc(start); auto cmp = search.begin(); while (itc != end() && cmp != search.end() && *itc == *cmp) { ++itc; ++cmp; } if (cmp == search.end()) return start; } return end(); } template<typename F> typename pstring_t<F>::iterator pstring_t<F>::find(const code_t search, iterator start) const { mem_t buf[traits::MAXCODELEN+1] = { 0 }; traits::encode(search, buf); return find(pstring_t(&buf[0], UTF8), start); } template<typename F> pstring_t<F> pstring_t<F>::replace(const pstring_t &search, const pstring_t &replace) const { pstring_t ret(""); const size_type slen = search.len(); auto last_s = begin(); auto s = find(search, last_s); while (s != end()) { ret += substr(last_s, s); ret += replace; last_s = s + slen; s = find(search, last_s); } ret += substr(last_s, end()); return ret; } template<typename F> const pstring_t<F> pstring_t<F>::ltrim(const pstring_t ws) const { return substr(find_first_not_of(ws), end()); } template<typename F> const pstring_t<F> pstring_t<F>::rtrim(const pstring_t ws) const { auto f = find_last_not_of(ws); if (f==end()) return pstring_t(""); else return substr(begin(), f + 1); } template<typename F> const pstring_t<F> pstring_t<F>::rpad(const pstring_t &ws, const size_type cnt) const { // FIXME: pstringbuffer ret(*this); pstring_t ret(*this); size_type wsl = ws.len(); for (auto i = ret.len(); i < cnt; i+=wsl) ret += ws; return ret; } template<typename F> void pstring_t<F>::pcopy(const mem_t *from) { pcopy(from, strlen(from)); } template<typename F> double pstring_t<F>::as_double(bool *error) const { double ret; char *e = nullptr; if (error != nullptr) *error = false; ret = std::strtod(c_str(), &e); if (*e != 0) if (error != nullptr) *error = true; return ret; } template<typename F> long pstring_t<F>::as_long(bool *error) const { long ret; char *e = nullptr; if (error != nullptr) *error = false; if (startsWith("0x")) ret = std::strtol(substr(2).c_str(), &e, 16); else ret = std::strtol(c_str(), &e, 10); if (*e != 0) if (error != nullptr) *error = true; return ret; } template<typename F> bool pstring_t<F>::startsWith(const pstring_t &arg) const { if (arg.blen() > blen()) return false; else return (memcmp(arg.c_str(), c_str(), arg.blen()) == 0); } template<typename F> bool pstring_t<F>::endsWith(const pstring_t &arg) const { if (arg.blen() > blen()) return false; else return (memcmp(c_str()+this->blen()-arg.blen(), arg.c_str(), arg.blen()) == 0); } template<typename F> int pstring_t<F>::pcmp(const mem_t *right) const { return std::strcmp(m_ptr->str(), right); } // ---------------------------------------------------------------------------------------- // pstringbuffer // ---------------------------------------------------------------------------------------- pstringbuffer::~pstringbuffer() { if (m_ptr != nullptr) plib::pfree_array(m_ptr); } void pstringbuffer::resize(const std::size_t size) { if (m_ptr == nullptr) { m_size = DEFAULT_SIZE; while (m_size <= size) m_size *= 2; m_ptr = plib::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 = plib::palloc_array<char>(m_size); std::memcpy(new_buf, m_ptr, m_len + 1); plib::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.c_str(), 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, std::size_t 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.c_str(), slen); m_len += slen; m_ptr[m_len] = 0; } // ---------------------------------------------------------------------------------------- // static stuff ... // ---------------------------------------------------------------------------------------- /* * Cached allocation of string memory * * This improves startup performance by 30%. */ #if 1 static std::stack<pstr_t *> *stk = nullptr; static inline std::size_t countleadbits(std::size_t x) { #ifndef count_leading_zeros std::size_t msk; std::size_t 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<typename F> void pstring_t<F>::sfree(pstr_t *s) { bool b = s->dec_and_check(); if ( b && s != &m_zero) { if (stk != nullptr) { size_type sn= ((32 - countleadbits(s->len())) + 1) / 2; stk[sn].push(s); } else plib::pfree_array(reinterpret_cast<char *>(s)); } } template<typename F> pstr_t *pstring_t<F>::salloc(std::size_t n) { if (stk == nullptr) stk = plib::palloc_array<std::stack<pstr_t *>>(17); pstr_t *p; std::size_t sn= ((32 - countleadbits(n)) + 1) / 2; std::size_t size = sizeof(pstr_t) + (static_cast<std::size_t>(1)<<(sn * 2)) + 1; if (stk[sn].empty()) p = reinterpret_cast<pstr_t *>(plib::palloc_array<char>(size)); else { p = stk[sn].top(); stk[sn].pop(); } // str_t *p = (str_t *) mm_malloc(size, 8); p->init(n); return p; } template<typename F> void pstring_t<F>::resetmem() { if (stk != nullptr) { for (std::size_t i=0; i<=16; i++) { for (; stk[i].size() > 0; ) { plib::pfree_array(stk[i].top()); stk[i].pop(); } } plib::pfree_array(stk); stk = nullptr; } } #else template<typename F> void pstring_t<F>::sfree(pstr_t *s) { bool b = s->dec_and_check(); if ( b && s != &m_zero) { plib::pfree_array(((char *)s)); } } template<typename F> pstr_t *pstring_t<F>::salloc(std::size_t n) { int size = sizeof(pstr_t) + n + 1; pstr_t *p = (pstr_t *) plib::palloc_array<char>(size); // str_t *p = (str_t *) mm_malloc(size, 8); p->init(n); return p; } template<typename F> void pstring_t<F>::resetmem() { // Release the 0 string } #endif // ---------------------------------------------------------------------------------------- // template stuff ... // ---------------------------------------------------------------------------------------- template struct pstring_t<pu8_traits>; template struct pstring_t<putf8_traits>; const unsigned pu8_traits::MAXCODELEN; const unsigned putf8_traits::MAXCODELEN; const int pstringbuffer::DEFAULT_SIZE;