fd6e739b63d6b575f717bb4e3bdea1a6b5175a2e
[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
65 char *spprintf (char *dst, const char *format, ...);
66 \f
67 /* Common character classes for use with substring and string functions. */
68
69 #define CC_SPACES " \t\v\r\n"
70 #define CC_DIGITS "0123456789"
71 #define CC_XDIGITS "0123456789abcdefABCDEF"
72 #define CC_LETTERS "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
73 #define CC_ALNUM CC_LETTERS CC_DIGITS
74 \f
75 /* Substrings. */
76 struct substring 
77   {
78     char *string;
79     size_t length;
80   };
81
82 #define SS_EMPTY_INITIALIZER {NULL, 0}
83 #define SS_LITERAL_INITIALIZER(LITERAL) {LITERAL, (sizeof LITERAL) - 1}
84
85 /* Constructors.
86    These functions do not allocate any memory, so the substrings
87    they create should not normally be destroyed. */
88 struct substring ss_empty (void);
89 struct substring ss_cstr (const char *);
90 struct substring ss_buffer (const char *, size_t);
91 struct substring ss_substr (struct substring, size_t start, size_t);
92 struct substring ss_head (struct substring, size_t);
93 struct substring ss_tail (struct substring, size_t);
94
95 /* Constructors and destructor that allocate and deallocate
96    memory. */
97 void ss_alloc_substring (struct substring *, struct substring);
98 void ss_alloc_uninit (struct substring *, size_t);
99 void ss_dealloc (struct substring *);
100
101 /* Mutators.
102    Functions that advance the beginning of a string should not be
103    used if a substring is to be deallocated. */
104 void ss_truncate (struct substring *, size_t);
105 size_t ss_rtrim (struct substring *, struct substring trim_set);
106 size_t ss_ltrim (struct substring *, struct substring trim_set);
107 void ss_trim (struct substring *, struct substring trim_set);
108 bool ss_chomp (struct substring *, char);
109 bool ss_separate (struct substring src, struct substring delimiters,
110                   size_t *save_idx, struct substring *token);
111 bool ss_tokenize (struct substring src, struct substring delimiters,
112                   size_t *save_idx, struct substring *token);
113 void ss_advance (struct substring *, size_t);
114 bool ss_match_char (struct substring *, char);
115 int ss_get_char (struct substring *);
116 size_t ss_get_chars (struct substring *, size_t cnt, struct substring *);
117 bool ss_get_until (struct substring *, char delimiter, struct substring *);
118
119 /* Inspectors. */
120 bool ss_is_empty (struct substring);
121 size_t ss_length (struct substring);
122 char *ss_data (struct substring);
123 char *ss_end (struct substring);
124 int ss_at (struct substring, size_t idx);
125 int ss_first (struct substring);
126 int ss_last (struct substring);
127 size_t ss_span (struct substring, struct substring skip_set);
128 size_t ss_cspan (struct substring, struct substring stop_set);
129 size_t ss_find_char (struct substring, char);
130 int ss_compare (struct substring, struct substring);
131 size_t ss_pointer_to_position (struct substring, const char *);
132 char *ss_xstrdup (struct substring);
133 \f
134 /* Variable length strings. */
135
136 struct string
137   {
138     struct substring ss;
139
140     size_t capacity;    /* Allocated capacity, not including one
141                            extra byte allocated for null terminator. */
142   };
143
144 #define DS_EMPTY_INITIALIZER {SS_EMPTY_INITIALIZER, 0}
145
146 /* Constructors, destructors. */
147 void ds_init_empty (struct string *);
148 void ds_init_string (struct string *, const struct string *);
149 void ds_init_substring (struct string *, struct substring);
150 void ds_init_cstr (struct string *, const char *);
151 void ds_destroy (struct string *);
152 void ds_swap (struct string *, struct string *);
153
154 /* Pools. */
155 struct pool;
156 void ds_register_pool (struct string *, struct pool *);
157 void ds_unregister_pool (struct string *, struct pool *);
158
159 /* Replacement. */
160 void ds_assign_string (struct string *, const struct string *);
161 void ds_assign_substring (struct string *, struct substring);
162 void ds_assign_cstr (struct string *, const char *);
163
164 /* Shrink, extend. */
165 void ds_clear (struct string *);
166 void ds_extend (struct string *, size_t);
167 void ds_shrink (struct string *);
168 void ds_truncate (struct string *, size_t);
169
170 /* Padding, trimming. */
171 size_t ds_rtrim (struct string *, struct substring trim_set);
172 size_t ds_ltrim (struct string *, struct substring trim_set);
173 size_t ds_trim (struct string *, struct substring trim_set);
174 bool ds_chomp (struct string *, char);
175 bool ds_separate (const struct string *src, struct substring delimiters,
176                   size_t *save_idx, struct substring *token);
177 bool ds_tokenize (const struct string *src, struct substring delimiters,
178                   size_t *save_idx, struct substring *token);
179 void ds_rpad (struct string *, size_t length, char pad);
180 void ds_set_length (struct string *, size_t new_length, char pad);
181
182 /* Extracting substrings. */
183 struct substring ds_ss (const struct string *);
184 struct substring ds_substr (const struct string *, size_t start, size_t);
185 struct substring ds_head (const struct string *, size_t);
186 struct substring ds_tail (const struct string *, size_t);
187
188 /* Inspectors. */
189 bool ds_is_empty (const struct string *);
190 size_t ds_length (const struct string *);
191 char *ds_data (const struct string *);
192 char *ds_end (const struct string *);
193 int ds_at (const struct string *, size_t idx);
194 int ds_first (const struct string *);
195 int ds_last (const struct string *);
196 size_t ds_span (const struct string *, struct substring skip_set);
197 size_t ds_cspan (const struct string *, struct substring stop_set);
198 size_t ds_find_char (const struct string *, char);
199 int ds_compare (const struct string *, const struct string *);
200 size_t ds_pointer_to_position (const struct string *, const char *);
201 char *ds_xstrdup (const struct string *);
202
203 size_t ds_capacity (const struct string *);
204 char *ds_cstr (const struct string *);
205
206 /* File input. */
207 bool ds_read_line (struct string *, FILE *);
208 bool ds_read_config_line (struct string *, int *line_number, FILE *);
209 size_t ds_read_stream (struct string *, size_t size, size_t cnt, FILE *stream);
210
211 /* Append. */
212 void ds_put_char (struct string *, int ch);
213 void ds_put_char_multiple (struct string *, int ch, size_t);
214 void ds_put_cstr (struct string *, const char *);
215 void ds_put_substring (struct string *, struct substring);
216 void ds_put_vformat (struct string *st, const char *, va_list)
217      PRINTF_FORMAT (2, 0);
218 void ds_put_format (struct string *, const char *, ...)
219      PRINTF_FORMAT (2, 3);
220 char *ds_put_uninit (struct string *st, size_t incr);
221
222 #endif /* str_h */