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., 51 Franklin Street, Fifth Floor, Boston, MA
29 /* sprintf() wrapper functions for convenience. */
33 spprintf (char *buf, const char *format,...)
40 va_start (args, format);
44 vsprintf (buf, format, args);
50 return strchr (buf, 0);
53 #endif /* !__GNUC__ */
55 #if !__GNUC__ && !HAVE_GOOD_SPRINTF
57 nsprintf (char *buf, const char *format,...)
61 va_start (args, format);
62 vsprintf (buf, format, args);
69 nvsprintf (char *buf, const char *format, va_list args)
71 vsprintf (buf, format, args);
74 #endif /* Not GNU C and not good sprintf(). */
76 /* Reverses the order of NBYTES bytes at address P, thus converting
77 between little- and big-endian byte orders. */
79 buf_reverse (char *p, size_t nbytes)
81 char *h = p, *t = &h[nbytes - 1];
93 /* Finds the last NEEDLE of length NEEDLE_LEN in a HAYSTACK of length
94 HAYSTACK_LEN. Returns a pointer to the needle found. */
96 buf_find_reverse (const char *haystack, size_t haystack_len,
97 const char *needle, size_t needle_len)
100 for (i = haystack_len - needle_len; i >= 0; i--)
101 if (!memcmp (needle, &haystack[i], needle_len))
102 return (char *) &haystack[i];
106 /* Compares the SIZE bytes in A to those in B, disregarding case,
107 and returns a strcmp()-type result. */
109 buf_compare_case (const char *a_, const char *b_, size_t size)
111 const unsigned char *a = (unsigned char *) a_;
112 const unsigned char *b = (unsigned char *) b_;
116 unsigned char ac = toupper (*a++);
117 unsigned char bc = toupper (*b++);
120 return ac > bc ? 1 : -1;
126 /* Compares A of length A_LEN to B of length B_LEN. The shorter
127 string is considered to be padded with spaces to the length of
130 buf_compare_rpad (const char *a, size_t a_len, const char *b, size_t b_len)
135 min_len = a_len < b_len ? a_len : b_len;
136 result = memcmp (a, b, min_len);
145 for (idx = min_len; idx < b_len; idx++)
147 return ' ' > b[idx] ? 1 : -1;
151 for (idx = min_len; idx < a_len; idx++)
153 return a[idx] > ' ' ? 1 : -1;
159 /* Compares strin A to string B. The shorter string is
160 considered to be padded with spaces to the length of the
163 str_compare_rpad (const char *a, const char *b)
165 return buf_compare_rpad (a, strlen (a), b, strlen (b));
168 /* Copies string SRC to buffer DST, of size DST_SIZE bytes.
169 DST is truncated to DST_SIZE bytes or padded on the right with
172 buf_copy_str_rpad (char *dst, size_t dst_size, const char *src)
174 size_t src_len = strlen (src);
175 if (src_len >= dst_size)
176 memcpy (dst, src, dst_size);
179 memcpy (dst, src, src_len);
180 memset (&dst[src_len], ' ', dst_size - src_len);
184 /* Copies string SRC to buffer DST, of size DST_SIZE bytes.
185 DST is truncated to DST_SIZE bytes or padded on the left with
188 buf_copy_str_lpad (char *dst, size_t dst_size, const char *src)
190 size_t src_len = strlen (src);
191 if (src_len >= dst_size)
192 memcpy (dst, src, dst_size);
195 size_t pad_cnt = dst_size - src_len;
196 memset (&dst[0], ' ', pad_cnt);
197 memcpy (dst + pad_cnt, src, src_len);
201 /* Copies buffer SRC, of SRC_SIZE bytes, to DST, of DST_SIZE bytes.
202 DST is truncated to DST_SIZE bytes or padded on the right with
205 buf_copy_rpad (char *dst, size_t dst_size,
206 const char *src, size_t src_size)
208 if (src_size >= dst_size)
209 memmove (dst, src, dst_size);
212 memmove (dst, src, src_size);
213 memset (&dst[src_size], ' ', dst_size - src_size);
217 /* Copies string SRC to string DST, which is in a buffer DST_SIZE
219 Truncates DST to DST_SIZE - 1 characters or right-pads with
220 spaces to DST_SIZE - 1 characters if necessary. */
222 str_copy_rpad (char *dst, size_t dst_size, const char *src)
224 size_t src_len = strlen (src);
225 if (src_len < dst_size - 1)
227 memcpy (dst, src, src_len);
228 memset (&dst[src_len], ' ', dst_size - 1 - src_len);
231 memcpy (dst, src, dst_size - 1);
232 dst[dst_size - 1] = 0;
235 /* Copies SRC to DST, which is in a buffer DST_SIZE bytes long.
236 Truncates DST to DST_SIZE - 1 characters, if necessary. */
238 str_copy_trunc (char *dst, size_t dst_size, const char *src)
240 size_t src_len = strlen (src);
241 assert (dst_size > 0);
242 if (src_len + 1 < dst_size)
243 memcpy (dst, src, src_len + 1);
246 memcpy (dst, src, dst_size - 1);
247 dst[dst_size - 1] = '\0';
251 /* Copies buffer SRC, of SRC_LEN bytes,
252 to DST, which is in a buffer DST_SIZE bytes long.
253 Truncates DST to DST_SIZE - 1 characters, if necessary. */
255 str_copy_buf_trunc (char *dst, size_t dst_size,
256 const char *src, size_t src_size)
259 assert (dst_size > 0);
261 dst_len = src_size < dst_size ? src_size : dst_size - 1;
262 memcpy (dst, src, dst_len);
266 /* Converts each character in S to uppercase. */
268 str_uppercase (char *s)
270 for (; *s != '\0'; s++)
271 *s = toupper ((unsigned char) *s);
274 /* Initializes ST with initial contents S. */
276 ds_create (struct string *st, const char *s)
278 st->length = strlen (s);
279 st->capacity = 8 + st->length * 2;
280 st->string = xmalloc (st->capacity + 1);
281 strcpy (st->string, s);
284 /* Initializes ST, making room for at least CAPACITY characters. */
286 ds_init (struct string *st, size_t capacity)
290 st->capacity = capacity;
293 st->string = xmalloc (st->capacity + 1);
296 /* Replaces the contents of ST with STRING. STRING may overlap with
299 ds_replace (struct string *st, const char *string)
301 size_t new_length = strlen (string);
302 if (new_length > st->capacity)
304 /* The new length is longer than the allocated length, so
305 there can be no overlap. */
307 ds_concat (st, string, new_length);
311 /* Overlap is possible, but the new string will fit in the
312 allocated space, so we can just copy data. */
313 st->length = new_length;
314 memmove (st->string, string, st->length);
320 ds_destroy (struct string *st)
326 /* Truncates ST to zero length. */
328 ds_clear (struct string *st)
333 /* Pad ST on the right with copies of PAD until ST is at least
334 LENGTH characters in size. If ST is initially LENGTH
335 characters or longer, this is a no-op. */
337 ds_rpad (struct string *st, size_t length, char pad)
340 if (st->length < length)
342 if (st->capacity < length)
343 ds_extend (st, length);
344 memset (&st->string[st->length], pad, length - st->length);
349 /* Ensures that ST can hold at least MIN_CAPACITY characters plus a null
352 ds_extend (struct string *st, size_t min_capacity)
354 if (min_capacity > st->capacity)
357 if (st->capacity < min_capacity)
358 st->capacity = min_capacity * 2;
360 st->string = xrealloc (st->string, st->capacity + 1);
364 /* Shrink ST to the minimum capacity need to contain its content. */
366 ds_shrink (struct string *st)
368 if (st->capacity != st->length)
370 st->capacity = st->length;
371 st->string = xrealloc (st->string, st->capacity + 1);
375 /* Truncates ST to at most LENGTH characters long. */
377 ds_truncate (struct string *st, size_t length)
379 if (length >= st->length)
384 /* Returns the length of ST. */
386 ds_length (const struct string *st)
391 /* Returns the allocation size of ST. */
393 ds_capacity (const struct string *st)
398 /* Returns the value of ST as a null-terminated string. */
400 ds_c_str (const struct string *st)
402 ((char *) st->string)[st->length] = '\0';
406 /* Returns the string data inside ST. */
408 ds_data (const struct string *st)
413 /* Returns a pointer to the null terminator ST.
414 This might not be an actual null character unless ds_c_str() has
415 been called since the last modification to ST. */
417 ds_end (const struct string *st)
419 return st->string + st->length;
422 /* Concatenates S onto ST. */
424 ds_puts (struct string *st, const char *s)
431 ds_extend (st, st->length + s_len);
432 strcpy (st->string + st->length, s);
436 /* Concatenates LEN characters from BUF onto ST. */
438 ds_concat (struct string *st, const char *buf, size_t len)
440 ds_extend (st, st->length + len);
441 memcpy (st->string + st->length, buf, len);
445 void ds_vprintf (struct string *st, const char *format, va_list args);
448 /* Formats FORMAT as a printf string and appends the result to ST. */
450 ds_printf (struct string *st, const char *format, ...)
454 va_start (args, format);
455 ds_vprintf(st,format,args);
460 /* Formats FORMAT as a printf string and appends the result to ST. */
462 ds_vprintf (struct string *st, const char *format, va_list args)
464 /* Fscking glibc silently changed behavior between 2.0 and 2.1.
465 Fsck fsck fsck. Before, it returned -1 on buffer overflow. Now,
466 it returns the number of characters (not bytes) that would have
473 avail = st->capacity - st->length + 1;
474 needed = vsnprintf (st->string + st->length, avail, format, args);
479 ds_extend (st, st->length + needed);
481 vsprintf (st->string + st->length, format, a1);
489 ds_extend (st, (st->capacity + 1) * 2);
490 avail = st->capacity - st->length + 1;
492 needed = vsnprintf (st->string + st->length, avail, format, a2);
498 st->length += needed;
501 /* Appends character CH to ST. */
503 ds_putc (struct string *st, int ch)
505 if (st->length == st->capacity)
506 ds_extend (st, st->length + 1);
507 st->string[st->length++] = ch;
510 /* Appends to ST a newline-terminated line read from STREAM.
511 Newline is the last character of ST on return, unless an I/O error
512 or end of file is encountered after reading some characters.
513 Returns 1 if a line is successfully read, or 0 if no characters at
514 all were read before an I/O error or end of file was
517 ds_gets (struct string *st, FILE *stream)
537 /* Reads a line from STREAM into ST, then preprocesses as follows:
539 - Splices lines terminated with `\'.
541 - Deletes comments introduced by `#' outside of single or double
544 - Trailing whitespace will be deleted.
546 Increments cust_ln as appropriate.
548 Returns nonzero only if a line was successfully read. */
550 ds_get_config_line (FILE *stream, struct string *st, struct file_locator *where)
552 /* Read the first line. */
554 where->line_number++;
555 if (!ds_gets (st, stream))
558 /* Read additional lines, if any. */
561 /* Remove trailing whitespace. */
563 char *s = ds_c_str (st);
564 size_t len = ds_length (st);
566 while (len > 0 && isspace ((unsigned char) s[len - 1]))
568 ds_truncate (st, len);
571 /* Check for trailing \. Remove if found, bail otherwise. */
572 if (ds_length (st) == 0 || ds_c_str (st)[ds_length (st) - 1] != '\\')
574 ds_truncate (st, ds_length (st) - 1);
576 /* Append another line and go around again. */
578 int success = ds_gets (st, stream);
579 where->line_number++;
585 /* Find a comment and remove. */
590 for (cp = ds_c_str (st); *cp; cp++)
595 else if (*cp == '\\')
598 else if (*cp == '\'' || *cp == '"')
602 ds_truncate (st, cp - ds_c_str (st));
610 /* Lengthed strings. */
612 /* Creates a new lengthed string LS with contents as a copy of
615 ls_create (struct fixed_string *ls, const char *s)
617 ls->length = strlen (s);
618 ls->string = xmalloc (ls->length + 1);
619 memcpy (ls->string, s, ls->length + 1);
622 /* Creates a new lengthed string LS with contents as a copy of
623 BUFFER with length LEN. */
625 ls_create_buffer (struct fixed_string *ls,
626 const char *buffer, size_t len)
629 ls->string = xmalloc (len + 1);
630 memcpy (ls->string, buffer, len);
631 ls->string[len] = '\0';
634 /* Sets the fields of LS to the specified values. */
636 ls_init (struct fixed_string *ls, const char *string, size_t length)
638 ls->string = (char *) string;
642 /* Copies the fields of SRC to DST. */
644 ls_shallow_copy (struct fixed_string *dst, const struct fixed_string *src)
649 /* Frees the memory backing LS. */
651 ls_destroy (struct fixed_string *ls)
656 /* Sets LS to a null pointer value. */
658 ls_null (struct fixed_string *ls)
663 /* Returns nonzero only if LS has a null pointer value. */
665 ls_null_p (const struct fixed_string *ls)
667 return ls->string == NULL;
670 /* Returns nonzero only if LS is a null pointer or has length 0. */
672 ls_empty_p (const struct fixed_string *ls)
674 return ls->string == NULL || ls->length == 0;
677 /* Returns the length of LS, which must not be null. */
679 ls_length (const struct fixed_string *ls)
684 /* Returns a pointer to the character string in LS. */
686 ls_c_str (const struct fixed_string *ls)
688 return (char *) ls->string;
691 /* Returns a pointer to the null terminator of the character string in
694 ls_end (const struct fixed_string *ls)
696 return (char *) (ls->string + ls->length);