1 /* PSPP - computes sample statistics.
2 Copyright (C) 1997-9, 2000 Free Software Foundation, Inc.
3 Written by Ben Pfaff <blp@gnu.org>.
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 License, or (at your option) any later version.
10 This program is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
30 /* sprintf() wrapper functions for convenience. */
34 spprintf (char *buf, const char *format,...)
41 va_start (args, format);
45 vsprintf (buf, format, args);
51 return strchr (buf, 0);
54 #endif /* !__GNUC__ */
56 #if !__GNUC__ && !HAVE_GOOD_SPRINTF
58 nsprintf (char *buf, const char *format,...)
62 va_start (args, format);
63 vsprintf (buf, format, args);
70 nvsprintf (char *buf, const char *format, va_list args)
72 vsprintf (buf, format, args);
75 #endif /* Not GNU C and not good sprintf(). */
77 /* Reverses the order of NBYTES bytes at address P, thus converting
78 between little- and big-endian byte orders. */
80 mm_reverse (void *p, size_t nbytes)
82 unsigned char *h = p, *t = &h[nbytes - 1];
94 /* Finds the last NEEDLE of length NEEDLE_LEN in a HAYSTACK of length
95 HAYSTACK_LEN. Returns a pointer to the needle found. */
97 mm_find_reverse (const char *haystack, size_t haystack_len,
98 const char *needle, size_t needle_len)
101 for (i = haystack_len - needle_len; i >= 0; i--)
102 if (!memcmp (needle, &haystack[i], needle_len))
103 return (char *) &haystack[i];
107 /* Compares S0 of length S0L to S1 of length S1L. The shorter string
108 is considered to be padded with spaces to the length of the
111 st_compare_pad (const char *s0, int s0l, const char *s1, int s1l)
114 static char blanks[254] =
121 int diff = s0l - s1l;
128 return memcmp (s0, s1, s0l);
135 r = memcmp (s0, s1, s1l);
139 return memcmp (&s0[s1l], blanks, diff);
147 r = memcmp (s0, s1, s0l);
151 return memcmp (blanks, &s1[s0l], -diff);
155 /* Copies SRC to DEST, truncating to N characters or right-padding
156 with spaces to N characters as necessary. Does not append a null
157 character. SRC must be null-terminated. */
159 st_bare_pad_copy (char *dest, const char *src, size_t n)
165 memcpy (dest, src, n);
168 memcpy (dest, src, len);
169 memset (&dest[len], ' ', n - len);
173 /* Copies SRC to DEST, truncating SRC to N characters or right-padding
174 with spaces to N characters if necessary. Does not append a null
175 character. SRC must be LEN characters long but does not need to be
178 st_bare_pad_len_copy (char *dest, const char *src, size_t n, size_t len)
181 memcpy (dest, src, n);
184 memcpy (dest, src, len);
185 memset (&dest[len], ' ', n - len);
189 /* Copies SRC to DEST, truncating SRC to N-1 characters or
190 right-padding with spaces to N-1 characters if necessary. Always
191 appends a null character. */
193 st_pad_copy (char *dest, const char *src, size_t n)
200 else if (len < n - 1)
202 memcpy (dest, src, len);
203 memset (&dest[len], ' ', n - 1 - len);
208 memcpy (dest, src, n - 1);
213 /* Initializes ST inside pool POOL (which may be a null pointer) with
214 initial contents S. */
216 ds_create (struct pool *pool, struct string *st, const char *s)
219 st->length = strlen (s);
220 st->size = 8 + st->length * 2;
221 st->string = pool_malloc (pool, st->size + 1);
222 strcpy (st->string, s);
225 /* Initializes ST inside POOL (which may be null), making room for at
226 least SIZE characters. */
228 ds_init (struct pool *pool, struct string *st, size_t size)
236 st->string = pool_malloc (pool, st->size + 1);
239 /* Replaces the contents of ST with STRING. STRING may overlap with
242 ds_replace (struct string *st, const char *string)
244 char *s = st->string;
246 ds_create (st->pool, st, string);
247 pool_free (st->pool, s);
252 ds_destroy (struct string *st)
254 pool_free (st->pool, st->string);
257 /* Truncates ST to zero length. */
259 ds_clear (struct string *st)
264 /* Ensures that ST can hold at least MIN_SIZE characters plus a null
267 ds_extend (struct string *st, size_t min_size)
269 if (min_size > st->size)
272 if (st->size < min_size)
273 st->size = min_size * 2;
275 st->string = pool_realloc (st->pool, st->string, st->size + 1);
279 /* Shrink ST to the minimum size need to contain its content. */
281 ds_shrink (struct string *st)
283 if (st->size != st->length)
285 st->size = st->length;
286 st->string = pool_realloc (st->pool, st->string, st->size + 1);
290 /* Truncates ST to at most LENGTH characters long. */
292 ds_truncate (struct string *st, size_t length)
294 if (length >= st->length)
299 /* Returns the length of ST. */
301 ds_length (const struct string *st)
306 /* Returns the allocation size of ST. */
308 ds_size (const struct string *st)
313 /* Returns the value of ST as a null-terminated string. */
315 ds_value (const struct string *st)
317 ((char *) st->string)[st->length] = '\0';
321 /* Returns a pointer to the null terminator ST.
322 This might not be an actual null character unless ds_value() has
323 been called since the last modification to ST. */
325 ds_end (const struct string *st)
327 return st->string + st->length;
330 /* Concatenates S onto ST. */
332 ds_concat (struct string *st, const char *s)
339 ds_extend (st, st->length + s_len);
340 strcpy (st->string + st->length, s);
344 /* Concatenates LEN characters from BUF onto ST. */
346 ds_concat_buffer (struct string *st, const char *buf, size_t len)
348 ds_extend (st, st->length + len);
349 memcpy (st->string + st->length, buf, len);
353 /* Formats FORMAT as a printf string and appends the result to ST. */
355 ds_printf (struct string *st, const char *format, ...)
357 /* Fscking glibc silently changed behavior between 2.0 and 2.1.
358 Fsck fsck fsck. Before, it returned -1 on buffer overflow. Now,
359 it returns the number of characters (not bytes) that would have
365 va_start (args, format);
366 avail = st->size - st->length + 1;
367 needed = vsnprintf (st->string + st->length, avail, format, args);
372 ds_extend (st, st->length + needed);
374 va_start (args, format);
375 vsprintf (st->string + st->length, format, args);
381 ds_extend (st, (st->size + 1) * 2);
382 avail = st->size - st->length + 1;
384 va_start (args, format);
385 needed = vsnprintf (st->string + st->length, avail, format, args);
389 st->length += needed;
392 /* Appends character CH to ST. */
394 ds_putchar (struct string *st, int ch)
396 if (st->length == st->size)
397 ds_extend (st, st->length + 1);
398 st->string[st->length++] = ch;
401 /* Reads a newline-terminated line from STREAM into ST.
402 Newline is the last character of ST on return, unless an I/O error
403 or end of file is encountered after reading some characters.
404 Returns 1 if a line is successfully read, or 0 if no characters at
405 all were read before an I/O error or end of file was
408 ds_getline (struct string *st, FILE *stream)
428 /* Reads a line from STREAM into ST, then preprocesses as follows:
430 - Splices lines terminated with `\'.
432 - Deletes comments introduced by `#' outside of single or double
435 - Trailing whitespace will be deleted.
437 Increments cust_ln as appropriate.
439 Returns nonzero only if a line was successfully read. */
441 ds_get_config_line (FILE *stream, struct string *st, struct file_locator *where)
443 /* Read the first line. */
445 where->line_number++;
446 if (!ds_getline (st, stream))
449 /* Read additional lines, if any. */
452 /* Remove trailing whitespace. */
454 char *s = ds_value (st);
455 size_t len = ds_length (st);
457 while (len > 0 && isspace ((unsigned char) s[len - 1]))
459 ds_truncate (st, len);
462 /* Check for trailing \. Remove if found, bail otherwise. */
463 if (ds_length (st) == 0 || ds_value (st)[ds_length (st) - 1] != '\\')
465 ds_truncate (st, ds_length (st) - 1);
467 /* Append another line and go around again. */
469 int success = ds_getline (st, stream);
470 where->line_number++;
476 /* Find a comment and remove. */
481 for (cp = ds_value (st); *cp; cp++)
486 else if (*cp == '\\')
489 else if (*cp == '\'' || *cp == '"')
493 ds_truncate (st, cp - ds_value (st));
501 /* Lengthed strings. */
503 /* Creates a new lengthed string LS in POOL with contents as a copy of
506 ls_create (struct pool *pool, struct len_string *ls, const char *s)
508 ls->length = strlen (s);
509 ls->string = pool_alloc (pool, ls->length + 1);
510 memcpy (ls->string, s, ls->length + 1);
513 /* Creates a new lengthed string LS in POOL with contents as a copy of
514 BUFFER with length LEN. */
516 ls_create_buffer (struct pool *pool, struct len_string *ls,
517 const char *buffer, size_t len)
520 ls->string = pool_malloc (pool, len + 1);
521 memcpy (ls->string, buffer, len);
522 ls->string[len] = '\0';
525 /* Sets the fields of LS to the specified values. */
527 ls_init (struct len_string *ls, const char *string, size_t length)
529 ls->string = (char *) string;
533 /* Copies the fields of SRC to DST. */
535 ls_shallow_copy (struct len_string *dst, const struct len_string *src)
540 /* Frees the memory in POOL backing LS. */
542 ls_destroy (struct pool *pool, struct len_string *ls)
544 pool_free (pool, ls->string);
547 /* Sets LS to a null pointer value. */
549 ls_null (struct len_string *ls)
554 /* Returns nonzero only if LS has a null pointer value. */
556 ls_null_p (const struct len_string *ls)
558 return ls->string == NULL;
561 /* Returns nonzero only if LS is a null pointer or has length 0. */
563 ls_empty_p (const struct len_string *ls)
565 return ls->string == NULL || ls->length == 0;
568 /* Returns the length of LS, which must not be null. */
570 ls_length (const struct len_string *ls)
575 /* Returns a pointer to the character string in LS. */
577 ls_value (const struct len_string *ls)
579 return (char *) ls->string;
582 /* Returns a pointer to the null terminator of the character string in
585 ls_end (const struct len_string *ls)
587 return (char *) (ls->string + ls->length);