File: stringhandling.c

package info (click to toggle)
espeakup 1%3A0.80-5%2Bdeb9u3
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 520 kB
  • sloc: ansic: 1,283; sh: 207; makefile: 63
file content (112 lines) | stat: -rw-r--r-- 2,613 bytes parent folder | download | duplicates (3)
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
/*
 *  espeakup - interface which allows speakup to use espeak
 *
 *  Copyright (C) 2011 William Hubbs
 *
 *  This program 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 3 of the License, or
 *  (at your option) any later version.
 *
 *   This program 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, see <http://www.gnu.org/licenses/>.
 */

/*
 * These string routines were borrowed from edbrowse, which was
 * originally written by Karl Dahlke, and is being currently maintained
 * by Christopher Brannon.
 * I would like to thank both of them for their work on this software.
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char *EMPTYSTRING = "";

void *allocMem(size_t n)
{
	void *s;
	if (!n)
		return EMPTYSTRING;
	if (!(s = malloc(n))) {
		fprintf(stderr, "Out of memory!\n");
		exit(1);
	}
	return s;
}

void *reallocMem(void *p, size_t n)
{
	void *s;
	if (!n) {
		fprintf(stderr, "Trying to reallocate memory with size of 0.\n");
		exit(1);
	}
	if (!p) {
		fprintf(stderr, "realloc called with a NULL pointer!\n");
		exit(1);
	}
	if (p == EMPTYSTRING)
		return allocMem(n);
	if (!(s = realloc(p, n))) {
		fprintf(stderr, "Failed to allocate memory.\n");
		exit(1);
	}
	return s;
}

char *initString(int *l)
{
	*l = 0;
	return EMPTYSTRING;
}

void stringAndString(char **s, int *l, const char *t)
{
	char *p = *s;
	int oldlen, newlen, x;
	oldlen = *l;
	newlen = oldlen + strlen(t);
	*l = newlen;
	++newlen;					/* room for the 0 */
	x = oldlen ^ newlen;
	if (x > oldlen) {			/* must realloc */
		newlen |= (newlen >> 1);
		newlen |= (newlen >> 2);
		newlen |= (newlen >> 4);
		newlen |= (newlen >> 8);
		newlen |= (newlen >> 16);
		p = reallocMem(p, newlen);
		*s = p;
	}
	strcpy(p + oldlen, t);
}

void stringAndBytes(char **s, int *l, const char *t, int cnt)
{
	char *p = *s;
	int oldlen, newlen, x;
	oldlen = *l;
	newlen = oldlen + cnt;
	*l = newlen;
	++newlen;
	x = oldlen ^ newlen;
	if (x > oldlen) {			/* must realloc */
		newlen |= (newlen >> 1);
		newlen |= (newlen >> 2);
		newlen |= (newlen >> 4);
		newlen |= (newlen >> 8);
		newlen |= (newlen >> 16);
		p = reallocMem(p, newlen);
		*s = p;
	}
	memcpy(p + oldlen, t, cnt);
	p[oldlen + cnt] = 0;
}