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
28 #include <libpspp/alloc.h>
29 #include <libpspp/message.h>
34 /* Reverses the order of NBYTES bytes at address P, thus converting
35 between little- and big-endian byte orders. */
37 buf_reverse (char *p, size_t nbytes)
39 char *h = p, *t = &h[nbytes - 1];
51 /* Finds the last NEEDLE of length NEEDLE_LEN in a HAYSTACK of length
52 HAYSTACK_LEN. Returns a pointer to the needle found. */
54 buf_find_reverse (const char *haystack, size_t haystack_len,
55 const char *needle, size_t needle_len)
58 for (i = haystack_len - needle_len; i >= 0; i--)
59 if (!memcmp (needle, &haystack[i], needle_len))
60 return (char *) &haystack[i];
64 /* Compares the SIZE bytes in A to those in B, disregarding case,
65 and returns a strcmp()-type result. */
67 buf_compare_case (const char *a_, const char *b_, size_t size)
69 const unsigned char *a = (unsigned char *) a_;
70 const unsigned char *b = (unsigned char *) b_;
74 unsigned char ac = toupper (*a++);
75 unsigned char bc = toupper (*b++);
78 return ac > bc ? 1 : -1;
84 /* Compares A of length A_LEN to B of length B_LEN. The shorter
85 string is considered to be padded with spaces to the length of
88 buf_compare_rpad (const char *a, size_t a_len, const char *b, size_t b_len)
93 min_len = a_len < b_len ? a_len : b_len;
94 result = memcmp (a, b, min_len);
103 for (idx = min_len; idx < b_len; idx++)
105 return ' ' > b[idx] ? 1 : -1;
109 for (idx = min_len; idx < a_len; idx++)
111 return a[idx] > ' ' ? 1 : -1;
117 /* Compares strin A to string B. The shorter string is
118 considered to be padded with spaces to the length of the
121 str_compare_rpad (const char *a, const char *b)
123 return buf_compare_rpad (a, strlen (a), b, strlen (b));
126 /* Copies string SRC to buffer DST, of size DST_SIZE bytes.
127 DST is truncated to DST_SIZE bytes or padded on the right with
130 buf_copy_str_rpad (char *dst, size_t dst_size, const char *src)
132 size_t src_len = strlen (src);
133 if (src_len >= dst_size)
134 memcpy (dst, src, dst_size);
137 memcpy (dst, src, src_len);
138 memset (&dst[src_len], ' ', dst_size - src_len);
142 /* Copies string SRC to buffer DST, of size DST_SIZE bytes.
143 DST is truncated to DST_SIZE bytes or padded on the left with
146 buf_copy_str_lpad (char *dst, size_t dst_size, const char *src)
148 size_t src_len = strlen (src);
149 if (src_len >= dst_size)
150 memcpy (dst, src, dst_size);
153 size_t pad_cnt = dst_size - src_len;
154 memset (&dst[0], ' ', pad_cnt);
155 memcpy (dst + pad_cnt, src, src_len);
159 /* Copies buffer SRC, of SRC_SIZE bytes, to DST, of DST_SIZE bytes.
160 DST is truncated to DST_SIZE bytes or padded on the right with
163 buf_copy_rpad (char *dst, size_t dst_size,
164 const char *src, size_t src_size)
166 if (src_size >= dst_size)
167 memmove (dst, src, dst_size);
170 memmove (dst, src, src_size);
171 memset (&dst[src_size], ' ', dst_size - src_size);
175 /* Copies string SRC to string DST, which is in a buffer DST_SIZE
177 Truncates DST to DST_SIZE - 1 characters or right-pads with
178 spaces to DST_SIZE - 1 characters if necessary. */
180 str_copy_rpad (char *dst, size_t dst_size, const char *src)
182 size_t src_len = strlen (src);
183 if (src_len < dst_size - 1)
185 memcpy (dst, src, src_len);
186 memset (&dst[src_len], ' ', dst_size - 1 - src_len);
189 memcpy (dst, src, dst_size - 1);
190 dst[dst_size - 1] = 0;
193 /* Copies SRC to DST, which is in a buffer DST_SIZE bytes long.
194 Truncates DST to DST_SIZE - 1 characters, if necessary. */
196 str_copy_trunc (char *dst, size_t dst_size, const char *src)
198 size_t src_len = strlen (src);
199 assert (dst_size > 0);
200 if (src_len + 1 < dst_size)
201 memcpy (dst, src, src_len + 1);
204 memcpy (dst, src, dst_size - 1);
205 dst[dst_size - 1] = '\0';
209 /* Copies buffer SRC, of SRC_LEN bytes,
210 to DST, which is in a buffer DST_SIZE bytes long.
211 Truncates DST to DST_SIZE - 1 characters, if necessary. */
213 str_copy_buf_trunc (char *dst, size_t dst_size,
214 const char *src, size_t src_size)
217 assert (dst_size > 0);
219 dst_len = src_size < dst_size ? src_size : dst_size - 1;
220 memcpy (dst, src, dst_len);
224 /* Converts each character in S to uppercase. */
226 str_uppercase (char *s)
228 for (; *s != '\0'; s++)
229 *s = toupper ((unsigned char) *s);
232 /* Converts each character in S to lowercase. */
234 str_lowercase (char *s)
236 for (; *s != '\0'; s++)
237 *s = tolower ((unsigned char) *s);
240 /* Initializes ST with initial contents S. */
242 ds_create (struct string *st, const char *s)
244 st->length = strlen (s);
245 st->capacity = MAX (8, st->length * 2);
246 st->string = xmalloc (st->capacity + 1);
247 strcpy (st->string, s);
250 /* Initializes ST as an empty string. */
252 ds_init (struct string *st)
261 ds_destroy (struct string *st)
272 /* Swaps the contents of strings A and B. */
274 ds_swap (struct string *a, struct string *b)
276 struct string tmp = *a;
281 /* Initializes DST with the CNT characters from SRC starting at
284 ds_init_substring (struct string *dst,
285 const struct string *src, size_t idx, size_t cnt)
289 ds_assign_substring (dst, src, idx, cnt);
292 /* Copies SRC into DST.
293 DST and SRC may be the same string. */
295 ds_assign_string (struct string *dst, const struct string *src)
297 ds_assign_buffer (dst, ds_data (src), ds_length (src));
300 /* Replaces DST by CNT characters from SRC starting at position
302 DST and SRC may be the same string. */
304 ds_assign_substring (struct string *dst,
305 const struct string *src, size_t idx, size_t cnt)
307 if (idx < src->length)
308 ds_assign_buffer (dst, src->string + idx, MIN (cnt, src->length - idx));
313 /* Replaces DST by the LENGTH characters in SRC.
314 SRC may be a substring within DST. */
316 ds_assign_buffer (struct string *dst, const char *src, size_t length)
318 dst->length = length;
319 ds_extend (dst, length);
320 memmove (dst->string, src, length);
323 /* Replaces DST by null-terminated string SRC. SRC may overlap
326 ds_assign_c_str (struct string *dst, const char *src)
328 ds_assign_buffer (dst, src, strlen (src));
331 /* Truncates ST to zero length. */
333 ds_clear (struct string *st)
338 /* Ensures that ST can hold at least MIN_CAPACITY characters plus a null
341 ds_extend (struct string *st, size_t min_capacity)
343 if (min_capacity > st->capacity)
346 if (st->capacity < min_capacity)
347 st->capacity = 2 * min_capacity;
349 st->string = xrealloc (st->string, st->capacity + 1);
353 /* Shrink ST to the minimum capacity need to contain its content. */
355 ds_shrink (struct string *st)
357 if (st->capacity != st->length)
359 st->capacity = st->length;
360 st->string = xrealloc (st->string, st->capacity + 1);
364 /* Truncates ST to at most LENGTH characters long. */
366 ds_truncate (struct string *st, size_t length)
368 if (st->length > length)
372 /* Pad ST on the right with copies of PAD until ST is at least
373 LENGTH characters in size. If ST is initially LENGTH
374 characters or longer, this is a no-op. */
376 ds_rpad (struct string *st, size_t length, char pad)
378 if (length > st->length)
379 ds_putc_multiple (st, pad, length - st->length);
382 /* Removes trailing spaces from ST.
383 Returns number of spaces removed. */
385 ds_rtrim_spaces (struct string *st)
388 while (isspace (ds_last (st)))
396 /* Removes leading spaces from ST.
397 Returns number of spaces removed. */
399 ds_ltrim_spaces (struct string *st)
402 while (isspace (ds_at (st, cnt)))
405 ds_assign_substring (st, st, cnt, SIZE_MAX);
409 /* Trims leading and trailing spaces from ST. */
411 ds_trim_spaces (struct string *st)
413 ds_rtrim_spaces (st);
414 ds_ltrim_spaces (st);
417 /* If the last character in ST is C, removes it and returns true.
418 Otherwise, returns false without modifying ST. */
420 ds_chomp (struct string *st, char c_)
422 unsigned char c = c_;
423 if (ds_last (st) == c)
432 /* Divides ST into tokens separated by any of the DELIMITERS.
433 Each call replaces TOKEN by the next token in ST, or by an
434 empty string if no tokens remain. Returns true if a token was
435 obtained, false otherwise.
437 Before the first call, initialize *SAVE_IDX to 0. Do not
438 modify *SAVE_IDX between calls.
440 ST divides into exactly one more tokens than it contains
441 delimiters. That is, a delimiter at the start or end of ST or
442 a pair of adjacent delimiters yields an empty token, and the
443 empty string contains a single token. */
445 ds_separate (const struct string *st, struct string *token,
446 const char *delimiters, size_t *save_idx)
448 if (*save_idx <= ds_length (st))
450 size_t length = ds_cspan (st, *save_idx, delimiters);
451 ds_assign_substring (token, st, *save_idx, length);
452 *save_idx += length + 1;
459 /* Divides ST into tokens separated by any of the DELIMITERS,
460 merging adjacent delimiters so that the empty string is never
461 produced as a token. Each call replaces TOKEN by the next
462 token in ST, or by an empty string if no tokens remain.
463 Returns true if a token was obtained, false otherwise.
465 Before the first call, initialize *SAVE_IDX to 0. Do not
466 modify *SAVE_IDX between calls. */
468 ds_tokenize (const struct string *st, struct string *token,
469 const char *delimiters, size_t *save_idx)
471 size_t start = *save_idx + ds_span (st, *save_idx, delimiters);
472 size_t length = ds_cspan (st, start, delimiters);
473 ds_assign_substring (token, st, start, length);
474 *save_idx = start + length;
478 /* Returns true if ST is empty, false otherwise. */
480 ds_is_empty (const struct string *st)
482 return st->length == 0;
485 /* Returns the length of ST. */
487 ds_length (const struct string *st)
492 /* Returns the value of ST as a null-terminated string. */
494 ds_c_str (const struct string *st_)
496 struct string *st = (struct string *) st_;
497 if (st->string == NULL)
499 st->string[st->length] = '\0';
503 /* Returns the string data inside ST. */
505 ds_data (const struct string *st)
510 /* Returns a pointer to the null terminator ST.
511 This might not be an actual null character unless ds_c_str() has
512 been called since the last modification to ST. */
514 ds_end (const struct string *st)
516 return st->string + st->length;
519 /* Returns the allocation size of ST. */
521 ds_capacity (const struct string *st)
526 /* Returns the character in position IDX in ST, as a value in the
527 range of unsigned char. Returns EOF if IDX is out of the
528 range of indexes for ST. */
530 ds_at (const struct string *st, size_t idx)
532 return idx < st->length ? (unsigned char) st->string[idx] : EOF;
535 /* Returns the first character in ST as a value in the range of
536 unsigned char. Returns EOF if ST is the empty string. */
538 ds_first (const struct string *st)
540 return ds_at (st, 0);
543 /* Returns the last character in ST as a value in the range of
544 unsigned char. Returns EOF if ST is the empty string. */
546 ds_last (const struct string *st)
548 return st->length > 0 ? (unsigned char) st->string[st->length - 1] : EOF;
551 /* Returns the number of consecutive characters starting at OFS
552 in ST that are in SKIP_SET. (The null terminator is not
553 considered to be part of SKIP_SET.) */
555 ds_span (const struct string *st, size_t ofs, const char skip_set[])
558 for (i = ofs; i < st->length; i++)
560 int c = st->string[i];
561 if (strchr (skip_set, c) == NULL || c == '\0')
567 /* Returns the number of consecutive characters starting at OFS
568 in ST that are not in STOP_SET. (The null terminator is not
569 considered to be part of STOP_SET.) */
571 ds_cspan (const struct string *st, size_t ofs, const char stop_set[])
574 for (i = ofs; i < st->length; i++)
576 int c = st->string[i];
577 if (strchr (stop_set, c) != NULL)
583 /* Appends to ST a newline-terminated line read from STREAM.
584 Newline is the last character of ST on return, unless an I/O error
585 or end of file is encountered after reading some characters.
586 Returns true if a line is successfully read, false if no characters at
587 all were read before an I/O error or end of file was
590 ds_gets (struct string *st, FILE *stream)
610 /* Removes a comment introduced by `#' from ST,
611 ignoring occurrences inside quoted strings. */
613 remove_comment (struct string *st)
618 for (cp = ds_c_str (st); cp < ds_end (st); cp++)
623 else if (*cp == '\\')
626 else if (*cp == '\'' || *cp == '"')
630 ds_truncate (st, cp - ds_c_str (st));
635 /* Reads a line from STREAM into ST, then preprocesses as follows:
637 - Splices lines terminated with `\'.
639 - Deletes comments introduced by `#' outside of single or double
642 - Deletes trailing white space.
644 Returns true if a line was successfully read, false on
645 failure. If LINE_NUMBER is non-null, then *LINE_NUMBER is
646 incremented by the number of lines read. */
648 ds_get_config_line (FILE *stream, struct string *st, int *line_number)
653 if (!ds_gets (st, stream))
656 ds_rtrim_spaces (st);
658 while (ds_chomp (st, '\\'));
664 /* Concatenates S onto ST. */
666 ds_puts (struct string *st, const char *s)
673 ds_extend (st, st->length + s_len);
674 strcpy (st->string + st->length, s);
678 /* Concatenates LEN characters from BUF onto ST. */
680 ds_concat (struct string *st, const char *buf, size_t len)
682 ds_extend (st, st->length + len);
683 memcpy (st->string + st->length, buf, len);
687 /* Returns ds_end(ST) and THEN increases the length by INCR. */
689 ds_append_uninit(struct string *st, size_t incr)
693 ds_extend(st, ds_length(st) + incr);
702 /* Formats FORMAT as a printf string and appends the result to ST. */
704 ds_printf (struct string *st, const char *format, ...)
708 va_start (args, format);
709 ds_vprintf(st, format, args);
713 /* Formats FORMAT as a printf string and appends the result to ST. */
715 ds_vprintf (struct string *st, const char *format, va_list args_)
720 va_copy (args, args_);
721 avail = st->string != NULL ? st->capacity - st->length + 1 : 0;
722 needed = vsnprintf (st->string + st->length, avail, format, args);
727 ds_extend (st, st->length + needed);
729 va_copy (args, args_);
730 vsprintf (st->string + st->length, format, args);
735 /* Some old libc's returned -1 when the destination string
739 ds_extend (st, (st->capacity + 1) * 2);
740 avail = st->capacity - st->length + 1;
742 va_copy (args, args_);
743 needed = vsnprintf (st->string + st->length, avail, format, args);
748 st->length += needed;
751 /* Appends character CH to ST. */
753 ds_putc (struct string *st, int ch)
755 if (st->length >= st->capacity)
756 ds_extend (st, st->length + 1);
757 st->string[st->length++] = ch;
760 /* Appends CNT copies of character CH to ST. */
762 ds_putc_multiple (struct string *st, int ch, size_t cnt)
764 ds_extend (st, st->length + cnt);
765 memset (&st->string[st->length], ch, cnt);
770 /* Lengthed strings. */
772 /* Creates a new lengthed string LS with contents as a copy of
775 ls_create (struct fixed_string *ls, const char *s)
777 ls->length = strlen (s);
778 ls->string = xmalloc (ls->length + 1);
779 memcpy (ls->string, s, ls->length + 1);
782 /* Creates a new lengthed string LS with contents as a copy of
783 BUFFER with length LEN. */
785 ls_create_buffer (struct fixed_string *ls,
786 const char *buffer, size_t len)
789 ls->string = xmalloc (len + 1);
790 memcpy (ls->string, buffer, len);
791 ls->string[len] = '\0';
794 /* Sets the fields of LS to the specified values. */
796 ls_init (struct fixed_string *ls, const char *string, size_t length)
798 ls->string = (char *) string;
802 /* Copies the fields of SRC to DST. */
804 ls_shallow_copy (struct fixed_string *dst, const struct fixed_string *src)
809 /* Frees the memory backing LS. */
811 ls_destroy (struct fixed_string *ls)
816 /* Sets LS to a null pointer value. */
818 ls_null (struct fixed_string *ls)
823 /* Returns nonzero only if LS has a null pointer value. */
825 ls_null_p (const struct fixed_string *ls)
827 return ls->string == NULL;
830 /* Returns nonzero only if LS is a null pointer or has length 0. */
832 ls_empty_p (const struct fixed_string *ls)
834 return ls->string == NULL || ls->length == 0;
837 /* Returns the length of LS, which must not be null. */
839 ls_length (const struct fixed_string *ls)
844 /* Returns a pointer to the character string in LS. */
846 ls_c_str (const struct fixed_string *ls)
848 return (char *) ls->string;
851 /* Returns a pointer to the null terminator of the character string in
854 ls_end (const struct fixed_string *ls)
856 return (char *) (ls->string + ls->length);