apr_strings.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. /* Licensed to the Apache Software Foundation (ASF) under one or more
  2. * contributor license agreements. See the NOTICE file distributed with
  3. * this work for additional information regarding copyright ownership.
  4. * The ASF licenses this file to You under the Apache License, Version 2.0
  5. * (the "License"); you may not use this file except in compliance with
  6. * the License. You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. /* Portions of this file are covered by */
  17. /* -*- mode: c; c-file-style: "k&r" -*-
  18. strnatcmp.c -- Perform 'natural order' comparisons of strings in C.
  19. Copyright (C) 2000 by Martin Pool <mbp@humbug.org.au>
  20. This software is provided 'as-is', without any express or implied
  21. warranty. In no event will the authors be held liable for any damages
  22. arising from the use of this software.
  23. Permission is granted to anyone to use this software for any purpose,
  24. including commercial applications, and to alter it and redistribute it
  25. freely, subject to the following restrictions:
  26. 1. The origin of this software must not be misrepresented; you must not
  27. claim that you wrote the original software. If you use this software
  28. in a product, an acknowledgment in the product documentation would be
  29. appreciated but is not required.
  30. 2. Altered source versions must be plainly marked as such, and must not be
  31. misrepresented as being the original software.
  32. 3. This notice may not be removed or altered from any source distribution.
  33. */
  34. #ifndef APR_STRINGS_H
  35. #define APR_STRINGS_H
  36. /**
  37. * @file apr_strings.h
  38. * @brief APR Strings library
  39. */
  40. #include "apr.h"
  41. #include "apr_errno.h"
  42. #include "apr_pools.h"
  43. #define APR_WANT_IOVEC
  44. #include "apr_want.h"
  45. #if APR_HAVE_STDARG_H
  46. #include <stdarg.h>
  47. #endif
  48. #ifdef __cplusplus
  49. extern "C" {
  50. #endif /* __cplusplus */
  51. /**
  52. * @defgroup apr_strings String routines
  53. * @ingroup APR
  54. * @{
  55. */
  56. /**
  57. * Do a natural order comparison of two strings.
  58. * @param a The first string to compare
  59. * @param b The second string to compare
  60. * @return Either <0, 0, or >0. If the first string is less than the second
  61. * this returns <0, if they are equivalent it returns 0, and if the
  62. * first string is greater than second string it retuns >0.
  63. */
  64. APR_DECLARE(int) apr_strnatcmp(char const *a, char const *b);
  65. /**
  66. * Do a natural order comparison of two strings ignoring the case of the
  67. * strings.
  68. * @param a The first string to compare
  69. * @param b The second string to compare
  70. * @return Either <0, 0, or >0. If the first string is less than the second
  71. * this returns <0, if they are equivalent it returns 0, and if the
  72. * first string is greater than second string it retuns >0.
  73. */
  74. APR_DECLARE(int) apr_strnatcasecmp(char const *a, char const *b);
  75. /**
  76. * duplicate a string into memory allocated out of a pool
  77. * @param p The pool to allocate out of
  78. * @param s The string to duplicate
  79. * @return The new string or NULL if s == NULL
  80. */
  81. APR_DECLARE(char *) apr_pstrdup(apr_pool_t *p, const char *s);
  82. /**
  83. * Create a null-terminated string by making a copy of a sequence
  84. * of characters and appending a null byte
  85. * @param p The pool to allocate out of
  86. * @param s The block of characters to duplicate
  87. * @param n The number of characters to duplicate
  88. * @return The new string or NULL if s == NULL
  89. * @remark This is a faster alternative to apr_pstrndup(), for use
  90. * when you know that the string being duplicated really
  91. * has 'n' or more characters. If the string might contain
  92. * fewer characters, use apr_pstrndup().
  93. */
  94. APR_DECLARE(char *) apr_pstrmemdup(apr_pool_t *p, const char *s, apr_size_t n)
  95. #if defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 4))
  96. __attribute__((alloc_size(3)))
  97. #endif
  98. ;
  99. /**
  100. * Duplicate at most n characters of a string into memory allocated
  101. * out of a pool; the new string will be NUL-terminated
  102. * @param p The pool to allocate out of
  103. * @param s The string to duplicate
  104. * @param n The maximum number of characters to duplicate
  105. * @return The new string or NULL if s == NULL
  106. * @remark The amount of memory allocated from the pool is the length
  107. * of the returned string including the NUL terminator
  108. */
  109. APR_DECLARE(char *) apr_pstrndup(apr_pool_t *p, const char *s, apr_size_t n);
  110. /**
  111. * Duplicate a block of memory.
  112. *
  113. * @param p The pool to allocate from
  114. * @param m The memory to duplicate
  115. * @param n The number of bytes to duplicate
  116. * @return The new block of memory or NULL if m == NULL
  117. */
  118. APR_DECLARE(void *) apr_pmemdup(apr_pool_t *p, const void *m, apr_size_t n)
  119. #if defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 4))
  120. __attribute__((alloc_size(3)))
  121. #endif
  122. ;
  123. /**
  124. * Concatenate multiple strings, allocating memory out a pool
  125. * @param p The pool to allocate out of
  126. * @param ... The strings to concatenate. The final string must be NULL
  127. * @return The new string
  128. */
  129. APR_DECLARE_NONSTD(char *) apr_pstrcat(apr_pool_t *p, ...)
  130. #if defined(__GNUC__) && __GNUC__ >= 4
  131. __attribute__((sentinel))
  132. #endif
  133. ;
  134. /**
  135. * Concatenate multiple strings specified in a writev-style vector
  136. * @param p The pool from which to allocate
  137. * @param vec The strings to concatenate
  138. * @param nvec The number of strings to concatenate
  139. * @param nbytes (output) strlen of new string (pass in NULL to omit)
  140. * @return The new string
  141. */
  142. APR_DECLARE(char *) apr_pstrcatv(apr_pool_t *p, const struct iovec *vec,
  143. apr_size_t nvec, apr_size_t *nbytes);
  144. /**
  145. * printf-style style printing routine. The data is output to a string
  146. * allocated from a pool
  147. * @param p The pool to allocate out of
  148. * @param fmt The format of the string
  149. * @param ap The arguments to use while printing the data
  150. * @return The new string
  151. */
  152. APR_DECLARE(char *) apr_pvsprintf(apr_pool_t *p, const char *fmt, va_list ap);
  153. /**
  154. * printf-style style printing routine. The data is output to a string
  155. * allocated from a pool
  156. * @param p The pool to allocate out of
  157. * @param fmt The format of the string
  158. * @param ... The arguments to use while printing the data
  159. * @return The new string
  160. */
  161. APR_DECLARE_NONSTD(char *) apr_psprintf(apr_pool_t *p, const char *fmt, ...)
  162. __attribute__((format(printf,2,3)));
  163. /**
  164. * Copy up to dst_size characters from src to dst; does not copy
  165. * past a NUL terminator in src, but always terminates dst with a NUL
  166. * regardless.
  167. * @param dst The destination string
  168. * @param src The source string
  169. * @param dst_size The space available in dst; dst always receives
  170. * NUL termination, so if src is longer than
  171. * dst_size, the actual number of characters copied is
  172. * dst_size - 1.
  173. * @return Pointer to the NUL terminator of the destination string, dst
  174. * @remark
  175. * <PRE>
  176. * Note the differences between this function and strncpy():
  177. * 1) strncpy() doesn't always NUL terminate; apr_cpystrn() does.
  178. * 2) strncpy() pads the destination string with NULs, which is often
  179. * unnecessary; apr_cpystrn() does not.
  180. * 3) strncpy() returns a pointer to the beginning of the dst string;
  181. * apr_cpystrn() returns a pointer to the NUL terminator of dst,
  182. * to allow a check for truncation.
  183. * </PRE>
  184. */
  185. APR_DECLARE(char *) apr_cpystrn(char *dst, const char *src,
  186. apr_size_t dst_size);
  187. /**
  188. * Remove all whitespace from a string
  189. * @param dest The destination string. It is okay to modify the string
  190. * in place. Namely dest == src
  191. * @param src The string to rid the spaces from.
  192. * @return A pointer to the destination string's null terminator.
  193. */
  194. APR_DECLARE(char *) apr_collapse_spaces(char *dest, const char *src);
  195. /**
  196. * Convert the arguments to a program from one string to an array of
  197. * strings terminated by a NULL pointer
  198. * @param arg_str The arguments to convert
  199. * @param argv_out Output location. This is a pointer to an array of strings.
  200. * @param token_context Pool to use.
  201. */
  202. APR_DECLARE(apr_status_t) apr_tokenize_to_argv(const char *arg_str,
  203. char ***argv_out,
  204. apr_pool_t *token_context);
  205. /**
  206. * Split a string into separate null-terminated tokens. The tokens are
  207. * delimited in the string by one or more characters from the sep
  208. * argument.
  209. * @param str The string to separate; this should be specified on the
  210. * first call to apr_strtok() for a given string, and NULL
  211. * on subsequent calls.
  212. * @param sep The set of delimiters
  213. * @param last State saved by apr_strtok() between calls.
  214. * @return The next token from the string
  215. * @note the 'last' state points to the trailing NUL char of the final
  216. * token, otherwise it points to the character following the current
  217. * token (all successive or empty occurances of sep are skiped on the
  218. * subsequent call to apr_strtok). Therefore it is possible to avoid
  219. * a strlen() determination, with the following logic;
  220. * toklen = last - retval; if (*last) --toklen;
  221. */
  222. APR_DECLARE(char *) apr_strtok(char *str, const char *sep, char **last);
  223. /**
  224. * @defgroup APR_Strings_Snprintf snprintf implementations
  225. * @warning
  226. * These are snprintf implementations based on apr_vformatter().
  227. *
  228. * Note that various standards and implementations disagree on the return
  229. * value of snprintf, and side-effects due to %n in the formatting string.
  230. * apr_snprintf (and apr_vsnprintf) behaves as follows:
  231. *
  232. * Process the format string until the entire string is exhausted, or
  233. * the buffer fills. If the buffer fills then stop processing immediately
  234. * (so no further %n arguments are processed), and return the buffer
  235. * length. In all cases the buffer is NUL terminated. It will return the
  236. * number of characters inserted into the buffer, not including the
  237. * terminating NUL. As a special case, if len is 0, apr_snprintf will
  238. * return the number of characters that would have been inserted if
  239. * the buffer had been infinite (in this case, *buffer can be NULL)
  240. *
  241. * In no event does apr_snprintf return a negative number.
  242. * @{
  243. */
  244. /**
  245. * snprintf routine based on apr_vformatter. This means it understands the
  246. * same extensions.
  247. * @param buf The buffer to write to
  248. * @param len The size of the buffer
  249. * @param format The format string
  250. * @param ... The arguments to use to fill out the format string.
  251. */
  252. APR_DECLARE_NONSTD(int) apr_snprintf(char *buf, apr_size_t len,
  253. const char *format, ...)
  254. __attribute__((format(printf,3,4)));
  255. /**
  256. * vsnprintf routine based on apr_vformatter. This means it understands the
  257. * same extensions.
  258. * @param buf The buffer to write to
  259. * @param len The size of the buffer
  260. * @param format The format string
  261. * @param ap The arguments to use to fill out the format string.
  262. */
  263. APR_DECLARE(int) apr_vsnprintf(char *buf, apr_size_t len, const char *format,
  264. va_list ap);
  265. /** @} */
  266. /**
  267. * create a string representation of an int, allocated from a pool
  268. * @param p The pool from which to allocate
  269. * @param n The number to format
  270. * @return The string representation of the number
  271. */
  272. APR_DECLARE(char *) apr_itoa(apr_pool_t *p, int n);
  273. /**
  274. * create a string representation of a long, allocated from a pool
  275. * @param p The pool from which to allocate
  276. * @param n The number to format
  277. * @return The string representation of the number
  278. */
  279. APR_DECLARE(char *) apr_ltoa(apr_pool_t *p, long n);
  280. /**
  281. * create a string representation of an apr_off_t, allocated from a pool
  282. * @param p The pool from which to allocate
  283. * @param n The number to format
  284. * @return The string representation of the number
  285. */
  286. APR_DECLARE(char *) apr_off_t_toa(apr_pool_t *p, apr_off_t n);
  287. /**
  288. * Convert a numeric string into an apr_off_t numeric value.
  289. * @param offset The value of the parsed string.
  290. * @param buf The string to parse. It may contain optional whitespace,
  291. * followed by an optional '+' (positive, default) or '-' (negative)
  292. * character, followed by an optional '0x' prefix if base is 0 or 16,
  293. * followed by numeric digits appropriate for base.
  294. * @param end A pointer to the end of the valid character in buf. If
  295. * not NULL, it is set to the first invalid character in buf.
  296. * @param base A numeric base in the range between 2 and 36 inclusive,
  297. * or 0. If base is zero, buf will be treated as base ten unless its
  298. * digits are prefixed with '0x', in which case it will be treated as
  299. * base 16.
  300. * @bug *end breaks type safety; where *buf is const, *end needs to be
  301. * declared as const in APR 2.0
  302. */
  303. APR_DECLARE(apr_status_t) apr_strtoff(apr_off_t *offset, const char *buf,
  304. char **end, int base);
  305. /**
  306. * parse a numeric string into a 64-bit numeric value
  307. * @param buf The string to parse. It may contain optional whitespace,
  308. * followed by an optional '+' (positive, default) or '-' (negative)
  309. * character, followed by an optional '0x' prefix if base is 0 or 16,
  310. * followed by numeric digits appropriate for base.
  311. * @param end A pointer to the end of the valid character in buf. If
  312. * not NULL, it is set to the first invalid character in buf.
  313. * @param base A numeric base in the range between 2 and 36 inclusive,
  314. * or 0. If base is zero, buf will be treated as base ten unless its
  315. * digits are prefixed with '0x', in which case it will be treated as
  316. * base 16.
  317. * @return The numeric value of the string. On overflow, errno is set
  318. * to ERANGE. On success, errno is set to 0.
  319. */
  320. APR_DECLARE(apr_int64_t) apr_strtoi64(const char *buf, char **end, int base);
  321. /**
  322. * parse a base-10 numeric string into a 64-bit numeric value.
  323. * Equivalent to apr_strtoi64(buf, (char**)NULL, 10).
  324. * @param buf The string to parse
  325. * @return The numeric value of the string. On overflow, errno is set
  326. * to ERANGE. On success, errno is set to 0.
  327. */
  328. APR_DECLARE(apr_int64_t) apr_atoi64(const char *buf);
  329. /**
  330. * Format a binary size (magnitiudes are 2^10 rather than 10^3) from an apr_off_t,
  331. * as bytes, K, M, T, etc, to a four character compacted human readable string.
  332. * @param size The size to format
  333. * @param buf The 5 byte text buffer (counting the trailing null)
  334. * @return The buf passed to apr_strfsize()
  335. * @remark All negative sizes report ' - ', apr_strfsize only formats positive values.
  336. */
  337. APR_DECLARE(char *) apr_strfsize(apr_off_t size, char *buf);
  338. /** @} */
  339. #ifdef __cplusplus
  340. }
  341. #endif
  342. #endif /* !APR_STRINGS_H */