3 * $Id: util.c,v 1.2 2004/04/08 01:36:19 mdw Exp $
5 * Various useful utilities, stolen from mLib
7 * (c) 2001 Mark Wooding
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of Anag: a simple wordgame helper.
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.
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.
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.
29 /*----- Header files ------------------------------------------------------*/
33 /*----- Static variables --------------------------------------------------*/
35 static const char *quis = "<unset>";
37 /*----- Error reporting ---------------------------------------------------*/
41 * Arguments: @const char *p@ = pointer to program name
45 * Use: Stores what the program's name is.
49 # if defined(__riscos)
51 # elif defined(__unix) || defined(unix)
58 void ego(const char *p)
74 * Arguments: @FILE *fp@ = output stream to write on
75 * @const char *p@ = pointer to string to write
77 * Returns: Zero if everything worked, EOF if not.
79 * Use: Writes the string @p@ to the output stream @fp@. Occurrences
80 * of the character `$' in @p@ are replaced by the program name
81 * as reported by @quis@. A `$$' is replaced by a single `$'
85 int pquis(FILE *fp, const char *p)
92 if (fwrite(p, 1, sz, fp) < sz)
99 if (fputc('$', fp) == EOF)
103 if (fputs(quis, fp) == EOF)
113 * Arguments: @const char *f@ = a @printf@-style format string
114 * @...@ = other arguments
118 * Use: Reports an error and exits.
121 void die(const char *f, ...)
125 fprintf(stderr, "%s: ", quis);
126 vfprintf(stderr, f, ap);
132 /*----- Memory allocation -------------------------------------------------*/
134 /* --- @xmalloc@ --- *
136 * Arguments: @size_t sz@ = size of block to allocate
138 * Returns: Pointer to allocated block.
140 * Use: Allocates memory. If there's not enough memory, the
144 void *xmalloc(size_t sz)
146 void *p = malloc(sz);
148 die("not enough memory");
152 /* --- @xrealloc@ --- *
154 * Arguments: @void *p@ = a pointer to allocated memory
155 * @size_t sz@ = new size of block wanted
157 * Returns: Pointer to resized block.
159 * Use: Resizes an allocated block. If there's not enough memory,
163 void *xrealloc(void *p, size_t sz)
167 die("not enough memory");
171 /*----- Dynamic string handling -------------------------------------------*/
173 #define DSTR_INITSZ 64
175 /* --- @dstr_destroy@ --- *
177 * Arguments: @dstr *d@ = pointer to a dynamic string block
181 * Use: Reclaims the space used by a dynamic string.
184 void dstr_destroy(dstr *d) { free(d->buf); d->len = 0; d->sz = 0; }
186 /* --- @dstr_reset@ --- *
188 * Arguments: @dstr *d@ = pointer to a dynamic string block
192 * Use: Resets a string so that new data gets put at the beginning.
195 void dstr_reset(dstr *d) { d->len = 0; }
197 /* --- @dstr_ensure@ --- *
199 * Arguments: @dstr *d@ = pointer to a dynamic string block
200 * @size_t sz@ = amount of free space to ensure
204 * Use: Ensures that at least @sz@ bytes are available in the
208 void dstr_ensure(dstr *d, size_t sz)
210 size_t rq = d->len + sz;
213 /* --- If we have enough space, just leave it --- */
218 /* --- Grow the buffer --- */
223 nsz = (DSTR_INITSZ >> 1);
224 do nsz <<= 1; while (nsz < rq);
227 d->buf = xrealloc(d->buf, nsz);
229 d->buf = xmalloc(nsz);
233 /* --- @dstr_putline@ --- *
235 * Arguments: @dstr *d@ = pointer to a dynamic string block
236 * @FILE *fp@ = a stream to read from
238 * Returns: The number of characters read into the buffer, or @EOF@ if
239 * end-of-file was reached before any characters were read.
241 * Use: Appends the next line from the given input stream to the
242 * string. A trailing newline is not added; a trailing null
243 * byte is appended, as for @dstr_putz@.
246 int dstr_putline(dstr *d, FILE *fp)
248 size_t left = d->sz - d->len;
255 /* --- Read the next byte --- */
259 /* --- End-of-file when no characters read is special --- */
261 if (ch == EOF && !rd)
264 /* --- Make sure there's some buffer space --- */
272 /* --- End-of-file or newline ends the loop --- */
274 if (ch == EOF || ch == '\n') {
280 /* --- Append the character and continue --- */
287 /*----- That's all, folks -------------------------------------------------*/