avstring.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /*
  2. * Copyright (c) 2007 Mans Rullgard
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg 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. * FFmpeg 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 FFmpeg; 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. * Return non-zero if pfx is a prefix of str. If it is, *ptr is set to
  26. * the address of the first character in str after the prefix.
  27. *
  28. * @param str input string
  29. * @param pfx prefix to test
  30. * @param ptr updated if the prefix is matched inside str
  31. * @return non-zero if the prefix matches, zero otherwise
  32. */
  33. int av_strstart(const char *str, const char *pfx, const char **ptr);
  34. /**
  35. * Return non-zero if pfx is a prefix of str independent of case. If
  36. * it is, *ptr is set to the address of the first character in str
  37. * after the prefix.
  38. *
  39. * @param str input string
  40. * @param pfx prefix to test
  41. * @param ptr updated if the prefix is matched inside str
  42. * @return non-zero if the prefix matches, zero otherwise
  43. */
  44. int av_stristart(const char *str, const char *pfx, const char **ptr);
  45. /**
  46. * Locate the first case-independent occurrence in the string haystack
  47. * of the string needle. A zero-length string needle is considered to
  48. * match at the start of haystack.
  49. *
  50. * This function is a case-insensitive version of the standard strstr().
  51. *
  52. * @param haystack string to search in
  53. * @param needle string to search for
  54. * @return pointer to the located match within haystack
  55. * or a null pointer if no match
  56. */
  57. char *av_stristr(const char *haystack, const char *needle);
  58. /**
  59. * Copy the string src to dst, but no more than size - 1 bytes, and
  60. * null-terminate dst.
  61. *
  62. * This function is the same as BSD strlcpy().
  63. *
  64. * @param dst destination buffer
  65. * @param src source string
  66. * @param size size of destination buffer
  67. * @return the length of src
  68. *
  69. * WARNING: since the return value is the length of src, src absolutely
  70. * _must_ be a properly 0-terminated string, otherwise this will read beyond
  71. * the end of the buffer and possibly crash.
  72. */
  73. size_t av_strlcpy(char *dst, const char *src, size_t size);
  74. /**
  75. * Append the string src to the string dst, but to a total length of
  76. * no more than size - 1 bytes, and null-terminate dst.
  77. *
  78. * This function is similar to BSD strlcat(), but differs when
  79. * size <= strlen(dst).
  80. *
  81. * @param dst destination buffer
  82. * @param src source string
  83. * @param size size of destination buffer
  84. * @return the total length of src and dst
  85. *
  86. * WARNING: since the return value use the length of src and dst, these absolutely
  87. * _must_ be a properly 0-terminated strings, otherwise this will read beyond
  88. * the end of the buffer and possibly crash.
  89. */
  90. size_t av_strlcat(char *dst, const char *src, size_t size);
  91. /**
  92. * Append output to a string, according to a format. Never write out of
  93. * the destination buffer, and always put a terminating 0 within
  94. * the buffer.
  95. * @param dst destination buffer (string to which the output is
  96. * appended)
  97. * @param size total size of the destination buffer
  98. * @param fmt printf-compatible format string, specifying how the
  99. * following parameters are used
  100. * @return the length of the string that would have been generated
  101. * if enough space had been available
  102. */
  103. size_t av_strlcatf(char *dst, size_t size, const char *fmt, ...) av_printf_format(3, 4);
  104. /**
  105. * Print arguments following specified format into a large enough auto
  106. * allocated buffer. It is similar to GNU asprintf().
  107. * @param fmt printf-compatible format string, specifying how the
  108. * following parameters are used.
  109. * @return the allocated string
  110. * @note You have to free the string yourself with av_free().
  111. */
  112. char *av_asprintf(const char *fmt, ...) av_printf_format(1, 2);
  113. /**
  114. * Convert a number to a av_malloced string.
  115. */
  116. char *av_d2str(double d);
  117. /**
  118. * Unescape the given string until a non escaped terminating char,
  119. * and return the token corresponding to the unescaped string.
  120. *
  121. * The normal \ and ' escaping is supported. Leading and trailing
  122. * whitespaces are removed, unless they are escaped with '\' or are
  123. * enclosed between ''.
  124. *
  125. * @param buf the buffer to parse, buf will be updated to point to the
  126. * terminating char
  127. * @param term a 0-terminated list of terminating chars
  128. * @return the malloced unescaped string, which must be av_freed by
  129. * the user, NULL in case of allocation failure
  130. */
  131. char *av_get_token(const char **buf, const char *term);
  132. /**
  133. * Split the string into several tokens which can be accessed by
  134. * successive calls to av_strtok().
  135. *
  136. * A token is defined as a sequence of characters not belonging to the
  137. * set specified in delim.
  138. *
  139. * On the first call to av_strtok(), s should point to the string to
  140. * parse, and the value of saveptr is ignored. In subsequent calls, s
  141. * should be NULL, and saveptr should be unchanged since the previous
  142. * call.
  143. *
  144. * This function is similar to strtok_r() defined in POSIX.1.
  145. *
  146. * @param s the string to parse, may be NULL
  147. * @param delim 0-terminated list of token delimiters, must be non-NULL
  148. * @param saveptr user-provided pointer which points to stored
  149. * information necessary for av_strtok() to continue scanning the same
  150. * string. saveptr is updated to point to the next character after the
  151. * first delimiter found, or to NULL if the string was terminated
  152. * @return the found token, or NULL when no token is found
  153. */
  154. char *av_strtok(char *s, const char *delim, char **saveptr);
  155. #endif /* AVUTIL_AVSTRING_H */