summaryrefslogtreecommitdiffstatshomepage
path: root/src/emu/netlist/plib/pstring.c
blob: ec930808e7b4db2cf1846d6af56a99bbc5139b62 (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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
// license:GPL-2.0+
// copyright-holders:Couriersud
/*
 * nl_string.c
 *
 */

#include <new>
#include <cstdio>
#include <cstring>
//FIXME:: pstring should be locale free
#include <cctype>
#include <cstdlib>

#include "pstring.h"
#include "palloc.h"

// The following will work on linux, however not on Windows ....

//pblockpool *pstring::m_pool = new pblockpool;
//pstring::str_t *pstring::m_zero = new(pstring::m_pool, 0) pstring::str_t(0);

pstring::str_t pstring::m_zero = str_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

pstring::~pstring()
{
	sfree(m_ptr);
}

void pstring::pcat(const char *s)
{
	int slen = strlen(s);
	str_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;
}

void pstring::pcopy(const char *from, int size)
{
	str_t *n = salloc(size);
	if (size > 0)
		std::memcpy(n->str(), from, size);
	*(n->str() + size) = 0;
	sfree(m_ptr);
	m_ptr = n;
}

pstring pstring::substr(unsigned int start, int count) const
{
	pstring ret;
	unsigned alen = len();
	if (start >= alen)
		return ret;
	if (count <0 || start + count > alen)
		count = alen - start;
	ret.pcopy(cstr() + start, count);
	return ret;
}

pstring pstring::ucase() const
{
	pstring ret = *this;
	ret.pcopy(cstr(), len());
	for (int i=0; i<ret.len(); i++)
		ret.m_ptr->str()[i] = toupper((unsigned) ret.m_ptr->str()[i]);
	return ret;
}

int pstring::find_first_not_of(const pstring no) const
{
	for (int i=0; i < len(); i++)
	{
		bool f = true;
		for (int j=0; j < no.len(); j++)
			if (m_ptr->str()[i] == no.m_ptr->str()[j])
				f = false;
		if (f)
			return i;
	}
	return -1;
}

int pstring::find_last_not_of(const pstring no) const
{
	for (int i=len() - 1; i >= 0; i--)
	{
		bool f = true;
		for (int j=0; j < no.len(); j++)
			if (m_ptr->str()[i] == no.m_ptr->str()[j])
				f = false;
		if (f)
			return i;
	}
	return -1;
}

pstring pstring::replace(const pstring &search, const pstring &replace) const
{
	pstring ret = "";

	if (search.len() == 0)
		return *this;
	int i = 0;
	while (i<len())
	{
		if (strncmp(cstr()+i,search.cstr(),search.len()) == 0)
		{
			ret += replace;
			i += search.len();
		}
		else
		{
			ret += *(cstr() + i);
			i++;
		}
	}
	return ret;
}
pstring pstring::ltrim(const pstring ws) const
{
	int f = find_first_not_of(ws);
	if (f>=0)
		return substr(f);
	else
		return "";
}

pstring pstring::rtrim(const pstring ws) const
{
	int f = find_last_not_of(ws);
	if (f>=0)
		return left(f+1);
	else
		return "";
}

void pstring::pcopy(const char *from)
{
	pcopy(from, strlen(from));
}

//-------------------------------------------------
//  pcmpi - compare a character array to an nstring
//-------------------------------------------------

int pstring::pcmpi(const char *lhs, const char *rhs, int count) const
{
	// loop while equal until we hit the end of strings
	int index;
	for (index = 0; index < count; index++)
		if (lhs[index] == 0 || std::tolower(lhs[index]) != std::tolower(rhs[index]))
			break;

	// determine the final result
	if (index < count)
		return std::tolower(lhs[index]) - std::tolower(rhs[index]);
	if (lhs[index] == 0)
		return 0;
	return 1;
}

double pstring::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;
}

long pstring::as_long(bool *error) const
{
	long ret;
	char *e = NULL;

	if (error != NULL)
		*error = false;
	if (startsWith("0x"))
		ret = strtol(&(cstr()[2]), &e, 16);
	else
		ret = strtol(cstr(), &e, 10);
	if (*e != 0)
		if (error != NULL)
			*error = true;
	return ret;
}

pstring pstring::vprintf(va_list args) const
{
	// sprintf into the temporary buffer
	char tempbuf[4096];
	std::vsprintf(tempbuf, cstr(), args);

	return pstring(tempbuf);
}

// ----------------------------------------------------------------------------------------
// static stuff ...
// ----------------------------------------------------------------------------------------

void pstring::sfree(str_t *s)
{
	s->m_ref_count--;
	if (s->m_ref_count == 0 && s != &m_zero)
	{
		pfree_array(((char *)s));
		//_mm_free(((char *)s));
	}
}

pstring::str_t *pstring::salloc(int n)
{
	int size = sizeof(str_t) + n + 1;
	str_t *p = (str_t *) palloc_array(char, size);
	//  str_t *p = (str_t *) _mm_malloc(size, 8);
	p->init(n);
	return p;
}

void pstring::resetmem()
{
	// Release the 0 string
}

// ----------------------------------------------------------------------------------------
// pstring ...
// ----------------------------------------------------------------------------------------

pstring pstring::sprintf(const char *format, ...)
{
	va_list ap;
	va_start(ap, format);
	pstring ret = pstring(format).vprintf(ap);
	va_end(ap);
	return ret;
}


int pstring::find(const char *search, int start) const
{
	int alen = len();
	const char *result = std::strstr(cstr() + std::min(start, alen), search);
	return (result != NULL) ? (result - cstr()) : -1;
}

int pstring::find(const char search, int start) const
{
	int alen = len();
	const char *result = std::strchr(cstr() + std::min(start, alen), search);
	return (result != NULL) ? (result - cstr()) : -1;
}

bool pstring::startsWith(const char *arg) const
{
	return (pcmp(cstr(), arg, std::strlen(arg)) == 0);
}

int pstring::pcmp(const char *left, const char *right, int count) const
{
	if (count < 0)
		return std::strcmp(left, right);
	else
		return std::strncmp(left, right, count);
}

// ----------------------------------------------------------------------------------------
// 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_len = 0;
	}
	else if (m_size < size)
	{
		while (m_size < size)
			m_size *= 2;
		char *new_buf = palloc_array(char, m_size);
		std::strncpy(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::strncpy(m_ptr, from, nl);
}

void pstringbuffer::pcopy(const pstring &from)
{
	std::size_t nl = from.len() + 1;
	resize(nl);
	std::strncpy(m_ptr, from.cstr(), nl);
}

void pstringbuffer::pcat(const char *s)
{
	std::size_t slen = std::strlen(s);
	std::size_t nl = m_len + slen + 1;
	resize(nl);
	std::strncpy(m_ptr + m_len, s, slen + 1);
	m_len += slen;
}

void pstringbuffer::pcat(const pstring &s)
{
	std::size_t slen = s.len();
	std::size_t nl = m_len + slen + 1;
	resize(nl);
	std::strncpy(m_ptr + m_len, s.cstr(), slen + 1);
	m_len += slen;
}