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