00001 /* Licensed to the Apache Software Foundation (ASF) under one or more 00002 * contributor license agreements. See the NOTICE file distributed with 00003 * this work for additional information regarding copyright ownership. 00004 * The ASF licenses this file to You under the Apache License, Version 2.0 00005 * (the "License"); you may not use this file except in compliance with 00006 * the License. You may obtain a copy of the License at 00007 * 00008 * http://www.apache.org/licenses/LICENSE-2.0 00009 * 00010 * Unless required by applicable law or agreed to in writing, software 00011 * distributed under the License is distributed on an "AS IS" BASIS, 00012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00013 * See the License for the specific language governing permissions and 00014 * limitations under the License. 00015 */ 00016 00017 /* Portions of this file are covered by */ 00018 /* -*- mode: c; c-file-style: "k&r" -*- 00019 00020 strnatcmp.c -- Perform 'natural order' comparisons of strings in C. 00021 Copyright (C) 2000 by Martin Pool <mbp@humbug.org.au> 00022 00023 This software is provided 'as-is', without any express or implied 00024 warranty. In no event will the authors be held liable for any damages 00025 arising from the use of this software. 00026 00027 Permission is granted to anyone to use this software for any purpose, 00028 including commercial applications, and to alter it and redistribute it 00029 freely, subject to the following restrictions: 00030 00031 1. The origin of this software must not be misrepresented; you must not 00032 claim that you wrote the original software. If you use this software 00033 in a product, an acknowledgment in the product documentation would be 00034 appreciated but is not required. 00035 2. Altered source versions must be plainly marked as such, and must not be 00036 misrepresented as being the original software. 00037 3. This notice may not be removed or altered from any source distribution. 00038 */ 00039 00040 #ifndef APR_STRINGS_H 00041 #define APR_STRINGS_H 00042 00043 /** 00044 * @file apr_strings.h 00045 * @brief APR Strings library 00046 */ 00047 00048 #include "apr.h" 00049 #include "apr_errno.h" 00050 #include "apr_pools.h" 00051 #define APR_WANT_IOVEC 00052 #include "apr_want.h" 00053 00054 #if APR_HAVE_STDARG_H 00055 #include <stdarg.h> 00056 #endif 00057 00058 #ifdef __cplusplus 00059 extern "C" { 00060 #endif /* __cplusplus */ 00061 00062 /** 00063 * @defgroup apr_strings String routines 00064 * @ingroup APR 00065 * @{ 00066 */ 00067 00068 /** 00069 * Do a natural order comparison of two strings. 00070 * @param a The first string to compare 00071 * @param b The second string to compare 00072 * @return Either <0, 0, or >0. If the first string is less than the second 00073 * this returns <0, if they are equivalent it returns 0, and if the 00074 * first string is greater than second string it retuns >0. 00075 */ 00076 APR_DECLARE(int) apr_strnatcmp(char const *a, char const *b); 00077 00078 /** 00079 * Do a natural order comparison of two strings ignoring the case of the 00080 * strings. 00081 * @param a The first string to compare 00082 * @param b The second string to compare 00083 * @return Either <0, 0, or >0. If the first string is less than the second 00084 * this returns <0, if they are equivalent it returns 0, and if the 00085 * first string is greater than second string it retuns >0. 00086 */ 00087 APR_DECLARE(int) apr_strnatcasecmp(char const *a, char const *b); 00088 00089 /** 00090 * duplicate a string into memory allocated out of a pool 00091 * @param p The pool to allocate out of 00092 * @param s The string to duplicate 00093 * @return The new string or NULL if s == NULL 00094 */ 00095 APR_DECLARE(char *) apr_pstrdup(apr_pool_t *p, const char *s); 00096 00097 /** 00098 * Create a null-terminated string by making a copy of a sequence 00099 * of characters and appending a null byte 00100 * @param p The pool to allocate out of 00101 * @param s The block of characters to duplicate 00102 * @param n The number of characters to duplicate 00103 * @return The new string or NULL if s == NULL 00104 * @remark This is a faster alternative to apr_pstrndup, for use 00105 * when you know that the string being duplicated really 00106 * has 'n' or more characters. If the string might contain 00107 * fewer characters, use apr_pstrndup. 00108 */ 00109 APR_DECLARE(char *) apr_pstrmemdup(apr_pool_t *p, const char *s, apr_size_t n) 00110 #if defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 4)) 00111 __attribute__((alloc_size(3))) 00112 #endif 00113 ; 00114 00115 /** 00116 * Duplicate at most n characters of a string into memory allocated 00117 * out of a pool; the new string will be NUL-terminated 00118 * @param p The pool to allocate out of 00119 * @param s The string to duplicate 00120 * @param n The maximum number of characters to duplicate 00121 * @return The new string or NULL if s == NULL 00122 * @remark The amount of memory allocated from the pool is the length 00123 * of the returned string including the NUL terminator 00124 */ 00125 APR_DECLARE(char *) apr_pstrndup(apr_pool_t *p, const char *s, apr_size_t n); 00126 00127 /** 00128 * Duplicate a block of memory. 00129 * 00130 * @param p The pool to allocate from 00131 * @param m The memory to duplicate 00132 * @param n The number of bytes to duplicate 00133 * @return The new block of memory or NULL if m == NULL 00134 */ 00135 APR_DECLARE(void *) apr_pmemdup(apr_pool_t *p, const void *m, apr_size_t n) 00136 #if defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 4)) 00137 __attribute__((alloc_size(3))) 00138 #endif 00139 ; 00140 00141 /** 00142 * Concatenate multiple strings, allocating memory out a pool 00143 * @param p The pool to allocate out of 00144 * @param ... The strings to concatenate. The final string must be NULL 00145 * @return The new string 00146 */ 00147 APR_DECLARE_NONSTD(char *) apr_pstrcat(apr_pool_t *p, ...) 00148 #if defined(__GNUC__) && __GNUC__ >= 4 00149 __attribute__((sentinel)) 00150 #endif 00151 ; 00152 00153 /** 00154 * Concatenate multiple strings specified in a writev-style vector 00155 * @param p The pool from which to allocate 00156 * @param vec The strings to concatenate 00157 * @param nvec The number of strings to concatenate 00158 * @param nbytes (output) strlen of new string (pass in NULL to omit) 00159 * @return The new string 00160 */ 00161 APR_DECLARE(char *) apr_pstrcatv(apr_pool_t *p, const struct iovec *vec, 00162 apr_size_t nvec, apr_size_t *nbytes); 00163 00164 /** 00165 * printf-style style printing routine. The data is output to a string 00166 * allocated from a pool 00167 * @param p The pool to allocate out of 00168 * @param fmt The format of the string 00169 * @param ap The arguments to use while printing the data 00170 * @return The new string 00171 */ 00172 APR_DECLARE(char *) apr_pvsprintf(apr_pool_t *p, const char *fmt, va_list ap); 00173 00174 /** 00175 * printf-style style printing routine. The data is output to a string 00176 * allocated from a pool 00177 * @param p The pool to allocate out of 00178 * @param fmt The format of the string 00179 * @param ... The arguments to use while printing the data 00180 * @return The new string 00181 */ 00182 APR_DECLARE_NONSTD(char *) apr_psprintf(apr_pool_t *p, const char *fmt, ...) 00183 __attribute__((format(printf,2,3))); 00184 00185 /** 00186 * Copy up to dst_size characters from src to dst; does not copy 00187 * past a NUL terminator in src, but always terminates dst with a NUL 00188 * regardless. 00189 * @param dst The destination string 00190 * @param src The source string 00191 * @param dst_size The space available in dst; dst always receives 00192 * NUL termination, so if src is longer than 00193 * dst_size, the actual number of characters copied is 00194 * dst_size - 1. 00195 * @return Pointer to the NUL terminator of the destination string, dst 00196 * @remark 00197 * <PRE> 00198 * Note the differences between this function and strncpy(): 00199 * 1) strncpy() doesn't always NUL terminate; apr_cpystrn() does. 00200 * 2) strncpy() pads the destination string with NULs, which is often 00201 * unnecessary; apr_cpystrn() does not. 00202 * 3) strncpy() returns a pointer to the beginning of the dst string; 00203 * apr_cpystrn() returns a pointer to the NUL terminator of dst, 00204 * to allow a check for truncation. 00205 * </PRE> 00206 */ 00207 APR_DECLARE(char *) apr_cpystrn(char *dst, const char *src, 00208 apr_size_t dst_size); 00209 00210 /** 00211 * Remove all whitespace from a string 00212 * @param dest The destination string. It is okay to modify the string 00213 * in place. Namely dest == src 00214 * @param src The string to rid the spaces from. 00215 * @return A pointer to the destination string's null terminator. 00216 */ 00217 APR_DECLARE(char *) apr_collapse_spaces(char *dest, const char *src); 00218 00219 /** 00220 * Convert the arguments to a program from one string to an array of 00221 * strings terminated by a NULL pointer 00222 * @param arg_str The arguments to convert 00223 * @param argv_out Output location. This is a pointer to an array of strings. 00224 * @param token_context Pool to use. 00225 */ 00226 APR_DECLARE(apr_status_t) apr_tokenize_to_argv(const char *arg_str, 00227 char ***argv_out, 00228 apr_pool_t *token_context); 00229 00230 /** 00231 * Split a string into separate null-terminated tokens. The tokens are 00232 * delimited in the string by one or more characters from the sep 00233 * argument. 00234 * @param str The string to separate; this should be specified on the 00235 * first call to apr_strtok() for a given string, and NULL 00236 * on subsequent calls. 00237 * @param sep The set of delimiters 00238 * @param last State saved by apr_strtok() between calls. 00239 * @return The next token from the string 00240 * @note the 'last' state points to the trailing NUL char of the final 00241 * token, otherwise it points to the character following the current 00242 * token (all successive or empty occurances of sep are skiped on the 00243 * subsequent call to apr_strtok). Therefore it is possible to avoid 00244 * a strlen() determination, with the following logic; 00245 * toklen = last - retval; if (*last) --toklen; 00246 */ 00247 APR_DECLARE(char *) apr_strtok(char *str, const char *sep, char **last); 00248 00249 /** 00250 * @defgroup APR_Strings_Snprintf snprintf implementations 00251 * @warning 00252 * These are snprintf implementations based on apr_vformatter(). 00253 * 00254 * Note that various standards and implementations disagree on the return 00255 * value of snprintf, and side-effects due to %n in the formatting string. 00256 * apr_snprintf (and apr_vsnprintf) behaves as follows: 00257 * 00258 * Process the format string until the entire string is exhausted, or 00259 * the buffer fills. If the buffer fills then stop processing immediately 00260 * (so no further %n arguments are processed), and return the buffer 00261 * length. In all cases the buffer is NUL terminated. It will return the 00262 * number of characters inserted into the buffer, not including the 00263 * terminating NUL. As a special case, if len is 0, apr_snprintf will 00264 * return the number of characters that would have been inserted if 00265 * the buffer had been infinite (in this case, *buffer can be NULL) 00266 * 00267 * In no event does apr_snprintf return a negative number. 00268 * @{ 00269 */ 00270 00271 /** 00272 * snprintf routine based on apr_vformatter. This means it understands the 00273 * same extensions. 00274 * @param buf The buffer to write to 00275 * @param len The size of the buffer 00276 * @param format The format string 00277 * @param ... The arguments to use to fill out the format string. 00278 */ 00279 APR_DECLARE_NONSTD(int) apr_snprintf(char *buf, apr_size_t len, 00280 const char *format, ...) 00281 __attribute__((format(printf,3,4))); 00282 00283 /** 00284 * vsnprintf routine based on apr_vformatter. This means it understands the 00285 * same extensions. 00286 * @param buf The buffer to write to 00287 * @param len The size of the buffer 00288 * @param format The format string 00289 * @param ap The arguments to use to fill out the format string. 00290 */ 00291 APR_DECLARE(int) apr_vsnprintf(char *buf, apr_size_t len, const char *format, 00292 va_list ap); 00293 /** @} */ 00294 00295 /** 00296 * create a string representation of an int, allocated from a pool 00297 * @param p The pool from which to allocate 00298 * @param n The number to format 00299 * @return The string representation of the number 00300 */ 00301 APR_DECLARE(char *) apr_itoa(apr_pool_t *p, int n); 00302 00303 /** 00304 * create a string representation of a long, allocated from a pool 00305 * @param p The pool from which to allocate 00306 * @param n The number to format 00307 * @return The string representation of the number 00308 */ 00309 APR_DECLARE(char *) apr_ltoa(apr_pool_t *p, long n); 00310 00311 /** 00312 * create a string representation of an apr_off_t, allocated from a pool 00313 * @param p The pool from which to allocate 00314 * @param n The number to format 00315 * @return The string representation of the number 00316 */ 00317 APR_DECLARE(char *) apr_off_t_toa(apr_pool_t *p, apr_off_t n); 00318 00319 /** 00320 * Convert a numeric string into an apr_off_t numeric value. 00321 * @param offset The value of the parsed string. 00322 * @param buf The string to parse. It may contain optional whitespace, 00323 * followed by an optional '+' (positive, default) or '-' (negative) 00324 * character, followed by an optional '0x' prefix if base is 0 or 16, 00325 * followed by numeric digits appropriate for base. 00326 * @param end A pointer to the end of the valid character in buf. If 00327 * not NULL, it is set to the first invalid character in buf. 00328 * @param base A numeric base in the range between 2 and 36 inclusive, 00329 * or 0. If base is zero, buf will be treated as base ten unless its 00330 * digits are prefixed with '0x', in which case it will be treated as 00331 * base 16. 00332 * @bug *end breaks type safety; where *buf is const, *end needs to be 00333 * declared as const in APR 2.0 00334 */ 00335 APR_DECLARE(apr_status_t) apr_strtoff(apr_off_t *offset, const char *buf, 00336 char **end, int base); 00337 00338 /** 00339 * parse a numeric string into a 64-bit numeric value 00340 * @param buf The string to parse. It may contain optional whitespace, 00341 * followed by an optional '+' (positive, default) or '-' (negative) 00342 * character, followed by an optional '0x' prefix if base is 0 or 16, 00343 * followed by numeric digits appropriate for base. 00344 * @param end A pointer to the end of the valid character in buf. If 00345 * not NULL, it is set to the first invalid character in buf. 00346 * @param base A numeric base in the range between 2 and 36 inclusive, 00347 * or 0. If base is zero, buf will be treated as base ten unless its 00348 * digits are prefixed with '0x', in which case it will be treated as 00349 * base 16. 00350 * @return The numeric value of the string. On overflow, errno is set 00351 * to ERANGE. On success, errno is set to 0. 00352 */ 00353 APR_DECLARE(apr_int64_t) apr_strtoi64(const char *buf, char **end, int base); 00354 00355 /** 00356 * parse a base-10 numeric string into a 64-bit numeric value. 00357 * Equivalent to apr_strtoi64(buf, (char**)NULL, 10). 00358 * @param buf The string to parse 00359 * @return The numeric value of the string. On overflow, errno is set 00360 * to ERANGE. On success, errno is set to 0. 00361 */ 00362 APR_DECLARE(apr_int64_t) apr_atoi64(const char *buf); 00363 00364 /** 00365 * Format a binary size (magnitiudes are 2^10 rather than 10^3) from an apr_off_t, 00366 * as bytes, K, M, T, etc, to a four character compacted human readable string. 00367 * @param size The size to format 00368 * @param buf The 5 byte text buffer (counting the trailing null) 00369 * @return The buf passed to apr_strfsize() 00370 * @remark All negative sizes report ' - ', apr_strfsize only formats positive values. 00371 */ 00372 APR_DECLARE(char *) apr_strfsize(apr_off_t size, char *buf); 00373 00374 /** @} */ 00375 00376 #ifdef __cplusplus 00377 } 00378 #endif 00379 00380 #endif /* !APR_STRINGS_H */