File: strbuf.c

package info (click to toggle)
global 4.8.6-2
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 3,356 kB
  • ctags: 4,245
  • sloc: ansic: 26,150; lex: 1,471; perl: 1,233; sh: 1,032; lisp: 410; makefile: 158; yacc: 123
file content (578 lines) | stat: -rw-r--r-- 11,864 bytes parent folder | download
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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
/*
 * Copyright (c) 1997, 1998, 1999, 2000, 2002, 2005
 *	Tama Communications Corporation
 *
 * This file is part of GNU GLOBAL.
 *
 * GNU GLOBAL is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
 * GNU GLOBAL is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA.
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <ctype.h>
#ifdef STDC_HEADERS
#include <stdlib.h>
#endif
#ifdef HAVE_STRING_H
#include <string.h>
#else
#include <strings.h>
#endif

#include "die.h"
#include "strbuf.h"

#ifndef isblank
#define isblank(c)	((c) == ' ' || (c) == '\t')
#endif
/*

String buffer: usage and memory status

					[xxx]: string buffer
					'v': current pointer

Function call                           Memory status
----------------------------------------------------------
                                        (not exist)
                                         v
sb = strbuf_open(0);                    []
                                          v
strbuf_putc(sb, 'a');                   [a]
                                          v
char *s = strbuf_value(sb);             [a\0]           s == "a"
                                            v
strbuf_puts(sb, "bc");                  [abc]
                                            v
char *s = strbuf_value(sb);             [abc\0]         s == "abc"
                                            v
int len = strbuf_getlen(sb);            [abc\0]         len == 3
                                         v
strbuf_reset(sb);                       [abc\0]
                                         v
int len = strbuf_getlen(sb);            [abc\0]         len == 0
                                           v
strbuf_puts(sb, "XY");                  [XYc\0]
                                           v
char *s = strbuf_value(sb);             [XY\0]          s == "XY"

fp = fopen("/etc/passwd", "r");                                             v
char *s = strbuf_fgets(sb, fp, 0)       [root:*:0:0:Charlie &:/root:/bin/csh\0]
fclose(fp)				s == "root:*:0:0:Charlie &:/root:/bin/csh"

strbuf_close(sb);                       (not exist)

*/

static void print_and_abort (void);
void (*strbuf_alloc_failed_handler) (void) = print_and_abort;

static void
print_and_abort(void)
{
	die("short of memory.");
}

#ifdef STRBUF_LINK
STRBUF	top;
/*
 * strbuf_dump: dump string buffers
 */
void
strbuf_dump(msg)
	const char *msg;
{
	STRBUF *sb;
	int i = 0;

	fprintf(stderr, "[%s/%s]\n", progname, msg);
	for (sb = top.next; sb && sb != &top; sb = sb->next) {
		char *p = strbuf_value(sb);
		char *end = p + strbuf_getlen(sb);

		*sb->curp = 0;
		fprintf(stderr, "%d\tsize=%d", strbuf_getlen(sb));
		if (strbuf_getlen(sb) <= strlen(p))
			fprintf(stderr, ", value=|%s|\n", p);
		else {
			fputc('\n', stderr);
			for (; p < end; p += strlen(p) + 1)
				fprintf(stderr, "\t|%s|", p);
		}
	}
}
#endif
/*
 * __strbuf_expandbuf: expand buffer so that afford to the length data at least.
 *
 *	i)	sb	STRBUF structure
 *	i)	length	required room
 */
void
__strbuf_expandbuf(sb, length)
	STRBUF *sb;
	int length;
{
	int count = sb->curp - sb->sbuf;
	int newsize = sb->sbufsize + (length > EXPANDSIZE ? length : EXPANDSIZE);
	char *newbuf;

	if (sb->alloc_failed)
		return;
	newbuf = (char *)realloc(sb->sbuf, newsize + 1);
	if (newbuf == NULL) {
		(*strbuf_alloc_failed_handler)();
		sb->alloc_failed = 1;
		return;
	}
	sb->sbufsize = newsize;
	sb->sbuf = newbuf;

	sb->curp = sb->sbuf + count;
	sb->endp = sb->sbuf + sb->sbufsize;
}
/*
 * strbuf_open: open string buffer.
 *
 *	i)	init	initial buffer size
 *			if 0 is specified then use default value.
 *	r)	sb	STRBUF structure
 */
STRBUF *
strbuf_open(init)
	int init;
{
	STRBUF *sb = (STRBUF *)calloc(sizeof(STRBUF), 1);

	if (sb == NULL) {
		(*strbuf_alloc_failed_handler)();
		return NULL;
	}
	sb->sbufsize = (init > 0) ? init : INITIALSIZE;
	if (!(sb->sbuf = (char *)malloc(sb->sbufsize + 1))) {
		(*strbuf_alloc_failed_handler)();
		(void)free(sb);
		return NULL;
	}
	sb->curp = sb->sbuf;
	sb->endp = sb->sbuf + sb->sbufsize;

#ifdef STRBUF_LINK
	if (top.next == NULL) {
		top.next = top.prev = sb;
		sb->next = sb->prev = &top;
	} else {
		sb->next = &top;
		sb->prev = top.prev;
		top.prev->next = sb;
		top.prev = sb;
	}
#endif
	return sb;
}
/*
 * strbuf_clear: clear static string buffer.
 *
 *	i)	sb	statically defined string buffer
 *
 * This function is used for the initializing of static string buffer.
 * For the detail, see 'STATIC_STRBUF(sb)' macro in strbuf.h.
 */
