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)
60 if (*q++ == PATHSEP) p = q;
69 * Arguments: @FILE *fp@ = output stream to write on
70 * @const char *p@ = pointer to string to write
72 * Returns: Zero if everything worked, EOF if not.
74 * Use: Writes the string @p@ to the output stream @fp@. Occurrences
75 * of the character `$' in @p@ are replaced by the program name
76 * as reported by @quis@. A `$$' is replaced by a single `$'
80 int pquis(FILE *fp, const char *p)
87 if (fwrite(p, 1, sz, fp) < sz) return (EOF);
93 if (fputc('$', fp) == EOF) return (EOF);
95 } else if (fputs(quis, fp) == EOF)
104 * Arguments: @const char *f@ = a @printf@-style format string
105 * @...@ = other arguments
109 * Use: Reports an error and exits.
112 void die(const char *f, ...)
116 fprintf(stderr, "%s: ", quis);
117 vfprintf(stderr, f, ap);
123 /*----- Memory allocation -------------------------------------------------*/
125 /* --- @xmalloc@ --- *
127 * Arguments: @size_t sz@ = size of block to allocate
129 * Returns: Pointer to allocated block.
131 * Use: Allocates memory. If there's not enough memory, the
135 void *xmalloc(size_t sz)
137 void *p = malloc(sz);
138 if (!p) die("not enough memory");
142 /* --- @xrealloc@ --- *
144 * Arguments: @void *p@ = a pointer to allocated memory
145 * @size_t sz@ = new size of block wanted
147 * Returns: Pointer to resized block.
149 * Use: Resizes an allocated block. If there's not enough memory,
153 void *xrealloc(void *p, size_t sz)
156 if (!p) die("not enough memory");
160 /*----- Dynamic string handling -------------------------------------------*/
162 #define DSTR_INITSZ 64
164 /* --- @dstr_destroy@ --- *
166 * Arguments: @dstr *d@ = pointer to a dynamic string block
170 * Use: Reclaims the space used by a dynamic string.
173 void dstr_destroy(dstr *d) { free(d->buf); d->len = 0; d->sz = 0; }
175 /* --- @dstr_reset@ --- *
177 * Arguments: @dstr *d@ = pointer to a dynamic string block
181 * Use: Resets a string so that new data gets put at the beginning.
184 void dstr_reset(dstr *d) { d->len = 0; }
186 /* --- @dstr_ensure@ --- *
188 * Arguments: @dstr *d@ = pointer to a dynamic string block
189 * @size_t sz@ = amount of free space to ensure
193 * Use: Ensures that at least @sz@ bytes are available in the
197 void dstr_ensure(dstr *d, size_t sz)
199 size_t rq = d->len + sz;
202 /* If we have enough space, just leave it. */
203 if (rq <= d->sz) return;
205 /* Grow the buffer. */
207 if (nsz == 0) nsz = (DSTR_INITSZ >> 1);
208 do nsz <<= 1; while (nsz < rq);
209 if (d->buf) d->buf = xrealloc(d->buf, nsz);
210 else d->buf = xmalloc(nsz);
214 /* --- @dstr_putline@ --- *
216 * Arguments: @dstr *d@ = pointer to a dynamic string block
217 * @FILE *fp@ = a stream to read from
219 * Returns: The number of characters read into the buffer, or @EOF@ if
220 * end-of-file was reached before any characters were read.
222 * Use: Appends the next line from the given input stream to the
223 * string. A trailing newline is not added; a trailing null
224 * byte is appended, as for @dstr_putz@.
227 int dstr_putline(dstr *d, FILE *fp)
229 size_t left = d->sz - d->len;
236 /* Read the next byte. */
239 /* End-of-file when no characters read is special. */
240 if (ch == EOF && !rd) return (EOF);
242 /* Make sure there's some buffer space. */
249 /* End-of-file or newline ends the loop. */
250 if (ch == EOF || ch == '\n') {
256 /* Append the character and continue. */
262 /*----- That's all, folks -------------------------------------------------*/