22adc29fa030e0420819d3d716a268bf9ee2c6d6
[pspp-builds.git] / src / libpspp / 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 #include <stdarg.h>
24 #include <stdbool.h>
25 #include <stdio.h>
26 #include <string.h>
27
28 #include "memcasecmp.h"
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 #include "xvasprintf.h"
38
39 #ifndef HAVE_STRCHR
40 #define strchr index
41 #endif
42 #ifndef HAVE_STRRCHR
43 #define strrchr rindex
44 #endif
45 \f
46 /* sprintf() wrapper functions for convenience. */
47
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. */
51 #if __GNUC__
52   #if HAVE_GOOD_SPRINTF
53     #define spprintf(BUF, FORMAT, ARGS...)                      \
54             ((BUF) + sprintf ((BUF), (FORMAT) , ## ARGS))
55   #else
56     #define spprintf(BUF, FORMAT, ARGS...)              \
57             ({ sprintf ((BUF), (FORMAT) , ## ARGS);     \
58                strchr ((BUF), 0); })
59   #endif
60 #else /* Not GNU C. */
61   char *spprintf (char *buf, const char *format, ...);
62 #endif /* Not GNU C. */
63
64 /* nsprintf() calls sprintf() and returns the strlen() of the
65    resulting string.  It should be portable the way it's been
66    implemented. */
67 #if __GNUC__
68   #if HAVE_GOOD_SPRINTF
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...)              \
75             ({                                          \
76               char *pbuf = BUF;                         \
77               sprintf ((pbuf), (FORMAT) , ## ARGS);     \
78               strlen (pbuf);                            \
79             })
80     #define nvsprintf(BUF, FORMAT, ARGS)                \
81             ({                                          \
82               char *pbuf = BUF;                         \
83               vsprintf ((pbuf), (FORMAT), (ARGS));      \
84               strlen (pbuf);                            \
85             })
86   #endif /* Not good sprintf(). */
87 #else /* Not GNU C. */
88   #if HAVE_GOOD_SPRINTF
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. */
96 \f
97 /* Miscellaneous. */
98
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 *);
106
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 *);
113 \f
114 /* Fixed-length strings. */
115 struct fixed_string 
116   {
117     char *string;
118     size_t length;
119   };
120
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 *);
127
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 *);
131
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 *);
135
136 #if __GNUC__ > 1
137 extern inline size_t
138 ls_length (const struct fixed_string *st)
139 {
140   return st->length;
141 }
142
143 extern inline char *
144 ls_c_str (const struct fixed_string *st)
145 {
146   return st->string;
147 }
148
149 extern inline char *
150 ls_end (const struct fixed_string *st)
151 {
152   return st->string + st->length;
153 }
154 #endif
155 \f
156 /* Variable length strings. */
157
158 struct string
159   {
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
164                            terminated. */
165   };
166
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 *);
171 void ds_swap (struct string *, struct string *);
172
173 /* Copy, shrink, extend. */
174 void ds_replace (struct string *, const char *);
175 void ds_clear (struct string *);
176 void ds_extend (struct string *, size_t);
177 void ds_shrink (struct string *);
178 void ds_truncate (struct string *, size_t);
179 void ds_rpad (struct string *, size_t length, char pad);
180 int ds_rtrim_spaces (struct string *);
181 bool ds_chomp (struct string *, char);
182
183 /* Inspectors. */
184 bool ds_is_empty (const struct string *);
185 size_t ds_length (const struct string *);
186 char *ds_c_str (const struct string *);
187 char *ds_data (const struct string *);
188 char *ds_end (const struct string *);
189 size_t ds_capacity (const struct string *);
190 int ds_first (const struct string *);
191 int ds_last (const struct string *);
192
193 /* File input. */
194 struct file_locator;
195 int ds_gets (struct string *, FILE *);
196 int ds_get_config_line (FILE *, struct string *, struct file_locator *);
197
198 /* Append. */
199 void ds_putc (struct string *, int ch);
200 void ds_puts (struct string *, const char *);
201 void ds_concat (struct string *, const char *, size_t);
202 void ds_vprintf (struct string *st, const char *, va_list);
203 void ds_printf (struct string *, const char *, ...)
204      PRINTF_FORMAT (2, 3);
205
206 #if __GNUC__ > 1
207 extern inline void
208 ds_putc (struct string *st, int ch)
209 {
210   if (st->length == st->capacity)
211     ds_extend (st, st->length + 1);
212   st->string[st->length++] = ch;
213 }
214
215 extern inline size_t
216 ds_length (const struct string *st)
217 {
218   return st->length;
219 }
220
221 extern inline char *
222 ds_c_str (const struct string *st)
223 {
224   ((char *) st->string)[st->length] = '\0';
225   return st->string;
226 }
227
228 extern inline char *
229 ds_data (const struct string *st)
230 {
231   return st->string;
232 }
233
234 extern inline char *
235 ds_end (const struct string *st)
236 {
237   return st->string + st->length;
238 }
239 #endif
240
241 #endif /* str_h */