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