db961959c45205b3a63da19ca773e01c544b121f
[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_copy_buf_trunc (char *, size_t, const char *, size_t);
110 void str_uppercase (char *);
111 \f
112 /* Fixed-length strings. */
113 struct fixed_string 
114   {
115     char *string;
116     size_t length;
117   };
118
119 void ls_create (struct fixed_string *, const char *);
120 void ls_create_buffer (struct fixed_string *,
121                        const char *, size_t len);
122 void ls_init (struct fixed_string *, const char *, size_t);
123 void ls_shallow_copy (struct fixed_string *, const struct fixed_string *);
124 void ls_destroy (struct fixed_string *);
125
126 void ls_null (struct fixed_string *);
127 int ls_null_p (const struct fixed_string *);
128 int ls_empty_p (const struct fixed_string *);
129
130 size_t ls_length (const struct fixed_string *);
131 char *ls_c_str (const struct fixed_string *);
132 char *ls_end (const struct fixed_string *);
133
134 #if __GNUC__ > 1
135 extern inline size_t
136 ls_length (const struct fixed_string *st)
137 {
138   return st->length;
139 }
140
141 extern inline char *
142 ls_c_str (const struct fixed_string *st)
143 {
144   return st->string;
145 }
146
147 extern inline char *
148 ls_end (const struct fixed_string *st)
149 {
150   return st->string + st->length;
151 }
152 #endif
153 \f
154 /* Variable length strings. */
155
156 struct string
157   {
158     size_t length;      /* Length, not including a null terminator. */
159     size_t capacity;    /* Allocated capacity, not including one
160                            extra byte allocated for null terminator. */
161     char *string;       /* String data, not necessarily null
162                            terminated. */
163   };
164
165 /* Constructors, destructors. */
166 void ds_create (struct string *, const char *);
167 void ds_init (struct string *, size_t);
168 void ds_destroy (struct string *);
169
170 /* Copy, shrink, extend. */
171 void ds_replace (struct string *, const char *);
172 void ds_clear (struct string *);
173 void ds_extend (struct string *, size_t);
174 void ds_shrink (struct string *);
175 void ds_truncate (struct string *, size_t);
176 void ds_rpad (struct string *, size_t length, char pad);
177
178 /* Inspectors. */
179 size_t ds_length (const struct string *);
180 char *ds_c_str (const struct string *);
181 char *ds_data (const struct string *);
182 char *ds_end (const struct string *);
183 size_t ds_capacity (const struct string *);
184
185 /* File input. */
186 struct file_locator;
187 int ds_gets (struct string *, FILE *);
188 int ds_get_config_line (FILE *, struct string *, struct file_locator *);
189
190 /* Append. */
191 void ds_putc (struct string *, int ch);
192 void ds_puts (struct string *, const char *);
193 void ds_concat (struct string *, const char *, size_t);
194 void ds_vprintf (struct string *st, const char *, va_list);
195 void ds_printf (struct string *, const char *, ...)
196      PRINTF_FORMAT (2, 3);
197
198 #if __GNUC__ > 1
199 extern inline void
200 ds_putc (struct string *st, int ch)
201 {
202   if (st->length == st->capacity)
203     ds_extend (st, st->length + 1);
204   st->string[st->length++] = ch;
205 }
206
207 extern inline size_t
208 ds_length (const struct string *st)
209 {
210   return st->length;
211 }
212
213 extern inline char *
214 ds_c_str (const struct string *st)
215 {
216   ((char *) st->string)[st->length] = '\0';
217   return st->string;
218 }
219
220 extern inline char *
221 ds_data (const struct string *st)
222 {
223   return st->string;
224 }
225
226 extern inline char *
227 ds_end (const struct string *st)
228 {
229   return st->string + st->length;
230 }
231 #endif
232
233 #endif /* str_h */