1 /* PSPP - computes sample statistics.
2 Copyright (C) 1997-9, 2000, 2006 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
31 /* Reverses the order of NBYTES bytes at address P, thus converting
32 between little- and big-endian byte orders. */
34 buf_reverse (char *p, size_t nbytes)
36 char *h = p, *t = &h[nbytes - 1];
48 /* Finds the last NEEDLE of length NEEDLE_LEN in a HAYSTACK of length
49 HAYSTACK_LEN. Returns a pointer to the needle found. */
51 buf_find_reverse (const char *haystack, size_t haystack_len,
52 const char *needle, size_t needle_len)
55 for (i = haystack_len - needle_len; i >= 0; i--)
56 if (!memcmp (needle, &haystack[i], needle_len))
57 return (char *) &haystack[i];
61 /* Compares the SIZE bytes in A to those in B, disregarding case,
62 and returns a strcmp()-type result. */
64 buf_compare_case (const char *a_, const char *b_, size_t size)
66 const unsigned char *a = (unsigned char *) a_;
67 const unsigned char *b = (unsigned char *) b_;
71 unsigned char ac = toupper (*a++);
72 unsigned char bc = toupper (*b++);
75 return ac > bc ? 1 : -1;
81 /* Compares A of length A_LEN to B of length B_LEN. The shorter
82 string is considered to be padded with spaces to the length of
85 buf_compare_rpad (const char *a, size_t a_len, const char *b, size_t b_len)
90 min_len = a_len < b_len ? a_len : b_len;
91 result = memcmp (a, b, min_len);
100 for (idx = min_len; idx < b_len; idx++)
102 return ' ' > b[idx] ? 1 : -1;
106 for (idx = min_len; idx < a_len; idx++)
108 return a[idx] > ' ' ? 1 : -1;
114 /* Compares strin A to string B. The shorter string is
115 considered to be padded with spaces to the length of the
118 str_compare_rpad (const char *a, const char *b)
120 return buf_compare_rpad (a, strlen (a), b, strlen (b));
123 /* Copies string SRC to buffer DST, of size DST_SIZE bytes.
124 DST is truncated to DST_SIZE bytes or padded on the right with
127 buf_copy_str_rpad (char *dst, size_t dst_size, const char *src)
129 size_t src_len = strlen (src);
130 if (src_len >= dst_size)
131 memcpy (dst, src, dst_size);
134 memcpy (dst, src, src_len);
135 memset (&dst[src_len], ' ', dst_size - src_len);
139 /* Copies string SRC to buffer DST, of size DST_SIZE bytes.
140 DST is truncated to DST_SIZE bytes or padded on the left with
143 buf_copy_str_lpad (char *dst, size_t dst_size, const char *src)
145 size_t src_len = strlen (src);
146 if (src_len >= dst_size)
147 memcpy (dst, src, dst_size);
150 size_t pad_cnt = dst_size - src_len;
151 memset (&dst[0], ' ', pad_cnt);
152 memcpy (dst + pad_cnt, src, src_len);
156 /* Copies buffer SRC, of SRC_SIZE bytes, to DST, of DST_SIZE bytes.
157 DST is truncated to DST_SIZE bytes or padded on the right with
160 buf_copy_rpad (char *dst, size_t dst_size,
161 const char *src, size_t src_size)
163 if (src_size >= dst_size)
164 memmove (dst, src, dst_size);
167 memmove (dst, src, src_size);
168 memset (&dst[src_size], ' ', dst_size - src_size);
172 /* Copies string SRC to string DST, which is in a buffer DST_SIZE
174 Truncates DST to DST_SIZE - 1 characters or right-pads with
175 spaces to DST_SIZE - 1 characters if necessary. */
177 str_copy_rpad (char *dst, size_t dst_size, const char *src)
179 size_t src_len = strlen (src);
180 if (src_len < dst_size - 1)
182 memcpy (dst, src, src_len);
183 memset (&dst[src_len], ' ', dst_size - 1 - src_len);
186 memcpy (dst, src, dst_size - 1);
187 dst[dst_size - 1] = 0;
190 /* Copies SRC to DST, which is in a buffer DST_SIZE bytes long.
191 Truncates DST to DST_SIZE - 1 characters, if necessary. */
193 str_copy_trunc (char *dst, size_t dst_size, const char *src)
195 size_t src_len = strlen (src);
196 assert (dst_size > 0);
197 if (src_len + 1 < dst_size)
198 memcpy (dst, src, src_len + 1);
201 memcpy (dst, src, dst_size - 1);
202 dst[dst_size - 1] = '\0';
206 /* Copies buffer SRC, of SRC_LEN bytes,
207 to DST, which is in a buffer DST_SIZE bytes long.
208 Truncates DST to DST_SIZE - 1 characters, if necessary. */
210 str_copy_buf_trunc (char *dst, size_t dst_size,
211 const char *src, size_t src_size)
214 assert (dst_size > 0);
216 dst_len = src_size < dst_size ? src_size : dst_size - 1;
217 memcpy (dst, src, dst_len);
221 /* Converts each character in S to uppercase. */
223 str_uppercase (char *s)
225 for (; *s != '\0'; s++)
226 *s = toupper ((unsigned char) *s);
229 /* Converts each character in S to lowercase. */
231 str_lowercase (char *s)
233 for (; *s != '\0'; s++)
234 *s = tolower ((unsigned char) *s);
237 /* Initializes ST with initial contents S. */
239 ds_create (struct string *st, const char *s)
241 st->length = strlen (s);
242 st->capacity = MAX (8, st->length * 2);
243 st->string = xmalloc (st->capacity + 1);
244 strcpy (st->string, s);
247 /* Initializes ST, making room for at least CAPACITY characters. */
249 ds_init (struct string *st, size_t capacity)
252 st->capacity = MAX (8, capacity);
253 st->string = xmalloc (st->capacity + 1);
258 ds_destroy (struct string *st)
269 /* Swaps the contents of strings A and B. */
271 ds_swap (struct string *a, struct string *b)
273 struct string tmp = *a;
278 /* Initializes DST with the CNT characters from SRC starting at
281 ds_init_substring (struct string *dst,
282 const struct string *src, size_t idx, size_t cnt)
286 ds_assign_substring (dst, src, idx, cnt);
289 /* Copies SRC into DST.
290 DST and SRC may be the same string. */
292 ds_assign_string (struct string *dst, const struct string *src)
294 ds_assign_buffer (dst, ds_data (src), ds_length (src));
297 /* Replaces DST by CNT characters from SRC starting at position
299 DST and SRC may be the same string. */
301 ds_assign_substring (struct string *dst,
302 const struct string *src, size_t idx, size_t cnt)
304 if (idx < src->length)
305 ds_assign_buffer (dst, src->string + idx, MIN (cnt, src->length - idx));
310 /* Replaces DST by the LENGTH characters in SRC.
311 SRC may be a substring within DST. */
313 ds_assign_buffer (struct string *dst, const char *src, size_t length)
315 dst->length = length;
316 ds_extend (dst, length);
317 memmove (dst->string, src, length);
320 /* Replaces DST by null-terminated string SRC. SRC may overlap
323 ds_assign_c_str (struct string *dst, const char *src)
325 ds_assign_buffer (dst, src, strlen (src));
328 /* Truncates ST to zero length. */
330 ds_clear (struct string *st)
335 /* Ensures that ST can hold at least MIN_CAPACITY characters plus a null
338 ds_extend (struct string *st, size_t min_capacity)
340 if (min_capacity > st->capacity)
343 if (st->capacity < min_capacity)
344 st->capacity = 2 * min_capacity;
346 st->string = xrealloc (st->string, st->capacity + 1);
350 /* Shrink ST to the minimum capacity need to contain its content. */
352 ds_shrink (struct string *st)
354 if (st->capacity != st->length)
356 st->capacity = st->length;
357 st->string = xrealloc (st->string, st->capacity + 1);
361 /* Truncates ST to at most LENGTH characters long. */
363 ds_truncate (struct string *st, size_t length)
365 if (st->length > length)
369 /* Pad ST on the right with copies of PAD until ST is at least
370 LENGTH characters in size. If ST is initially LENGTH
371 characters or longer, this is a no-op. */
373 ds_rpad (struct string *st, size_t length, char pad)
375 if (length > st->length)
376 ds_putc_multiple (st, pad, length - st->length);
379 /* Removes trailing spaces from ST.
380 Returns number of spaces removed. */
382 ds_rtrim_spaces (struct string *st)
385 while (isspace (ds_last (st)))
393 /* Removes leading spaces from ST.
394 Returns number of spaces removed. */
396 ds_ltrim_spaces (struct string *st)
399 while (isspace (ds_at (st, cnt)))
402 ds_assign_substring (st, st, cnt, SIZE_MAX);
406 /* Trims leading and trailing spaces from ST. */
408 ds_trim_spaces (struct string *st)
410 ds_rtrim_spaces (st);
411 ds_ltrim_spaces (st);
414 /* If the last character in ST is C, removes it and returns true.
415 Otherwise, returns false without modifying ST. */
417 ds_chomp (struct string *st, char c_)
419 unsigned char c = c_;
420 if (ds_last (st) == c)
429 /* Divides ST into tokens separated by any of the DELIMITERS.
430 Each call replaces TOKEN by the next token in ST, or by an
431 empty string if no tokens remain. Returns true if a token was
432 obtained, false otherwise.
434 Before the first call, initialize *SAVE_IDX to -1. Do not
435 modify *SAVE_IDX between calls.
437 ST divides into exactly one more tokens than it contains
438 delimiters. That is, a delimiter at the start or end of ST or
439 a pair of adjacent delimiters yields an empty token, and the
440 empty string contains a single token. */
442 ds_separate (const struct string *st, struct string *token,
443 const char *delimiters, int *save_idx)
451 if (ds_is_empty (st))
454 else if (*save_idx < ds_length (st))
459 start_idx = *save_idx;
460 while (*save_idx < ds_length (st)
461 && strchr (delimiters, ds_data (st)[*save_idx]) == NULL)
463 ds_assign_substring (token, st, start_idx, *save_idx - start_idx);
467 /* Returns true if ST is empty, false otherwise. */
469 ds_is_empty (const struct string *st)
471 return st->length == 0;
474 /* Returns the length of ST. */
476 ds_length (const struct string *st)
481 /* Returns the value of ST as a null-terminated string. */
483 ds_c_str (const struct string *st_)
485 struct string *st = (struct string *) st_;
486 if (st->string == NULL)
488 st->string[st->length] = '\0';
492 /* Returns the string data inside ST. */
494 ds_data (const struct string *st)
499 /* Returns a pointer to the null terminator ST.
500 This might not be an actual null character unless ds_c_str() has
501 been called since the last modification to ST. */
503 ds_end (const struct string *st)
505 return st->string + st->length;
508 /* Returns the allocation size of ST. */
510 ds_capacity (const struct string *st)
515 /* Returns the character in position IDX in ST, as a value in the
516 range of unsigned char. Returns EOF if IDX is out of the
517 range of indexes for ST. */
519 ds_at (const struct string *st, size_t idx)
521 return idx < st->length ? (unsigned char) st->string[idx] : EOF;
524 /* Returns the first character in ST as a value in the range of
525 unsigned char. Returns EOF if ST is the empty string. */
527 ds_first (const struct string *st)
529 return ds_at (st, 0);
532 /* Returns the last character in ST as a value in the range of
533 unsigned char. Returns EOF if ST is the empty string. */
535 ds_last (const struct string *st)
537 return st->length > 0 ? (unsigned char) st->string[st->length - 1] : EOF;
540 /* Returns the number of consecutive characters starting at OFS
541 in ST that are in SKIP_SET. (The null terminator is not
542 considered to be part of SKIP_SET.) */
544 ds_span (const struct string *st, size_t ofs, const char skip_set[])
547 for (i = ofs; i < st->length; i++)
549 int c = st->string[i];
550 if (strchr (skip_set, c) == NULL || c == '\0')
556 /* Returns the number of consecutive characters starting at OFS
557 in ST that are not in STOP_SET. (The null terminator is not
558 considered to be part of STOP_SET.) */
560 ds_cspan (const struct string *st, size_t ofs, const char stop_set[])
563 for (i = ofs; i < st->length; i++)
565 int c = st->string[i];
566 if (strchr (stop_set, c) != NULL)
572 /* Appends to ST a newline-terminated line read from STREAM.
573 Newline is the last character of ST on return, unless an I/O error
574 or end of file is encountered after reading some characters.
575 Returns true if a line is successfully read, false if no characters at
576 all were read before an I/O error or end of file was
579 ds_gets (struct string *st, FILE *stream)
599 /* Removes a comment introduced by `#' from ST,
600 ignoring occurrences inside quoted strings. */
602 remove_comment (struct string *st)
607 for (cp = ds_c_str (st); cp < ds_end (st); cp++)
612 else if (*cp == '\\')
615 else if (*cp == '\'' || *cp == '"')
619 ds_truncate (st, cp - ds_c_str (st));
624 /* Reads a line from STREAM into ST, then preprocesses as follows:
626 - Splices lines terminated with `\'.
628 - Deletes comments introduced by `#' outside of single or double
631 - Deletes trailing white space.
633 Returns true if a line was successfully read, false on
634 failure. If LINE_NUMBER is non-null, then *LINE_NUMBER is
635 incremented by the number of lines read. */
637 ds_get_config_line (FILE *stream, struct string *st, int *line_number)
642 if (!ds_gets (st, stream))
645 ds_rtrim_spaces (st);
647 while (ds_chomp (st, '\\'));
653 /* Concatenates S onto ST. */
655 ds_puts (struct string *st, const char *s)
662 ds_extend (st, st->length + s_len);
663 strcpy (st->string + st->length, s);
667 /* Concatenates LEN characters from BUF onto ST. */
669 ds_concat (struct string *st, const char *buf, size_t len)
671 ds_extend (st, st->length + len);
672 memcpy (st->string + st->length, buf, len);
676 /* Formats FORMAT as a printf string and appends the result to ST. */
678 ds_printf (struct string *st, const char *format, ...)
682 va_start (args, format);
683 ds_vprintf(st, format, args);
687 /* Formats FORMAT as a printf string and appends the result to ST. */
689 ds_vprintf (struct string *st, const char *format, va_list args_)
695 #define va_copy(DST, SRC) (DST) = (SRC)
698 va_copy (args, args_);
699 avail = st->capacity - st->length + 1;
700 needed = vsnprintf (st->string + st->length, avail, format, args);
705 ds_extend (st, st->length + needed);
707 va_copy (args, args_);
708 vsprintf (st->string + st->length, format, args);
713 /* Some old libc's returned -1 when the destination string
717 ds_extend (st, (st->capacity + 1) * 2);
718 avail = st->capacity - st->length + 1;
720 va_copy (args, args_);
721 needed = vsnprintf (st->string + st->length, avail, format, args);
726 st->length += needed;
729 /* Appends character CH to ST. */
731 ds_putc (struct string *st, int ch)
733 if (st->length >= st->capacity)
734 ds_extend (st, st->length + 1);
735 st->string[st->length++] = ch;
738 /* Appends CNT copies of character CH to ST. */
740 ds_putc_multiple (struct string *st, int ch, size_t cnt)
742 ds_extend (st, st->length + cnt);
743 memset (&st->string[st->length], ch, cnt);
748 /* Lengthed strings. */
750 /* Creates a new lengthed string LS with contents as a copy of
753 ls_create (struct fixed_string *ls, const char *s)
755 ls->length = strlen (s);
756 ls->string = xmalloc (ls->length + 1);
757 memcpy (ls->string, s, ls->length + 1);
760 /* Creates a new lengthed string LS with contents as a copy of
761 BUFFER with length LEN. */
763 ls_create_buffer (struct fixed_string *ls,
764 const char *buffer, size_t len)
767 ls->string = xmalloc (len + 1);
768 memcpy (ls->string, buffer, len);
769 ls->string[len] = '\0';
772 /* Sets the fields of LS to the specified values. */
774 ls_init (struct fixed_string *ls, const char *string, size_t length)
776 ls->string = (char *) string;
780 /* Copies the fields of SRC to DST. */
782 ls_shallow_copy (struct fixed_string *dst, const struct fixed_string *src)
787 /* Frees the memory backing LS. */
789 ls_destroy (struct fixed_string *ls)
794 /* Sets LS to a null pointer value. */
796 ls_null (struct fixed_string *ls)
801 /* Returns nonzero only if LS has a null pointer value. */
803 ls_null_p (const struct fixed_string *ls)
805 return ls->string == NULL;
808 /* Returns nonzero only if LS is a null pointer or has length 0. */
810 ls_empty_p (const struct fixed_string *ls)
812 return ls->string == NULL || ls->length == 0;
815 /* Returns the length of LS, which must not be null. */
817 ls_length (const struct fixed_string *ls)
822 /* Returns a pointer to the character string in LS. */
824 ls_c_str (const struct fixed_string *ls)
826 return (char *) ls->string;
829 /* Returns a pointer to the null terminator of the character string in
832 ls_end (const struct fixed_string *ls)
834 return (char *) (ls->string + ls->length);