You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

250 lines
7.3KB

  1. /*
  2. * Copyright (c) 2007 Mans Rullgard
  3. *
  4. * This file is part of Libav.
  5. *
  6. * Libav is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * Libav is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with Libav; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #ifndef AVUTIL_AVSTRING_H
  21. #define AVUTIL_AVSTRING_H
  22. #include <stddef.h>
  23. #include "attributes.h"
  24. /**
  25. * @addtogroup lavu_string
  26. * @{
  27. */
  28. /**
  29. * Return non-zero if pfx is a prefix of str. If it is, *ptr is set to
  30. * the address of the first character in str after the prefix.
  31. *
  32. * @param str input string
  33. * @param pfx prefix to test
  34. * @param ptr updated if the prefix is matched inside str
  35. * @return non-zero if the prefix matches, zero otherwise
  36. */
  37. int av_strstart(const char *str, const char *pfx, const char **ptr);
  38. /**
  39. * Return non-zero if pfx is a prefix of str independent of case. If
  40. * it is, *ptr is set to the address of the first character in str
  41. * after the prefix.
  42. *
  43. * @param str input string
  44. * @param pfx prefix to test
  45. * @param ptr updated if the prefix is matched inside str
  46. * @return non-zero if the prefix matches, zero otherwise
  47. */
  48. int av_stristart(const char *str, const char *pfx, const char **ptr);
  49. /**
  50. * Locate the first case-independent occurrence in the string haystack
  51. * of the string needle. A zero-length string needle is considered to
  52. * match at the start of haystack.
  53. *
  54. * This function is a case-insensitive version of the standard strstr().
  55. *
  56. * @param haystack string to search in
  57. * @param needle string to search for
  58. * @return pointer to the located match within haystack
  59. * or a null pointer if no match
  60. */
  61. char *av_stristr(const char *haystack, const char *needle);
  62. /**
  63. * Locate the first occurrence of the string needle in the string haystack
  64. * where not more than hay_length characters are searched. A zero-length
  65. * string needle is considered to match at the start of haystack.
  66. *
  67. * This function is a length-limited version of the standard strstr().
  68. *
  69. * @param haystack string to search in
  70. * @param needle string to search for
  71. * @param hay_length length of string to search in
  72. * @return pointer to the located match within haystack
  73. * or a null pointer if no match
  74. */
  75. char *av_strnstr(const char *haystack, const char *needle, size_t hay_length);
  76. /**
  77. * Copy the string src to dst, but no more than size - 1 bytes, and
  78. * null-terminate dst.
  79. *
  80. * This function is the same as BSD strlcpy().
  81. *
  82. * @param dst destination buffer
  83. * @param src source string
  84. * @param size size of destination buffer
  85. * @return the length of src
  86. *
  87. * @warning since the return value is the length of src, src absolutely
  88. * _must_ be a properly 0-terminated string, otherwise this will read beyond
  89. * the end of the buffer and possibly crash.
  90. */
  91. size_t av_strlcpy(char *dst, const char *src, size_t size);
  92. /**
  93. * Append the string src to the string dst, but to a total length of
  94. * no more than size - 1 bytes, and null-terminate dst.
  95. *
  96. * This function is similar to BSD strlcat(), but differs when
  97. * size <= strlen(dst).
  98. *
  99. * @param dst destination buffer
  100. * @param src source string
  101. * @param size size of destination buffer
  102. * @return the total length of src and dst
  103. *
  104. * @warning since the return value use the length of src and dst, these
  105. * absolutely _must_ be a properly 0-terminated strings, otherwise this
  106. * will read beyond the end of the buffer and possibly crash.
  107. */
  108. size_t av_strlcat(char *dst, const char *src, size_t size);
  109. /**
  110. * Append output to a string, according to a format. Never write out of
  111. * the destination buffer, and always put a terminating 0 within
  112. * the buffer.
  113. * @param dst destination buffer (string to which the output is
  114. * appended)
  115. * @param size total size of the destination buffer
  116. * @param fmt printf-compatible format string, specifying how the
  117. * following parameters are used
  118. * @return the length of the string that would have been generated
  119. * if enough space had been available
  120. */
  121. size_t av_strlcatf(char *dst, size_t size, const char *fmt, ...) av_printf_format(3, 4);
  122. /**
  123. * Convert a number to a av_malloced string.
  124. */
  125. char *av_d2str(double d);
  126. /**
  127. * Unescape the given string until a non escaped terminating char,
  128. * and return the token corresponding to the unescaped string.
  129. *
  130. * The normal \ and ' escaping is supported. Leading and trailing
  131. * whitespaces are removed, unless they are escaped with '\' or are
  132. * enclosed between ''.
  133. *
  134. * @param buf the buffer to parse, buf will be updated to point to the
  135. * terminating char
  136. * @param term a 0-terminated list of terminating chars
  137. * @return the malloced unescaped string, which must be av_freed by
  138. * the user, NULL in case of allocation failure
  139. */
  140. char *av_get_token(const char **buf, const char *term);
  141. /**
  142. * Locale-independent conversion of ASCII isdigit.
  143. */
  144. static inline av_const int av_isdigit(int c)
  145. {
  146. return c >= '0' && c <= '9';
  147. }
  148. /**
  149. * Locale-independent conversion of ASCII isgraph.
  150. */
  151. static inline av_const int av_isgraph(int c)
  152. {
  153. return c > 32 && c < 127;
  154. }
  155. /**
  156. * Locale-independent conversion of ASCII isspace.
  157. */
  158. static inline av_const int av_isspace(int c)
  159. {
  160. return c == ' ' || c == '\f' || c == '\n' || c == '\r' || c == '\t' ||
  161. c == '\v';
  162. }
  163. /**
  164. * Locale-independent conversion of ASCII characters to uppercase.
  165. */
  166. static inline av_const int av_toupper(int c)
  167. {
  168. if (c >= 'a' && c <= 'z')
  169. c ^= 0x20;
  170. return c;
  171. }
  172. /**
  173. * Locale-independent conversion of ASCII characters to lowercase.
  174. */
  175. static inline av_const int av_tolower(int c)
  176. {
  177. if (c >= 'A' && c <= 'Z')
  178. c ^= 0x20;
  179. return c;
  180. }
  181. /**
  182. * Locale-independent conversion of ASCII isxdigit.
  183. */
  184. static inline av_const int av_isxdigit(int c)
  185. {
  186. c = av_tolower(c);
  187. return av_isdigit(c) || (c >= 'a' && c <= 'f');
  188. }
  189. /*
  190. * Locale-independent case-insensitive compare.
  191. * @note This means only ASCII-range characters are case-insensitive
  192. */
  193. int av_strcasecmp(const char *a, const char *b);
  194. /**
  195. * Locale-independent case-insensitive compare.
  196. * @note This means only ASCII-range characters are case-insensitive
  197. */
  198. int av_strncasecmp(const char *a, const char *b, size_t n);
  199. /**
  200. * Thread safe basename.
  201. * @param path the path, on DOS both \ and / are considered separators.
  202. * @return pointer to the basename substring.
  203. */
  204. const char *av_basename(const char *path);
  205. /**
  206. * Thread safe dirname.
  207. * @param path the path, on DOS both \ and / are considered separators.
  208. * @return the path with the separator replaced by the string terminator or ".".
  209. * @note the function may change the input string.
  210. */
  211. const char *av_dirname(char *path);
  212. /**
  213. * Match instances of a name in a comma-separated list of names.
  214. * @param name Name to look for.
  215. * @param names List of names.
  216. * @return 1 on match, 0 otherwise.
  217. */
  218. int av_match_name(const char *name, const char *names);
  219. /**
  220. * @}
  221. */
  222. #endif /* AVUTIL_AVSTRING_H */