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
29 /* Reverses the order of NBYTES bytes at address P, thus converting
30 between little- and big-endian byte orders. */
32 buf_reverse (char *p, size_t nbytes)
34 char *h = p, *t = &h[nbytes - 1];
46 /* Finds the last NEEDLE of length NEEDLE_LEN in a HAYSTACK of length
47 HAYSTACK_LEN. Returns a pointer to the needle found. */
49 buf_find_reverse (const char *haystack, size_t haystack_len,
50 const char *needle, size_t needle_len)
53 for (i = haystack_len - needle_len; i >= 0; i--)
54 if (!memcmp (needle, &haystack[i], needle_len))
55 return (char *) &haystack[i];
59 /* Compares the SIZE bytes in A to those in B, disregarding case,
60 and returns a strcmp()-type result. */
62 buf_compare_case (const char *a_, const char *b_, size_t size)
64 const unsigned char *a = (unsigned char *) a_;
65 const unsigned char *b = (unsigned char *) b_;
69 unsigned char ac = toupper (*a++);
70 unsigned char bc = toupper (*b++);
73 return ac > bc ? 1 : -1;
79 /* Compares A of length A_LEN to B of length B_LEN. The shorter
80 string is considered to be padded with spaces to the length of
83 buf_compare_rpad (const char *a, size_t a_len, const char *b, size_t b_len)
88 min_len = a_len < b_len ? a_len : b_len;
89 result = memcmp (a, b, min_len);
98 for (idx = min_len; idx < b_len; idx++)
100 return ' ' > b[idx] ? 1 : -1;
104 for (idx = min_len; idx < a_len; idx++)
106 return a[idx] > ' ' ? 1 : -1;
112 /* Compares strin A to string B. The shorter string is
113 considered to be padded with spaces to the length of the
116 str_compare_rpad (const char *a, const char *b)
118 return buf_compare_rpad (a, strlen (a), b, strlen (b));
121 /* Copies string SRC to buffer DST, of size DST_SIZE bytes.
122 DST is truncated to DST_SIZE bytes or padded on the right with
125 buf_copy_str_rpad (char *dst, size_t dst_size, const char *src)
127 size_t src_len = strlen (src);
128 if (src_len >= dst_size)
129 memcpy (dst, src, dst_size);
132 memcpy (dst, src, src_len);
133 memset (&dst[src_len], ' ', dst_size - src_len);
137 /* Copies string SRC to buffer DST, of size DST_SIZE bytes.
138 DST is truncated to DST_SIZE bytes or padded on the left with
141 buf_copy_str_lpad (char *dst, size_t dst_size, const char *src)
143 size_t src_len = strlen (src);
144 if (src_len >= dst_size)
145 memcpy (dst, src, dst_size);
148 size_t pad_cnt = dst_size - src_len;
149 memset (&dst[0], ' ', pad_cnt);
150 memcpy (dst + pad_cnt, src, src_len);
154 /* Copies buffer SRC, of SRC_SIZE bytes, to DST, of DST_SIZE bytes.
155 DST is truncated to DST_SIZE bytes or padded on the right with
158 buf_copy_rpad (char *dst, size_t dst_size,
159 const char *src, size_t src_size)
161 if (src_size >= dst_size)
162 memmove (dst, src, dst_size);
165 memmove (dst, src, src_size);
166 memset (&dst[src_size], ' ', dst_size - src_size);
170 /* Copies string SRC to string DST, which is in a buffer DST_SIZE
172 Truncates DST to DST_SIZE - 1 characters or right-pads with
173 spaces to DST_SIZE - 1 characters if necessary. */
175 str_copy_rpad (char *dst, size_t dst_size, const char *src)
177 size_t src_len = strlen (src);
178 if (src_len < dst_size - 1)
180 memcpy (dst, src, src_len);
181 memset (&dst[src_len], ' ', dst_size - 1 - src_len);
184 memcpy (dst, src, dst_size - 1);
185 dst[dst_size - 1] = 0;
188 /* Copies SRC to DST, which is in a buffer DST_SIZE bytes long.
189 Truncates DST to DST_SIZE - 1 characters, if necessary. */
191 str_copy_trunc (char *dst, size_t dst_size, const char *src)
193 size_t src_len = strlen (src);
194 assert (dst_size > 0);
195 if (src_len + 1 < dst_size)
196 memcpy (dst, src, src_len + 1);
199 memcpy (dst, src, dst_size - 1);
200 dst[dst_size - 1] = '\0';
204 /* Copies buffer SRC, of SRC_LEN bytes,
205 to DST, which is in a buffer DST_SIZE bytes long.
206 Truncates DST to DST_SIZE - 1 characters, if necessary. */
208 str_copy_buf_trunc (char *dst, size_t dst_size,
209 const char *src, size_t src_size)
212 assert (dst_size > 0);
214 dst_len = src_size < dst_size ? src_size : dst_size - 1;
215 memcpy (dst, src, dst_len);
219 /* Converts each character in S to uppercase. */
221 str_uppercase (char *s)
223 for (; *s != '\0'; s++)
224 *s = toupper ((unsigned char) *s);
227 /* Converts each character in S to lowercase. */
229 str_lowercase (char *s)
231 for (; *s != '\0'; s++)
232 *s = tolower ((unsigned char) *s);
235 /* Initializes ST with initial contents S. */
237 ds_create (struct string *st, const char *s)
239 st->length = strlen (s);
240 st->capacity = 8 + st->length * 2;
241 st->string = xmalloc (st->capacity + 1);
242 strcpy (st->string, s);
245 /* Initializes ST, making room for at least CAPACITY characters. */
247 ds_init (struct string *st, size_t capacity)
251 st->capacity = capacity;
254 st->string = xmalloc (st->capacity + 1);
257 /* Replaces the contents of ST with STRING. STRING may overlap with
260 ds_replace (struct string *st, const char *string)
262 size_t new_length = strlen (string);
263 if (new_length > st->capacity)
265 /* The new length is longer than the allocated length, so
266 there can be no overlap. */
268 ds_concat (st, string, new_length);
272 /* Overlap is possible, but the new string will fit in the
273 allocated space, so we can just copy data. */
274 st->length = new_length;
275 memmove (st->string, string, st->length);
281 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 /* Truncates ST to zero length. */
298 ds_clear (struct string *st)
303 /* Pad ST on the right with copies of PAD until ST is at least
304 LENGTH characters in size. If ST is initially LENGTH
305 characters or longer, this is a no-op. */
307 ds_rpad (struct string *st, size_t length, char pad)
310 if (st->length < length)
312 if (st->capacity < length)
313 ds_extend (st, length);
314 memset (&st->string[st->length], pad, length - st->length);
319 /* Removes trailing spaces from ST.
320 Returns number of spaces removed. */
322 ds_rtrim_spaces (struct string *st)
325 while (isspace (ds_last (st)))
333 /* If the last character in ST is C, removes it and returns true.
334 Otherwise, returns false without modifying ST. */
336 ds_chomp (struct string *st, char c_)
338 unsigned char c = c_;
339 if (ds_last (st) == c)
348 /* Ensures that ST can hold at least MIN_CAPACITY characters plus a null
351 ds_extend (struct string *st, size_t min_capacity)
353 if (min_capacity > st->capacity)
356 if (st->capacity < min_capacity)
357 st->capacity = min_capacity * 2;
359 st->string = xrealloc (st->string, st->capacity + 1);
363 /* Shrink ST to the minimum capacity need to contain its content. */
365 ds_shrink (struct string *st)
367 if (st->capacity != st->length)
369 st->capacity = st->length;
370 st->string = xrealloc (st->string, st->capacity + 1);
374 /* Truncates ST to at most LENGTH characters long. */
376 ds_truncate (struct string *st, size_t length)
378 if (length >= st->length)
383 /* Returns true if ST is empty, false otherwise. */
385 ds_is_empty (const struct string *st)
387 return st->length == 0;
390 /* Returns the length of ST. */
392 ds_length (const struct string *st)
397 /* Returns the allocation size of ST. */
399 ds_capacity (const struct string *st)
404 /* Returns the first character in ST as a value in the range of
405 unsigned char. Returns EOF if ST is the empty string. */
407 ds_first (const struct string *st)
409 return st->length > 0 ? (unsigned char) st->string[0] : EOF;
412 /* Returns the last character in ST as a value in the range of
413 unsigned char. Returns EOF if ST is the empty string. */
415 ds_last (const struct string *st)
417 return st->length > 0 ? (unsigned char) st->string[st->length - 1] : EOF;
420 /* Returns the value of ST as a null-terminated string. */
422 ds_c_str (const struct string *st)
424 ((char *) st->string)[st->length] = '\0';
428 /* Returns the string data inside ST. */
430 ds_data (const struct string *st)
435 /* Returns a pointer to the null terminator ST.
436 This might not be an actual null character unless ds_c_str() has
437 been called since the last modification to ST. */
439 ds_end (const struct string *st)
441 return st->string + st->length;
444 /* Concatenates S onto ST. */
446 ds_puts (struct string *st, const char *s)
453 ds_extend (st, st->length + s_len);
454 strcpy (st->string + st->length, s);
458 /* Concatenates LEN characters from BUF onto ST. */
460 ds_concat (struct string *st, const char *buf, size_t len)
462 ds_extend (st, st->length + len);
463 memcpy (st->string + st->length, buf, len);
467 void ds_vprintf (struct string *st, const char *format, va_list args);
470 /* Formats FORMAT as a printf string and appends the result to ST. */
472 ds_printf (struct string *st, const char *format, ...)
476 va_start (args, format);
477 ds_vprintf(st,format,args);
482 /* Formats FORMAT as a printf string and appends the result to ST. */
484 ds_vprintf (struct string *st, const char *format, va_list args)
486 /* Fscking glibc silently changed behavior between 2.0 and 2.1.
487 Fsck fsck fsck. Before, it returned -1 on buffer overflow. Now,
488 it returns the number of characters (not bytes) that would have
495 avail = st->capacity - st->length + 1;
496 needed = vsnprintf (st->string + st->length, avail, format, args);
501 ds_extend (st, st->length + needed);
503 vsprintf (st->string + st->length, format, a1);
511 ds_extend (st, (st->capacity + 1) * 2);
512 avail = st->capacity - st->length + 1;
514 needed = vsnprintf (st->string + st->length, avail, format, a2);
520 st->length += needed;
523 /* Appends character CH to ST. */
525 ds_putc (struct string *st, int ch)
527 if (st->length == st->capacity)
528 ds_extend (st, st->length + 1);
529 st->string[st->length++] = ch;
532 /* Appends to ST a newline-terminated line read from STREAM.
533 Newline is the last character of ST on return, unless an I/O error
534 or end of file is encountered after reading some characters.
535 Returns 1 if a line is successfully read, or 0 if no characters at
536 all were read before an I/O error or end of file was
539 ds_gets (struct string *st, FILE *stream)
559 /* Reads a line from STREAM into ST, then preprocesses as follows:
561 - Splices lines terminated with `\'.
563 - Deletes comments introduced by `#' outside of single or double
566 - Trailing whitespace will be deleted.
568 Increments cust_ln as appropriate.
570 Returns nonzero only if a line was successfully read. */
572 ds_get_config_line (FILE *stream, struct string *st, struct file_locator *where)
574 /* Read the first line. */
576 where->line_number++;
577 if (!ds_gets (st, stream))
580 /* Read additional lines, if any. */
583 /* Remove trailing whitespace. */
585 char *s = ds_c_str (st);
586 size_t len = ds_length (st);
588 while (len > 0 && isspace ((unsigned char) s[len - 1]))
590 ds_truncate (st, len);
593 /* Check for trailing \. Remove if found, bail otherwise. */
594 if (ds_length (st) == 0 || ds_c_str (st)[ds_length (st) - 1] != '\\')
596 ds_truncate (st, ds_length (st) - 1);
598 /* Append another line and go around again. */
600 int success = ds_gets (st, stream);
601 where->line_number++;
607 /* Find a comment and remove. */
612 for (cp = ds_c_str (st); *cp; cp++)
617 else if (*cp == '\\')
620 else if (*cp == '\'' || *cp == '"')
624 ds_truncate (st, cp - ds_c_str (st));
632 /* Lengthed strings. */
634 /* Creates a new lengthed string LS with contents as a copy of
637 ls_create (struct fixed_string *ls, const char *s)
639 ls->length = strlen (s);
640 ls->string = xmalloc (ls->length + 1);
641 memcpy (ls->string, s, ls->length + 1);
644 /* Creates a new lengthed string LS with contents as a copy of
645 BUFFER with length LEN. */
647 ls_create_buffer (struct fixed_string *ls,
648 const char *buffer, size_t len)
651 ls->string = xmalloc (len + 1);
652 memcpy (ls->string, buffer, len);
653 ls->string[len] = '\0';
656 /* Sets the fields of LS to the specified values. */
658 ls_init (struct fixed_string *ls, const char *string, size_t length)
660 ls->string = (char *) string;
664 /* Copies the fields of SRC to DST. */
666 ls_shallow_copy (struct fixed_string *dst, const struct fixed_string *src)
671 /* Frees the memory backing LS. */
673 ls_destroy (struct fixed_string *ls)
678 /* Sets LS to a null pointer value. */
680 ls_null (struct fixed_string *ls)
685 /* Returns nonzero only if LS has a null pointer value. */
687 ls_null_p (const struct fixed_string *ls)
689 return ls->string == NULL;
692 /* Returns nonzero only if LS is a null pointer or has length 0. */
694 ls_empty_p (const struct fixed_string *ls)
696 return ls->string == NULL || ls->length == 0;
699 /* Returns the length of LS, which must not be null. */
701 ls_length (const struct fixed_string *ls)
706 /* Returns a pointer to the character string in LS. */
708 ls_c_str (const struct fixed_string *ls)
710 return (char *) ls->string;
713 /* Returns a pointer to the null terminator of the character string in
716 ls_end (const struct fixed_string *ls)
718 return (char *) (ls->string + ls->length);