Adopt use of gnulib for portability.
[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 #include <string.h>
28
29 #include "memmem.h"
30 #include "snprintf.h"
31 #include "stpcpy.h"
32 #include "strcase.h"
33 #include "strftime.h"
34 #include "strstr.h"
35 #include "strtok_r.h"
36 #include "vsnprintf.h"
37
38 #ifndef HAVE_STRCHR
39 #define strchr index
40 #endif
41 #ifndef HAVE_STRRCHR
42 #define strrchr rindex
43 #endif
44 \f
45 /* sprintf() wrapper functions for convenience. */
46
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. */
50 #if __GNUC__
51   #if HAVE_GOOD_SPRINTF
52     #define spprintf(BUF, FORMAT, ARGS...)                      \
53             ((BUF) + sprintf ((BUF), (FORMAT) , ## ARGS))
54   #else
55     #define spprintf(BUF, FORMAT, ARGS...)              \
56             ({ sprintf ((BUF), (FORMAT) , ## ARGS);     \
57                strchr ((BUF), 0); })
58   #endif
59 #else /* Not GNU C. */
60   char *spprintf (char *buf, const char *format, ...);
61 #endif /* Not GNU C. */
62
63 /* nsprintf() calls sprintf() and returns the strlen() of the
64    resulting string.  It should be portable the way it's been
65    implemented. */
66 #if __GNUC__
67   #if HAVE_GOOD_SPRINTF
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...)              \
74             ({                                          \
75               char *pbuf = BUF;                         \
76               sprintf ((pbuf), (FORMAT) , ## ARGS);     \
77               strlen (pbuf);                            \
78             })
79     #define nvsprintf(BUF, FORMAT, ARGS)                \
80             ({                                          \
81               char *pbuf = BUF;                         \
82               vsprintf ((pbuf), (FORMAT), (ARGS));      \
83               strlen (pbuf);                            \
84             })
85   #endif /* Not good sprintf(). */
86 #else /* Not GNU C. */
87   #if HAVE_GOOD_SPRINTF
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. */
95 \f
96 /* Miscellaneous. */
97
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 *);
105
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 *);
110 \f
111 /* Fixed-length strings. */
112 struct fixed_string 
113   {
114     char *string;
115     size_t length;
116   };
117
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 *);
124
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 *);
128
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 *);
132
133 #if __GNUC__ > 1
134 extern inline size_t
135 ls_length (const struct fixed_string *st)
136 {
137   return st->length;
138 }
139
140 extern inline char *
141 ls_c_str (const struct fixed_string *st)
142 {
143   return st->string;
144 }
145
146 extern inline char *
147 ls_end (const struct fixed_string *st)
148 {
149   return st->string + st->length;
150 }
151 #endif
152 \f
153 /* Variable length strings. */
154
155 struct string
156   {
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
161                            terminated. */
162   };
163
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 *);
168
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);
176
177 /* Inspectors. */
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 *);
183
184 /* File input. */
185 struct file_locator;
186 int ds_gets (struct string *, FILE *);
187 int ds_get_config_line (FILE *, struct string *, struct file_locator *);
188
189 /* Append. */
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);
196
197 #if __GNUC__ > 1
198 extern inline void
199 ds_putc (struct string *st, int ch)
200 {
201   if (st->length == st->capacity)
202     ds_extend (st, st->length + 1);
203   st->string[st->length++] = ch;
204 }
205
206 extern inline size_t
207 ds_length (const struct string *st)
208 {
209   return st->length;
210 }
211
212 extern inline char *
213 ds_c_str (const struct string *st)
214 {
215   ((char *) st->string)[st->length] = '\0';
216   return st->string;
217 }
218
219 extern inline char *
220 ds_data (const struct string *st)
221 {
222   return st->string;
223 }
224
225 extern inline char *
226 ds_end (const struct string *st)
227 {
228   return st->string + st->length;
229 }
230 #endif
231
232 #endif /* str_h */