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"
37 #include "xvasprintf.h"
43 #define strrchr rindex
46 /* sprintf() wrapper functions for convenience. */
48 /* spprintf() calls sprintf() and returns the address of the null
49 terminator in the resulting string. It should be portable the way
50 it's been implemented. */
53 #define spprintf(BUF, FORMAT, ARGS...) \
54 ((BUF) + sprintf ((BUF), (FORMAT) , ## ARGS))
56 #define spprintf(BUF, FORMAT, ARGS...) \
57 ({ sprintf ((BUF), (FORMAT) , ## ARGS); \
60 #else /* Not GNU C. */
61 char *spprintf (char *buf, const char *format, ...);
62 #endif /* Not GNU C. */
64 /* nsprintf() calls sprintf() and returns the strlen() of the
65 resulting string. It should be portable the way it's been
69 #define nsprintf(BUF, FORMAT, ARGS...) \
70 (sprintf ((BUF), (FORMAT) , ## ARGS))
71 #define nvsprintf(BUF, FORMAT, ARGS) \
72 (vsprintf ((BUF), (FORMAT), (ARGS)))
73 #else /* Not good sprintf(). */
74 #define nsprintf(BUF, FORMAT, ARGS...) \
77 sprintf ((pbuf), (FORMAT) , ## ARGS); \
80 #define nvsprintf(BUF, FORMAT, ARGS) \
83 vsprintf ((pbuf), (FORMAT), (ARGS)); \
86 #endif /* Not good sprintf(). */
87 #else /* Not GNU C. */
89 #define nsprintf sprintf
90 #define nvsprintf vsprintf
91 #else /* Not good sprintf(). */
92 int nsprintf (char *buf, const char *format, ...);
93 int nvsprintf (char *buf, const char *format, va_list args);
94 #endif /* Not good sprintf(). */
95 #endif /* Not GNU C. */
99 void buf_reverse (char *, size_t);
100 char *buf_find_reverse (const char *, size_t, const char *, size_t);
101 int buf_compare_case (const char *, const char *, size_t);
102 int buf_compare_rpad (const char *, size_t, const char *, size_t);
103 void buf_copy_rpad (char *, size_t, const char *, size_t);
104 void buf_copy_str_lpad (char *, size_t, const char *);
105 void buf_copy_str_rpad (char *, size_t, const char *);
107 int str_compare_rpad (const char *, const char *);
108 void str_copy_rpad (char *, size_t, const char *);
109 void str_copy_trunc (char *, size_t, const char *);
110 void str_copy_buf_trunc (char *, size_t, const char *, size_t);
111 void str_uppercase (char *);
112 void str_lowercase (char *);
114 /* Fixed-length strings. */
121 void ls_create (struct fixed_string *, const char *);
122 void ls_create_buffer (struct fixed_string *,
123 const char *, size_t len);
124 void ls_init (struct fixed_string *, const char *, size_t);
125 void ls_shallow_copy (struct fixed_string *, const struct fixed_string *);
126 void ls_destroy (struct fixed_string *);
128 void ls_null (struct fixed_string *);
129 int ls_null_p (const struct fixed_string *);
130 int ls_empty_p (const struct fixed_string *);
132 size_t ls_length (const struct fixed_string *);
133 char *ls_c_str (const struct fixed_string *);
134 char *ls_end (const struct fixed_string *);
138 ls_length (const struct fixed_string *st)
144 ls_c_str (const struct fixed_string *st)
150 ls_end (const struct fixed_string *st)
152 return st->string + st->length;
156 /* Variable length strings. */
160 size_t length; /* Length, not including a null terminator. */
161 size_t capacity; /* Allocated capacity, not including one
162 extra byte allocated for null terminator. */
163 char *string; /* String data, not necessarily null
167 /* Constructors, destructors. */
168 void ds_create (struct string *, const char *);
169 void ds_init (struct string *, size_t);
170 void ds_destroy (struct string *);
172 /* Copy, shrink, extend. */
173 void ds_replace (struct string *, const char *);
174 void ds_clear (struct string *);
175 void ds_extend (struct string *, size_t);
176 void ds_shrink (struct string *);
177 void ds_truncate (struct string *, size_t);
178 void ds_rpad (struct string *, size_t length, char pad);
181 size_t ds_length (const struct string *);
182 char *ds_c_str (const struct string *);
183 char *ds_data (const struct string *);
184 char *ds_end (const struct string *);
185 size_t ds_capacity (const struct string *);
189 int ds_gets (struct string *, FILE *);
190 int ds_get_config_line (FILE *, struct string *, struct file_locator *);
193 void ds_putc (struct string *, int ch);
194 void ds_puts (struct string *, const char *);
195 void ds_concat (struct string *, const char *, size_t);
196 void ds_vprintf (struct string *st, const char *, va_list);
197 void ds_printf (struct string *, const char *, ...)
198 PRINTF_FORMAT (2, 3);
202 ds_putc (struct string *st, int ch)
204 if (st->length == st->capacity)
205 ds_extend (st, st->length + 1);
206 st->string[st->length++] = ch;
210 ds_length (const struct string *st)
216 ds_c_str (const struct string *st)
218 ((char *) st->string)[st->length] = '\0';
223 ds_data (const struct string *st)
229 ds_end (const struct string *st)
231 return st->string + st->length;