7816acf2ec78fb4ba290f3d42b2230b10ae4d373
[pspp-builds.git] / src / str.h
1 /* PSPP - computes sample statistics.
2    Copyright (C) 1997-9, 2000 Free Software Foundation, Inc.
3    Written by Ben Pfaff <blp@gnu.org>.
4
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.
9
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.
14
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
18    02110-1301, USA. */
19
20 #if !str_h
21 #define str_h 1
22
23 /* Headers and miscellaneous. */
24
25 #include <stdarg.h>
26 #include <stdio.h>
27
28 #if STDC_HEADERS
29   #include <string.h>
30 #else
31   #ifndef HAVE_STRCHR 
32     #define strchr index
33     #define strrchr rindex
34   #endif
35
36   char *strchr (), *strrchr ();
37 #endif
38
39 #if !HAVE_STRTOK_R
40   char *strtok_r (char *, const char *, char **);
41 #endif
42
43 #if !HAVE_STPCPY
44   char *stpcpy (char *dest, const char *src);
45 #endif
46
47 #if !HAVE_STRCASECMP
48   int strcasecmp (const char *s1, const char *s2);
49 #endif
50
51 #if !HAVE_STRNCASECMP
52   int strncasecmp (const char *s1, const char *s2, size_t n);
53 #endif
54
55 #if !HAVE_MEMMEM
56   void *memmem (const void *haystack, size_t haystack_len,
57                 const void *needle, size_t needle_len);
58 #endif
59 \f
60 /* sprintf() wrapper functions for convenience. */
61
62 /* spprintf() calls sprintf() and returns the address of the null
63    terminator in the resulting string.  It should be portable the way
64    it's been implemented. */
65 #if __GNUC__
66   #if HAVE_GOOD_SPRINTF
67     #define spprintf(BUF, FORMAT, ARGS...)                      \
68             ((BUF) + sprintf ((BUF), (FORMAT) , ## ARGS))
69   #else
70     #define spprintf(BUF, FORMAT, ARGS...)              \
71             ({ sprintf ((BUF), (FORMAT) , ## ARGS);     \
72                strchr ((BUF), 0); })
73   #endif
74 #else /* Not GNU C. */
75   char *spprintf (char *buf, const char *format, ...);
76 #endif /* Not GNU C. */
77
78 /* nsprintf() calls sprintf() and returns the strlen() of the
79    resulting string.  It should be portable the way it's been
80    implemented. */
81 #if __GNUC__
82   #if HAVE_GOOD_SPRINTF
83     #define nsprintf(BUF, FORMAT, ARGS...)              \
84             (sprintf ((BUF), (FORMAT) , ## ARGS))
85     #define nvsprintf(BUF, FORMAT, ARGS)                \
86             (vsprintf ((BUF), (FORMAT), (ARGS)))
87   #else /* Not good sprintf(). */
88     #define nsprintf(BUF, FORMAT, ARGS...)              \
89             ({                                          \
90               char *pbuf = BUF;                         \
91               sprintf ((pbuf), (FORMAT) , ## ARGS);     \
92               strlen (pbuf);                            \
93             })
94     #define nvsprintf(BUF, FORMAT, ARGS)                \
95             ({                                          \
96               char *pbuf = BUF;                         \
97               vsprintf ((pbuf), (FORMAT), (ARGS));      \
98               strlen (pbuf);                            \
99             })
100   #endif /* Not good sprintf(). */
101 #else /* Not GNU C. */
102   #if HAVE_GOOD_SPRINTF
103     #define nsprintf sprintf
104     #define nvsprintf vsprintf
105   #else /* Not good sprintf(). */
106     int nsprintf (char *buf, const char *format, ...);
107     int nvsprintf (char *buf, const char *format, va_list args);
108   #endif /* Not good sprintf(). */
109 #endif /* Not GNU C. */
110
111 #if !HAVE_GETLINE
112 long getline (char **lineptr, size_t *n, FILE *stream);
113 #endif
114
115 #if !HAVE_GETDELIM
116 long getdelim (char **lineptr, size_t * n, int delimiter, FILE * stream);
117 #endif
118 \f
119 /* Miscellaneous. */
120
121 void buf_reverse (char *, size_t);
122 char *buf_find_reverse (const char *, size_t, const char *, size_t);
123 int buf_compare_case (const char *, const char *, size_t);
124 int buf_compare_rpad (const char *, size_t, const char *, size_t);
125 void buf_copy_rpad (char *, size_t, const char *, size_t);
126 void buf_copy_str_lpad (char *, size_t, const char *);
127 void buf_copy_str_rpad (char *, size_t, const char *);
128
129 int str_compare_rpad (const char *, const char *);
130 void str_copy_rpad (char *, size_t, const char *);
131 void str_copy_trunc (char *, size_t, const char *);
132 void str_uppercase (char *);
133 \f
134 /* Fixed-length strings. */
135 struct fixed_string 
136   {
137     char *string;
138     size_t length;
139   };
140
141 void ls_create (struct fixed_string *, const char *);
142 void ls_create_buffer (struct fixed_string *,
143                        const char *, size_t len);
144 void ls_init (struct fixed_string *, const char *, size_t);
145 void ls_shallow_copy (struct fixed_string *, const struct fixed_string *);
146 void ls_destroy (struct fixed_string *);
147
148 void ls_null (struct fixed_string *);
149 int ls_null_p (const struct fixed_string *);
150 int ls_empty_p (const struct fixed_string *);
151
152 size_t ls_length (const struct fixed_string *);
153 char *ls_c_str (const struct fixed_string *);
154 char *ls_end (const struct fixed_string *);
155
156 #if __GNUC__ > 1
157 extern inline size_t
158 ls_length (const struct fixed_string *st)
159 {
160   return st->length;
161 }
162
163 extern inline char *
164 ls_c_str (const struct fixed_string *st)
165 {
166   return st->string;
167 }
168
169 extern inline char *
170 ls_end (const struct fixed_string *st)
171 {
172   return st->string + st->length;
173 }
174 #endif
175 \f
176 /* Variable length strings. */
177
178 struct string
179   {
180     size_t length;      /* Length, not including a null terminator. */
181     size_t capacity;    /* Allocated capacity, not including one
182                            extra byte allocated for null terminator. */
183     char *string;       /* String data, not necessarily null
184                            terminated. */
185   };
186
187 /* Constructors, destructors. */
188 void ds_create (struct string *, const char *);
189 void ds_init (struct string *, size_t);
190 void ds_destroy (struct string *);
191
192 /* Copy, shrink, extend. */
193 void ds_replace (struct string *, const char *);
194 void ds_clear (struct string *);
195 void ds_extend (struct string *, size_t);
196 void ds_shrink (struct string *);
197 void ds_truncate (struct string *, size_t);
198 void ds_rpad (struct string *, size_t length, char pad);
199
200 /* Inspectors. */
201 size_t ds_length (const struct string *);
202 char *ds_c_str (const struct string *);
203 char *ds_data (const struct string *);
204 char *ds_end (const struct string *);
205 size_t ds_capacity (const struct string *);
206
207 /* File input. */
208 struct file_locator;
209 int ds_gets (struct string *, FILE *);
210 int ds_get_config_line (FILE *, struct string *, struct file_locator *);
211
212 /* Append. */
213 void ds_putc (struct string *, int ch);
214 void ds_puts (struct string *, const char *);
215 void ds_concat (struct string *, const char *, size_t);
216 void ds_vprintf (struct string *st, const char *, va_list);
217 void ds_printf (struct string *, const char *, ...)
218      PRINTF_FORMAT (2, 3);
219
220 #if __GNUC__ > 1
221 extern inline void
222 ds_putc (struct string *st, int ch)
223 {
224   if (st->length == st->capacity)
225     ds_extend (st, st->length + 1);
226   st->string[st->length++] = ch;
227 }
228
229 extern inline size_t
230 ds_length (const struct string *st)
231 {
232   return st->length;
233 }
234
235 extern inline char *
236 ds_c_str (const struct string *st)
237 {
238   ((char *) st->string)[st->length] = '\0';
239   return st->string;
240 }
241
242 extern inline char *
243 ds_data (const struct string *st)
244 {
245   return st->string;
246 }
247
248 extern inline char *
249 ds_end (const struct string *st)
250 {
251   return st->string + st->length;
252 }
253 #endif
254
255 #endif /* str_h */