void
strbuf_clear(sb)
	STRBUF *sb;
{
	if (sb == NULL)
		die("NULL string buffer. (strbuf_clear)");
	if (strbuf_empty(sb)) {
		sb->sbufsize = INITIALSIZE;
		if (!(sb->sbuf = (char *)malloc(sb->sbufsize + 1))) {
			(*strbuf_alloc_failed_handler)();
			return;
		}
		sb->curp = sb->sbuf;
		sb->endp = sb->sbuf + sb->sbufsize;
	} else {
		strbuf_reset(sb);
	}
}
/*
 * strbuf_nputs: Put string with length
 *
 *	i)	sb	statically defined string buffer
 *	i)	s	string
 *	i)	len	length of string
 */
void
strbuf_nputs(sb, s, len)
	STRBUF *sb;
	const char *s;
	int len;
{
	if (!sb->alloc_failed && len > 0) {
		if (sb->curp + len > sb->endp)
			__strbuf_expandbuf(sb, len);
		while (len-- > 0)
			*sb->curp++ = *s++;
	}
}
/*
 * strbuf_puts: Put string
 *
 *	i)	sb	statically defined string buffer
 *	i)	s	string
 */
void
strbuf_puts(sb, s)
	STRBUF *sb;
	const char *s;
{
	if (!sb->alloc_failed) {
		while (*s) {
			if (sb->curp >= sb->endp)
				__strbuf_expandbuf(sb, 0);
			*sb->curp++ = *s++;
		}
	}
}
/*
 * strbuf_puts_nl: Put string with a new line
 *
 *	i)	sb	statically defined string buffer
 *	i)	s	string
 */
void
strbuf_puts_nl(sb, s)
	STRBUF *sb;
	const char *s;
{
	if (!sb->alloc_failed) {
		while (*s) {
			if (sb->curp >= sb->endp)
				__strbuf_expandbuf(sb, 0);
			*sb->curp++ = *s++;
		}
		if (sb->curp >= sb->endp)
			__strbuf_expandbuf(sb, 0);
		*sb->curp++ = '\n';
	}
}
/*
 * strbuf_putn: put digit string at the last of buffer.
 *
 *	i)	sb	STRBUF structure
 *	i)	n	number
 */
void
strbuf_putn(sb, n)
	STRBUF *sb;
	int n;
{
	if (n == 0) {
		strbuf_putc(sb, '0');
	} else {
		char num[128];
		int i = 0;

		while (n) {
			if (i >= sizeof(num))
				die("Too big integer value.");
			num[i++] = n % 10 + '0';
			n = n / 10;
		}
		while (--i >= 0)
			strbuf_putc(sb, num[i]);
	}
}
/*
 * strbuf_unputc: remove specified char from the last of buffer
 *
 *	i)	sb	STRBUF structure
 *	i)	c	character
 *	r)		0: do nothing, 1: removed
 */
int
strbuf_unputc(sb, c)
	STRBUF *sb;
	int c;
{
	if (sb->curp > sb->sbuf && *(sb->curp - 1) == c) {
		sb->curp--;
		return 1;
	}
	return 0;
}
/*
 * strbuf_value: return the content of string buffer.
 *
 *	i)	sb	STRBUF structure
 *	r)		string
 */
char *
strbuf_value(sb)
	STRBUF *sb;
{
	*sb->curp = 0;
	return sb->sbuf;
}
/*
 * strbuf_trim: trim following blanks.
 *
 *	i)	sb	STRBUR structure
 */
void
strbuf_trim(sb)
	STRBUF *sb;
{
	char *p = sb->curp;

	while (p > sb->sbuf && isblank(*(p - 1)))
		*--p = 0;
	sb->curp = p;
}
/*
 * strbuf_fgets: read whole record into string buffer
 *
 *	o)	sb	string buffer
 *	i)	ip	input stream
 *	i)	flags	flags
 *			STRBUF_NOCRLF	remove last '\n' if exist.
 *			STRBUF_APPEND	append next record to existing data
 *	r)		record buffer (NULL at end of file)
 *
 * Returned buffer has whole record.
 * The buffer end with '\0'.If STRBUF_NOCRLF is set then buffer doesn't
 * include '\r' and '\n'.
 */
