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 /* Formats FORMAT into DST, as with sprintf(), and returns the
241 address of the terminating null written to DST. */
243 spprintf (char *dst, const char *format, ...)
248 va_start (args, format);
249 count = vsprintf (dst, format, args);
255 /* Initializes ST with initial contents S. */
257 ds_create (struct string *st, const char *s)
259 st->length = strlen (s);
260 st->capacity = MAX (8, st->length * 2);
261 st->string = xmalloc (st->capacity + 1);
262 strcpy (st->string, s);
265 /* Initializes ST as an empty string. */
267 ds_init (struct string *st)
276 ds_destroy (struct string *st)
287 /* Swaps the contents of strings A and B. */
289 ds_swap (struct string *a, struct string *b)
291 struct string tmp = *a;
296 /* Initializes DST with the CNT characters from SRC starting at
299 ds_init_substring (struct string *dst,
300 const struct string *src, size_t idx, size_t cnt)
304 ds_assign_substring (dst, src, idx, cnt);
307 /* Copies SRC into DST.
308 DST and SRC may be the same string. */
310 ds_assign_string (struct string *dst, const struct string *src)
312 ds_assign_buffer (dst, ds_data (src), ds_length (src));
315 /* Replaces DST by CNT characters from SRC starting at position
317 DST and SRC may be the same string. */
319 ds_assign_substring (struct string *dst,
320 const struct string *src, size_t idx, size_t cnt)
322 if (idx < src->length)
323 ds_assign_buffer (dst, src->string + idx, MIN (cnt, src->length - idx));
328 /* Replaces DST by the LENGTH characters in SRC.
329 SRC may be a substring within DST. */
331 ds_assign_buffer (struct string *dst, const char *src, size_t length)
333 dst->length = length;
334 ds_extend (dst, length);
335 memmove (dst->string, src, length);
338 /* Replaces DST by null-terminated string SRC. SRC may overlap
341 ds_assign_c_str (struct string *dst, const char *src)
343 ds_assign_buffer (dst, src, strlen (src));
346 /* Truncates ST to zero length. */
348 ds_clear (struct string *st)
353 /* Ensures that ST can hold at least MIN_CAPACITY characters plus a null
356 ds_extend (struct string *st, size_t min_capacity)
358 if (min_capacity > st->capacity)
361 if (st->capacity < min_capacity)
362 st->capacity = 2 * min_capacity;
364 st->string = xrealloc (st->string, st->capacity + 1);
368 /* Shrink ST to the minimum capacity need to contain its content. */
370 ds_shrink (struct string *st)
372 if (st->capacity != st->length)
374 st->capacity = st->length;
375 st->string = xrealloc (st->string, st->capacity + 1);
379 /* Truncates ST to at most LENGTH characters long. */
381 ds_truncate (struct string *st, size_t length)
383 if (st->length > length)
387 /* Pad ST on the right with copies of PAD until ST is at least
388 LENGTH characters in size. If ST is initially LENGTH
389 characters or longer, this is a no-op. */
391 ds_rpad (struct string *st, size_t length, char pad)
393 if (length > st->length)
394 ds_putc_multiple (st, pad, length - st->length);
397 /* Removes trailing spaces from ST.
398 Returns number of spaces removed. */
400 ds_rtrim_spaces (struct string *st)
403 while (isspace (ds_last (st)))
411 /* Removes leading spaces from ST.
412 Returns number of spaces removed. */
414 ds_ltrim_spaces (struct string *st)
417 while (isspace (ds_at (st, cnt)))
420 ds_assign_substring (st, st, cnt, SIZE_MAX);
424 /* Trims leading and trailing spaces from ST. */
426 ds_trim_spaces (struct string *st)
428 ds_rtrim_spaces (st);
429 ds_ltrim_spaces (st);
432 /* If the last character in ST is C, removes it and returns true.
433 Otherwise, returns false without modifying ST. */
435 ds_chomp (struct string *st, char c_)
437 unsigned char c = c_;
438 if (ds_last (st) == c)
447 /* Divides ST into tokens separated by any of the DELIMITERS.
448 Each call replaces TOKEN by the next token in ST, or by an
449 empty string if no tokens remain. Returns true if a token was
450 obtained, false otherwise.
452 Before the first call, initialize *SAVE_IDX to 0. Do not
453 modify *SAVE_IDX between calls.
455 ST divides into exactly one more tokens than it contains
456 delimiters. That is, a delimiter at the start or end of ST or
457 a pair of adjacent delimiters yields an empty token, and the
458 empty string contains a single token. */
460 ds_separate (const struct string *st, struct string *token,
461 const char *delimiters, size_t *save_idx)
463 if (*save_idx <= ds_length (st))
465 size_t length = ds_cspan (st, *save_idx, delimiters);
466 ds_assign_substring (token, st, *save_idx, length);
467 *save_idx += length + 1;
474 /* Divides ST into tokens separated by any of the DELIMITERS,
475 merging adjacent delimiters so that the empty string is never
476 produced as a token. Each call replaces TOKEN by the next
477 token in ST, or by an empty string if no tokens remain.
478 Returns true if a token was obtained, false otherwise.
480 Before the first call, initialize *SAVE_IDX to 0. Do not
481 modify *SAVE_IDX between calls. */
483 ds_tokenize (const struct string *st, struct string *token,
484 const char *delimiters, size_t *save_idx)
486 size_t start = *save_idx + ds_span (st, *save_idx, delimiters);
487 size_t length = ds_cspan (st, start, delimiters);
488 ds_assign_substring (token, st, start, length);
489 *save_idx = start + length;
493 /* Returns true if ST is empty, false otherwise. */
495 ds_is_empty (const struct string *st)
497 return st->length == 0;
500 /* Returns the length of ST. */
502 ds_length (const struct string *st)
507 /* Returns the value of ST as a null-terminated string. */
509 ds_c_str (const struct string *st_)
511 struct string *st = (struct string *) st_;
512 if (st->string == NULL)
514 st->string[st->length] = '\0';
518 /* Returns the string data inside ST. */
520 ds_data (const struct string *st)
525 /* Returns a pointer to the null terminator ST.
526 This might not be an actual null character unless ds_c_str() has
527 been called since the last modification to ST. */
529 ds_end (const struct string *st)
531 return st->string + st->length;
534 /* Returns the allocation size of ST. */
536 ds_capacity (const struct string *st)
541 /* Returns the character in position IDX in ST, as a value in the
542 range of unsigned char. Returns EOF if IDX is out of the
543 range of indexes for ST. */
545 ds_at (const struct string *st, size_t idx)
547 return idx < st->length ? (unsigned char) st->string[idx] : EOF;
550 /* Returns the first character in ST as a value in the range of
551 unsigned char. Returns EOF if ST is the empty string. */
553 ds_first (const struct string *st)
555 return ds_at (st, 0);
558 /* Returns the last character in ST as a value in the range of
559 unsigned char. Returns EOF if ST is the empty string. */
561 ds_last (const struct string *st)
563 return st->length > 0 ? (unsigned char) st->string[st->length - 1] : EOF;
566 /* Returns the number of consecutive characters starting at OFS
567 in ST that are in SKIP_SET. (The null terminator is not
568 considered to be part of SKIP_SET.) */
570 ds_span (const struct string *st, size_t ofs, const char skip_set[])
573 for (i = ofs; i < st->length; i++)
575 int c = st->string[i];
576 if (strchr (skip_set, c) == NULL || c == '\0')
582 /* Returns the number of consecutive characters starting at OFS
583 in ST that are not in STOP_SET. (The null terminator is not
584 considered to be part of STOP_SET.) */
586 ds_cspan (const struct string *st, size_t ofs, const char stop_set[])
589 for (i = ofs; i < st->length; i++)
591 int c = st->string[i];
592 if (strchr (stop_set, c) != NULL)
598 /* Appends to ST a newline-terminated line read from STREAM.
599 Newline is the last character of ST on return, unless an I/O error
600 or end of file is encountered after reading some characters.
601 Returns true if a line is successfully read, false if no characters at
602 all were read before an I/O error or end of file was
605 ds_gets (struct string *st, FILE *stream)
625 /* Removes a comment introduced by `#' from ST,
626 ignoring occurrences inside quoted strings. */
628 remove_comment (struct string *st)
633 for (cp = ds_c_str (st); cp < ds_end (st); cp++)
638 else if (*cp == '\\')
641 else if (*cp == '\'' || *cp == '"')
645 ds_truncate (st, cp - ds_c_str (st));
650 /* Reads a line from STREAM into ST, then preprocesses as follows:
652 - Splices lines terminated with `\'.
654 - Deletes comments introduced by `#' outside of single or double
657 - Deletes trailing white space.
659 Returns true if a line was successfully read, false on
660 failure. If LINE_NUMBER is non-null, then *LINE_NUMBER is
661 incremented by the number of lines read. */
663 ds_get_config_line (FILE *stream, struct string *st, int *line_number)
668 if (!ds_gets (st, stream))
671 ds_rtrim_spaces (st);
673 while (ds_chomp (st, '\\'));
679 /* Concatenates S onto ST. */
681 ds_puts (struct string *st, const char *s)
688 ds_extend (st, st->length + s_len);
689 strcpy (st->string + st->length, s);
693 /* Concatenates LEN characters from BUF onto ST. */
695 ds_concat (struct string *st, const char *buf, size_t len)
697 ds_extend (st, st->length + len);
698 memcpy (st->string + st->length, buf, len);
702 /* Returns ds_end(ST) and THEN increases the length by INCR. */
704 ds_append_uninit(struct string *st, size_t incr)
708 ds_extend(st, ds_length(st) + incr);
717 /* Formats FORMAT as a printf string and appends the result to ST. */
719 ds_printf (struct string *st, const char *format, ...)
723 va_start (args, format);
724 ds_vprintf(st, format, args);
728 /* Formats FORMAT as a printf string and appends the result to ST. */
730 ds_vprintf (struct string *st, const char *format, va_list args_)
735 va_copy (args, args_);
736 avail = st->string != NULL ? st->capacity - st->length + 1 : 0;
737 needed = vsnprintf (st->string + st->length, avail, format, args);
742 ds_extend (st, st->length + needed);
744 va_copy (args, args_);
745 vsprintf (st->string + st->length, format, args);
750 /* Some old libc's returned -1 when the destination string
754 ds_extend (st, (st->capacity + 1) * 2);
755 avail = st->capacity - st->length + 1;
757 va_copy (args, args_);
758 needed = vsnprintf (st->string + st->length, avail, format, args);
763 st->length += needed;
766 /* Appends character CH to ST. */
768 ds_putc (struct string *st, int ch)
770 if (st->length >= st->capacity)
771 ds_extend (st, st->length + 1);
772 st->string[st->length++] = ch;
775 /* Appends CNT copies of character CH to ST. */
777 ds_putc_multiple (struct string *st, int ch, size_t cnt)
779 ds_extend (st, st->length + cnt);
780 memset (&st->string[st->length], ch, cnt);
785 /* Lengthed strings. */
787 /* Creates a new lengthed string LS with contents as a copy of
790 ls_create (struct fixed_string *ls, const char *s)
792 ls->length = strlen (s);
793 ls->string = xmalloc (ls->length + 1);
794 memcpy (ls->string, s, ls->length + 1);
797 /* Creates a new lengthed string LS with contents as a copy of
798 BUFFER with length LEN. */
800 ls_create_buffer (struct fixed_string *ls,
801 const char *buffer, size_t len)
804 ls->string = xmalloc (len + 1);
805 memcpy (ls->string, buffer, len);
806 ls->string[len] = '\0';
809 /* Sets the fields of LS to the specified values. */
811 ls_init (struct fixed_string *ls, const char *string, size_t length)
813 ls->string = (char *) string;
817 /* Copies the fields of SRC to DST. */
819 ls_shallow_copy (struct fixed_string *dst, const struct fixed_string *src)
824 /* Frees the memory backing LS. */
826 ls_destroy (struct fixed_string *ls)
831 /* Sets LS to a null pointer value. */
833 ls_null (struct fixed_string *ls)
838 /* Returns nonzero only if LS has a null pointer value. */
840 ls_null_p (const struct fixed_string *ls)
842 return ls->string == NULL;
845 /* Returns nonzero only if LS is a null pointer or has length 0. */
847 ls_empty_p (const struct fixed_string *ls)
849 return ls->string == NULL || ls->length == 0;
852 /* Returns the length of LS, which must not be null. */
854 ls_length (const struct fixed_string *ls)
859 /* Returns a pointer to the character string in LS. */
861 ls_c_str (const struct fixed_string *ls)
863 return (char *) ls->string;
866 /* Returns a pointer to the null terminator of the character string in
869 ls_end (const struct fixed_string *ls)
871 return (char *) (ls->string + ls->length);