apr_strings.inc 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. { Copyright 2000-2005 The Apache Software Foundation or its licensors, as
  2. * applicable.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * 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 <[email protected]>
  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. {
  35. * @file apr_strings.h
  36. * @brief APR Strings library
  37. }
  38. {#include "apr.h"
  39. #include "apr_errno.h"
  40. #include "apr_pools.h"
  41. #define APR_WANT_IOVEC
  42. #include "apr_want.h"
  43. #if APR_HAVE_STDARG_H
  44. #include <stdarg.h>
  45. #endif}
  46. {
  47. * @defgroup apr_strings String routines
  48. * @ingroup APR
  49. }
  50. {
  51. * Do a natural order comparison of two strings.
  52. * @param a The first string to compare
  53. * @param b The second string to compare
  54. * @return Either <0, 0, or >0. If the first string is less than the second
  55. * this returns <0, if they are equivalent it returns 0, and if the
  56. * first string is greater than second string it retuns >0.
  57. }
  58. function apr_strnatcmp(a, b: PChar): Integer;
  59. {$IFDEF WINDOWS} stdcall; {$ELSE} cdecl; {$ENDIF}
  60. external LibAPR name LibNamePrefix + 'apr_strnatcmp' + LibSuff8;
  61. {
  62. * Do a natural order comparison of two strings ignoring the case of the
  63. * strings.
  64. * @param a The first string to compare
  65. * @param b The second string to compare
  66. * @return Either <0, 0, or >0. If the first string is less than the second
  67. * this returns <0, if they are equivalent it returns 0, and if the
  68. * first string is greater than second string it retuns >0.
  69. }
  70. function apr_strnatcasecmp(a, b: PChar): Integer;
  71. {$IFDEF WINDOWS} stdcall; {$ELSE} cdecl; {$ENDIF}
  72. external LibAPR name LibNamePrefix + 'apr_strnatcasecmp' + LibSuff8;
  73. {
  74. * duplicate a string into memory allocated out of a pool
  75. * @param p The pool to allocate out of
  76. * @param s The string to duplicate
  77. * @return The new string
  78. }
  79. function apr_pstrdup(p: Papr_pool_t; s: PChar): PChar;
  80. {$IFDEF WINDOWS} stdcall; {$ELSE} cdecl; {$ENDIF}
  81. external LibAPR name LibNamePrefix + 'apr_pstrdup' + LibSuff8;
  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 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. function apr_pstrmemdup(p: Papr_pool_t; s: PChar; n: apr_size_t): PChar;
  95. {$IFDEF WINDOWS} stdcall; {$ELSE} cdecl; {$ENDIF}
  96. external LibAPR name LibNamePrefix + 'apr_pstrmemdup' + LibSuff12;
  97. {
  98. * duplicate the first n characters of a string into memory allocated
  99. * out of a pool; the new string will be null-terminated
  100. * @param p The pool to allocate out of
  101. * @param s The string to duplicate
  102. * @param n The number of characters to duplicate
  103. * @return The new string
  104. }
  105. function apr_pstrndup(p: Papr_pool_t; s: PChar; n: apr_size_t): PChar;
  106. {$IFDEF WINDOWS} stdcall; {$ELSE} cdecl; {$ENDIF}
  107. external LibAPR name LibNamePrefix + 'apr_pstrndup' + LibSuff12;
  108. {
  109. * Duplicate a block of memory.
  110. *
  111. * @param p The pool to allocate from
  112. * @param m The memory to duplicate
  113. * @param n The number of bytes to duplicate
  114. * @return The new block of memory
  115. }
  116. function apr_pmemdup(p: Papr_pool_t; m: Pointer; n: apr_size_t): Pointer;
  117. {$IFDEF WINDOWS} stdcall; {$ELSE} cdecl; {$ENDIF}
  118. external LibAPR name LibNamePrefix + 'apr_pmemdup' + LibSuff12;
  119. {
  120. * Concatenate multiple strings, allocating memory out a pool
  121. * @param p The pool to allocate out of
  122. * @param ... The strings to concatenate. The final string must be NULL
  123. * @return The new string
  124. }
  125. function apr_pstrcat(p: Papr_pool_t; others: array of const): PChar;
  126. cdecl; external LibAPR name 'apr_pstrcat';
  127. {
  128. * Concatenate multiple strings specified in a writev-style vector
  129. * @param p The pool from which to allocate
  130. * @param vec The strings to concatenate
  131. * @param nvec The number of strings to concatenate
  132. * @param nbytes (output) strlen of new string (pass in NULL to omit)
  133. * @return The new string
  134. }
  135. function apr_pstrcatv(p: Papr_pool_t; const vec: Piovec;
  136. nvec: apr_size_t; nbytes: Papr_size_t): PChar;
  137. {$IFDEF WINDOWS} stdcall; {$ELSE} cdecl; {$ENDIF}
  138. external LibAPR name LibNamePrefix + 'apr_pstrcatv' + LibSuff16;
  139. {
  140. * printf-style style printing routine. The data is output to a string
  141. * allocated from a pool
  142. * @param p The pool to allocate out of
  143. * @param fmt The format of the string
  144. * @param ap The arguments to use while printing the data
  145. * @return The new string
  146. }
  147. function apr_pvsprintf(p: Papr_pool_t; const fmt: PChar; ap: va_list): PChar;
  148. {$IFDEF WINDOWS} stdcall; {$ELSE} cdecl; {$ENDIF}
  149. external LibAPR name LibNamePrefix + 'apr_pvsprintf' + LibSuff12;
  150. {
  151. * printf-style style printing routine. The data is output to a string
  152. * allocated from a pool
  153. * @param p The pool to allocate out of
  154. * @param fmt The format of the string
  155. * @param ... The arguments to use while printing the data
  156. * @return The new string
  157. }
  158. function apr_psprintf(p: Papr_pool_t; const fmt: PChar; others: array of const): PChar;
  159. cdecl; external LibAPR name 'apr_psprintf';
  160. {
  161. * Copy up to dst_size characters from src to dst; does not copy
  162. * past a NUL terminator in src, but always terminates dst with a NUL
  163. * regardless.
  164. * @param dst The destination string
  165. * @param src The source string
  166. * @param dst_size The space available in dst; dst always receives
  167. * NUL termination, so if src is longer than
  168. * dst_size, the actual number of characters copied is
  169. * dst_size - 1.
  170. * @return Pointer to the NUL terminator of the destination string, dst
  171. * @remark
  172. * <PRE>
  173. * Note the differences between this function and strncpy():
  174. * 1) strncpy() doesn't always NUL terminate; apr_cpystrn() does.
  175. * 2) strncpy() pads the destination string with NULs, which is often
  176. * unnecessary; apr_cpystrn() does not.
  177. * 3) strncpy() returns a pointer to the beginning of the dst string;
  178. * apr_cpystrn() returns a pointer to the NUL terminator of dst,
  179. * to allow a check for truncation.
  180. * </PRE>
  181. }
  182. function apr_cpystrn(dst: PChar; const src: PChar;
  183. dst_size: apr_size_t): PChar;
  184. {$IFDEF WINDOWS} stdcall; {$ELSE} cdecl; {$ENDIF}
  185. external LibAPR name LibNamePrefix + 'apr_cpystrn' + LibSuff12;
  186. {
  187. * Strip spaces from a string
  188. * @param dest The destination string. It is okay to modify the string
  189. * in place. Namely dest == src
  190. * @param src The string to rid the spaces from.
  191. }
  192. function apr_collapse_spaces(dst: PChar; const src: PChar): PChar;
  193. {$IFDEF WINDOWS} stdcall; {$ELSE} cdecl; {$ENDIF}
  194. external LibAPR name LibNamePrefix + 'apr_collapse_spaces' + LibSuff8;
  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. function apr_tokenize_to_argv(const arg_str: PChar;
  203. var argv_out: PPChar; token_context: Papr_pool_t): apr_status_t;
  204. {$IFDEF WINDOWS} stdcall; {$ELSE} cdecl; {$ENDIF}
  205. external LibAPR name LibNamePrefix + 'apr_tokenize_to_argv' + LibSuff12;
  206. {
  207. * Split a string into separate null-terminated tokens. The tokens are
  208. * delimited in the string by one or more characters from the sep
  209. * argument.
  210. * @param str The string to separate; this should be specified on the
  211. * first call to apr_strtok() for a given string, and NULL
  212. * on subsequent calls.
  213. * @param sep The set of delimiters
  214. * @param last Internal state saved by apr_strtok() between calls.
  215. * @return The next token from the string
  216. }
  217. function apr_strtok(str: PChar;
  218. const sep: PChar; last: PPChar): PChar;
  219. {$IFDEF WINDOWS} stdcall; {$ELSE} cdecl; {$ENDIF}
  220. external LibAPR name LibNamePrefix + 'apr_strtok' + LibSuff12;
  221. {
  222. * @defgroup APR_Strings_Snprintf snprintf implementations
  223. * @warning
  224. * These are snprintf implementations based on apr_vformatter().
  225. *
  226. * Note that various standards and implementations disagree on the return
  227. * value of snprintf, and side-effects due to %n in the formatting string.
  228. * apr_snprintf (and apr_vsnprintf) behaves as follows:
  229. *
  230. * Process the format string until the entire string is exhausted, or
  231. * the buffer fills. If the buffer fills then stop processing immediately
  232. * (so no further %n arguments are processed), and return the buffer
  233. * length. In all cases the buffer is NUL terminated. It will return the
  234. * number of characters inserted into the buffer, not including the
  235. * terminating NUL. As a special case, if len is 0, apr_snprintf will
  236. * return the number of characters that would have been inserted if
  237. * the buffer had been infinite (in this case, *buffer can be NULL)
  238. *
  239. * In no event does apr_snprintf return a negative number.
  240. }
  241. {
  242. * snprintf routine based on apr_vformatter. This means it understands the
  243. * same extensions.
  244. * @param buf The buffer to write to
  245. * @param len The size of the buffer
  246. * @param format The format string
  247. * @param ... The arguments to use to fill out the format string.
  248. }
  249. function apr_snprintf(buf: PChar; len: apr_size_t;
  250. const format: PChar; others: array of const): PChar;
  251. cdecl; external LibAPR name 'apr_snprintf';
  252. {
  253. * vsnprintf routine based on apr_vformatter. This means it understands the
  254. * same extensions.
  255. * @param buf The buffer to write to
  256. * @param len The size of the buffer
  257. * @param format The format string
  258. * @param ap The arguments to use to fill out the format string.
  259. }
  260. function apr_vsnprintf(buf: PChar; len: apr_size_t;
  261. const format: PChar; ap: va_list): Integer;
  262. {$IFDEF WINDOWS} stdcall; {$ELSE} cdecl; {$ENDIF}
  263. external LibAPR name LibNamePrefix + 'apr_vsnprintf' + LibSuff16;
  264. {
  265. * create a string representation of an int, allocated from a pool
  266. * @param p The pool from which to allocate
  267. * @param n The number to format
  268. * @return The string representation of the number
  269. }
  270. function apr_itoa(p: Papr_pool_t; n: Integer): PChar;
  271. {$IFDEF WINDOWS} stdcall; {$ELSE} cdecl; {$ENDIF}
  272. external LibAPR name LibNamePrefix + 'apr_itoa' + LibSuff8;
  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. function apr_ltoa(p: Papr_pool_t; n: Integer): PChar;
  280. {$IFDEF WINDOWS} stdcall; {$ELSE} cdecl; {$ENDIF}
  281. external LibAPR name LibNamePrefix + 'apr_ltoa' + LibSuff8;
  282. {
  283. * create a string representation of an apr_off_t, allocated from a pool
  284. * @param p The pool from which to allocate
  285. * @param n The number to format
  286. * @return The string representation of the number
  287. }
  288. function apr_off_t_toa(p: Papr_pool_t; n: apr_off_t): PChar;
  289. {$IFDEF WINDOWS} stdcall; {$ELSE} cdecl; {$ENDIF}
  290. external LibAPR name LibNamePrefix + 'apr_off_t_toa' + LibSuff12;
  291. {
  292. * Convert a numeric string into an apr_off_t numeric value.
  293. * @param offset The value of the parsed string.
  294. * @param buf The string to parse. It may contain optional whitespace,
  295. * followed by an optional '+' (positive, default) or '-' (negative)
  296. * character, followed by an optional '0x' prefix if base is 0 or 16,
  297. * followed by numeric digits appropriate for base.
  298. * @param end A pointer to the end of the valid character in buf. If
  299. * not NULL, it is set to the first invalid character in buf.
  300. * @param base A numeric base in the range between 2 and 36 inclusive,
  301. * or 0. If base is zero, buf will be treated as base ten unless its
  302. * digits are prefixed with '0x', in which case it will be treated as
  303. * base 16.
  304. }
  305. function apr_strtoff(offset: Papr_off_t;
  306. const buf: PChar; end_: PPChar; base: cint): apr_status_t;
  307. {$IFDEF WINDOWS} stdcall; {$ELSE} cdecl; {$ENDIF}
  308. external LibAPR name LibNamePrefix + 'apr_strtoff' + LibSuff16;
  309. {
  310. * parse a numeric string into a 64-bit numeric value
  311. * @param buf The string to parse. It may contain optional whitespace,
  312. * followed by an optional '+' (positive, default) or '-' (negative)
  313. * character, followed by an optional '0x' prefix if base is 0 or 16,
  314. * followed by numeric digits appropriate for base.
  315. * @param end A pointer to the end of the valid character in buf. If
  316. * not nil, it is set to the first invalid character in buf.
  317. * @param base A numeric base in the range between 2 and 36 inclusive,
  318. * or 0. If base is zero, buf will be treated as base ten unless its
  319. * digits are prefixed with '0x', in which case it will be treated as
  320. * base 16.
  321. * @return The numeric value of the string.
  322. }
  323. function apr_strtoi64(const buf: PChar; end_: PPChar; base: Integer): apr_int64_t;
  324. {$IFDEF WINDOWS} stdcall; {$ELSE} cdecl; {$ENDIF}
  325. external LibAPR name LibNamePrefix + 'apr_strtoi64' + LibSuff12;
  326. {
  327. * parse a base-10 numeric string into a 64-bit numeric value.
  328. * Equivalent to apr_strtoi64(buf, (char**)NULL, 10).
  329. * @param buf The string to parse
  330. * @return The numeric value of the string
  331. }
  332. function apr_atoi64(const buf: PChar): apr_int64_t;
  333. {$IFDEF WINDOWS} stdcall; {$ELSE} cdecl; {$ENDIF}
  334. external LibAPR name LibNamePrefix + 'apr_atoi64' + LibSuff4;
  335. {
  336. * Format a binary size (magnitiudes are 2^10 rather than 10^3) from an apr_off_t,
  337. * as bytes, K, M, T, etc, to a four character compacted human readable string.
  338. * @param size The size to format
  339. * @param buf The 5 byte text buffer (counting the trailing null)
  340. * @return The buf passed to apr_strfsize()
  341. * @remark All negative sizes report ' - ', apr_strfsize only formats positive values.
  342. }
  343. function apr_strfsize(size: apr_off_t; buf: PChar): PChar;
  344. {$IFDEF WINDOWS} stdcall; {$ELSE} cdecl; {$ENDIF}
  345. external LibAPR name LibNamePrefix + 'apr_strfsize' + LibSuff12;