fspr_strings.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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 fspr_strings.h
  38. * @brief APR Strings library
  39. */
  40. #include "fspr.h"
  41. #include "fspr_errno.h"
  42. #include "fspr_pools.h"
  43. #define APR_WANT_IOVEC
  44. #include "fspr_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 fspr_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) fspr_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) fspr_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
  80. */
  81. APR_DECLARE(char *) fspr_pstrdup(fspr_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
  89. * @remark This is a faster alternative to fspr_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 fspr_pstrndup.
  93. */
  94. APR_DECLARE(char *) fspr_pstrmemdup(fspr_pool_t *p, const char *s, fspr_size_t n);
  95. /**
  96. * Duplicate at most n characters of a string into memory allocated
  97. * out of a pool; the new string will be NUL-terminated
  98. * @param p The pool to allocate out of
  99. * @param s The string to duplicate
  100. * @param n The maximum number of characters to duplicate
  101. * @return The new string
  102. * @remark The amount of memory allocated from the pool is the length
  103. * of the returned string including the NUL terminator
  104. */
  105. APR_DECLARE(char *) fspr_pstrndup(fspr_pool_t *p, const char *s, fspr_size_t n);
  106. /**
  107. * Duplicate a block of memory.
  108. *
  109. * @param p The pool to allocate from
  110. * @param m The memory to duplicate
  111. * @param n The number of bytes to duplicate
  112. * @return The new block of memory
  113. */
  114. APR_DECLARE(void *) fspr_pmemdup(fspr_pool_t *p, const void *m, fspr_size_t n);
  115. /**
  116. * Concatenate multiple strings, allocating memory out a pool
  117. * @param p The pool to allocate out of
  118. * @param ... The strings to concatenate. The final string must be NULL
  119. * @return The new string
  120. */
  121. APR_DECLARE_NONSTD(char *) fspr_pstrcat(fspr_pool_t *p, ...);
  122. /**
  123. * Concatenate multiple strings specified in a writev-style vector
  124. * @param p The pool from which to allocate
  125. * @param vec The strings to concatenate
  126. * @param nvec The number of strings to concatenate
  127. * @param nbytes (output) strlen of new string (pass in NULL to omit)
  128. * @return The new string
  129. */
  130. APR_DECLARE(char *) fspr_pstrcatv(fspr_pool_t *p, const struct iovec *vec,
  131. fspr_size_t nvec, fspr_size_t *nbytes);
  132. /**
  133. * printf-style style printing routine. The data is output to a string
  134. * allocated from a pool
  135. * @param p The pool to allocate out of
  136. * @param fmt The format of the string
  137. * @param ap The arguments to use while printing the data
  138. * @return The new string
  139. */
  140. APR_DECLARE(char *) fspr_pvsprintf(fspr_pool_t *p, const char *fmt, va_list ap);
  141. /**
  142. * printf-style style printing routine. The data is output to a string
  143. * allocated from a pool
  144. * @param p The pool to allocate out of
  145. * @param fmt The format of the string
  146. * @param ... The arguments to use while printing the data
  147. * @return The new string
  148. */
  149. APR_DECLARE_NONSTD(char *) fspr_psprintf(fspr_pool_t *p, const char *fmt, ...)
  150. __attribute__((format(printf,2,3)));
  151. /**
  152. * Copy up to dst_size characters from src to dst; does not copy
  153. * past a NUL terminator in src, but always terminates dst with a NUL
  154. * regardless.
  155. * @param dst The destination string
  156. * @param src The source string
  157. * @param dst_size The space available in dst; dst always receives
  158. * NUL termination, so if src is longer than
  159. * dst_size, the actual number of characters copied is
  160. * dst_size - 1.
  161. * @return Pointer to the NUL terminator of the destination string, dst
  162. * @remark
  163. * <PRE>
  164. * Note the differences between this function and strncpy():
  165. * 1) strncpy() doesn't always NUL terminate; fspr_cpystrn() does.
  166. * 2) strncpy() pads the destination string with NULs, which is often
  167. * unnecessary; fspr_cpystrn() does not.
  168. * 3) strncpy() returns a pointer to the beginning of the dst string;
  169. * fspr_cpystrn() returns a pointer to the NUL terminator of dst,
  170. * to allow a check for truncation.
  171. * </PRE>
  172. */
  173. APR_DECLARE(char *) fspr_cpystrn(char *dst, const char *src,
  174. fspr_size_t dst_size);
  175. /**
  176. * Strip spaces from a string
  177. * @param dest The destination string. It is okay to modify the string
  178. * in place. Namely dest == src
  179. * @param src The string to rid the spaces from.
  180. * @return The destination string, dest.
  181. */
  182. APR_DECLARE(char *) fspr_collapse_spaces(char *dest, const char *src);
  183. /**
  184. * Convert the arguments to a program from one string to an array of
  185. * strings terminated by a NULL pointer
  186. * @param arg_str The arguments to convert
  187. * @param argv_out Output location. This is a pointer to an array of strings.
  188. * @param token_context Pool to use.
  189. */
  190. APR_DECLARE(fspr_status_t) fspr_tokenize_to_argv(const char *arg_str,
  191. char ***argv_out,
  192. fspr_pool_t *token_context);
  193. /**
  194. * Split a string into separate null-terminated tokens. The tokens are
  195. * delimited in the string by one or more characters from the sep
  196. * argument.
  197. * @param str The string to separate; this should be specified on the
  198. * first call to fspr_strtok() for a given string, and NULL
  199. * on subsequent calls.
  200. * @param sep The set of delimiters
  201. * @param last Internal state saved by fspr_strtok() between calls.
  202. * @return The next token from the string
  203. */
  204. APR_DECLARE(char *) fspr_strtok(char *str, const char *sep, char **last);
  205. /**
  206. * @defgroup APR_Strings_Snprintf snprintf implementations
  207. * @warning
  208. * These are snprintf implementations based on fspr_vformatter().
  209. *
  210. * Note that various standards and implementations disagree on the return
  211. * value of snprintf, and side-effects due to %n in the formatting string.
  212. * fspr_snprintf (and fspr_vsnprintf) behaves as follows:
  213. *
  214. * Process the format string until the entire string is exhausted, or
  215. * the buffer fills. If the buffer fills then stop processing immediately
  216. * (so no further %n arguments are processed), and return the buffer
  217. * length. In all cases the buffer is NUL terminated. It will return the
  218. * number of characters inserted into the buffer, not including the
  219. * terminating NUL. As a special case, if len is 0, fspr_snprintf will
  220. * return the number of characters that would have been inserted if
  221. * the buffer had been infinite (in this case, *buffer can be NULL)
  222. *
  223. * In no event does fspr_snprintf return a negative number.
  224. * @{
  225. */
  226. /**
  227. * snprintf routine based on fspr_vformatter. This means it understands the
  228. * same extensions.
  229. * @param buf The buffer to write to
  230. * @param len The size of the buffer
  231. * @param format The format string
  232. * @param ... The arguments to use to fill out the format string.
  233. */
  234. APR_DECLARE_NONSTD(int) fspr_snprintf(char *buf, fspr_size_t len,
  235. const char *format, ...)
  236. __attribute__((format(printf,3,4)));
  237. /**
  238. * vsnprintf routine based on fspr_vformatter. This means it understands the
  239. * same extensions.
  240. * @param buf The buffer to write to
  241. * @param len The size of the buffer
  242. * @param format The format string
  243. * @param ap The arguments to use to fill out the format string.
  244. */
  245. APR_DECLARE(int) fspr_vsnprintf(char *buf, fspr_size_t len, const char *format,
  246. va_list ap);
  247. /** @} */
  248. /**
  249. * create a string representation of an int, allocated from a pool
  250. * @param p The pool from which to allocate
  251. * @param n The number to format
  252. * @return The string representation of the number
  253. */
  254. APR_DECLARE(char *) fspr_itoa(fspr_pool_t *p, int n);
  255. /**
  256. * create a string representation of a long, allocated from a pool
  257. * @param p The pool from which to allocate
  258. * @param n The number to format
  259. * @return The string representation of the number
  260. */
  261. APR_DECLARE(char *) fspr_ltoa(fspr_pool_t *p, long n);
  262. /**
  263. * create a string representation of an fspr_off_t, allocated from a pool
  264. * @param p The pool from which to allocate
  265. * @param n The number to format
  266. * @return The string representation of the number
  267. */
  268. APR_DECLARE(char *) fspr_off_t_toa(fspr_pool_t *p, fspr_off_t n);
  269. /**
  270. * Convert a numeric string into an fspr_off_t numeric value.
  271. * @param offset The value of the parsed string.
  272. * @param buf The string to parse. It may contain optional whitespace,
  273. * followed by an optional '+' (positive, default) or '-' (negative)
  274. * character, followed by an optional '0x' prefix if base is 0 or 16,
  275. * followed by numeric digits appropriate for base.
  276. * @param end A pointer to the end of the valid character in buf. If
  277. * not NULL, it is set to the first invalid character in buf.
  278. * @param base A numeric base in the range between 2 and 36 inclusive,
  279. * or 0. If base is zero, buf will be treated as base ten unless its
  280. * digits are prefixed with '0x', in which case it will be treated as
  281. * base 16.
  282. */
  283. APR_DECLARE(fspr_status_t) fspr_strtoff(fspr_off_t *offset, const char *buf,
  284. char **end, int base);
  285. /**
  286. * parse a numeric string into a 64-bit numeric value
  287. * @param buf The string to parse. It may contain optional whitespace,
  288. * followed by an optional '+' (positive, default) or '-' (negative)
  289. * character, followed by an optional '0x' prefix if base is 0 or 16,
  290. * followed by numeric digits appropriate for base.
  291. * @param end A pointer to the end of the valid character in buf. If
  292. * not NULL, it is set to the first invalid character in buf.
  293. * @param base A numeric base in the range between 2 and 36 inclusive,
  294. * or 0. If base is zero, buf will be treated as base ten unless its
  295. * digits are prefixed with '0x', in which case it will be treated as
  296. * base 16.
  297. * @return The numeric value of the string. On overflow, errno is set
  298. * to ERANGE.
  299. */
  300. APR_DECLARE(fspr_int64_t) fspr_strtoi64(const char *buf, char **end, int base);
  301. /**
  302. * parse a base-10 numeric string into a 64-bit numeric value.
  303. * Equivalent to fspr_strtoi64(buf, (char**)NULL, 10).
  304. * @param buf The string to parse
  305. * @return The numeric value of the string
  306. */
  307. APR_DECLARE(fspr_int64_t) fspr_atoi64(const char *buf);
  308. /**
  309. * Format a binary size (magnitiudes are 2^10 rather than 10^3) from an fspr_off_t,
  310. * as bytes, K, M, T, etc, to a four character compacted human readable string.
  311. * @param size The size to format
  312. * @param buf The 5 byte text buffer (counting the trailing null)
  313. * @return The buf passed to fspr_strfsize()
  314. * @remark All negative sizes report ' - ', fspr_strfsize only formats positive values.
  315. */
  316. APR_DECLARE(char *) fspr_strfsize(fspr_off_t size, char *buf);
  317. /** @} */
  318. #ifdef __cplusplus
  319. }
  320. #endif
  321. #endif /* !APR_STRINGS_H */