apr_strings.h revision 266735
185556Siwasaki/* Licensed to the Apache Software Foundation (ASF) under one or more
285556Siwasaki * contributor license agreements.  See the NOTICE file distributed with
385556Siwasaki * this work for additional information regarding copyright ownership.
485556Siwasaki * The ASF licenses this file to You under the Apache License, Version 2.0
585556Siwasaki * (the "License"); you may not use this file except in compliance with
685556Siwasaki * the License.  You may obtain a copy of the License at
785556Siwasaki *
885556Siwasaki *     http://www.apache.org/licenses/LICENSE-2.0
985556Siwasaki *
1085556Siwasaki * Unless required by applicable law or agreed to in writing, software
1185556Siwasaki * distributed under the License is distributed on an "AS IS" BASIS,
1285556Siwasaki * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1385556Siwasaki * See the License for the specific language governing permissions and
1485556Siwasaki * limitations under the License.
1585556Siwasaki */
1685556Siwasaki
1785556Siwasaki/* Portions of this file are covered by */
1885556Siwasaki/* -*- mode: c; c-file-style: "k&r" -*-
1985556Siwasaki
2085556Siwasaki  strnatcmp.c -- Perform 'natural order' comparisons of strings in C.
2185556Siwasaki  Copyright (C) 2000 by Martin Pool <mbp@humbug.org.au>
2285556Siwasaki
2385556Siwasaki  This software is provided 'as-is', without any express or implied
2485556Siwasaki  warranty.  In no event will the authors be held liable for any damages
2585556Siwasaki  arising from the use of this software.
2685556Siwasaki
27115681Sobrien  Permission is granted to anyone to use this software for any purpose,
28115681Sobrien  including commercial applications, and to alter it and redistribute it
29115681Sobrien  freely, subject to the following restrictions:
3085556Siwasaki
3185556Siwasaki  1. The origin of this software must not be misrepresented; you must not
32168191Sjhb     claim that you wrote the original software. If you use this software
3385556Siwasaki     in a product, an acknowledgment in the product documentation would be
3485556Siwasaki     appreciated but is not required.
35215097Sjkim  2. Altered source versions must be plainly marked as such, and must not be
36170976Snjl     misrepresented as being the original software.
37170976Snjl  3. This notice may not be removed or altered from any source distribution.
3885556Siwasaki*/
3985556Siwasaki
40193530Sjkim#ifndef APR_STRINGS_H
41193530Sjkim#define APR_STRINGS_H
4285556Siwasaki
4385556Siwasaki/**
4485556Siwasaki * @file apr_strings.h
45215072Sjkim * @brief APR Strings library
46177157Sjhb */
4785556Siwasaki
4885556Siwasaki#include "apr.h"
4985556Siwasaki#include "apr_errno.h"
5085556Siwasaki#include "apr_pools.h"
51215072Sjkim#define APR_WANT_IOVEC
5285556Siwasaki#include "apr_want.h"
53132619Snjl
54170976Snjl#if APR_HAVE_STDARG_H
5585556Siwasaki#include <stdarg.h>
56170976Snjl#endif
5785556Siwasaki
58170976Snjl#ifdef __cplusplus
59170976Snjlextern "C" {
60170976Snjl#endif /* __cplusplus */
61170976Snjl
62170976Snjl/**
63170976Snjl * @defgroup apr_strings String routines
64170976Snjl * @ingroup APR
65170976Snjl * @{
66197134Srwatson */
67197134Srwatson
68197134Srwatson/**
69197134Srwatson * Do a natural order comparison of two strings.
70197134Srwatson * @param a The first string to compare
71170976Snjl * @param b The second string to compare
7285556Siwasaki * @return Either <0, 0, or >0.  If the first string is less than the second
73126080Sphk *          this returns <0, if they are equivalent it returns 0, and if the
74179726Sed *          first string is greater than second string it retuns >0.
75111815Sphk */
76111815SphkAPR_DECLARE(int) apr_strnatcmp(char const *a, char const *b);
77111815Sphk
78111815Sphk/**
79111815Sphk * Do a natural order comparison of two strings ignoring the case of the
80111815Sphk * strings.
81170976Snjl * @param a The first string to compare
8285556Siwasaki * @param b The second string to compare
8385556Siwasaki * @return Either <0, 0, or >0.  If the first string is less than the second
8485556Siwasaki *         this returns <0, if they are equivalent it returns 0, and if the
8585556Siwasaki *         first string is greater than second string it retuns >0.
8685556Siwasaki */
8785556SiwasakiAPR_DECLARE(int) apr_strnatcasecmp(char const *a, char const *b);
8885556Siwasaki
89131218Simp/**
9085556Siwasaki * duplicate a string into memory allocated out of a pool
9185556Siwasaki * @param p The pool to allocate out of
92119530Snjl * @param s The string to duplicate
9385556Siwasaki * @return The new string or NULL if s == NULL
94119530Snjl */
9585556SiwasakiAPR_DECLARE(char *) apr_pstrdup(apr_pool_t *p, const char *s);
9685556Siwasaki
97119530Snjl/**
9885556Siwasaki * Create a null-terminated string by making a copy of a sequence
99119530Snjl * of characters and appending a null byte
10085556Siwasaki * @param p The pool to allocate out of
101119530Snjl * @param s The block of characters to duplicate
102120156Siwasaki * @param n The number of characters to duplicate
103131218Simp * @return The new string or NULL if s == NULL
104128975Snjl * @remark This is a faster alternative to apr_pstrndup, for use
105120156Siwasaki *         when you know that the string being duplicated really
106128975Snjl *         has 'n' or more characters.  If the string might contain
107120156Siwasaki *         fewer characters, use apr_pstrndup.
108120156Siwasaki */
109120156SiwasakiAPR_DECLARE(char *) apr_pstrmemdup(apr_pool_t *p, const char *s, apr_size_t n)
11085556Siwasaki#if defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 4))
11185556Siwasaki    __attribute__((alloc_size(3)))
11285556Siwasaki#endif
11385556Siwasaki    ;
11485556Siwasaki
11585556Siwasaki/**
11685556Siwasaki * Duplicate at most n characters of a string into memory allocated
11785556Siwasaki * out of a pool; the new string will be NUL-terminated
11885556Siwasaki * @param p The pool to allocate out of
11985556Siwasaki * @param s The string to duplicate
120128975Snjl * @param n The maximum number of characters to duplicate
12185556Siwasaki * @return The new string or NULL if s == NULL
122128975Snjl * @remark The amount of memory allocated from the pool is the length
123119530Snjl *         of the returned string including the NUL terminator
12485556Siwasaki */
125119530SnjlAPR_DECLARE(char *) apr_pstrndup(apr_pool_t *p, const char *s, apr_size_t n);
12685556Siwasaki
12785556Siwasaki/**
128119530Snjl * Duplicate a block of memory.
12985556Siwasaki *
130119530Snjl * @param p The pool to allocate from
13185556Siwasaki * @param m The memory to duplicate
13285556Siwasaki * @param n The number of bytes to duplicate
13385556Siwasaki * @return The new block of memory or NULL if m == NULL
13485556Siwasaki */
13585556SiwasakiAPR_DECLARE(void *) apr_pmemdup(apr_pool_t *p, const void *m, apr_size_t n)
13685556Siwasaki#if defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 4))
13785556Siwasaki    __attribute__((alloc_size(3)))
13885556Siwasaki#endif
13985556Siwasaki    ;
14085556Siwasaki
14185556Siwasaki/**
14285556Siwasaki * Concatenate multiple strings, allocating memory out a pool
14385556Siwasaki * @param p The pool to allocate out of
14485556Siwasaki * @param ... The strings to concatenate.  The final string must be NULL
145132619Snjl * @return The new string
146131218Simp */
14785556SiwasakiAPR_DECLARE_NONSTD(char *) apr_pstrcat(apr_pool_t *p, ...)
148119530Snjl#if defined(__GNUC__) && __GNUC__ >= 4
149131218Simp    __attribute__((sentinel))
150119530Snjl#endif
15185556Siwasaki    ;
15285556Siwasaki
153148352Snjl/**
154131218Simp * Concatenate multiple strings specified in a writev-style vector
155131218Simp * @param p The pool from which to allocate
156131218Simp * @param vec The strings to concatenate
157131218Simp * @param nvec The number of strings to concatenate
15885556Siwasaki * @param nbytes (output) strlen of new string (pass in NULL to omit)
15985556Siwasaki * @return The new string
16085556Siwasaki */
16185556SiwasakiAPR_DECLARE(char *) apr_pstrcatv(apr_pool_t *p, const struct iovec *vec,
16285556Siwasaki                                 apr_size_t nvec, apr_size_t *nbytes);
16385556Siwasaki
16485556Siwasaki/**
16585556Siwasaki * printf-style style printing routine.  The data is output to a string
16685556Siwasaki * allocated from a pool
16785556Siwasaki * @param p The pool to allocate out of
16885556Siwasaki * @param fmt The format of the string
16985556Siwasaki * @param ap The arguments to use while printing the data
17085556Siwasaki * @return The new string
171148352Snjl */
172148352SnjlAPR_DECLARE(char *) apr_pvsprintf(apr_pool_t *p, const char *fmt, va_list ap);
17385556Siwasaki
17485556Siwasaki/**
17585556Siwasaki * printf-style style printing routine.  The data is output to a string
176128975Snjl * allocated from a pool
17785556Siwasaki * @param p The pool to allocate out of
17885556Siwasaki * @param fmt The format of the string
179119530Snjl * @param ... The arguments to use while printing the data
180148352Snjl * @return The new string
181148352Snjl */
182148352SnjlAPR_DECLARE_NONSTD(char *) apr_psprintf(apr_pool_t *p, const char *fmt, ...)
183148352Snjl        __attribute__((format(printf,2,3)));
184148352Snjl
185148352Snjl/**
186148352Snjl * Copy up to dst_size characters from src to dst; does not copy
187148352Snjl * past a NUL terminator in src, but always terminates dst with a NUL
188148352Snjl * regardless.
189148352Snjl * @param dst The destination string
19085556Siwasaki * @param src The source string
19185556Siwasaki * @param dst_size The space available in dst; dst always receives
19285556Siwasaki *                 NUL termination, so if src is longer than
19385556Siwasaki *                 dst_size, the actual number of characters copied is
19485556Siwasaki *                 dst_size - 1.
19585556Siwasaki * @return Pointer to the NUL terminator of the destination string, dst
19685556Siwasaki * @remark
197119530Snjl * <PRE>
198131218Simp * Note the differences between this function and strncpy():
199119530Snjl *  1) strncpy() doesn't always NUL terminate; apr_cpystrn() does.
20085556Siwasaki *  2) strncpy() pads the destination string with NULs, which is often
20185556Siwasaki *     unnecessary; apr_cpystrn() does not.
20285556Siwasaki *  3) strncpy() returns a pointer to the beginning of the dst string;
20385556Siwasaki *     apr_cpystrn() returns a pointer to the NUL terminator of dst,
20485556Siwasaki *     to allow a check for truncation.
205170976Snjl * </PRE>
206170976Snjl */
207170976SnjlAPR_DECLARE(char *) apr_cpystrn(char *dst, const char *src,
208170976Snjl                                apr_size_t dst_size);
209170976Snjl
210170976Snjl/**
211170976Snjl * Remove all whitespace from a string
212170976Snjl * @param dest The destination string.  It is okay to modify the string
213170976Snjl *             in place.  Namely dest == src
214170976Snjl * @param src The string to rid the spaces from.
215170976Snjl * @return A pointer to the destination string's null terminator.
216170976Snjl */
217170976SnjlAPR_DECLARE(char *) apr_collapse_spaces(char *dest, const char *src);
218170976Snjl
219170976Snjl/**
220170976Snjl * Convert the arguments to a program from one string to an array of
221170976Snjl * strings terminated by a NULL pointer
222170976Snjl * @param arg_str The arguments to convert
223170976Snjl * @param argv_out Output location.  This is a pointer to an array of strings.
224170976Snjl * @param token_context Pool to use.
225183381Sed */
226170976SnjlAPR_DECLARE(apr_status_t) apr_tokenize_to_argv(const char *arg_str,
227170976Snjl                                               char ***argv_out,
228183381Sed                                               apr_pool_t *token_context);
229170976Snjl
230170976Snjl/**
231170976Snjl * Split a string into separate null-terminated tokens.  The tokens are
232170976Snjl * delimited in the string by one or more characters from the sep
233170976Snjl * argument.
234170976Snjl * @param str The string to separate; this should be specified on the
235170976Snjl *            first call to apr_strtok() for a given string, and NULL
236170976Snjl *            on subsequent calls.
237170976Snjl * @param sep The set of delimiters
238170976Snjl * @param last State saved by apr_strtok() between calls.
239170976Snjl * @return The next token from the string
240170976Snjl * @note the 'last' state points to the trailing NUL char of the final
241170976Snjl * token, otherwise it points to the character following the current
242170976Snjl * token (all successive or empty occurances of sep are skiped on the
243170976Snjl * subsequent call to apr_strtok).  Therefore it is possible to avoid
244170976Snjl * a strlen() determination, with the following logic;
245170976Snjl * toklen = last - retval; if (*last) --toklen;
246170976Snjl */
247170976SnjlAPR_DECLARE(char *) apr_strtok(char *str, const char *sep, char **last);
248170976Snjl
249193951Skib/**
250170976Snjl * @defgroup APR_Strings_Snprintf snprintf implementations
251170976Snjl * @warning
252170976Snjl * These are snprintf implementations based on apr_vformatter().
253170976Snjl *
254170976Snjl * Note that various standards and implementations disagree on the return
255170976Snjl * value of snprintf, and side-effects due to %n in the formatting string.
256170976Snjl * apr_snprintf (and apr_vsnprintf) behaves as follows:
257170976Snjl *
258170976Snjl * Process the format string until the entire string is exhausted, or
259170976Snjl * the buffer fills.  If the buffer fills then stop processing immediately
260170976Snjl * (so no further %n arguments are processed), and return the buffer
261170976Snjl * length.  In all cases the buffer is NUL terminated. It will return the
262170976Snjl * number of characters inserted into the buffer, not including the
263170976Snjl * terminating NUL. As a special case, if len is 0, apr_snprintf will
264170976Snjl * return the number of characters that would have been inserted if
265170976Snjl * the buffer had been infinite (in this case, *buffer can be NULL)
26685556Siwasaki *
267192442Simp * In no event does apr_snprintf return a negative number.
26885556Siwasaki * @{
269170976Snjl */
270170976Snjl
271170976Snjl/**
272170976Snjl * snprintf routine based on apr_vformatter.  This means it understands the
273170976Snjl * same extensions.
274170976Snjl * @param buf The buffer to write to
275170976Snjl * @param len The size of the buffer
276170976Snjl * @param format The format string
277170976Snjl * @param ... The arguments to use to fill out the format string.
278170976Snjl */
279170976SnjlAPR_DECLARE_NONSTD(int) apr_snprintf(char *buf, apr_size_t len,
28085556Siwasaki                                     const char *format, ...)
28185556Siwasaki        __attribute__((format(printf,3,4)));
28285556Siwasaki
28385556Siwasaki/**
284192442Simp * vsnprintf routine based on apr_vformatter.  This means it understands the
28585556Siwasaki * same extensions.
286170976Snjl * @param buf The buffer to write to
287170976Snjl * @param len The size of the buffer
288170976Snjl * @param format The format string
289170976Snjl * @param ap The arguments to use to fill out the format string.
290170976Snjl */
291170976SnjlAPR_DECLARE(int) apr_vsnprintf(char *buf, apr_size_t len, const char *format,
292170976Snjl                               va_list ap);
293170976Snjl/** @} */
294170976Snjl
295170976Snjl/**
296170976Snjl * create a string representation of an int, allocated from a pool
297170976Snjl * @param p The pool from which to allocate
298170976Snjl * @param n The number to format
299170976Snjl * @return The string representation of the number
300170976Snjl */
301170976SnjlAPR_DECLARE(char *) apr_itoa(apr_pool_t *p, int n);
302170976Snjl
303171297Snjl/**
30485556Siwasaki * create a string representation of a long, allocated from a pool
30585556Siwasaki * @param p The pool from which to allocate
30685556Siwasaki * @param n The number to format
30785556Siwasaki * @return The string representation of the number
308192442Simp */
30985556SiwasakiAPR_DECLARE(char *) apr_ltoa(apr_pool_t *p, long n);
310170976Snjl
311170976Snjl/**
31285556Siwasaki * create a string representation of an apr_off_t, allocated from a pool
313170976Snjl * @param p The pool from which to allocate
314170976Snjl * @param n The number to format
31585556Siwasaki * @return The string representation of the number
31685556Siwasaki */
317170976SnjlAPR_DECLARE(char *) apr_off_t_toa(apr_pool_t *p, apr_off_t n);
318170976Snjl
319170976Snjl/**
32085556Siwasaki * Convert a numeric string into an apr_off_t numeric value.
32185556Siwasaki * @param offset The value of the parsed string.
32285556Siwasaki * @param buf The string to parse. It may contain optional whitespace,
323119530Snjl *   followed by an optional '+' (positive, default) or '-' (negative)
32485556Siwasaki *   character, followed by an optional '0x' prefix if base is 0 or 16,
325170976Snjl *   followed by numeric digits appropriate for base.
326170976Snjl * @param end A pointer to the end of the valid character in buf. If
327170976Snjl *   not NULL, it is set to the first invalid character in buf.
328170976Snjl * @param base A numeric base in the range between 2 and 36 inclusive,
329170976Snjl *   or 0.  If base is zero, buf will be treated as base ten unless its
330170976Snjl *   digits are prefixed with '0x', in which case it will be treated as
331170976Snjl *   base 16.
332170976Snjl * @bug *end breaks type safety; where *buf is const, *end needs to be
333170976Snjl * declared as const in APR 2.0
334170976Snjl */
335170976SnjlAPR_DECLARE(apr_status_t) apr_strtoff(apr_off_t *offset, const char *buf,
33685556Siwasaki                                      char **end, int base);
33785556Siwasaki
338119530Snjl/**
33985556Siwasaki * parse a numeric string into a 64-bit numeric value
340170976Snjl * @param buf The string to parse. It may contain optional whitespace,
341170976Snjl *   followed by an optional '+' (positive, default) or '-' (negative)
342170976Snjl *   character, followed by an optional '0x' prefix if base is 0 or 16,
343170976Snjl *   followed by numeric digits appropriate for base.
344170976Snjl * @param end A pointer to the end of the valid character in buf. If
345170976Snjl *   not NULL, it is set to the first invalid character in buf.
346170976Snjl * @param base A numeric base in the range between 2 and 36 inclusive,
347170976Snjl *   or 0.  If base is zero, buf will be treated as base ten unless its
348170976Snjl *   digits are prefixed with '0x', in which case it will be treated as
349170976Snjl *   base 16.
350170976Snjl * @return The numeric value of the string.  On overflow, errno is set
35185556Siwasaki * to ERANGE.  On success, errno is set to 0.
352170976Snjl */
353170976SnjlAPR_DECLARE(apr_int64_t) apr_strtoi64(const char *buf, char **end, int base);
354170976Snjl
355170976Snjl/**
356170976Snjl * parse a base-10 numeric string into a 64-bit numeric value.
357170976Snjl * Equivalent to apr_strtoi64(buf, (char**)NULL, 10).
358170976Snjl * @param buf The string to parse
359170976Snjl * @return The numeric value of the string.  On overflow, errno is set
360170976Snjl * to ERANGE.  On success, errno is set to 0.
361170976Snjl */
362170976SnjlAPR_DECLARE(apr_int64_t) apr_atoi64(const char *buf);
363170976Snjl
364170976Snjl/**
365170976Snjl * Format a binary size (magnitiudes are 2^10 rather than 10^3) from an apr_off_t,
366170976Snjl * as bytes, K, M, T, etc, to a four character compacted human readable string.
367170976Snjl * @param size The size to format
368170976Snjl * @param buf The 5 byte text buffer (counting the trailing null)
36985556Siwasaki * @return The buf passed to apr_strfsize()
37085556Siwasaki * @remark All negative sizes report '  - ', apr_strfsize only formats positive values.
37185556Siwasaki */
37285556SiwasakiAPR_DECLARE(char *) apr_strfsize(apr_off_t size, char *buf);
37385556Siwasaki
37485556Siwasaki/** @} */
37585556Siwasaki
37685556Siwasaki#ifdef __cplusplus
37785556Siwasaki}
37885556Siwasaki#endif
37985556Siwasaki
38085556Siwasaki#endif  /* !APR_STRINGS_H */
38185556Siwasaki