Clean up output subsystem.
[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 <assert.h>
24 #include <stdarg.h>
25 #include <stdbool.h>
26 #include <stdio.h>
27 #include <string.h>
28
29 #include "compiler.h"
30 #include "memcasecmp.h"
31 #include "memmem.h"
32 #include "snprintf.h"
33 #include "stpcpy.h"
34 #include "strcase.h"
35 #include "strftime.h"
36 #include "strstr.h"
37 #include "strtok_r.h"
38 #include "vsnprintf.h"
39 #include "xvasprintf.h"
40
41 #ifndef HAVE_STRCHR
42 #define strchr index
43 #endif
44 #ifndef HAVE_STRRCHR
45 #define strrchr rindex
46 #endif
47 \f
48 /* Miscellaneous. */
49
50 void buf_reverse (char *, size_t);
51 char *buf_find_reverse (const char *, size_t, const char *, size_t);
52 int buf_compare_case (const char *, const char *, size_t);
53 int buf_compare_rpad (const char *, size_t, const char *, size_t);
54 void buf_copy_rpad (char *, size_t, const char *, size_t);
55 void buf_copy_str_lpad (char *, size_t, const char *);
56 void buf_copy_str_rpad (char *, size_t, const char *);
57
58 int str_compare_rpad (const char *, const char *);
59 void str_copy_rpad (char *, size_t, const char *);
60 void str_copy_trunc (char *, size_t, const char *);
61 void str_copy_buf_trunc (char *, size_t, const char *, size_t);
62 void str_uppercase (char *);
63 void str_lowercase (char *);
64 \f
65 /* Fixed-length strings. */
66 struct fixed_string 
67   {
68     char *string;
69     size_t length;
70   };
71
72 void ls_create (struct fixed_string *, const char *);
73 void ls_create_buffer (struct fixed_string *,
74                        const char *, size_t len);
75 void ls_init (struct fixed_string *, const char *, size_t);
76 void ls_shallow_copy (struct fixed_string *, const struct fixed_string *);
77 void ls_destroy (struct fixed_string *);
78
79 void ls_null (struct fixed_string *);
80 int ls_null_p (const struct fixed_string *);
81 int ls_empty_p (const struct fixed_string *);
82
83 size_t ls_length (const struct fixed_string *);
84 char *ls_c_str (const struct fixed_string *);
85 char *ls_end (const struct fixed_string *);
86
87 #if __GNUC__ > 1
88 extern inline size_t
89 ls_length (const struct fixed_string *st)
90 {
91   return st->length;
92 }
93
94 extern inline char *
95 ls_c_str (const struct fixed_string *st)
96 {
97   return st->string;
98 }
99
100 extern inline char *
101 ls_end (const struct fixed_string *st)
102 {
103   return st->string + st->length;
104 }
105 #endif
106 \f
107 /* Variable length strings. */
108
109 struct string
110   {
111     char *string;       /* String data, not necessarily null terminated. */
112     size_t length;      /* Length, not including a null terminator. */
113     size_t capacity;    /* Allocated capacity, not including one
114                            extra byte allocated for null terminator. */
115   };
116
117 #define DS_INITIALIZER {NULL, 0, 0}
118
119 /* Constructors, destructors. */
120 void ds_init (struct string *, size_t);
121 void ds_init_substring (struct string *,
122                         const struct string *src, size_t start, size_t cnt);
123 void ds_create (struct string *, const char *);
124 void ds_destroy (struct string *);
125 void ds_swap (struct string *, struct string *);
126
127 /* Replacement. */
128 void ds_assign_string (struct string *, const struct string *);
129 void ds_assign_substring (struct string *,
130                           const struct string *, size_t start, size_t cnt);
131 void ds_assign_buffer (struct string *, const char *, size_t);
132 void ds_assign_c_str (struct string *, const char *);
133
134 /* Shrink, extend. */
135 void ds_clear (struct string *);
136 void ds_extend (struct string *, size_t);
137 void ds_shrink (struct string *);
138 void ds_truncate (struct string *, size_t);
139
140 /* Padding, trimming. */
141 void ds_rpad (struct string *, size_t length, char pad);
142 int ds_rtrim_spaces (struct string *);
143 int ds_ltrim_spaces (struct string *);
144 void ds_trim_spaces (struct string *);
145 bool ds_chomp (struct string *, char);
146 bool ds_separate (const struct string *src, struct string *token,
147                   const char *delimiters, size_t *save_idx);
148 bool ds_tokenize (const struct string *src, struct string *token,
149                   const char *delimiters, size_t *save_idx);
150
151 /* Inspectors. */
152 bool ds_is_empty (const struct string *);
153 size_t ds_length (const struct string *);
154 char *ds_c_str (const struct string *);
155 char *ds_data (const struct string *);
156 char *ds_end (const struct string *);
157 size_t ds_capacity (const struct string *);
158 int ds_at (const struct string *, size_t idx);
159 int ds_first (const struct string *);
160 int ds_last (const struct string *);
161 size_t ds_span (const struct string *st, size_t ofs, const char skip_set[]);
162 size_t ds_cspan (const struct string *st, size_t ofs, const char stop_set[]);
163
164 /* File input. */
165 bool ds_gets (struct string *, FILE *);
166 bool ds_get_config_line (FILE *, struct string *, int *line_number);
167
168 /* Append. */
169 void ds_putc (struct string *, int ch);
170 void ds_putc_multiple (struct string *, int ch, size_t);
171 void ds_puts (struct string *, const char *);
172 void ds_concat (struct string *, const char *, size_t);
173 void ds_vprintf (struct string *st, const char *, va_list);
174 void ds_printf (struct string *, const char *, ...)
175      PRINTF_FORMAT (2, 3);
176
177 #if __GNUC__ > 1
178 extern inline void
179 ds_putc (struct string *st, int ch)
180 {
181   if (st->length == st->capacity)
182     ds_extend (st, st->length + 1);
183   st->string[st->length++] = ch;
184 }
185
186 extern inline size_t
187 ds_length (const struct string *st)
188 {
189   return st->length;
190 }
191
192 extern inline char *
193 ds_data (const struct string *st)
194 {
195   return st->string;
196 }
197
198 extern inline char *
199 ds_end (const struct string *st)
200 {
201   return st->string + st->length;
202 }
203 #endif
204
205 #define nsprintf sprintf
206 #define nvsprintf vsprintf
207
208 /* Formats FORMAT into DST, as with sprintf(), and returns the
209    address of the terminating null written to DST. */
210 static inline char *
211 spprintf (char *dst, const char *format, ...) 
212 {
213   va_list args;
214   int count;
215
216   va_start (args, format);
217   count = nvsprintf (dst, format, args);
218   va_end (args);
219
220   return dst + count;
221 }
222
223
224
225
226 #endif /* str_h */