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