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