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