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