1 /* PSPP - computes sample statistics.
2 Copyright (C) 1997-9, 2000 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
30 /* sprintf() wrapper functions for convenience. */
34 spprintf (char *buf, const char *format,...)
41 va_start (args, format);
45 vsprintf (buf, format, args);
51 return strchr (buf, 0);
54 #endif /* !__GNUC__ */
56 #if !__GNUC__ && !HAVE_GOOD_SPRINTF
58 nsprintf (char *buf, const char *format,...)
62 va_start (args, format);
63 vsprintf (buf, format, args);
70 nvsprintf (char *buf, const char *format, va_list args)
72 vsprintf (buf, format, args);
75 #endif /* Not GNU C and not good sprintf(). */
77 /* Reverses the order of NBYTES bytes at address P, thus converting
78 between little- and big-endian byte orders. */
80 buf_reverse (char *p, size_t nbytes)
82 char *h = p, *t = &h[nbytes - 1];
94 /* Finds the last NEEDLE of length NEEDLE_LEN in a HAYSTACK of length
95 HAYSTACK_LEN. Returns a pointer to the needle found. */
97 buf_find_reverse (const char *haystack, size_t haystack_len,
98 const char *needle, size_t needle_len)
101 for (i = haystack_len - needle_len; i >= 0; i--)
102 if (!memcmp (needle, &haystack[i], needle_len))
103 return (char *) &haystack[i];
107 /* Compares the SIZE bytes in A to those in B, disregarding case,
108 and returns a strcmp()-type result. */
110 buf_compare_case (const char *a_, const char *b_, size_t size)
112 const unsigned char *a = (unsigned char *) a_;
113 const unsigned char *b = (unsigned char *) b_;
117 unsigned char ac = toupper (*a++);
118 unsigned char bc = toupper (*b++);
121 return ac > bc ? 1 : -1;
127 /* Compares A of length A_LEN to B of length B_LEN. The shorter
128 string is considered to be padded with spaces to the length of
131 buf_compare_rpad (const char *a, size_t a_len, const char *b, size_t b_len)
136 min_len = a_len < b_len ? a_len : b_len;
137 result = memcmp (a, b, min_len);
146 for (idx = min_len; idx < b_len; idx++)
148 return ' ' > b[idx] ? 1 : -1;
152 for (idx = min_len; idx < a_len; idx++)
154 return a[idx] > ' ' ? 1 : -1;
160 /* Compares strin A to string B. The shorter string is
161 considered to be padded with spaces to the length of the
164 str_compare_rpad (const char *a, const char *b)
166 return buf_compare_rpad (a, strlen (a), b, strlen (b));
169 /* Copies string SRC to buffer DST, of size DST_SIZE bytes.
170 DST is truncated to DST_SIZE bytes or padded on the right with
173 buf_copy_str_rpad (char *dst, size_t dst_size, const char *src)
175 size_t src_len = strlen (src);
176 if (src_len >= dst_size)
177 memcpy (dst, src, dst_size);
180 memcpy (dst, src, src_len);
181 memset (&dst[src_len], ' ', dst_size - src_len);
185 /* Copies string SRC to buffer DST, of size DST_SIZE bytes.
186 DST is truncated to DST_SIZE bytes or padded on the left with
189 buf_copy_str_lpad (char *dst, size_t dst_size, const char *src)
191 size_t src_len = strlen (src);
192 if (src_len >= dst_size)
193 memcpy (dst, src, dst_size);
196 size_t pad_cnt = dst_size - src_len;
197 memset (&dst[0], ' ', pad_cnt);
198 memcpy (dst + pad_cnt, src, src_len);
202 /* Copies buffer SRC, of SRC_SIZE bytes, to DST, of DST_SIZE bytes.
203 DST is truncated to DST_SIZE bytes or padded on the right with
206 buf_copy_rpad (char *dst, size_t dst_size,
207 const char *src, size_t src_size)
209 if (src_size >= dst_size)
210 memmove (dst, src, dst_size);
213 memmove (dst, src, src_size);
214 memset (&dst[src_size], ' ', dst_size - src_size);
218 /* Copies string SRC to string DST, which is in a buffer DST_SIZE
220 Truncates DST to DST_SIZE - 1 characters or right-pads with
221 spaces to DST_SIZE - 1 characters if necessary. */
223 str_copy_rpad (char *dst, size_t dst_size, const char *src)
225 size_t src_len = strlen (src);
226 if (src_len < dst_size - 1)
228 memcpy (dst, src, src_len);
229 memset (&dst[src_len], ' ', dst_size - 1 - src_len);
232 memcpy (dst, src, dst_size - 1);
233 dst[dst_size - 1] = 0;
236 /* Copies SRC to DST, which is in a buffer DST_SIZE bytes long.
237 Truncates DST to DST_SIZE - 1 characters, if necessary. */
239 str_copy_trunc (char *dst, size_t dst_size, const char *src)
241 size_t src_len = strlen (src);
242 assert (dst_size > 0);
243 if (src_len + 1 < dst_size)
244 memcpy (dst, src, src_len + 1);
247 memcpy (dst, src, dst_size - 1);
248 dst[dst_size - 1] = '\0';
252 /* Copies buffer SRC, of SRC_LEN bytes,
253 to DST, which is in a buffer DST_SIZE bytes long.
254 Truncates DST to DST_SIZE - 1 characters, if necessary. */
256 str_copy_buf_trunc (char *dst, size_t dst_size,
257 const char *src, size_t src_size)
260 assert (dst_size > 0);
262 dst_len = src_size < dst_size ? src_size : dst_size - 1;
263 memcpy (dst, src, dst_len);
267 /* Converts each character in S to uppercase. */
269 str_uppercase (char *s)
271 for (; *s != '\0'; s++)
272 *s = toupper ((unsigned char) *s);
275 /* Initializes ST with initial contents S. */
277 ds_create (struct string *st, const char *s)
279 st->length = strlen (s);
280 st->capacity = 8 + st->length * 2;
281 st->string = xmalloc (st->capacity + 1);
282 strcpy (st->string, s);
285 /* Initializes ST, making room for at least CAPACITY characters. */
287 ds_init (struct string *st, size_t capacity)
291 st->capacity = capacity;
294 st->string = xmalloc (st->capacity + 1);
297 /* Replaces the contents of ST with STRING. STRING may overlap with
300 ds_replace (struct string *st, const char *string)
302 size_t new_length = strlen (string);
303 if (new_length > st->capacity)
305 /* The new length is longer than the allocated length, so
306 there can be no overlap. */
308 ds_concat (st, string, new_length);
312 /* Overlap is possible, but the new string will fit in the
313 allocated space, so we can just copy data. */
314 st->length = new_length;
315 memmove (st->string, string, st->length);
321 ds_destroy (struct string *st)
327 /* Truncates ST to zero length. */
329 ds_clear (struct string *st)
334 /* Pad ST on the right with copies of PAD until ST is at least
335 LENGTH characters in size. If ST is initially LENGTH
336 characters or longer, this is a no-op. */
338 ds_rpad (struct string *st, size_t length, char pad)
341 if (st->length < length)
343 if (st->capacity < length)
344 ds_extend (st, length);
345 memset (&st->string[st->length], pad, length - st->length);
350 /* Ensures that ST can hold at least MIN_CAPACITY characters plus a null
353 ds_extend (struct string *st, size_t min_capacity)
355 if (min_capacity > st->capacity)
358 if (st->capacity < min_capacity)
359 st->capacity = min_capacity * 2;
361 st->string = xrealloc (st->string, st->capacity + 1);
365 /* Shrink ST to the minimum capacity need to contain its content. */
367 ds_shrink (struct string *st)
369 if (st->capacity != st->length)
371 st->capacity = st->length;
372 st->string = xrealloc (st->string, st->capacity + 1);
376 /* Truncates ST to at most LENGTH characters long. */
378 ds_truncate (struct string *st, size_t length)
380 if (length >= st->length)
385 /* Returns the length of ST. */
387 ds_length (const struct string *st)
392 /* Returns the allocation size of ST. */
394 ds_capacity (const struct string *st)
399 /* Returns the value of ST as a null-terminated string. */
401 ds_c_str (const struct string *st)
403 ((char *) st->string)[st->length] = '\0';
407 /* Returns the string data inside ST. */
409 ds_data (const struct string *st)
414 /* Returns a pointer to the null terminator ST.
415 This might not be an actual null character unless ds_c_str() has
416 been called since the last modification to ST. */
418 ds_end (const struct string *st)
420 return st->string + st->length;
423 /* Concatenates S onto ST. */
425 ds_puts (struct string *st, const char *s)
432 ds_extend (st, st->length + s_len);
433 strcpy (st->string + st->length, s);
437 /* Concatenates LEN characters from BUF onto ST. */
439 ds_concat (struct string *st, const char *buf, size_t len)
441 ds_extend (st, st->length + len);
442 memcpy (st->string + st->length, buf, len);
446 void ds_vprintf (struct string *st, const char *format, va_list args);
449 /* Formats FORMAT as a printf string and appends the result to ST. */
451 ds_printf (struct string *st, const char *format, ...)
455 va_start (args, format);
456 ds_vprintf(st,format,args);
461 /* Formats FORMAT as a printf string and appends the result to ST. */
463 ds_vprintf (struct string *st, const char *format, va_list args)
465 /* Fscking glibc silently changed behavior between 2.0 and 2.1.
466 Fsck fsck fsck. Before, it returned -1 on buffer overflow. Now,
467 it returns the number of characters (not bytes) that would have
474 avail = st->capacity - st->length + 1;
475 needed = vsnprintf (st->string + st->length, avail, format, args);
480 ds_extend (st, st->length + needed);
482 vsprintf (st->string + st->length, format, a1);
490 ds_extend (st, (st->capacity + 1) * 2);
491 avail = st->capacity - st->length + 1;
493 needed = vsnprintf (st->string + st->length, avail, format, a2);
499 st->length += needed;
502 /* Appends character CH to ST. */
504 ds_putc (struct string *st, int ch)
506 if (st->length == st->capacity)
507 ds_extend (st, st->length + 1);
508 st->string[st->length++] = ch;
511 /* Appends to ST a newline-terminated line read from STREAM.
512 Newline is the last character of ST on return, unless an I/O error
513 or end of file is encountered after reading some characters.
514 Returns 1 if a line is successfully read, or 0 if no characters at
515 all were read before an I/O error or end of file was
518 ds_gets (struct string *st, FILE *stream)
538 /* Reads a line from STREAM into ST, then preprocesses as follows:
540 - Splices lines terminated with `\'.
542 - Deletes comments introduced by `#' outside of single or double
545 - Trailing whitespace will be deleted.
547 Increments cust_ln as appropriate.
549 Returns nonzero only if a line was successfully read. */
551 ds_get_config_line (FILE *stream, struct string *st, struct file_locator *where)
553 /* Read the first line. */
555 where->line_number++;
556 if (!ds_gets (st, stream))
559 /* Read additional lines, if any. */
562 /* Remove trailing whitespace. */
564 char *s = ds_c_str (st);
565 size_t len = ds_length (st);
567 while (len > 0 && isspace ((unsigned char) s[len - 1]))
569 ds_truncate (st, len);
572 /* Check for trailing \. Remove if found, bail otherwise. */
573 if (ds_length (st) == 0 || ds_c_str (st)[ds_length (st) - 1] != '\\')
575 ds_truncate (st, ds_length (st) - 1);
577 /* Append another line and go around again. */
579 int success = ds_gets (st, stream);
580 where->line_number++;
586 /* Find a comment and remove. */
591 for (cp = ds_c_str (st); *cp; cp++)
596 else if (*cp == '\\')
599 else if (*cp == '\'' || *cp == '"')
603 ds_truncate (st, cp - ds_c_str (st));
611 /* Lengthed strings. */
613 /* Creates a new lengthed string LS with contents as a copy of
616 ls_create (struct fixed_string *ls, const char *s)
618 ls->length = strlen (s);
619 ls->string = xmalloc (ls->length + 1);
620 memcpy (ls->string, s, ls->length + 1);
623 /* Creates a new lengthed string LS with contents as a copy of
624 BUFFER with length LEN. */
626 ls_create_buffer (struct fixed_string *ls,
627 const char *buffer, size_t len)
630 ls->string = xmalloc (len + 1);
631 memcpy (ls->string, buffer, len);
632 ls->string[len] = '\0';
635 /* Sets the fields of LS to the specified values. */
637 ls_init (struct fixed_string *ls, const char *string, size_t length)
639 ls->string = (char *) string;
643 /* Copies the fields of SRC to DST. */
645 ls_shallow_copy (struct fixed_string *dst, const struct fixed_string *src)
650 /* Frees the memory backing LS. */
652 ls_destroy (struct fixed_string *ls)
657 /* Sets LS to a null pointer value. */
659 ls_null (struct fixed_string *ls)
664 /* Returns nonzero only if LS has a null pointer value. */
666 ls_null_p (const struct fixed_string *ls)
668 return ls->string == NULL;
671 /* Returns nonzero only if LS is a null pointer or has length 0. */
673 ls_empty_p (const struct fixed_string *ls)
675 return ls->string == NULL || ls->length == 0;
678 /* Returns the length of LS, which must not be null. */
680 ls_length (const struct fixed_string *ls)
685 /* Returns a pointer to the character string in LS. */
687 ls_c_str (const struct fixed_string *ls)
689 return (char *) ls->string;
692 /* Returns a pointer to the null terminator of the character string in
695 ls_end (const struct fixed_string *ls)
697 return (char *) (ls->string + ls->length);