6e403221 |
1 | /* -*-c-*- |
2 | * |
3 | * $Id: util.c,v 1.1 2001/02/04 17:14:42 mdw Exp $ |
4 | * |
5 | * Various useful utilities, stolen from mLib |
6 | * |
7 | * (c) 2001 Mark Wooding |
8 | */ |
9 | |
10 | /*----- Licensing notice --------------------------------------------------* |
11 | * |
12 | * This file is part of Anag: a simple wordgame helper. |
13 | * |
14 | * Anag is free software; you can redistribute it and/or modify |
15 | * it under the terms of the GNU General Public License as published by |
16 | * the Free Software Foundation; either version 2 of the License, or |
17 | * (at your option) any later version. |
18 | * |
19 | * Anag is distributed in the hope that it will be useful, |
20 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
21 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
22 | * GNU General Public License for more details. |
23 | * |
24 | * You should have received a copy of the GNU General Public License |
25 | * along with Anag; if not, write to the Free Software Foundation, |
26 | * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
27 | */ |
28 | |
29 | /*----- Revision history --------------------------------------------------* |
30 | * |
31 | * $Log: util.c,v $ |
32 | * Revision 1.1 2001/02/04 17:14:42 mdw |
33 | * Initial checkin |
34 | * |
35 | */ |
36 | |
37 | /*----- Header files ------------------------------------------------------*/ |
38 | |
39 | #include "anag.h" |
40 | |
41 | /*----- Static variables --------------------------------------------------*/ |
42 | |
43 | static const char *quis = "<unset>"; |
44 | |
45 | /*----- Error reporting ---------------------------------------------------*/ |
46 | |
47 | /* --- @ego@ --- * |
48 | * |
49 | * Arguments: @const char *p@ = pointer to program name |
50 | * |
51 | * Returns: --- |
52 | * |
53 | * Use: Stores what the program's name is. |
54 | */ |
55 | |
56 | #ifndef PATHSEP |
57 | # if defined(__riscos) |
58 | # define PATHSEP '.' |
59 | # elif defined(__unix) || defined(unix) |
60 | # define PATHSEP '/' |
61 | # else |
62 | # define PATHSEP '\\' |
63 | # endif |
64 | #endif |
65 | |
66 | void ego(const char *p) |
67 | { |
68 | const char *q = p; |
69 | while (*q) { |
70 | if (*q++ == PATHSEP) |
71 | p = q; |
72 | } |
73 | if (*p == '-') |
74 | p++; |
75 | quis = p; |
76 | } |
77 | |
78 | #undef PATHSEP |
79 | |
80 | /* --- @pquis@ --- * |
81 | * |
82 | * Arguments: @FILE *fp@ = output stream to write on |
83 | * @const char *p@ = pointer to string to write |
84 | * |
85 | * Returns: Zero if everything worked, EOF if not. |
86 | * |
87 | * Use: Writes the string @p@ to the output stream @fp@. Occurrences |
88 | * of the character `$' in @p@ are replaced by the program name |
89 | * as reported by @quis@. A `$$' is replaced by a single `$' |
90 | * sign. |
91 | */ |
92 | |
93 | int pquis(FILE *fp, const char *p) |
94 | { |
95 | size_t sz; |
96 | |
97 | while (*p) { |
98 | sz = strcspn(p, "$"); |
99 | if (sz) { |
100 | if (fwrite(p, 1, sz, fp) < sz) |
101 | return (EOF); |
102 | p += sz; |
103 | } |
104 | if (*p == '$') { |
105 | p++; |
106 | if (*p == '$') { |
107 | if (fputc('$', fp) == EOF) |
108 | return (EOF); |
109 | p++; |
110 | } else { |
111 | if (fputs(quis, fp) == EOF) |
112 | return (EOF); |
113 | } |
114 | } |
115 | } |
116 | return (0); |
117 | } |
118 | |
119 | /* --- @die@ --- * |
120 | * |
121 | * Arguments: @const char *f@ = a @printf@-style format string |
122 | * @...@ = other arguments |
123 | * |
124 | * Returns: Never. |
125 | * |
126 | * Use: Reports an error and exits. |
127 | */ |
128 | |
129 | void die(const char *f, ...) |
130 | { |
131 | va_list ap; |
132 | va_start(ap, f); |
133 | fprintf(stderr, "%s: ", quis); |
134 | vfprintf(stderr, f, ap); |
135 | va_end(ap); |
136 | putc('\n', stderr); |
137 | exit(EXIT_FAILURE); |
138 | } |
139 | |
140 | /*----- Memory allocation -------------------------------------------------*/ |
141 | |
142 | /* --- @xmalloc@ --- * |
143 | * |
144 | * Arguments: @size_t sz@ = size of block to allocate |
145 | * |
146 | * Returns: Pointer to allocated block. |
147 | * |
148 | * Use: Allocates memory. If there's not enough memory, the |
149 | * program exits. |
150 | */ |
151 | |
152 | void *xmalloc(size_t sz) |
153 | { |
154 | void *p = malloc(sz); |
155 | if (!p) |
156 | die("not enough memory"); |
157 | return (p); |
158 | } |
159 | |
160 | /* --- @xrealloc@ --- * |
161 | * |
162 | * Arguments: @void *p@ = a pointer to allocated memory |
163 | * @size_t sz@ = new size of block wanted |
164 | * |
165 | * Returns: Pointer to resized block. |
166 | * |
167 | * Use: Resizes an allocated block. If there's not enough memory, |
168 | * the program exits. |
169 | */ |
170 | |
171 | void *xrealloc(void *p, size_t sz) |
172 | { |
173 | p = realloc(p, sz); |
174 | if (!p) |
175 | die("not enough memory"); |
176 | return (p); |
177 | } |
178 | |
179 | /*----- Dynamic string handling -------------------------------------------*/ |
180 | |
181 | #define DSTR_INITSZ 64 |
182 | |
183 | /* --- @dstr_destroy@ --- * |
184 | * |
185 | * Arguments: @dstr *d@ = pointer to a dynamic string block |
186 | * |
187 | * Returns: --- |
188 | * |
189 | * Use: Reclaims the space used by a dynamic string. |
190 | */ |
191 | |
192 | void dstr_destroy(dstr *d) { free(d->buf); d->len = 0; d->sz = 0; } |
193 | |
194 | /* --- @dstr_reset@ --- * |
195 | * |
196 | * Arguments: @dstr *d@ = pointer to a dynamic string block |
197 | * |
198 | * Returns: --- |
199 | * |
200 | * Use: Resets a string so that new data gets put at the beginning. |
201 | */ |
202 | |
203 | void dstr_reset(dstr *d) { d->len = 0; } |
204 | |
205 | /* --- @dstr_ensure@ --- * |
206 | * |
207 | * Arguments: @dstr *d@ = pointer to a dynamic string block |
208 | * @size_t sz@ = amount of free space to ensure |
209 | * |
210 | * Returns: --- |
211 | * |
212 | * Use: Ensures that at least @sz@ bytes are available in the |
213 | * given string. |
214 | */ |
215 | |
216 | void dstr_ensure(dstr *d, size_t sz) |
217 | { |
218 | size_t rq = d->len + sz; |
219 | size_t nsz; |
220 | |
221 | /* --- If we have enough space, just leave it --- */ |
222 | |
223 | if (rq <= d->sz) |
224 | return; |
225 | |
226 | /* --- Grow the buffer --- */ |
227 | |
228 | nsz = d->sz; |
229 | |
230 | if (nsz == 0) |
231 | nsz = (DSTR_INITSZ >> 1); |
232 | do nsz <<= 1; while (nsz < rq); |
233 | |
234 | if (d->buf) |
235 | d->buf = xrealloc(d->buf, nsz); |
236 | else |
237 | d->buf = xmalloc(nsz); |
238 | d->sz = nsz; |
239 | } |
240 | |
241 | /* --- @dstr_putline@ --- * |
242 | * |
243 | * Arguments: @dstr *d@ = pointer to a dynamic string block |
244 | * @FILE *fp@ = a stream to read from |
245 | * |
246 | * Returns: The number of characters read into the buffer, or @EOF@ if |
247 | * end-of-file was reached before any characters were read. |
248 | * |
249 | * Use: Appends the next line from the given input stream to the |
250 | * string. A trailing newline is not added; a trailing null |
251 | * byte is appended, as for @dstr_putz@. |
252 | */ |
253 | |
254 | int dstr_putline(dstr *d, FILE *fp) |
255 | { |
256 | size_t left = d->sz - d->len; |
257 | size_t off = d->len; |
258 | int rd = 0; |
259 | int ch; |
260 | |
261 | for (;;) { |
262 | |
263 | /* --- Read the next byte --- */ |
264 | |
265 | ch = getc(fp); |
266 | |
267 | /* --- End-of-file when no characters read is special --- */ |
268 | |
269 | if (ch == EOF && !rd) |
270 | return (EOF); |
271 | |
272 | /* --- Make sure there's some buffer space --- */ |
273 | |
274 | if (!left) { |
275 | d->len = off; |
276 | dstr_ensure(d, 1); |
277 | left = d->sz - off; |
278 | } |
279 | |
280 | /* --- End-of-file or newline ends the loop --- */ |
281 | |
282 | if (ch == EOF || ch == '\n') { |
283 | d->buf[off] = 0; |
284 | d->len = off; |
285 | return rd; |
286 | } |
287 | |
288 | /* --- Append the character and continue --- */ |
289 | |
290 | d->buf[off++] = ch; |
291 | left--; rd++; |
292 | } |
293 | } |
294 | |
295 | /*----- That's all, folks -------------------------------------------------*/ |