1251875Speter/* Licensed to the Apache Software Foundation (ASF) under one or more 2251875Speter * contributor license agreements. See the NOTICE file distributed with 3251875Speter * this work for additional information regarding copyright ownership. 4251875Speter * The ASF licenses this file to You under the Apache License, Version 2.0 5251875Speter * (the "License"); you may not use this file except in compliance with 6251875Speter * the License. You may obtain a copy of the License at 7251875Speter * 8251875Speter * http://www.apache.org/licenses/LICENSE-2.0 9251875Speter * 10251875Speter * Unless required by applicable law or agreed to in writing, software 11251875Speter * distributed under the License is distributed on an "AS IS" BASIS, 12251875Speter * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13251875Speter * See the License for the specific language governing permissions and 14251875Speter * limitations under the License. 15251875Speter */ 16251875Speter 17251875Speter/* Portions of this file are covered by */ 18251875Speter/* -*- mode: c; c-file-style: "k&r" -*- 19251875Speter 20251875Speter strnatcmp.c -- Perform 'natural order' comparisons of strings in C. 21251875Speter Copyright (C) 2000 by Martin Pool <mbp@humbug.org.au> 22251875Speter 23251875Speter This software is provided 'as-is', without any express or implied 24251875Speter warranty. In no event will the authors be held liable for any damages 25251875Speter arising from the use of this software. 26251875Speter 27251875Speter Permission is granted to anyone to use this software for any purpose, 28251875Speter including commercial applications, and to alter it and redistribute it 29251875Speter freely, subject to the following restrictions: 30251875Speter 31251875Speter 1. The origin of this software must not be misrepresented; you must not 32251875Speter claim that you wrote the original software. If you use this software 33251875Speter in a product, an acknowledgment in the product documentation would be 34251875Speter appreciated but is not required. 35251875Speter 2. Altered source versions must be plainly marked as such, and must not be 36251875Speter misrepresented as being the original software. 37251875Speter 3. This notice may not be removed or altered from any source distribution. 38251875Speter*/ 39251875Speter 40251875Speter#ifndef APR_STRINGS_H 41251875Speter#define APR_STRINGS_H 42251875Speter 43251875Speter/** 44251875Speter * @file apr_strings.h 45251875Speter * @brief APR Strings library 46251875Speter */ 47251875Speter 48251875Speter#include "apr.h" 49251875Speter#include "apr_errno.h" 50251875Speter#include "apr_pools.h" 51251875Speter#define APR_WANT_IOVEC 52251875Speter#include "apr_want.h" 53251875Speter 54251875Speter#if APR_HAVE_STDARG_H 55251875Speter#include <stdarg.h> 56251875Speter#endif 57251875Speter 58251875Speter#ifdef __cplusplus 59251875Speterextern "C" { 60251875Speter#endif /* __cplusplus */ 61251875Speter 62251875Speter/** 63251875Speter * @defgroup apr_strings String routines 64251875Speter * @ingroup APR 65251875Speter * @{ 66251875Speter */ 67251875Speter 68251875Speter/** 69251875Speter * Do a natural order comparison of two strings. 70251875Speter * @param a The first string to compare 71251875Speter * @param b The second string to compare 72251875Speter * @return Either <0, 0, or >0. If the first string is less than the second 73251875Speter * this returns <0, if they are equivalent it returns 0, and if the 74251875Speter * first string is greater than second string it retuns >0. 75251875Speter */ 76251875SpeterAPR_DECLARE(int) apr_strnatcmp(char const *a, char const *b); 77251875Speter 78251875Speter/** 79251875Speter * Do a natural order comparison of two strings ignoring the case of the 80251875Speter * strings. 81251875Speter * @param a The first string to compare 82251875Speter * @param b The second string to compare 83251875Speter * @return Either <0, 0, or >0. If the first string is less than the second 84251875Speter * this returns <0, if they are equivalent it returns 0, and if the 85251875Speter * first string is greater than second string it retuns >0. 86251875Speter */ 87251875SpeterAPR_DECLARE(int) apr_strnatcasecmp(char const *a, char const *b); 88251875Speter 89251875Speter/** 90251875Speter * duplicate a string into memory allocated out of a pool 91251875Speter * @param p The pool to allocate out of 92251875Speter * @param s The string to duplicate 93266735Speter * @return The new string or NULL if s == NULL 94251875Speter */ 95251875SpeterAPR_DECLARE(char *) apr_pstrdup(apr_pool_t *p, const char *s); 96251875Speter 97251875Speter/** 98251875Speter * Create a null-terminated string by making a copy of a sequence 99251875Speter * of characters and appending a null byte 100251875Speter * @param p The pool to allocate out of 101251875Speter * @param s The block of characters to duplicate 102251875Speter * @param n The number of characters to duplicate 103266735Speter * @return The new string or NULL if s == NULL 104362181Sdim * @remark This is a faster alternative to apr_pstrndup(), for use 105251875Speter * when you know that the string being duplicated really 106251875Speter * has 'n' or more characters. If the string might contain 107362181Sdim * fewer characters, use apr_pstrndup(). 108251875Speter */ 109253734SpeterAPR_DECLARE(char *) apr_pstrmemdup(apr_pool_t *p, const char *s, apr_size_t n) 110253734Speter#if defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 4)) 111253734Speter __attribute__((alloc_size(3))) 112253734Speter#endif 113253734Speter ; 114251875Speter 115251875Speter/** 116251875Speter * Duplicate at most n characters of a string into memory allocated 117251875Speter * out of a pool; the new string will be NUL-terminated 118251875Speter * @param p The pool to allocate out of 119251875Speter * @param s The string to duplicate 120251875Speter * @param n The maximum number of characters to duplicate 121266735Speter * @return The new string or NULL if s == NULL 122251875Speter * @remark The amount of memory allocated from the pool is the length 123251875Speter * of the returned string including the NUL terminator 124251875Speter */ 125251875SpeterAPR_DECLARE(char *) apr_pstrndup(apr_pool_t *p, const char *s, apr_size_t n); 126251875Speter 127251875Speter/** 128251875Speter * Duplicate a block of memory. 129251875Speter * 130251875Speter * @param p The pool to allocate from 131251875Speter * @param m The memory to duplicate 132251875Speter * @param n The number of bytes to duplicate 133266735Speter * @return The new block of memory or NULL if m == NULL 134251875Speter */ 135253734SpeterAPR_DECLARE(void *) apr_pmemdup(apr_pool_t *p, const void *m, apr_size_t n) 136253734Speter#if defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 4)) 137253734Speter __attribute__((alloc_size(3))) 138253734Speter#endif 139253734Speter ; 140251875Speter 141251875Speter/** 142251875Speter * Concatenate multiple strings, allocating memory out a pool 143251875Speter * @param p The pool to allocate out of 144251875Speter * @param ... The strings to concatenate. The final string must be NULL 145251875Speter * @return The new string 146251875Speter */ 147251875SpeterAPR_DECLARE_NONSTD(char *) apr_pstrcat(apr_pool_t *p, ...) 148251875Speter#if defined(__GNUC__) && __GNUC__ >= 4 149251875Speter __attribute__((sentinel)) 150251875Speter#endif 151251875Speter ; 152251875Speter 153251875Speter/** 154251875Speter * Concatenate multiple strings specified in a writev-style vector 155251875Speter * @param p The pool from which to allocate 156251875Speter * @param vec The strings to concatenate 157251875Speter * @param nvec The number of strings to concatenate 158251875Speter * @param nbytes (output) strlen of new string (pass in NULL to omit) 159251875Speter * @return The new string 160251875Speter */ 161251875SpeterAPR_DECLARE(char *) apr_pstrcatv(apr_pool_t *p, const struct iovec *vec, 162251875Speter apr_size_t nvec, apr_size_t *nbytes); 163251875Speter 164251875Speter/** 165251875Speter * printf-style style printing routine. The data is output to a string 166251875Speter * allocated from a pool 167251875Speter * @param p The pool to allocate out of 168251875Speter * @param fmt The format of the string 169251875Speter * @param ap The arguments to use while printing the data 170251875Speter * @return The new string 171251875Speter */ 172251875SpeterAPR_DECLARE(char *) apr_pvsprintf(apr_pool_t *p, const char *fmt, va_list ap); 173251875Speter 174251875Speter/** 175251875Speter * printf-style style printing routine. The data is output to a string 176251875Speter * allocated from a pool 177251875Speter * @param p The pool to allocate out of 178251875Speter * @param fmt The format of the string 179251875Speter * @param ... The arguments to use while printing the data 180251875Speter * @return The new string 181251875Speter */ 182251875SpeterAPR_DECLARE_NONSTD(char *) apr_psprintf(apr_pool_t *p, const char *fmt, ...) 183251875Speter __attribute__((format(printf,2,3))); 184251875Speter 185251875Speter/** 186251875Speter * Copy up to dst_size characters from src to dst; does not copy 187251875Speter * past a NUL terminator in src, but always terminates dst with a NUL 188251875Speter * regardless. 189251875Speter * @param dst The destination string 190251875Speter * @param src The source string 191251875Speter * @param dst_size The space available in dst; dst always receives 192251875Speter * NUL termination, so if src is longer than 193251875Speter * dst_size, the actual number of characters copied is 194251875Speter * dst_size - 1. 195251875Speter * @return Pointer to the NUL terminator of the destination string, dst 196251875Speter * @remark 197251875Speter * <PRE> 198251875Speter * Note the differences between this function and strncpy(): 199251875Speter * 1) strncpy() doesn't always NUL terminate; apr_cpystrn() does. 200251875Speter * 2) strncpy() pads the destination string with NULs, which is often 201251875Speter * unnecessary; apr_cpystrn() does not. 202251875Speter * 3) strncpy() returns a pointer to the beginning of the dst string; 203251875Speter * apr_cpystrn() returns a pointer to the NUL terminator of dst, 204251875Speter * to allow a check for truncation. 205251875Speter * </PRE> 206251875Speter */ 207251875SpeterAPR_DECLARE(char *) apr_cpystrn(char *dst, const char *src, 208251875Speter apr_size_t dst_size); 209251875Speter 210251875Speter/** 211251875Speter * Remove all whitespace from a string 212251875Speter * @param dest The destination string. It is okay to modify the string 213251875Speter * in place. Namely dest == src 214251875Speter * @param src The string to rid the spaces from. 215251875Speter * @return A pointer to the destination string's null terminator. 216251875Speter */ 217251875SpeterAPR_DECLARE(char *) apr_collapse_spaces(char *dest, const char *src); 218251875Speter 219251875Speter/** 220251875Speter * Convert the arguments to a program from one string to an array of 221251875Speter * strings terminated by a NULL pointer 222251875Speter * @param arg_str The arguments to convert 223251875Speter * @param argv_out Output location. This is a pointer to an array of strings. 224251875Speter * @param token_context Pool to use. 225251875Speter */ 226251875SpeterAPR_DECLARE(apr_status_t) apr_tokenize_to_argv(const char *arg_str, 227251875Speter char ***argv_out, 228251875Speter apr_pool_t *token_context); 229251875Speter 230251875Speter/** 231251875Speter * Split a string into separate null-terminated tokens. The tokens are 232251875Speter * delimited in the string by one or more characters from the sep 233251875Speter * argument. 234251875Speter * @param str The string to separate; this should be specified on the 235251875Speter * first call to apr_strtok() for a given string, and NULL 236251875Speter * on subsequent calls. 237251875Speter * @param sep The set of delimiters 238266735Speter * @param last State saved by apr_strtok() between calls. 239251875Speter * @return The next token from the string 240266735Speter * @note the 'last' state points to the trailing NUL char of the final 241266735Speter * token, otherwise it points to the character following the current 242266735Speter * token (all successive or empty occurances of sep are skiped on the 243266735Speter * subsequent call to apr_strtok). Therefore it is possible to avoid 244266735Speter * a strlen() determination, with the following logic; 245266735Speter * toklen = last - retval; if (*last) --toklen; 246251875Speter */ 247251875SpeterAPR_DECLARE(char *) apr_strtok(char *str, const char *sep, char **last); 248251875Speter 249251875Speter/** 250251875Speter * @defgroup APR_Strings_Snprintf snprintf implementations 251251875Speter * @warning 252251875Speter * These are snprintf implementations based on apr_vformatter(). 253251875Speter * 254251875Speter * Note that various standards and implementations disagree on the return 255251875Speter * value of snprintf, and side-effects due to %n in the formatting string. 256251875Speter * apr_snprintf (and apr_vsnprintf) behaves as follows: 257251875Speter * 258251875Speter * Process the format string until the entire string is exhausted, or 259251875Speter * the buffer fills. If the buffer fills then stop processing immediately 260251875Speter * (so no further %n arguments are processed), and return the buffer 261251875Speter * length. In all cases the buffer is NUL terminated. It will return the 262251875Speter * number of characters inserted into the buffer, not including the 263251875Speter * terminating NUL. As a special case, if len is 0, apr_snprintf will 264251875Speter * return the number of characters that would have been inserted if 265251875Speter * the buffer had been infinite (in this case, *buffer can be NULL) 266251875Speter * 267251875Speter * In no event does apr_snprintf return a negative number. 268251875Speter * @{ 269251875Speter */ 270251875Speter 271251875Speter/** 272251875Speter * snprintf routine based on apr_vformatter. This means it understands the 273251875Speter * same extensions. 274251875Speter * @param buf The buffer to write to 275251875Speter * @param len The size of the buffer 276251875Speter * @param format The format string 277251875Speter * @param ... The arguments to use to fill out the format string. 278251875Speter */ 279251875SpeterAPR_DECLARE_NONSTD(int) apr_snprintf(char *buf, apr_size_t len, 280251875Speter const char *format, ...) 281251875Speter __attribute__((format(printf,3,4))); 282251875Speter 283251875Speter/** 284251875Speter * vsnprintf routine based on apr_vformatter. This means it understands the 285251875Speter * same extensions. 286251875Speter * @param buf The buffer to write to 287251875Speter * @param len The size of the buffer 288251875Speter * @param format The format string 289251875Speter * @param ap The arguments to use to fill out the format string. 290251875Speter */ 291251875SpeterAPR_DECLARE(int) apr_vsnprintf(char *buf, apr_size_t len, const char *format, 292251875Speter va_list ap); 293251875Speter/** @} */ 294251875Speter 295251875Speter/** 296251875Speter * create a string representation of an int, allocated from a pool 297251875Speter * @param p The pool from which to allocate 298251875Speter * @param n The number to format 299251875Speter * @return The string representation of the number 300251875Speter */ 301251875SpeterAPR_DECLARE(char *) apr_itoa(apr_pool_t *p, int n); 302251875Speter 303251875Speter/** 304251875Speter * create a string representation of a long, allocated from a pool 305251875Speter * @param p The pool from which to allocate 306251875Speter * @param n The number to format 307251875Speter * @return The string representation of the number 308251875Speter */ 309251875SpeterAPR_DECLARE(char *) apr_ltoa(apr_pool_t *p, long n); 310251875Speter 311251875Speter/** 312251875Speter * create a string representation of an apr_off_t, allocated from a pool 313251875Speter * @param p The pool from which to allocate 314251875Speter * @param n The number to format 315251875Speter * @return The string representation of the number 316251875Speter */ 317251875SpeterAPR_DECLARE(char *) apr_off_t_toa(apr_pool_t *p, apr_off_t n); 318251875Speter 319251875Speter/** 320251875Speter * Convert a numeric string into an apr_off_t numeric value. 321251875Speter * @param offset The value of the parsed string. 322251875Speter * @param buf The string to parse. It may contain optional whitespace, 323251875Speter * followed by an optional '+' (positive, default) or '-' (negative) 324251875Speter * character, followed by an optional '0x' prefix if base is 0 or 16, 325251875Speter * followed by numeric digits appropriate for base. 326251875Speter * @param end A pointer to the end of the valid character in buf. If 327251875Speter * not NULL, it is set to the first invalid character in buf. 328251875Speter * @param base A numeric base in the range between 2 and 36 inclusive, 329251875Speter * or 0. If base is zero, buf will be treated as base ten unless its 330251875Speter * digits are prefixed with '0x', in which case it will be treated as 331251875Speter * base 16. 332251875Speter * @bug *end breaks type safety; where *buf is const, *end needs to be 333251875Speter * declared as const in APR 2.0 334251875Speter */ 335251875SpeterAPR_DECLARE(apr_status_t) apr_strtoff(apr_off_t *offset, const char *buf, 336251875Speter char **end, int base); 337251875Speter 338251875Speter/** 339251875Speter * parse a numeric string into a 64-bit numeric value 340251875Speter * @param buf The string to parse. It may contain optional whitespace, 341251875Speter * followed by an optional '+' (positive, default) or '-' (negative) 342251875Speter * character, followed by an optional '0x' prefix if base is 0 or 16, 343251875Speter * followed by numeric digits appropriate for base. 344251875Speter * @param end A pointer to the end of the valid character in buf. If 345251875Speter * not NULL, it is set to the first invalid character in buf. 346251875Speter * @param base A numeric base in the range between 2 and 36 inclusive, 347251875Speter * or 0. If base is zero, buf will be treated as base ten unless its 348251875Speter * digits are prefixed with '0x', in which case it will be treated as 349251875Speter * base 16. 350251875Speter * @return The numeric value of the string. On overflow, errno is set 351251875Speter * to ERANGE. On success, errno is set to 0. 352251875Speter */ 353251875SpeterAPR_DECLARE(apr_int64_t) apr_strtoi64(const char *buf, char **end, int base); 354251875Speter 355251875Speter/** 356251875Speter * parse a base-10 numeric string into a 64-bit numeric value. 357251875Speter * Equivalent to apr_strtoi64(buf, (char**)NULL, 10). 358251875Speter * @param buf The string to parse 359251875Speter * @return The numeric value of the string. On overflow, errno is set 360251875Speter * to ERANGE. On success, errno is set to 0. 361251875Speter */ 362251875SpeterAPR_DECLARE(apr_int64_t) apr_atoi64(const char *buf); 363251875Speter 364251875Speter/** 365251875Speter * Format a binary size (magnitiudes are 2^10 rather than 10^3) from an apr_off_t, 366251875Speter * as bytes, K, M, T, etc, to a four character compacted human readable string. 367251875Speter * @param size The size to format 368251875Speter * @param buf The 5 byte text buffer (counting the trailing null) 369251875Speter * @return The buf passed to apr_strfsize() 370251875Speter * @remark All negative sizes report ' - ', apr_strfsize only formats positive values. 371251875Speter */ 372251875SpeterAPR_DECLARE(char *) apr_strfsize(apr_off_t size, char *buf); 373251875Speter 374251875Speter/** @} */ 375251875Speter 376251875Speter#ifdef __cplusplus 377251875Speter} 378251875Speter#endif 379251875Speter 380251875Speter#endif /* !APR_STRINGS_H */ 381