char *
strbuf_fgets(sb, ip, flags)
	STRBUF *sb;
	FILE *ip;
	int flags;
{
	if (!(flags & STRBUF_APPEND))
		strbuf_reset(sb);

	if (sb->curp >= sb->endp)
		__strbuf_expandbuf(sb, EXPANDSIZE);	/* expand buffer */
	if (sb->alloc_failed)
		return sb->sbuf;

	for (;;) {
		if (!fgets(sb->curp, sb->endp - sb->curp, ip)) {
			if (sb->curp == sb->sbuf)
				return NULL;
			break;
		}
		sb->curp += strlen(sb->curp);
		if (*(sb->curp - 1) == '\n')
			break;
		else if (feof(ip)) {
			return sb->sbuf;
		}
		__strbuf_expandbuf(sb, EXPANDSIZE);	/* expand buffer */
		if (sb->alloc_failed)
			return sb->sbuf;
	}
	if (flags & STRBUF_NOCRLF) {
		if (*(sb->curp - 1) == '\n')
			*(--sb->curp) = 0;
		if (*(sb->curp - 1) == '\r')
			*(--sb->curp) = 0;
	}
	return sb->sbuf;
}
/*
 * strbuf_sprintf: do sprintf into string buffer.
 *
 *	i)	sb	STRBUF structure
 *	i)	s	similar to sprintf()
 *			Currently the following format is supported.
 *			%s, %d, %<number>d, %<number>s
 */
void
#ifdef HAVE_STDARG_H
strbuf_sprintf(STRBUF *sb, const char *s, ...)
#else
strbuf_sprintf(sb, s, va_alist)
	STRBUF *sb;
	const char *s;
	va_dcl
#endif
{
	va_list ap;

#ifdef HAVE_STDARG_H
	va_start(ap, s);
#else
	va_start(ap);
#endif
	if (sb->alloc_failed)
		return;
	for (; *s; s++) {
		/*
		 * Put the before part of '%'.
		 */
		{
			const char *p;
			for (p = s; *p && *p != '%'; p++)
				;
			if (p > s) {
				strbuf_nputs(sb, s, p - s);
				s = p;
			}
		}
		if (*s == '\0')
			break;
		if (*s == '%') {
			int c = *++s;
			/*
			 * '%%' means '%'.
			 */
			if (c == '%') {
				strbuf_putc(sb, c);
			}
			/*
			 * If the optional number is specified then
			 * we forward the job to snprintf(3).
			 * o %<number>d
			 * o %<number>s
			 */
			else if (isdigit(c)) {
				char format[32], buf[1024];
				int i = 0;

				format[i++] = '%';
				while (isdigit(*s))
					format[i++] = *s++;
				format[i++] = c = *s;
				format[i] = '\0';
				if (c == 'd' || c == 'x')
					snprintf(buf, sizeof(buf), format, va_arg(ap, int));
				else if (c == 's')
					snprintf(buf, sizeof(buf), format, va_arg(ap, char *));
				else
					die("Unsupported control character '%c'.", c);
				strbuf_puts(sb, buf);
			} else if (c == 's') {
				strbuf_puts(sb, va_arg(ap, char *));
			} else if (c == 'd') {
				strbuf_putn(sb, va_arg(ap, int));
			} else {
				die("Unsupported control character '%c'.", c);
			}
		}
	}
	va_end(ap);
}
/*
 * strbuf_close: close string buffer.
 *
 *	i)	sb	STRBUF structure
 */
void
strbuf_close(sb)
	STRBUF	*sb;
{
#ifdef STRBUF_LINK
	sb->prev->next = sb->next;
	sb->next->prev = sb->prev;
#endif
	if (sb->name)
		(void)free(sb->name);
	(void)free(sb->sbuf);
	(void)free(sb);
}
/*
 * Temporary string buffer for general purpose.
 *
 * Usage:
 *
 *	STRBUF *sbt = strbuf_open_tempbuf();
 *	....
 *	strbuf_puts(sbtemp, "xxx");
 *	...
 *	strbuf_release_tempbuf(sbt);
 *
 */
int used = 0;

STRBUF *
strbuf_open_tempbuf(void)
{
	STATIC_STRBUF(sb);
	if (used)
		die("Internal error: temporary string buffer is already used.");
	used = 1;
	strbuf_clear(sb);
	return sb;
}
void
strbuf_release_tempbuf(sb)
	STRBUF *sb;
{
	used = 0;
}
#ifdef STRBUF_LINK
/*
 * strbuf_setname: set name to specified string buffer.
 *
 *	i)	sb	STRBUF structure
 *	i)	name	name
 */
void
strbuf_setname(sb, name)
	STRBUF  *sb;
	const char *name;
{
	char *p = strdup(name);
	if (p == NULL) {
		(*strbuf_alloc_failed_handler)();
		sb->alloc_failed = 1;
		return;
	}
	if (sb->name)
		(void)free(sb->name);
	sb->name = p;
}
/*
 * strbuf_getbuf: get named buffer
 *
 *	i)	name	name of buffer
 *	r)		STRBUF structure
 */
STRBUF *
strbuf_getbuf(name)
	const char *name;
{
	STRBUF *sb;

	for (sb = top.next; sb && sb != &top; sb = sb->next)
		if (sb->name && !strcmp(name, sb->name))
			return sb;
	return NULL;
}
/*
 * strbuf_closeall: close all string buffer.
 */
void
strbuf_closeall(void)
{
	while (top.next != &top)
		strbuf_close(top.next);
	top.next = top.prev = NULL;
}
#endif /* STRBUF_LINK */