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.

240 lines
5.9KB

  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. }
  121. time_t mktimegm(struct tm *tm)
  122. {
  123. time_t t;
  124. int y = tm->tm_year + 1900, m = tm->tm_mon + 1, d = tm->tm_mday;
  125. if (m < 3) {
  126. m += 12;
  127. y--;
  128. }
  129. t = 86400 *
  130. (d + (153 * m - 457) / 5 + 365 * y + y / 4 - y / 100 + y / 400 - 719469);
  131. t += 3600 * tm->tm_hour + 60 * tm->tm_min + tm->tm_sec;
  132. return t;
  133. }
  134. /* get a positive number between n_min and n_max, for a maximum length
  135. of len_max. Return -1 if error. */
  136. static int date_get_num(const char **pp,
  137. int n_min, int n_max, int len_max)
  138. {
  139. int i, val, c;
  140. const char *p;
  141. p = *pp;
  142. val = 0;
  143. for(i = 0; i < len_max; i++) {
  144. c = *p;
  145. if (!isdigit(c))
  146. break;
  147. val = (val * 10) + c - '0';
  148. p++;
  149. }
  150. /* no number read ? */
  151. if (p == *pp)
  152. return -1;
  153. if (val < n_min || val > n_max)
  154. return -1;
  155. *pp = p;
  156. return val;
  157. }
  158. /* small strptime for ffmpeg */
  159. const char *small_strptime(const char *p, const char *fmt,
  160. struct tm *dt)
  161. {
  162. int c, val;
  163. for(;;) {
  164. c = *fmt++;
  165. if (c == '\0') {
  166. return p;
  167. } else if (c == '%') {
  168. c = *fmt++;
  169. switch(c) {
  170. case 'H':
  171. val = date_get_num(&p, 0, 23, 2);
  172. if (val == -1)
  173. return NULL;
  174. dt->tm_hour = val;
  175. break;
  176. case 'M':
  177. val = date_get_num(&p, 0, 59, 2);
  178. if (val == -1)
  179. return NULL;
  180. dt->tm_min = val;
  181. break;
  182. case 'S':
  183. val = date_get_num(&p, 0, 59, 2);
  184. if (val == -1)
  185. return NULL;
  186. dt->tm_sec = val;
  187. break;
  188. case 'Y':
  189. val = date_get_num(&p, 0, 9999, 4);
  190. if (val == -1)
  191. return NULL;
  192. dt->tm_year = val - 1900;
  193. break;
  194. case 'm':
  195. val = date_get_num(&p, 1, 12, 2);
  196. if (val == -1)
  197. return NULL;
  198. dt->tm_mon = val - 1;
  199. break;
  200. case 'd':
  201. val = date_get_num(&p, 1, 31, 2);
  202. if (val == -1)
  203. return NULL;
  204. dt->tm_mday = val;
  205. break;
  206. case '%':
  207. goto match;
  208. default:
  209. return NULL;
  210. }
  211. } else {
  212. match:
  213. if (c != *p)
  214. return NULL;
  215. p++;
  216. }
  217. }
  218. return p;
  219. }