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>
35 /* Reverses the order of NBYTES bytes at address P, thus converting
36 between little- and big-endian byte orders. */
38 buf_reverse (char *p, size_t nbytes)
40 char *h = p, *t = &h[nbytes - 1];
52 /* Finds the last NEEDLE of length NEEDLE_LEN in a HAYSTACK of length
53 HAYSTACK_LEN. Returns a pointer to the needle found. */
55 buf_find_reverse (const char *haystack, size_t haystack_len,
56 const char *needle, size_t needle_len)
59 for (i = haystack_len - needle_len; i >= 0; i--)
60 if (!memcmp (needle, &haystack[i], needle_len))
61 return (char *) &haystack[i];
65 /* Compares the SIZE bytes in A to those in B, disregarding case,
66 and returns a strcmp()-type result. */
68 buf_compare_case (const char *a_, const char *b_, size_t size)
70 const unsigned char *a = (unsigned char *) a_;
71 const unsigned char *b = (unsigned char *) b_;
75 unsigned char ac = toupper (*a++);
76 unsigned char bc = toupper (*b++);
79 return ac > bc ? 1 : -1;
85 /* Compares A of length A_LEN to B of length B_LEN. The shorter
86 string is considered to be padded with spaces to the length of
89 buf_compare_rpad (const char *a, size_t a_len, const char *b, size_t b_len)
94 min_len = a_len < b_len ? a_len : b_len;
95 result = memcmp (a, b, min_len);
104 for (idx = min_len; idx < b_len; idx++)
106 return ' ' > b[idx] ? 1 : -1;
110 for (idx = min_len; idx < a_len; idx++)
112 return a[idx] > ' ' ? 1 : -1;
118 /* Compares strin A to string B. The shorter string is
119 considered to be padded with spaces to the length of the
122 str_compare_rpad (const char *a, const char *b)
124 return buf_compare_rpad (a, strlen (a), b, strlen (b));
127 /* Copies string SRC to buffer DST, of size DST_SIZE bytes.
128 DST is truncated to DST_SIZE bytes or padded on the right with
131 buf_copy_str_rpad (char *dst, size_t dst_size, const char *src)
133 size_t src_len = strlen (src);
134 if (src_len >= dst_size)
135 memcpy (dst, src, dst_size);
138 memcpy (dst, src, src_len);
139 memset (&dst[src_len], ' ', dst_size - src_len);
143 /* Copies string SRC to buffer DST, of size DST_SIZE bytes.
144 DST is truncated to DST_SIZE bytes or padded on the left with
147 buf_copy_str_lpad (char *dst, size_t dst_size, const char *src)
149 size_t src_len = strlen (src);
150 if (src_len >= dst_size)
151 memcpy (dst, src, dst_size);
154 size_t pad_cnt = dst_size - src_len;
155 memset (&dst[0], ' ', pad_cnt);
156 memcpy (dst + pad_cnt, src, src_len);
160 /* Copies buffer SRC, of SRC_SIZE bytes, to DST, of DST_SIZE bytes.
161 DST is truncated to DST_SIZE bytes or padded on the right with
164 buf_copy_rpad (char *dst, size_t dst_size,
165 const char *src, size_t src_size)
167 if (src_size >= dst_size)
168 memmove (dst, src, dst_size);
171 memmove (dst, src, src_size);
172 memset (&dst[src_size], ' ', dst_size - src_size);
176 /* Copies string SRC to string DST, which is in a buffer DST_SIZE
178 Truncates DST to DST_SIZE - 1 characters or right-pads with
179 spaces to DST_SIZE - 1 characters if necessary. */
181 str_copy_rpad (char *dst, size_t dst_size, const char *src)
183 size_t src_len = strlen (src);
184 if (src_len < dst_size - 1)
186 memcpy (dst, src, src_len);
187 memset (&dst[src_len], ' ', dst_size - 1 - src_len);
190 memcpy (dst, src, dst_size - 1);
191 dst[dst_size - 1] = 0;
194 /* Copies SRC to DST, which is in a buffer DST_SIZE bytes long.
195 Truncates DST to DST_SIZE - 1 characters, if necessary. */
197 str_copy_trunc (char *dst, size_t dst_size, const char *src)
199 size_t src_len = strlen (src);
200 assert (dst_size > 0);
201 if (src_len + 1 < dst_size)
202 memcpy (dst, src, src_len + 1);
205 memcpy (dst, src, dst_size - 1);
206 dst[dst_size - 1] = '\0';
210 /* Copies buffer SRC, of SRC_LEN bytes,
211 to DST, which is in a buffer DST_SIZE bytes long.
212 Truncates DST to DST_SIZE - 1 characters, if necessary. */
214 str_copy_buf_trunc (char *dst, size_t dst_size,
215 const char *src, size_t src_size)
218 assert (dst_size > 0);
220 dst_len = src_size < dst_size ? src_size : dst_size - 1;
221 memcpy (dst, src, dst_len);
225 /* Converts each character in S to uppercase. */
227 str_uppercase (char *s)
229 for (; *s != '\0'; s++)
230 *s = toupper ((unsigned char) *s);
233 /* Converts each character in S to lowercase. */
235 str_lowercase (char *s)
237 for (; *s != '\0'; s++)
238 *s = tolower ((unsigned char) *s);
241 /* Formats FORMAT into DST, as with sprintf(), and returns the
242 address of the terminating null written to DST. */
244 spprintf (char *dst, const char *format, ...)
249 va_start (args, format);
250 count = vsprintf (dst, format, args);
258 /* Returns an empty substring. */
268 /* Returns a substring whose contents are the given C-style
271 ss_cstr (const char *cstr)
273 return ss_buffer (cstr, strlen (cstr));
276 /* Returns a substring whose contents are the CNT characters in
279 ss_buffer (const char *buffer, size_t cnt)
282 ss.string = (char *) buffer;
287 /* Returns a substring whose contents are the CNT characters
288 starting at the (0-based) position START in SS. */
290 ss_substr (struct substring ss, size_t start, size_t cnt)
292 if (start < ss.length)
293 return ss_buffer (ss.string + start, MIN (cnt, ss.length - start));
295 return ss_buffer (ss.string + ss.length, 0);
298 /* Returns a substring whose contents are the first CNT
301 ss_head (struct substring ss, size_t cnt)
303 return ss_buffer (ss.string, MIN (cnt, ss.length));
306 /* Returns a substring whose contents are the last CNT characters
309 ss_tail (struct substring ss, size_t cnt)
312 return ss_buffer (ss.string + (ss.length - cnt), cnt);
317 /* Makes a malloc()'d copy of the contents of OLD
318 and stores it in NEW. */
320 ss_alloc_substring (struct substring *new, struct substring old)
322 new->string = xmalloc (old.length);
323 new->length = old.length;
324 memcpy (new->string, old.string, old.length);
327 /* Allocates room for a CNT-character string in NEW. */
329 ss_alloc_uninit (struct substring *new, size_t cnt)
331 new->string = xmalloc (cnt);
335 /* Frees the string that SS points to. */
337 ss_dealloc (struct substring *ss)
342 /* Truncates SS to at most CNT characters in length. */
344 ss_truncate (struct substring *ss, size_t cnt)
346 if (ss->length > cnt)
350 /* Removes trailing characters in TRIM_SET from SS.
351 Returns number of characters removed. */
353 ss_rtrim (struct substring *ss, struct substring trim_set)
356 while (cnt < ss->length
357 && ss_find_char (trim_set,
358 ss->string[ss->length - cnt - 1]) != SIZE_MAX)
364 /* Removes leading characters in TRIM_SET from SS.
365 Returns number of characters removed. */
367 ss_ltrim (struct substring *ss, struct substring trim_set)
369 size_t cnt = ss_span (*ss, trim_set);
370 ss_advance (ss, cnt);
374 /* Trims leading and trailing characters in TRIM_SET from SS. */
376 ss_trim (struct substring *ss, struct substring trim_set)
378 ss_ltrim (ss, trim_set);
379 ss_rtrim (ss, trim_set);
382 /* If the last character in SS is C, removes it and returns true.
383 Otherwise, returns false without changing the string. */
385 ss_chomp (struct substring *ss, char c)
387 if (ss_last (*ss) == c)
396 /* Divides SS into tokens separated by any of the DELIMITERS.
397 Each call replaces TOKEN by the next token in SS, or by an
398 empty string if no tokens remain. Returns true if a token was
399 obtained, false otherwise.
401 Before the first call, initialize *SAVE_IDX to 0. Do not
402 modify *SAVE_IDX between calls.
404 SS divides into exactly one more tokens than it contains
405 delimiters. That is, a delimiter at the start or end of SS or
406 a pair of adjacent delimiters yields an empty token, and the
407 empty string contains a single token. */
409 ss_separate (struct substring ss, struct substring delimiters,
410 size_t *save_idx, struct substring *token)
412 if (*save_idx <= ss_length (ss))
414 struct substring tmp = ss_substr (ss, *save_idx, SIZE_MAX);
415 size_t length = ss_cspan (tmp, delimiters);
416 *token = ss_head (tmp, length);
417 *save_idx += length + 1;
422 *token = ss_empty ();
427 /* Divides SS into tokens separated by any of the DELIMITERS,
428 merging adjacent delimiters so that the empty string is never
429 produced as a token. Each call replaces TOKEN by the next
430 token in SS, or by an empty string if no tokens remain.
431 Returns true if a token was obtained, false otherwise.
433 Before the first call, initialize *SAVE_IDX to 0. Do not
434 modify *SAVE_IDX between calls. */
436 ss_tokenize (struct substring ss, struct substring delimiters,
437 size_t *save_idx, struct substring *token)
439 ss_advance (&ss, *save_idx);
440 *save_idx += ss_ltrim (&ss, delimiters);
441 *save_idx += ss_get_chars (&ss, ss_cspan (ss, delimiters), token);
442 return ss_length (*token) > 0;
445 /* Removes the first CNT characters from SS. */
447 ss_advance (struct substring *ss, size_t cnt)
449 if (cnt > ss->length)
455 /* If the first character in SS is C, removes it and returns true.
456 Otherwise, returns false without changing the string. */
458 ss_match_char (struct substring *ss, char c)
460 if (ss_first (*ss) == c)
470 /* Removes the first character from SS and returns it.
471 If SS is empty, returns EOF without modifying SS. */
473 ss_get_char (struct substring *ss)
475 int c = ss_first (*ss);
484 /* Stores the prefix of SS up to the first DELIMITER in OUT (if
485 any). Trims those same characters from SS. DELIMITER is
486 removed from SS but not made part of OUT. Returns true if
487 DELIMITER was found (and removed), false otherwise. */
489 ss_get_until (struct substring *ss, char delimiter, struct substring *out)
491 ss_get_chars (ss, ss_cspan (*ss, ss_buffer (&delimiter, 1)), out);
492 return ss_match_char (ss, delimiter);
495 /* Stores the first CNT characters in SS in OUT (or fewer, if SS
496 is shorter than CNT characters). Trims the same characters
497 from the beginning of SS. */
499 ss_get_chars (struct substring *ss, size_t cnt, struct substring *out)
501 *out = ss_head (*ss, cnt);
502 ss_advance (ss, cnt);
506 /* Returns true if SS is empty (contains no characters),
509 ss_is_empty (struct substring ss)
511 return ss.length == 0;
514 /* Returns the number of characters in SS. */
516 ss_length (struct substring ss)
521 /* Returns a pointer to the characters in SS. */
523 ss_data (struct substring ss)
528 /* Returns a pointer just past the last character in SS. */
530 ss_end (struct substring ss)
532 return ss.string + ss.length;
535 /* Returns the character in position IDX in SS, as a value in the
536 range of unsigned char. Returns EOF if IDX is out of the
537 range of indexes for SS. */
539 ss_at (struct substring ss, size_t idx)
541 return idx < ss.length ? (unsigned char) ss.string[idx] : EOF;
544 /* Returns the first character in SS as a value in the range of
545 unsigned char. Returns EOF if SS is the empty string. */
547 ss_first (struct substring ss)
549 return ss_at (ss, 0);
552 /* Returns the last character in SS as a value in the range of
553 unsigned char. Returns EOF if SS is the empty string. */
555 ss_last (struct substring ss)
557 return ss.length > 0 ? (unsigned char) ss.string[ss.length - 1] : EOF;
560 /* Returns the number of contiguous characters at the beginning
561 of SS that are in SKIP_SET. */
563 ss_span (struct substring ss, struct substring skip_set)
566 for (i = 0; i < ss.length; i++)
567 if (ss_find_char (skip_set, ss.string[i]) == SIZE_MAX)
572 /* Returns the number of contiguous characters at the beginning
573 of SS that are not in SKIP_SET. */
575 ss_cspan (struct substring ss, struct substring stop_set)
578 for (i = 0; i < ss.length; i++)
579 if (ss_find_char (stop_set, ss.string[i]) != SIZE_MAX)
584 /* Returns the offset in SS of the first instance of C,
585 or SIZE_MAX if C does not occur in SS. */
587 ss_find_char (struct substring ss, char c)
589 const char *p = memchr (ss.string, c, ss.length);
590 return p != NULL ? p - ss.string : SIZE_MAX;
593 /* Compares A and B and returns a strcmp()-type comparison
596 ss_compare (struct substring a, struct substring b)
598 int retval = memcmp (a.string, b.string, MIN (a.length, b.length));
600 retval = a.length < b.length ? -1 : a.length > b.length;
604 /* Returns the position in SS that the character at P occupies.
605 P must point within SS or one past its end. */
607 ss_pointer_to_position (struct substring ss, const char *p)
609 size_t pos = p - ss.string;
610 assert (pos <= ss.length);
614 /* Allocates and returns a null-terminated string that contains
617 ss_xstrdup (struct substring ss)
619 char *s = xmalloc (ss.length + 1);
620 memcpy (s, ss.string, ss.length);
625 /* Initializes ST as an empty string. */
627 ds_init_empty (struct string *st)
629 st->ss = ss_empty ();
633 /* Initializes ST with initial contents S. */
635 ds_init_string (struct string *st, const struct string *s)
637 ds_init_substring (st, ds_ss (s));
640 /* Initializes ST with initial contents SS. */
642 ds_init_substring (struct string *st, struct substring ss)
644 st->capacity = MAX (8, ss.length * 2);
645 st->ss.string = xmalloc (st->capacity + 1);
646 memcpy (st->ss.string, ss.string, ss.length);
647 st->ss.length = ss.length;
650 /* Initializes ST with initial contents S. */
652 ds_init_cstr (struct string *st, const char *s)
654 ds_init_substring (st, ss_cstr (s));
659 ds_destroy (struct string *st)
663 ss_dealloc (&st->ss);
664 st->ss.string = NULL;
670 /* Swaps the contents of strings A and B. */
672 ds_swap (struct string *a, struct string *b)
674 struct string tmp = *a;
679 /* Copies SRC into DST.
680 DST and SRC may be the same string. */
682 ds_assign_string (struct string *dst, const struct string *src)
684 ds_assign_substring (dst, ds_ss (src));
687 /* Replaces DST by SS.
688 SS may be a substring of DST. */
690 ds_assign_substring (struct string *dst, struct substring ss)
692 dst->ss.length = ss.length;
693 ds_extend (dst, ss.length);
694 memmove (dst->ss.string, ss.string, ss.length);
697 /* Replaces DST by null-terminated string SRC. SRC may overlap
700 ds_assign_cstr (struct string *dst, const char *src)
702 ds_assign_substring (dst, ss_cstr (src));
705 /* Truncates ST to zero length. */
707 ds_clear (struct string *st)
712 /* Returns a substring that contains ST. */
714 ds_ss (const struct string *st)
719 /* Returns a substring that contains CNT characters from ST
720 starting at position START.
722 If START is greater than or equal to the length of ST, then
723 the substring will be the empty string. If START + CNT
724 exceeds the length of ST, then the substring will only be
725 ds_length(ST) - START characters long. */
727 ds_substr (const struct string *st, size_t start, size_t cnt)
729 return ss_substr (ds_ss (st), start, cnt);
732 /* Returns a substring that contains the first CNT characters in
733 ST. If CNT exceeds the length of ST, then the substring will
734 contain all of ST. */
736 ds_head (const struct string *st, size_t cnt)
738 return ss_head (ds_ss (st), cnt);
741 /* Returns a substring that contains the last CNT characters in
742 ST. If CNT exceeds the length of ST, then the substring will
743 contain all of ST. */
745 ds_tail (const struct string *st, size_t cnt)
747 return ss_tail (ds_ss (st), cnt);
750 /* Ensures that ST can hold at least MIN_CAPACITY characters plus a null
753 ds_extend (struct string *st, size_t min_capacity)
755 if (min_capacity > st->capacity)
758 if (st->capacity < min_capacity)
759 st->capacity = 2 * min_capacity;
761 st->ss.string = xrealloc (st->ss.string, st->capacity + 1);
765 /* Shrink ST to the minimum capacity need to contain its content. */
767 ds_shrink (struct string *st)
769 if (st->capacity != st->ss.length)
771 st->capacity = st->ss.length;
772 st->ss.string = xrealloc (st->ss.string, st->capacity + 1);
776 /* Truncates ST to at most LENGTH characters long. */
778 ds_truncate (struct string *st, size_t length)
780 ss_truncate (&st->ss, length);
783 /* Removes trailing characters in TRIM_SET from ST.
784 Returns number of characters removed. */
786 ds_rtrim (struct string *st, struct substring trim_set)
788 return ss_rtrim (&st->ss, trim_set);
791 /* Removes leading characters in TRIM_SET from ST.
792 Returns number of characters removed. */
794 ds_ltrim (struct string *st, struct substring trim_set)
796 size_t cnt = ds_span (st, trim_set);
798 ds_assign_substring (st, ds_substr (st, cnt, SIZE_MAX));
802 /* Trims leading and trailing characters in TRIM_SET from ST.
803 Returns number of charactesr removed. */
805 ds_trim (struct string *st, struct substring trim_set)
807 size_t cnt = ds_rtrim (st, trim_set);
808 return cnt + ds_ltrim (st, trim_set);
811 /* If the last character in ST is C, removes it and returns true.
812 Otherwise, returns false without modifying ST. */
814 ds_chomp (struct string *st, char c)
816 return ss_chomp (&st->ss, c);
819 /* Divides ST into tokens separated by any of the DELIMITERS.
820 Each call replaces TOKEN by the next token in ST, or by an
821 empty string if no tokens remain. Returns true if a token was
822 obtained, false otherwise.
824 Before the first call, initialize *SAVE_IDX to 0. Do not
825 modify *SAVE_IDX between calls.
827 ST divides into exactly one more tokens than it contains
828 delimiters. That is, a delimiter at the start or end of ST or
829 a pair of adjacent delimiters yields an empty token, and the
830 empty string contains a single token. */
832 ds_separate (const struct string *st, struct substring delimiters,
833 size_t *save_idx, struct substring *token)
835 return ss_separate (ds_ss (st), delimiters, save_idx, token);
838 /* Divides ST into tokens separated by any of the DELIMITERS,
839 merging adjacent delimiters so that the empty string is never
840 produced as a token. Each call replaces TOKEN by the next
841 token in ST, or by an empty string if no tokens remain.
842 Returns true if a token was obtained, false otherwise.
844 Before the first call, initialize *SAVE_IDX to 0. Do not
845 modify *SAVE_IDX between calls. */
847 ds_tokenize (const struct string *st, struct substring delimiters,
848 size_t *save_idx, struct substring *token)
850 return ss_tokenize (ds_ss (st), delimiters, save_idx, token);
853 /* Pad ST on the right with copies of PAD until ST is at least
854 LENGTH characters in size. If ST is initially LENGTH
855 characters or longer, this is a no-op. */
857 ds_rpad (struct string *st, size_t length, char pad)
859 if (length > st->ss.length)
860 ds_put_char_multiple (st, pad, length - st->ss.length);
863 /* Returns true if ST is empty, false otherwise. */
865 ds_is_empty (const struct string *st)
867 return ss_is_empty (st->ss);
870 /* Returns the length of ST. */
872 ds_length (const struct string *st)
874 return ss_length (ds_ss (st));
877 /* Returns the string data inside ST. */
879 ds_data (const struct string *st)
881 return ss_data (ds_ss (st));
884 /* Returns a pointer to the null terminator ST.
885 This might not be an actual null character unless ds_c_str() has
886 been called since the last modification to ST. */
888 ds_end (const struct string *st)
890 return ss_end (ds_ss (st));
893 /* Returns the character in position IDX in ST, as a value in the
894 range of unsigned char. Returns EOF if IDX is out of the
895 range of indexes for ST. */
897 ds_at (const struct string *st, size_t idx)
899 return ss_at (ds_ss (st), idx);
902 /* Returns the first character in ST as a value in the range of
903 unsigned char. Returns EOF if ST is the empty string. */
905 ds_first (const struct string *st)
907 return ss_first (ds_ss (st));
910 /* Returns the last character in ST as a value in the range of
911 unsigned char. Returns EOF if ST is the empty string. */
913 ds_last (const struct string *st)
915 return ss_last (ds_ss (st));
918 /* Returns the number of consecutive characters at the beginning
919 of ST that are in SKIP_SET. */
921 ds_span (const struct string *st, struct substring skip_set)
923 return ss_span (ds_ss (st), skip_set);
926 /* Returns the number of consecutive characters at the beginning
927 of ST that are not in STOP_SET. */
929 ds_cspan (const struct string *st, struct substring stop_set)
931 return ss_cspan (ds_ss (st), stop_set);
934 /* Returns the position of the first occurrence of character C in
935 ST at or after position OFS, or SIZE_MAX if there is no such
938 ds_find_char (const struct string *st, char c)
940 return ss_find_char (ds_ss (st), c);
943 /* Compares A and B and returns a strcmp()-type comparison
946 ds_compare (const struct string *a, const struct string *b)
948 return ss_compare (ds_ss (a), ds_ss (b));
951 /* Returns the position in ST that the character at P occupies.
952 P must point within ST or one past its end. */
954 ds_pointer_to_position (const struct string *st, const char *p)
956 return ss_pointer_to_position (ds_ss (st), p);
959 /* Allocates and returns a null-terminated string that contains
962 ds_xstrdup (const struct string *st)
964 return ss_xstrdup (ds_ss (st));
967 /* Returns the allocation size of ST. */
969 ds_capacity (const struct string *st)
974 /* Returns the value of ST as a null-terminated string. */
976 ds_cstr (const struct string *st_)
978 struct string *st = (struct string *) st_;
979 if (st->ss.string == NULL)
981 st->ss.string[st->ss.length] = '\0';
982 return st->ss.string;
985 /* Appends to ST a newline-terminated line read from STREAM.
986 Newline is the last character of ST on return, unless an I/O error
987 or end of file is encountered after reading some characters.
988 Returns true if a line is successfully read, false if no characters at
989 all were read before an I/O error or end of file was
992 ds_read_line (struct string *st, FILE *stream)
1002 ds_put_char (st, c);
1012 /* Removes a comment introduced by `#' from ST,
1013 ignoring occurrences inside quoted strings. */
1015 remove_comment (struct string *st)
1020 for (cp = ds_data (st); cp < ds_end (st); cp++)
1025 else if (*cp == '\\')
1028 else if (*cp == '\'' || *cp == '"')
1030 else if (*cp == '#')
1032 ds_truncate (st, cp - ds_cstr (st));
1037 /* Reads a line from STREAM into ST, then preprocesses as follows:
1039 - Splices lines terminated with `\'.
1041 - Deletes comments introduced by `#' outside of single or double
1044 - Deletes trailing white space.
1046 Returns true if a line was successfully read, false on
1047 failure. If LINE_NUMBER is non-null, then *LINE_NUMBER is
1048 incremented by the number of lines read. */
1050 ds_read_config_line (struct string *st, int *line_number, FILE *stream)
1055 if (!ds_read_line (st, stream))
1058 ds_rtrim (st, ss_cstr (CC_SPACES));
1060 while (ds_chomp (st, '\\'));
1062 remove_comment (st);
1066 /* Attempts to read SIZE * CNT bytes from STREAM and append them
1068 Returns number of bytes actually read. */
1070 ds_read_stream (struct string *st, size_t size, size_t cnt, FILE *stream)
1074 size_t try_bytes = xtimes (cnt, size);
1075 if (size_in_bounds_p (xsum (ds_length (st), try_bytes)))
1077 char *buffer = ds_put_uninit (st, try_bytes);
1078 size_t got_bytes = fread (buffer, size, cnt, stream);
1079 ds_truncate (st, ds_length (st) - (try_bytes - got_bytes));
1086 /* Concatenates S onto ST. */
1088 ds_put_cstr (struct string *st, const char *s)
1091 ds_put_substring (st, ss_cstr (s));
1094 /* Concatenates SS to ST. */
1096 ds_put_substring (struct string *st, struct substring ss)
1098 memcpy (ds_put_uninit (st, ss_length (ss)), ss_data (ss), ss_length (ss));
1101 /* Returns ds_end(ST) and THEN increases the length by INCR. */
1103 ds_put_uninit (struct string *st, size_t incr)
1106 ds_extend (st, ds_length (st) + incr);
1108 st->ss.length += incr;
1112 /* Formats FORMAT as a printf string and appends the result to ST. */
1114 ds_put_format (struct string *st, const char *format, ...)
1118 va_start (args, format);
1119 ds_put_vformat (st, format, args);
1123 /* Formats FORMAT as a printf string and appends the result to ST. */
1125 ds_put_vformat (struct string *st, const char *format, va_list args_)
1130 va_copy (args, args_);
1131 avail = st->ss.string != NULL ? st->capacity - st->ss.length + 1 : 0;
1132 needed = vsnprintf (st->ss.string + st->ss.length, avail, format, args);
1135 if (needed >= avail)
1137 va_copy (args, args_);
1138 vsprintf (ds_put_uninit (st, needed), format, args);
1143 /* Some old libc's returned -1 when the destination string
1145 while (needed == -1)
1147 ds_extend (st, (st->capacity + 1) * 2);
1148 avail = st->capacity - st->ss.length + 1;
1150 va_copy (args, args_);
1151 needed = vsnprintf (ds_end (st), avail, format, args);
1154 st->ss.length += needed;
1158 /* Appends character CH to ST. */
1160 ds_put_char (struct string *st, int ch)
1162 ds_put_uninit (st, 1)[0] = ch;
1165 /* Appends CNT copies of character CH to ST. */
1167 ds_put_char_multiple (struct string *st, int ch, size_t cnt)
1169 memset (ds_put_uninit (st, cnt), ch, cnt);