Debian Codegraph @

terps/scare/sxutils.c

raw symbols SCIP
1 /* vi: set ts=2 shiftwidth=2 expandtab:
2 *
3 * Copyright (C) 2003-2008 Simon Baldwin and Mark J. Tilford
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of version 2 of the GNU General Public License
7 * as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
17 * USA
18 */
19
20 /*
21 * Module notes:
22 *
23 * o ...
24 */
25
26 #include <assert.h>
27 #include <stdarg.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31
32 #include "scare.h"
33 #include "sxprotos.h"
34
35
36 /*
37 * sx_trace()
38 *
39 * Debugging trace function; printf wrapper that writes to stdout. Note that
40 * this differs from sc_trace(), which writes to stderr. We use stdout so
41 * that trace output is synchronized to test expectation failure messages.
42 */
43 void
44 sx_trace (const sc_char *format, ...)
45 {
46 va_list ap;
47 assert (format);
48
49 va_start (ap, format);
50 vfprintf (stdout, format, ap);
51 va_end (ap);
52 }
53
54
55 /*
56 * sx_error()
57 * sx_fatal()
58 *
59 * Error reporting functions. sx_error() prints a message and continues.
60 * sx_fatal() prints a message, then calls abort().
61 */
62 void
63 sx_error (const sc_char *format, ...)
64 {
65 va_list ap;
66 assert (format);
67
68 fprintf (stderr, "sx: ");
69 va_start (ap, format);
70 vfprintf (stderr, format, ap);
71 va_end (ap);
72 }
73
74 void
75 sx_fatal (const sc_char *format, ...)
76 {
77 va_list ap;
78 assert (format);
79
80 fprintf (stderr, "sx: internal error: ");
81 va_start (ap, format);
82 vfprintf (stderr, format, ap);
83 va_end (ap);
84 fprintf (stderr, "sx: aborting...\n");
85 abort ();
86 }
87
88
89 /* Unique non-heap address for zero size malloc() and realloc() requests. */
90 static void *sx_zero_allocation = &sx_zero_allocation;
91
92 /*
93 * sx_malloc()
94 * sx_realloc()
95 * sx_free()
96 *
97 * Non-failing wrappers around malloc functions. Newly allocated memory is
98 * cleared to zero. In ANSI/ISO C, zero byte allocations are implementation-
99 * defined, so we have to take special care to get predictable behavior.
100 */
101 void *
102 sx_malloc (size_t size)
103 {
104 void *allocated;
105
106 if (size == 0)
107 return sx_zero_allocation;
108
109 allocated = malloc (size);
110 if (!allocated)
111 sx_fatal ("sx_malloc: requested %lu bytes\n", (sc_uint) size);
112 else if (allocated == sx_zero_allocation)
113 sx_fatal ("sx_malloc: zero-byte allocation address returned\n");
114
115 memset (allocated, 0, size);
116 return allocated;
117 }
118
119 void *
120 sx_realloc (void *pointer, size_t size)
121 {
122 void *allocated;
123
124 if (size == 0)
125 {
126 sx_free (pointer);
127 return sx_zero_allocation;
128 }
129
130 if (pointer == sx_zero_allocation)
131 pointer = NULL;
132
133 allocated = realloc (pointer, size);
134 if (!allocated)
135 sx_fatal ("sx_realloc: requested %lu bytes\n", (sc_uint) size);
136 else if (allocated == sx_zero_allocation)
137 sx_fatal ("sx_realloc: zero-byte allocation address returned\n");
138
139 if (!pointer)
140 memset (allocated, 0, size);
141 return allocated;
142 }
143
144 void
145 sx_free (void *pointer)
146 {
147 if (sx_zero_allocation != &sx_zero_allocation)
148 sx_fatal ("sx_free: write to zero-byte allocation address detected\n");
149
150 if (pointer && pointer != sx_zero_allocation)
151 free (pointer);
152 }
153
154
155 /*
156 * sx_fopen()
157 *
158 * Open a file for a given test name with the extension and mode supplied.
159 * Returns NULL if unsuccessful.
160 */
161 FILE *
162 sx_fopen (const sc_char *name, const sc_char *extension, const sc_char *mode)
163 {
164 sc_char *filename;
165 FILE *stream;
166 assert (name && extension && mode);
167
168 filename = sx_malloc (strlen (name) + strlen (extension) + 2);
169 sprintf (filename, "%s.%s", name, extension);
170
171 stream = fopen (filename, mode);
172
173 sx_free (filename);
174 return stream;
175 }
176
177
178 /* Miscellaneous general ascii constants. */
179 static const sc_char NUL = '\0';
180
181 /*
182 * sx_isspace()
183 * sx_isprint()
184 *
185 * Built in replacements for locale-sensitive libc ctype.h functions.
186 */
187 static sc_bool
188 sx_isspace (sc_char character)
189 {
190 static const sc_char *const WHITESPACE = "\t\n\v\f\r ";
191
192 return character != NUL && strchr (WHITESPACE, character) != NULL;
193 }
194
195 static sc_bool
196 sx_isprint (sc_char character)
197 {
198 static const sc_int MIN_PRINTABLE = ' ', MAX_PRINTABLE = '~';
199
200 return character >= MIN_PRINTABLE && character <= MAX_PRINTABLE;
201 }
202
203
204 /*
205 * sx_trim_string()
206 *
207 * Trim leading and trailing whitespace from a string. Modifies the string
208 * in place, and returns the string address for convenience.
209 */
210 sc_char *
211 sx_trim_string (sc_char *string)
212 {
213 sc_int index_;
214 assert (string);
215
216 for (index_ = strlen (string) - 1;
217 index_ >= 0 && sx_isspace (string[index_]); index_--)
218 string[index_] = NUL;
219
220 for (index_ = 0; sx_isspace (string[index_]);)
221 index_++;
222 memmove (string, string + index_, strlen (string) - index_ + 1);
223
224 return string;
225 }
226
227
228 /*
229 * sx_normalize_string()
230 *
231 * Trim a string, set all runs of whitespace to a single space character,
232 * and convert all non-printing characters to '?'. Modifies the string in
233 * place, and returns the string address for convenience.
234 */
235 sc_char *
236 sx_normalize_string (sc_char *string)
237 {
238 sc_int index_;
239 assert (string);
240
241 string = sx_trim_string (string);
242
243 for (index_ = 0; string[index_] != NUL; index_++)
244 {
245 if (sx_isspace (string[index_]))
246 {
247 sc_int cursor;
248
249 string[index_] = ' ';
250 for (cursor = index_ + 1; sx_isspace (string[cursor]);)
251 cursor++;
252 memmove (string + index_ + 1,
253 string + cursor, strlen (string + cursor) + 1);
254 }
255 else if (!sx_isprint (string[index_]))
256 string[index_] = '?';
257 }
258
259 return string;
260 }