apr_strings.inc 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  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 n characters from src to dst
  162. * @param dst The destination string
  163. * @param src The source string
  164. * @param dst_size The space available in dst; dst always receives
  165. * null-termination, so if src is longer than
  166. * dst_size, the actual number of characters copied is
  167. * dst_size - 1.
  168. * @remark
  169. * <PRE>
  170. * We re-implement this function to implement these specific changes:
  171. * 1) strncpy() doesn't always null terminate and we want it to.
  172. * 2) strncpy() null fills, which is bogus, esp. when copy 8byte strings
  173. * into 8k blocks.
  174. * 3) Instead of returning the pointer to the beginning of the
  175. * destination string, we return a pointer to the terminating null
  176. * to allow us to check for truncation.
  177. * </PRE>
  178. }
  179. function apr_cpystrn(dst: PChar; const src: PChar;
  180. dst_size: apr_size_t): PChar;
  181. {$IFDEF WINDOWS} stdcall; {$ELSE} cdecl; {$ENDIF}
  182. external LibAPR name LibNamePrefix + 'apr_cpystrn' + LibSuff12;
  183. {
  184. * Strip spaces from a string
  185. * @param dest The destination string. It is okay to modify the string
  186. * in place. Namely dest == src
  187. * @param src The string to rid the spaces from.
  188. }
  189. function apr_collapse_spaces(dst: PChar; const src: PChar): PChar;
  190. {$IFDEF WINDOWS} stdcall; {$ELSE} cdecl; {$ENDIF}
  191. external LibAPR name LibNamePrefix + 'apr_collapse_spaces' + LibSuff8;
  192. {
  193. * Convert the arguments to a program from one string to an array of
  194. * strings terminated by a NULL pointer
  195. * @param arg_str The arguments to convert
  196. * @param argv_out Output location. This is a pointer to an array of strings.
  197. * @param token_context Pool to use.
  198. }
  199. function apr_tokenize_to_argv(const arg_str: PChar;
  200. var argv_out: PPChar; token_context: Papr_pool_t): apr_status_t;
  201. {$IFDEF WINDOWS} stdcall; {$ELSE} cdecl; {$ENDIF}
  202. external LibAPR name LibNamePrefix + 'apr_tokenize_to_argv' + LibSuff12;
  203. {
  204. * Split a string into separate null-terminated tokens. The tokens are
  205. * delimited in the string by one or more characters from the sep
  206. * argument.
  207. * @param str The string to separate; this should be specified on the
  208. * first call to apr_strtok() for a given string, and NULL
  209. * on subsequent calls.
  210. * @param sep The set of delimiters
  211. * @param last Internal state saved by apr_strtok() between calls.
  212. * @return The next token from the string
  213. }
  214. function apr_strtok(str: PChar;
  215. const sep: PChar; last: PPChar): PChar;
  216. {$IFDEF WINDOWS} stdcall; {$ELSE} cdecl; {$ENDIF}
  217. external LibAPR name LibNamePrefix + 'apr_strtok' + LibSuff12;
  218. {
  219. * @defgroup APR_Strings_Snprintf snprintf implementations
  220. * @warning
  221. * These are snprintf implementations based on apr_vformatter().
  222. *
  223. * Note that various standards and implementations disagree on the return
  224. * value of snprintf, and side-effects due to %n in the formatting string.
  225. * apr_snprintf (and apr_vsnprintf) behaves as follows:
  226. *
  227. * Process the format string until the entire string is exhausted, or
  228. * the buffer fills. If the buffer fills then stop processing immediately
  229. * (so no further %n arguments are processed), and return the buffer
  230. * length. In all cases the buffer is NUL terminated. It will return the
  231. * number of characters inserted into the buffer, not including the
  232. * terminating NUL. As a special case, if len is 0, apr_snprintf will
  233. * return the number of characters that would have been inserted if
  234. * the buffer had been infinite (in this case, *buffer can be NULL)
  235. *
  236. * In no event does apr_snprintf return a negative number.
  237. * @{
  238. }
  239. {
  240. * snprintf routine based on apr_vformatter. This means it understands the
  241. * same extensions.
  242. * @param buf The buffer to write to
  243. * @param len The size of the buffer
  244. * @param format The format string
  245. * @param ... The arguments to use to fill out the format string.
  246. }
  247. function apr_snprintf(buf: PChar; len: apr_size_t;
  248. const format: PChar; others: array of const): PChar;
  249. cdecl; external LibAPR name 'apr_snprintf';
  250. {
  251. * vsnprintf routine based on apr_vformatter. This means it understands the
  252. * same extensions.
  253. * @param buf The buffer to write to
  254. * @param len The size of the buffer
  255. * @param format The format string
  256. * @param ap The arguments to use to fill out the format string.
  257. }
  258. function apr_vsnprintf(buf: PChar; len: apr_size_t;
  259. const format: PChar; ap: va_list): Integer;
  260. {$IFDEF WINDOWS} stdcall; {$ELSE} cdecl; {$ENDIF}
  261. external LibAPR name LibNamePrefix + 'apr_vsnprintf' + LibSuff16;
  262. {
  263. * create a string representation of an int, 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. function apr_itoa(p: Papr_pool_t; n: Integer): PChar;
  269. {$IFDEF WINDOWS} stdcall; {$ELSE} cdecl; {$ENDIF}
  270. external LibAPR name LibNamePrefix + 'apr_itoa' + LibSuff8;
  271. {
  272. * create a string representation of a long, allocated from a pool
  273. * @param p The pool from which to allocate
  274. * @param n The number to format
  275. * @return The string representation of the number
  276. }
  277. function apr_ltoa(p: Papr_pool_t; n: Integer): PChar;
  278. {$IFDEF WINDOWS} stdcall; {$ELSE} cdecl; {$ENDIF}
  279. external LibAPR name LibNamePrefix + 'apr_ltoa' + LibSuff8;
  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. function apr_off_t_toa(p: Papr_pool_t; n: apr_off_t): PChar;
  287. {$IFDEF WINDOWS} stdcall; {$ELSE} cdecl; {$ENDIF}
  288. external LibAPR name LibNamePrefix + 'apr_off_t_toa' + LibSuff12;
  289. {
  290. * parse a numeric string into a 64-bit numeric value
  291. * @param buf The string to parse. It may contain optional whitespace,
  292. * followed by an optional '+' (positive, default) or '-' (negative)
  293. * character, followed by an optional '0x' prefix if base is 0 or 16,
  294. * followed by numeric digits appropriate for base.
  295. * @param end A pointer to the end of the valid character in buf. If
  296. * not nil, it is set to the first invalid character in buf.
  297. * @param base A numeric base in the range between 2 and 36 inclusive,
  298. * or 0. If base is zero, buf will be treated as base ten unless its
  299. * digits are prefixed with '0x', in which case it will be treated as
  300. * base 16.
  301. * @return The numeric value of the string.
  302. }
  303. function apr_strtoi64(const buf: PChar; end_: PPChar; base: Integer): apr_int64_t;
  304. {$IFDEF WINDOWS} stdcall; {$ELSE} cdecl; {$ENDIF}
  305. external LibAPR name LibNamePrefix + 'apr_strtoi64' + LibSuff12;
  306. {
  307. * parse a base-10 numeric string into a 64-bit numeric value.
  308. * Equivalent to apr_strtoi64(buf, (char**)NULL, 10).
  309. * @param buf The string to parse
  310. * @return The numeric value of the string
  311. }
  312. function apr_atoi64(const buf: PChar): apr_int64_t;
  313. {$IFDEF WINDOWS} stdcall; {$ELSE} cdecl; {$ENDIF}
  314. external LibAPR name LibNamePrefix + 'apr_atoi64' + LibSuff4;
  315. {
  316. * Format a binary size (magnitiudes are 2^10 rather than 10^3) from an apr_off_t,
  317. * as bytes, K, M, T, etc, to a four character compacted human readable string.
  318. * @param size The size to format
  319. * @param buf The 5 byte text buffer (counting the trailing null)
  320. * @return The buf passed to apr_strfsize()
  321. * @remark All negative sizes report ' - ', apr_strfsize only formats positive values.
  322. }
  323. function apr_strfsize(size: apr_off_t; buf: PChar): PChar;
  324. {$IFDEF WINDOWS} stdcall; {$ELSE} cdecl; {$ENDIF}
  325. external LibAPR name LibNamePrefix + 'apr_strfsize' + LibSuff12;