* lib/string_.h: If the gnulib module XYZ is not present, undefine
[pspp] / lib / string_.h
1 /* A GNU-like <string.h>.
2
3    Copyright (C) 1995-1996, 2001-2007 Free Software Foundation, Inc.
4
5    This program is free software; you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by
7    the Free Software Foundation; either version 2, or (at your option)
8    any later version.
9
10    This program is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13    GNU 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 Foundation,
17    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
18
19 #ifndef _GL_STRING_H
20 #define _GL_STRING_H
21
22 #include @ABSOLUTE_STRING_H@
23
24 #ifdef __cplusplus
25 extern "C" {
26 #endif
27
28 /* Return the first occurrence of NEEDLE in HAYSTACK.  */
29 #if @GNULIB_MEMMEM@
30 # if ! @HAVE_DECL_MEMMEM@
31 extern void *memmem (void const *__haystack, size_t __haystack_len,
32                      void const *__needle, size_t __needle_len);
33 # endif
34 #else
35 # undef memmem
36 # define memmem memmem_is_unportable__use_gnulib_module_memmem_for_portability
37 #endif
38
39 /* Copy N bytes of SRC to DEST, return pointer to bytes after the
40    last written byte.  */
41 #if @GNULIB_MEMPCPY@
42 # if ! @HAVE_MEMPCPY@
43 extern void *mempcpy (void *restrict __dest, void const *restrict __src,
44                       size_t __n);
45 # endif
46 #else
47 # undef mempcpy
48 # define mempcpy mempcpy_is_unportable__use_gnulib_module_mempcpy_for_portability
49 #endif
50
51 /* Search backwards through a block for a byte (specified as an int).  */
52 #if @GNULIB_MEMRCHR@
53 # if ! @HAVE_DECL_MEMRCHR@
54 extern void *memrchr (void const *, int, size_t);
55 # endif
56 #else
57 # undef memrchr
58 # define memrchr memrchr_is_unportable__use_gnulib_module_memrchr_for_portability
59 #endif
60
61 /* Copy SRC to DST, returning the address of the terminating '\0' in DST.  */
62 #if @GNULIB_STPCPY@
63 # if ! @HAVE_STPCPY@
64 extern char *stpcpy (char *restrict __dst, char const *restrict __src);
65 # endif
66 #else
67 # undef stpcpy
68 # define stpcpy stpcpy_is_unportable__use_gnulib_module_stpcpy_for_portability
69 #endif
70
71 /* Copy no more than N bytes of SRC to DST, returning a pointer past the
72    last non-NUL byte written into DST.  */
73 #if @GNULIB_STPNCPY@
74 # if ! @HAVE_STPNCPY@
75 #  define stpncpy gnu_stpncpy
76 extern char *stpncpy (char *restrict __dst, char const *restrict __src,
77                       size_t __n);
78 # endif
79 #else
80 # undef stpncpy
81 # define stpncpy stpncpy_is_unportable__use_gnulib_module_stpncpy_for_portability
82 #endif
83
84 /* Compare strings S1 and S2, ignoring case, returning less than, equal to or
85    greater than zero if S1 is lexicographically less than, equal to or greater
86    than S2.
87    Note: This function may, in multibyte locales, return 0 for strings of
88    different lengths!
89    No known system has a strcasecmp() function that works correctly in
90    multibyte locales.  Therefore use our version always, if the
91    strcase module is available.  */
92 #if @GNULIB_STRCASE@
93 # if @REPLACE_STRCASECMP@
94 #  define strcasecmp rpl_strcasecmp
95 extern int strcasecmp (char const *__s1, char const *__s2);
96 # endif
97 #else
98 # undef strcasecmp
99 # define strcasecmp strcasecmp_is_unportable__use_gnulib_module_strcase_for_portability
100 #endif
101
102 /* Compare no more than N bytes of strings S1 and S2, ignoring case,
103    returning less than, equal to or greater than zero if S1 is
104    lexicographically less than, equal to or greater than S2.
105    Note: This function cannot work correctly in multibyte locales.  */
106 #if @GNULIB_STRCASE@
107 # if ! @HAVE_DECL_STRNCASECMP@
108 extern int strncasecmp (char const *__s1, char const *__s2, size_t __n);
109 # endif
110 #else
111 # undef strncasecmp
112 # define strncasecmp strncasecmp_is_unportable__use_gnulib_module_strcase_for_portability
113 #endif
114
115 /* Find the first occurrence of C in S or the final NUL byte.  */
116 #if @GNULIB_STRCHRNUL@
117 # if ! @HAVE_STRCHRNUL@
118 extern char *strchrnul (char const *__s, int __c_in);
119 # endif
120 #else
121 # undef strchrnul
122 # define strchrnul strchrnul_is_unportable__use_gnulib_module_strchrnul_for_portability
123 #endif
124
125 /* Duplicate S, returning an identical malloc'd string.  */
126 #if @GNULIB_STRDUP@
127 # if ! @HAVE_DECL_STRDUP@ && ! defined strdup
128 extern char *strdup (char const *__s);
129 # endif
130 #else
131 # undef strdup
132 # define strdup strdup_is_unportable__use_gnulib_module_strdup_for_portability
133 #endif
134
135 /* Return a newly allocated copy of at most N bytes of STRING.  */
136 #if @GNULIB_STRNDUP@
137 # if ! @HAVE_STRNDUP@
138 #  undef strndup
139 #  define strndup rpl_strndup
140 #  if ! @HAVE_DECL_STRNDUP@
141 extern char *strndup (char const *__string, size_t __n);
142 #  endif
143 # endif
144 #else
145 # undef strndup
146 # define strndup strndup_is_unportable__use_gnulib_module_strndup_for_portability
147 #endif
148
149 /* Find the length (number of bytes) of STRING, but scan at most
150    MAXLEN bytes.  If no '\0' terminator is found in that many bytes,
151    return MAXLEN.  */
152 #if @GNULIB_STRNLEN@
153 # if ! @HAVE_DECL_STRNLEN@
154 extern size_t strnlen (char const *__string, size_t __maxlen);
155 # endif
156 #else
157 # undef strnlen
158 # define strnlen strnlen_is_unportable__use_gnulib_module_strnlen_for_portability
159 #endif
160
161 /* Find the first occurrence in S of any character in ACCEPT.  */
162 #if @GNULIB_STRPBRK@
163 # if ! @HAVE_STRPBRK@
164 extern char *strpbrk (char const *__s, char const *__accept);
165 # endif
166 #else
167 # undef strpbrk
168 # define strpbrk strpbrk_is_unportable__use_gnulib_module_strpbrk_for_portability
169 #endif
170
171 /* Search the next delimiter (char listed in DELIM) starting at *STRINGP.
172    If one is found, overwrite it with a NUL, and advance *STRINGP
173    to point to the next char after it.  Otherwise, set *STRINGP to NULL.
174    If *STRINGP was already NULL, nothing happens.
175    Return the old value of *STRINGP.
176
177    This is a variant of strtok() that is multithread-safe and supports
178    empty fields.
179
180    Caveat: It modifies the original string.
181    Caveat: These functions cannot be used on constant strings.
182    Caveat: The identity of the delimiting character is lost.
183    Caveat: It doesn't work with multibyte strings unless all of the delimiter
184            characters are ASCII characters < 0x30.
185
186    See also strtok_r().  */
187 #if @GNULIB_STRSEP@
188 # if ! @HAVE_STRSEP@
189 extern char *strsep (char **restrict __stringp, char const *restrict __delim);
190 # endif
191 #else
192 # undef strsep
193 # define strsep strsep_is_unportable__use_gnulib_module_strsep_for_portability
194 #endif
195
196 /* Find the first occurrence of NEEDLE in HAYSTACK.
197    No known system has a strstr() function that works correctly in
198    multibyte locales.  Therefore use our version always, if the strstr
199    module is available.  */
200 #if @GNULIB_STRSTR@
201 # if @REPLACE_STRSTR@
202 #  undef strstr
203 #  define strstr rpl_strstr
204 extern char *strstr (char const *__haystack, char const *__needle);
205 # endif
206 #else
207 # undef strstr
208 # define strstr strstr_is_unportable__use_gnulib_module_strstr_for_portability
209 #endif
210
211 /* Find the first occurrence of NEEDLE in HAYSTACK, using case-insensitive
212    comparison.
213    Note: This function may, in multibyte locales, return success even if
214    strlen (haystack) < strlen (needle) !  */
215 #if @GNULIB_STRCASESTR@
216 # if @REPLACE_STRCASESTR@
217 #  undef strcasestr
218 #  define strcasestr rpl_strcasestr
219 extern char *strcasestr (const char *haystack, const char *needle);
220 # endif
221 #else
222 # undef strcasestr
223 # define strcasestr strcasestr_is_unportable__use_gnulib_module_strcasestr_for_portability
224 #endif
225
226 /* Parse S into tokens separated by characters in DELIM.
227    If S is NULL, the saved pointer in SAVE_PTR is used as
228    the next starting point.  For example:
229         char s[] = "-abc-=-def";
230         char *sp;
231         x = strtok_r(s, "-", &sp);      // x = "abc", sp = "=-def"
232         x = strtok_r(NULL, "-=", &sp);  // x = "def", sp = NULL
233         x = strtok_r(NULL, "=", &sp);   // x = NULL
234                 // s = "abc\0-def\0"
235
236    This is a variant of strtok() that is multithread-safe.
237
238    For the POSIX documentation for this function, see:
239    http://www.opengroup.org/susv3xsh/strtok.html
240
241    Caveat: It modifies the original string.
242    Caveat: These functions cannot be used on constant strings.
243    Caveat: The identity of the delimiting character is lost.
244    Caveat: It doesn't work with multibyte strings unless all of the delimiter
245            characters are ASCII characters < 0x30.
246
247    See also strsep().  */
248 #if @GNULIB_STRTOK_R@
249 # if ! @HAVE_DECL_STRTOK_R@
250 extern char *strtok_r (char *restrict __s, char const *restrict __sep,
251                        char **restrict __lasts);
252 # endif
253 #else
254 # undef strtok_r
255 # define strtok_r strtok_r_is_unportable__use_gnulib_module_strtok_r_for_portability
256 #endif
257
258 #ifdef __cplusplus
259 }
260 #endif
261
262 #endif