1 /* PSPP - computes sample statistics.
2 Copyright (C) 1997-9, 2000 Free Software Foundation, Inc.
3 Written by Ben Pfaff <blp@gnu.org>.
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.
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.
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
30 #include "memcasecmp.h"
38 #include "vsnprintf.h"
39 #include "xvasprintf.h"
45 #define strrchr rindex
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 *);
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 *);
65 char *spprintf (char *dst, const char *format, ...);
67 /* Common character classes for use with substring and string functions. */
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
82 #define SS_EMPTY_INITIALIZER {NULL, 0}
83 #define SS_LITERAL_INITIALIZER(LITERAL) {LITERAL, (sizeof LITERAL) - 1}
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);
95 /* Constructors and destructor that allocate and deallocate
97 void ss_alloc_substring (struct substring *, struct substring);
98 void ss_alloc_uninit (struct substring *, size_t);
99 void ss_dealloc (struct substring *);
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 *);
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);
134 /* Variable length strings. */
140 size_t capacity; /* Allocated capacity, not including one
141 extra byte allocated for null terminator. */
144 #define DS_EMPTY_INITIALIZER {SS_EMPTY_INITIALIZER, 0}
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 *);
155 void ds_assign_string (struct string *, const struct string *);
156 void ds_assign_substring (struct string *, struct substring);
157 void ds_assign_cstr (struct string *, const char *);
159 /* Shrink, extend. */
160 void ds_clear (struct string *);
161 void ds_extend (struct string *, size_t);
162 void ds_shrink (struct string *);
163 void ds_truncate (struct string *, size_t);
165 /* Padding, trimming. */
166 size_t ds_rtrim (struct string *, struct substring trim_set);
167 size_t ds_ltrim (struct string *, struct substring trim_set);
168 size_t ds_trim (struct string *, struct substring trim_set);
169 bool ds_chomp (struct string *, char);
170 bool ds_separate (const struct string *src, struct substring delimiters,
171 size_t *save_idx, struct substring *token);
172 bool ds_tokenize (const struct string *src, struct substring delimiters,
173 size_t *save_idx, struct substring *token);
174 void ds_rpad (struct string *, size_t length, char pad);
176 /* Extracting substrings. */
177 struct substring ds_ss (const struct string *);
178 struct substring ds_substr (const struct string *, size_t start, size_t);
179 struct substring ds_head (const struct string *, size_t);
180 struct substring ds_tail (const struct string *, size_t);
183 bool ds_is_empty (const struct string *);
184 size_t ds_length (const struct string *);
185 char *ds_data (const struct string *);
186 char *ds_end (const struct string *);
187 int ds_at (const struct string *, size_t idx);
188 int ds_first (const struct string *);
189 int ds_last (const struct string *);
190 size_t ds_span (const struct string *, struct substring skip_set);
191 size_t ds_cspan (const struct string *, struct substring stop_set);
192 size_t ds_find_char (const struct string *, char);
193 int ds_compare (const struct string *, const struct string *);
194 size_t ds_pointer_to_position (const struct string *, const char *);
195 char *ds_xstrdup (const struct string *);
197 size_t ds_capacity (const struct string *);
198 char *ds_cstr (const struct string *);
201 bool ds_read_line (struct string *, FILE *);
202 bool ds_read_config_line (struct string *, int *line_number, FILE *);
203 size_t ds_read_stream (struct string *, size_t size, size_t cnt, FILE *stream);
206 void ds_put_char (struct string *, int ch);
207 void ds_put_char_multiple (struct string *, int ch, size_t);
208 void ds_put_cstr (struct string *, const char *);
209 void ds_put_substring (struct string *, struct substring);
210 void ds_put_vformat (struct string *st, const char *, va_list)
211 PRINTF_FORMAT (2, 0);
212 void ds_put_format (struct string *, const char *, ...)
213 PRINTF_FORMAT (2, 3);
214 char *ds_put_uninit (struct string *st, size_t incr);