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.

131 lines
3.3KB

  1. /*
  2. * Various simple utilities for ffmpeg system
  3. * Copyright (c) 2000, 2001, 2002 Fabrice Bellard
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. #include "avformat.h"
  20. #if !defined(CONFIG_NOCUTILS)
  21. /**
  22. * Return TRUE if val is a prefix of str. If it returns TRUE, ptr is
  23. * set to the next character in 'str' after the prefix.
  24. *
  25. * @param str input string
  26. * @param val prefix to test
  27. * @param ptr updated after the prefix in str in there is a match
  28. * @return TRUE if there is a match
  29. */
  30. int strstart(const char *str, const char *val, const char **ptr)
  31. {
  32. const char *p, *q;
  33. p = str;
  34. q = val;
  35. while (*q != '\0') {
  36. if (*p != *q)
  37. return 0;
  38. p++;
  39. q++;
  40. }
  41. if (ptr)
  42. *ptr = p;
  43. return 1;
  44. }
  45. /**
  46. * Return TRUE if val is a prefix of str (case independent). If it
  47. * returns TRUE, ptr is set to the next character in 'str' after the
  48. * prefix.
  49. *
  50. * @param str input string
  51. * @param val prefix to test
  52. * @param ptr updated after the prefix in str in there is a match
  53. * @return TRUE if there is a match */
  54. int stristart(const char *str, const char *val, const char **ptr)
  55. {
  56. const char *p, *q;
  57. p = str;
  58. q = val;
  59. while (*q != '\0') {
  60. if (toupper(*(const unsigned char *)p) != toupper(*(const unsigned char *)q))
  61. return 0;
  62. p++;
  63. q++;
  64. }
  65. if (ptr)
  66. *ptr = p;
  67. return 1;
  68. }
  69. /**
  70. * Copy the string str to buf. If str length is bigger than buf_size -
  71. * 1 then it is clamped to buf_size - 1.
  72. * NOTE: this function does what strncpy should have done to be
  73. * useful. NEVER use strncpy.
  74. *
  75. * @param buf destination buffer
  76. * @param buf_size size of destination buffer
  77. * @param str source string
  78. */
  79. void pstrcpy(char *buf, int buf_size, const char *str)
  80. {
  81. int c;
  82. char *q = buf;
  83. if (buf_size <= 0)
  84. return;
  85. for(;;) {
  86. c = *str++;
  87. if (c == 0 || q >= buf + buf_size - 1)
  88. break;
  89. *q++ = c;
  90. }
  91. *q = '\0';
  92. }
  93. /* strcat and truncate. */
  94. char *pstrcat(char *buf, int buf_size, const char *s)
  95. {
  96. int len;
  97. len = strlen(buf);
  98. if (len < buf_size)
  99. pstrcpy(buf + len, buf_size - len, s);
  100. return buf;
  101. }
  102. #endif
  103. /* add one element to a dynamic array */
  104. void __dynarray_add(unsigned long **tab_ptr, int *nb_ptr, unsigned long elem)
  105. {
  106. int nb, nb_alloc;
  107. unsigned long *tab;
  108. nb = *nb_ptr;
  109. tab = *tab_ptr;
  110. if ((nb & (nb - 1)) == 0) {
  111. if (nb == 0)
  112. nb_alloc = 1;
  113. else
  114. nb_alloc = nb * 2;
  115. tab = av_realloc(tab, nb_alloc * sizeof(unsigned long));
  116. *tab_ptr = tab;
  117. }
  118. tab[nb++] = elem;
  119. *nb_ptr = nb;
  120. }