3 * Various useful utilities, stolen from mLib
5 * (c) 2001 Mark Wooding
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of Anag: a simple wordgame helper.
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.
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.
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.
27 /*----- Header files ------------------------------------------------------*/
31 /*----- Static variables --------------------------------------------------*/
33 static const char *quis = "<unset>";
35 /*----- Error reporting ---------------------------------------------------*/
39 * Arguments: @const char *p@ = pointer to program name
43 * Use: Stores what the program's name is.
47 # if defined(__riscos)
49 # elif defined(__unix) || defined(unix)
56 void ego(const char *p)
72 * Arguments: @FILE *fp@ = output stream to write on
73 * @const char *p@ = pointer to string to write
75 * Returns: Zero if everything worked, EOF if not.
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 `$'
83 int pquis(FILE *fp, const char *p)
90 if (fwrite(p, 1, sz, fp) < sz)
97 if (fputc('$', fp) == EOF)
101 if (fputs(quis, fp) == EOF)
111 * Arguments: @const char *f@ = a @printf@-style format string
112 * @...@ = other arguments
116 * Use: Reports an error and exits.
119 void die(const char *f, ...)
123 fprintf(stderr, "%s: ", quis);
124 vfprintf(stderr, f, ap);
130 /*----- Memory allocation -------------------------------------------------*/
132 /* --- @xmalloc@ --- *
134 * Arguments: @size_t sz@ = size of block to allocate
136 * Returns: Pointer to allocated block.
138 * Use: Allocates memory. If there's not enough memory, the
142 void *xmalloc(size_t sz)
144 void *p = malloc(sz);
146 die("not enough memory");
150 /* --- @xrealloc@ --- *
152 * Arguments: @void *p@ = a pointer to allocated memory
153 * @size_t sz@ = new size of block wanted
155 * Returns: Pointer to resized block.
157 * Use: Resizes an allocated block. If there's not enough memory,
161 void *xrealloc(void *p, size_t sz)
165 die("not enough memory");
169 /*----- Dynamic string handling -------------------------------------------*/
171 #define DSTR_INITSZ 64
173 /* --- @dstr_destroy@ --- *
175 * Arguments: @dstr *d@ = pointer to a dynamic string block
179 * Use: Reclaims the space used by a dynamic string.
182 void dstr_destroy(dstr *d) { free(d->buf); d->len = 0; d->sz = 0; }
184 /* --- @dstr_reset@ --- *
186 * Arguments: @dstr *d@ = pointer to a dynamic string block
190 * Use: Resets a string so that new data gets put at the beginning.
193 void dstr_reset(dstr *d) { d->len = 0; }
195 /* --- @dstr_ensure@ --- *
197 * Arguments: @dstr *d@ = pointer to a dynamic string block
198 * @size_t sz@ = amount of free space to ensure
202 * Use: Ensures that at least @sz@ bytes are available in the
206 void dstr_ensure(dstr *d, size_t sz)
208 size_t rq = d->len + sz;
211 /* --- If we have enough space, just leave it --- */
216 /* --- Grow the buffer --- */
221 nsz = (DSTR_INITSZ >> 1);
222 do nsz <<= 1; while (nsz < rq);
225 d->buf = xrealloc(d->buf, nsz);
227 d->buf = xmalloc(nsz);
231 /* --- @dstr_putline@ --- *
233 * Arguments: @dstr *d@ = pointer to a dynamic string block
234 * @FILE *fp@ = a stream to read from
236 * Returns: The number of characters read into the buffer, or @EOF@ if
237 * end-of-file was reached before any characters were read.
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@.
244 int dstr_putline(dstr *d, FILE *fp)
246 size_t left = d->sz - d->len;
253 /* --- Read the next byte --- */
257 /* --- End-of-file when no characters read is special --- */
259 if (ch == EOF && !rd)
262 /* --- Make sure there's some buffer space --- */
270 /* --- End-of-file or newline ends the loop --- */
272 if (ch == EOF || ch == '\n') {
278 /* --- Append the character and continue --- */
285 /*----- That's all, folks -------------------------------------------------*/