summaryrefslogtreecommitdiffstats
path: root/contrib/byacc/mstring.c
blob: dc384e5a385de1a903207f38f0ef1d3e8189c86c (plain)
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
/* $Id: mstring.c,v 1.6 2014/04/22 23:36:31 tom Exp $ */

#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
#include <ctype.h>
#include <string.h>
#include "defs.h"

/* parameters about string length.  HEAD is the starting size and
** HEAD+TAIL should be a power of two */
#define HEAD	24
#define TAIL	8

#if defined(YYBTYACC)

static char *buf_ptr;
static size_t buf_len;

void
msprintf(struct mstring *s, const char *fmt,...)
{
    va_list args;
    size_t len;
#ifdef HAVE_VSNPRINTF
    int changed;
#endif

    if (!s || !s->base)
	return;

    if (buf_len == 0)
    {
	buf_ptr = malloc(buf_len = 4096);
    }
    if (buf_ptr == 0)
    {
	return;
    }

#ifdef HAVE_VSNPRINTF
    do
    {
	va_start(args, fmt);
	len = (size_t) vsnprintf(buf_ptr, buf_len, fmt, args);
	va_end(args);
	if ((changed = (len > buf_len)) != 0)
	{
	    char *new_ptr = realloc(buf_ptr, (buf_len * 3) / 2);
	    if (new_ptr == 0)
	    {
		free(buf_ptr);
		buf_ptr = 0;
		return;
	    }
	    buf_ptr = new_ptr;
	}
    }
    while (changed);
#else
    va_start(args, fmt);
    len = (size_t) vsprintf(buf_ptr, fmt, args);
    va_end(args);
    if (len >= buf_len)
	return;
#endif

    if (len > (size_t) (s->end - s->ptr))
    {
	char *new_base;
	size_t cp = (size_t) (s->ptr - s->base);
	size_t cl = (size_t) (s->end - s->base);
	size_t nl = cl;
	while (len > (nl - cp))
	    nl = nl + nl + TAIL;
	if ((new_base = realloc(s->base, nl)))
	{
	    s->base = new_base;
	    s->ptr = s->base + cp;
	    s->end = s->base + nl;
	}
	else
	{
	    free(s->base);
	    s->base = 0;
	    s->ptr = 0;
	    s->end = 0;
	    return;
	}
    }
    memcpy(s->ptr, buf_ptr, len);
    s->ptr += len;
}
#endif

int
mputchar(struct mstring *s, int ch)
{
    if (!s || !s->base)
	return ch;
    if (s->ptr == s->end)
    {
	size_t len = (size_t) (s->end - s->base);
	if ((s->base = realloc(s->base, len + len + TAIL)))
	{
	    s->ptr = s->base + len;
	    s->end = s->base + len + len + TAIL;
	}
	else
	{
	    s->ptr = s->end = 0;
	    return ch;
	}
    }
    *s->ptr++ = (char)ch;
    return ch;
}

struct mstring *
msnew(void)
{
    struct mstring *n = TMALLOC(struct mstring, 1);

    if (n)
    {
	if ((n->base = n->ptr = MALLOC(HEAD)) != 0)
	{
	    n->end = n->base + HEAD;
	}
	else
	{
	    free(n);
	    n = 0;
	}
    }
    return n;
}

char *
msdone(struct mstring *s)
{
    char *r = 0;
    if (s)
    {
	mputc(s, 0);
	r = s->base;
	free(s);
    }
    return r;
}

#if defined(YYBTYACC)
/* compare two strings, ignoring whitespace, except between two letters or
** digits (and treat all of these as equal) */
int
strnscmp(const char *a, const char *b)
{
    while (1)
    {
	while (isspace(*a))
	    a++;
	while (isspace(*b))
	    b++;
	while (*a && *a == *b)
	    a++, b++;
	if (isspace(*a))
	{
	    if (isalnum(a[-1]) && isalnum(*b))
		break;
	}
	else if (isspace(*b))
	{
	    if (isalnum(b[-1]) && isalnum(*a))
		break;
	}
	else
	    break;
    }
    return *a - *b;
}

unsigned int
strnshash(const char *s)
{
    unsigned int h = 0;

    while (*s)
    {
	if (!isspace(*s))
	    h = (h << 5) - h + (unsigned char)*s;
	s++;
    }
    return h;
}
#endif

#ifdef NO_LEAKS
void
mstring_leaks(void)
{
#if defined(YYBTYACC)
    free(buf_ptr);
    buf_ptr = 0;
    buf_len = 0;
#endif
}
#endif
OpenPOWER on IntegriCloud