3 * $Id: util.c,v 1.1 2001/02/04 17:14:42 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 /*----- Revision history --------------------------------------------------*
32 * Revision 1.1 2001/02/04 17:14:42 mdw
37 /*----- Header files ------------------------------------------------------*/
41 /*----- Static variables --------------------------------------------------*/
43 static const char *quis = "<unset>";
45 /*----- Error reporting ---------------------------------------------------*/
49 * Arguments: @const char *p@ = pointer to program name
53 * Use: Stores what the program's name is.
57 # if defined(__riscos)
59 # elif defined(__unix) || defined(unix)
66 void ego(const char *p)
82 * Arguments: @FILE *fp@ = output stream to write on
83 * @const char *p@ = pointer to string to write
85 * Returns: Zero if everything worked, EOF if not.
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 `$'
93 int pquis(FILE *fp, const char *p)
100 if (fwrite(p, 1, sz, fp) < sz)
107 if (fputc('$', fp) == EOF)
111 if (fputs(quis, fp) == EOF)
121 * Arguments: @const char *f@ = a @printf@-style format string
122 * @...@ = other arguments
126 * Use: Reports an error and exits.
129 void die(const char *f, ...)
133 fprintf(stderr, "%s: ", quis);
134 vfprintf(stderr, f, ap);
140 /*----- Memory allocation -------------------------------------------------*/
142 /* --- @xmalloc@ --- *
144 * Arguments: @size_t sz@ = size of block to allocate
146 * Returns: Pointer to allocated block.
148 * Use: Allocates memory. If there's not enough memory, the
152 void *xmalloc(size_t sz)
154 void *p = malloc(sz);
156 die("not enough memory");
160 /* --- @xrealloc@ --- *
162 * Arguments: @void *p@ = a pointer to allocated memory
163 * @size_t sz@ = new size of block wanted
165 * Returns: Pointer to resized block.
167 * Use: Resizes an allocated block. If there's not enough memory,
171 void *xrealloc(void *p, size_t sz)
175 die("not enough memory");
179 /*----- Dynamic string handling -------------------------------------------*/
181 #define DSTR_INITSZ 64
183 /* --- @dstr_destroy@ --- *
185 * Arguments: @dstr *d@ = pointer to a dynamic string block
189 * Use: Reclaims the space used by a dynamic string.
192 void dstr_destroy(dstr *d) { free(d->buf); d->len = 0; d->sz = 0; }
194 /* --- @dstr_reset@ --- *
196 * Arguments: @dstr *d@ = pointer to a dynamic string block
200 * Use: Resets a string so that new data gets put at the beginning.
203 void dstr_reset(dstr *d) { d->len = 0; }
205 /* --- @dstr_ensure@ --- *
207 * Arguments: @dstr *d@ = pointer to a dynamic string block
208 * @size_t sz@ = amount of free space to ensure
212 * Use: Ensures that at least @sz@ bytes are available in the
216 void dstr_ensure(dstr *d, size_t sz)
218 size_t rq = d->len + sz;
221 /* --- If we have enough space, just leave it --- */
226 /* --- Grow the buffer --- */
231 nsz = (DSTR_INITSZ >> 1);
232 do nsz <<= 1; while (nsz < rq);
235 d->buf = xrealloc(d->buf, nsz);
237 d->buf = xmalloc(nsz);
241 /* --- @dstr_putline@ --- *
243 * Arguments: @dstr *d@ = pointer to a dynamic string block
244 * @FILE *fp@ = a stream to read from
246 * Returns: The number of characters read into the buffer, or @EOF@ if
247 * end-of-file was reached before any characters were read.
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@.
254 int dstr_putline(dstr *d, FILE *fp)
256 size_t left = d->sz - d->len;
263 /* --- Read the next byte --- */
267 /* --- End-of-file when no characters read is special --- */
269 if (ch == EOF && !rd)
272 /* --- Make sure there's some buffer space --- */
280 /* --- End-of-file or newline ends the loop --- */
282 if (ch == EOF || ch == '\n') {
288 /* --- Append the character and continue --- */
295 /*----- That's all, folks -------------------------------------------------*/