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., 59 Temple Place - Suite 330, 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 mm_reverse (void *p, size_t nbytes)
82 unsigned 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 mm_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 A of length A_LEN to B of length B_LEN. The shorter
108 string is considered to be padded with spaces to the length of
111 st_compare_pad (const char *a, size_t a_len, const char *b, size_t b_len)
116 min_len = a_len < b_len ? a_len : b_len;
117 result = memcmp (a, b, min_len);
126 for (idx = min_len; idx < b_len; idx++)
128 return ' ' > b[idx] ? 1 : -1;
132 for (idx = min_len; idx < a_len; idx++)
134 return a[idx] > ' ' ? 1 : -1;
140 /* Copies SRC to DEST, truncating to N characters or right-padding
141 with spaces to N characters as necessary. Does not append a null
142 character. SRC must be null-terminated. */
144 st_bare_pad_copy (char *dest, const char *src, size_t n)
150 memcpy (dest, src, n);
153 memcpy (dest, src, len);
154 memset (&dest[len], ' ', n - len);
158 /* Copies SRC to DEST, truncating SRC to N characters or right-padding
159 with spaces to N characters if necessary. Does not append a null
160 character. SRC must be LEN characters long but does not need to be
163 st_bare_pad_len_copy (char *dest, const char *src, size_t n, size_t len)
166 memmove (dest, src, n);
169 memmove (dest, src, len);
170 memset (&dest[len], ' ', n - len);
174 /* Copies SRC to DEST, truncating SRC to N-1 characters or
175 right-padding with spaces to N-1 characters if necessary. Always
176 appends a null character. */
178 st_pad_copy (char *dest, const char *src, size_t n)
185 else if (len < n - 1)
187 memcpy (dest, src, len);
188 memset (&dest[len], ' ', n - 1 - len);
193 memcpy (dest, src, n - 1);
198 /* Initializes ST with initial contents S. */
200 ds_create (struct string *st, const char *s)
202 st->length = strlen (s);
203 st->capacity = 8 + st->length * 2;
204 st->string = xmalloc (st->capacity + 1);
205 strcpy (st->string, s);
208 /* Initializes ST, making room for at least CAPACITY characters. */
210 ds_init (struct string *st, size_t capacity)
214 st->capacity = capacity;
217 st->string = xmalloc (st->capacity + 1);
220 /* Replaces the contents of ST with STRING. STRING may overlap with
223 ds_replace (struct string *st, const char *string)
225 size_t new_length = strlen (string);
226 if (new_length > st->capacity)
228 /* The new length is longer than the allocated length, so
229 there can be no overlap. */
231 ds_concat (st, string, new_length);
235 /* Overlap is possible, but the new string will fit in the
236 allocated space, so we can just copy data. */
237 st->length = new_length;
238 memmove (st->string, string, st->length);
244 ds_destroy (struct string *st)
249 /* Truncates ST to zero length. */
251 ds_clear (struct string *st)
256 /* Pad ST on the right with copies of PAD until ST is at least
257 LENGTH characters in size. If ST is initially LENGTH
258 characters or longer, this is a no-op. */
260 ds_rpad (struct string *st, size_t length, char pad)
263 if (st->length < length)
265 if (st->capacity < length)
266 ds_extend (st, length);
267 memset (&st->string[st->length], pad, length - st->length);
272 /* Ensures that ST can hold at least MIN_CAPACITY characters plus a null
275 ds_extend (struct string *st, size_t min_capacity)
277 if (min_capacity > st->capacity)
280 if (st->capacity < min_capacity)
281 st->capacity = min_capacity * 2;
283 st->string = xrealloc (st->string, st->capacity + 1);
287 /* Shrink ST to the minimum capacity need to contain its content. */
289 ds_shrink (struct string *st)
291 if (st->capacity != st->length)
293 st->capacity = st->length;
294 st->string = xrealloc (st->string, st->capacity + 1);
298 /* Truncates ST to at most LENGTH characters long. */
300 ds_truncate (struct string *st, size_t length)
302 if (length >= st->length)
307 /* Returns the length of ST. */
309 ds_length (const struct string *st)
314 /* Returns the allocation size of ST. */
316 ds_capacity (const struct string *st)
321 /* Returns the value of ST as a null-terminated string. */
323 ds_c_str (const struct string *st)
325 ((char *) st->string)[st->length] = '\0';
329 /* Returns the string data inside ST. */
331 ds_data (const struct string *st)
336 /* Returns a pointer to the null terminator ST.
337 This might not be an actual null character unless ds_c_str() has
338 been called since the last modification to ST. */
340 ds_end (const struct string *st)
342 return st->string + st->length;
345 /* Concatenates S onto ST. */
347 ds_puts (struct string *st, const char *s)
354 ds_extend (st, st->length + s_len);
355 strcpy (st->string + st->length, s);
359 /* Concatenates LEN characters from BUF onto ST. */
361 ds_concat (struct string *st, const char *buf, size_t len)
363 ds_extend (st, st->length + len);
364 memcpy (st->string + st->length, buf, len);
368 void ds_vprintf (struct string *st, const char *format, va_list args);
371 /* Formats FORMAT as a printf string and appends the result to ST. */
373 ds_printf (struct string *st, const char *format, ...)
377 va_start (args, format);
378 ds_vprintf(st,format,args);
383 /* Formats FORMAT as a printf string and appends the result to ST. */
385 ds_vprintf (struct string *st, const char *format, va_list args)
387 /* Fscking glibc silently changed behavior between 2.0 and 2.1.
388 Fsck fsck fsck. Before, it returned -1 on buffer overflow. Now,
389 it returns the number of characters (not bytes) that would have
394 avail = st->capacity - st->length + 1;
395 needed = vsnprintf (st->string + st->length, avail, format, args);
400 ds_extend (st, st->length + needed);
402 vsprintf (st->string + st->length, format, args);
407 ds_extend (st, (st->capacity + 1) * 2);
408 avail = st->capacity - st->length + 1;
410 needed = vsnprintf (st->string + st->length, avail, format, args);
414 st->length += needed;
417 /* Appends character CH to ST. */
419 ds_putc (struct string *st, int ch)
421 if (st->length == st->capacity)
422 ds_extend (st, st->length + 1);
423 st->string[st->length++] = ch;
426 /* Appends to ST a newline-terminated line read from STREAM.
427 Newline is the last character of ST on return, unless an I/O error
428 or end of file is encountered after reading some characters.
429 Returns 1 if a line is successfully read, or 0 if no characters at
430 all were read before an I/O error or end of file was
433 ds_gets (struct string *st, FILE *stream)
453 /* Reads a line from STREAM into ST, then preprocesses as follows:
455 - Splices lines terminated with `\'.
457 - Deletes comments introduced by `#' outside of single or double
460 - Trailing whitespace will be deleted.
462 Increments cust_ln as appropriate.
464 Returns nonzero only if a line was successfully read. */
466 ds_get_config_line (FILE *stream, struct string *st, struct file_locator *where)
468 /* Read the first line. */
470 where->line_number++;
471 if (!ds_gets (st, stream))
474 /* Read additional lines, if any. */
477 /* Remove trailing whitespace. */
479 char *s = ds_c_str (st);
480 size_t len = ds_length (st);
482 while (len > 0 && isspace ((unsigned char) s[len - 1]))
484 ds_truncate (st, len);
487 /* Check for trailing \. Remove if found, bail otherwise. */
488 if (ds_length (st) == 0 || ds_c_str (st)[ds_length (st) - 1] != '\\')
490 ds_truncate (st, ds_length (st) - 1);
492 /* Append another line and go around again. */
494 int success = ds_gets (st, stream);
495 where->line_number++;
501 /* Find a comment and remove. */
506 for (cp = ds_c_str (st); *cp; cp++)
511 else if (*cp == '\\')
514 else if (*cp == '\'' || *cp == '"')
518 ds_truncate (st, cp - ds_c_str (st));
526 /* Lengthed strings. */
528 /* Creates a new lengthed string LS with contents as a copy of
531 ls_create (struct len_string *ls, const char *s)
533 ls->length = strlen (s);
534 ls->string = xmalloc (ls->length + 1);
535 memcpy (ls->string, s, ls->length + 1);
538 /* Creates a new lengthed string LS with contents as a copy of
539 BUFFER with length LEN. */
541 ls_create_buffer (struct len_string *ls,
542 const char *buffer, size_t len)
545 ls->string = xmalloc (len + 1);
546 memcpy (ls->string, buffer, len);
547 ls->string[len] = '\0';
550 /* Sets the fields of LS to the specified values. */
552 ls_init (struct len_string *ls, const char *string, size_t length)
554 ls->string = (char *) string;
558 /* Copies the fields of SRC to DST. */
560 ls_shallow_copy (struct len_string *dst, const struct len_string *src)
565 /* Frees the memory backing LS. */
567 ls_destroy (struct len_string *ls)
572 /* Sets LS to a null pointer value. */
574 ls_null (struct len_string *ls)
579 /* Returns nonzero only if LS has a null pointer value. */
581 ls_null_p (const struct len_string *ls)
583 return ls->string == NULL;
586 /* Returns nonzero only if LS is a null pointer or has length 0. */
588 ls_empty_p (const struct len_string *ls)
590 return ls->string == NULL || ls->length == 0;
593 /* Returns the length of LS, which must not be null. */
595 ls_length (const struct len_string *ls)
600 /* Returns a pointer to the character string in LS. */
602 ls_c_str (const struct len_string *ls)
604 return (char *) ls->string;
607 /* Returns a pointer to the null terminator of the character string in
610 ls_end (const struct len_string *ls)
612 return (char *) (ls->string + ls->length);