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
23 /* Headers and miscellaneous. */
36 #include "vsnprintf.h"
42 #define strrchr rindex
45 /* sprintf() wrapper functions for convenience. */
47 /* spprintf() calls sprintf() and returns the address of the null
48 terminator in the resulting string. It should be portable the way
49 it's been implemented. */
52 #define spprintf(BUF, FORMAT, ARGS...) \
53 ((BUF) + sprintf ((BUF), (FORMAT) , ## ARGS))
55 #define spprintf(BUF, FORMAT, ARGS...) \
56 ({ sprintf ((BUF), (FORMAT) , ## ARGS); \
59 #else /* Not GNU C. */
60 char *spprintf (char *buf, const char *format, ...);
61 #endif /* Not GNU C. */
63 /* nsprintf() calls sprintf() and returns the strlen() of the
64 resulting string. It should be portable the way it's been
68 #define nsprintf(BUF, FORMAT, ARGS...) \
69 (sprintf ((BUF), (FORMAT) , ## ARGS))
70 #define nvsprintf(BUF, FORMAT, ARGS) \
71 (vsprintf ((BUF), (FORMAT), (ARGS)))
72 #else /* Not good sprintf(). */
73 #define nsprintf(BUF, FORMAT, ARGS...) \
76 sprintf ((pbuf), (FORMAT) , ## ARGS); \
79 #define nvsprintf(BUF, FORMAT, ARGS) \
82 vsprintf ((pbuf), (FORMAT), (ARGS)); \
85 #endif /* Not good sprintf(). */
86 #else /* Not GNU C. */
88 #define nsprintf sprintf
89 #define nvsprintf vsprintf
90 #else /* Not good sprintf(). */
91 int nsprintf (char *buf, const char *format, ...);
92 int nvsprintf (char *buf, const char *format, va_list args);
93 #endif /* Not good sprintf(). */
94 #endif /* Not GNU C. */
98 void buf_reverse (char *, size_t);
99 char *buf_find_reverse (const char *, size_t, const char *, size_t);
100 int buf_compare_case (const char *, const char *, size_t);
101 int buf_compare_rpad (const char *, size_t, const char *, size_t);
102 void buf_copy_rpad (char *, size_t, const char *, size_t);
103 void buf_copy_str_lpad (char *, size_t, const char *);
104 void buf_copy_str_rpad (char *, size_t, const char *);
106 int str_compare_rpad (const char *, const char *);
107 void str_copy_rpad (char *, size_t, const char *);
108 void str_copy_trunc (char *, size_t, const char *);
109 void str_uppercase (char *);
111 /* Fixed-length strings. */
118 void ls_create (struct fixed_string *, const char *);
119 void ls_create_buffer (struct fixed_string *,
120 const char *, size_t len);
121 void ls_init (struct fixed_string *, const char *, size_t);
122 void ls_shallow_copy (struct fixed_string *, const struct fixed_string *);
123 void ls_destroy (struct fixed_string *);
125 void ls_null (struct fixed_string *);
126 int ls_null_p (const struct fixed_string *);
127 int ls_empty_p (const struct fixed_string *);
129 size_t ls_length (const struct fixed_string *);
130 char *ls_c_str (const struct fixed_string *);
131 char *ls_end (const struct fixed_string *);
135 ls_length (const struct fixed_string *st)
141 ls_c_str (const struct fixed_string *st)
147 ls_end (const struct fixed_string *st)
149 return st->string + st->length;
153 /* Variable length strings. */
157 size_t length; /* Length, not including a null terminator. */
158 size_t capacity; /* Allocated capacity, not including one
159 extra byte allocated for null terminator. */
160 char *string; /* String data, not necessarily null
164 /* Constructors, destructors. */
165 void ds_create (struct string *, const char *);
166 void ds_init (struct string *, size_t);
167 void ds_destroy (struct string *);
169 /* Copy, shrink, extend. */
170 void ds_replace (struct string *, const char *);
171 void ds_clear (struct string *);
172 void ds_extend (struct string *, size_t);
173 void ds_shrink (struct string *);
174 void ds_truncate (struct string *, size_t);
175 void ds_rpad (struct string *, size_t length, char pad);
178 size_t ds_length (const struct string *);
179 char *ds_c_str (const struct string *);
180 char *ds_data (const struct string *);
181 char *ds_end (const struct string *);
182 size_t ds_capacity (const struct string *);
186 int ds_gets (struct string *, FILE *);
187 int ds_get_config_line (FILE *, struct string *, struct file_locator *);
190 void ds_putc (struct string *, int ch);
191 void ds_puts (struct string *, const char *);
192 void ds_concat (struct string *, const char *, size_t);
193 void ds_vprintf (struct string *st, const char *, va_list);
194 void ds_printf (struct string *, const char *, ...)
195 PRINTF_FORMAT (2, 3);
199 ds_putc (struct string *st, int ch)
201 if (st->length == st->capacity)
202 ds_extend (st, st->length + 1);
203 st->string[st->length++] = ch;
207 ds_length (const struct string *st)
213 ds_c_str (const struct string *st)
215 ((char *) st->string)[st->length] = '\0';
220 ds_data (const struct string *st)
226 ds_end (const struct string *st)
228 return st->string + st->length;