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.

252 lines
6.6KB

  1. /*
  2. * Various simple utilities for ffmpeg system
  3. * Copyright (c) 2000, 2001, 2002 Fabrice Bellard
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include "avformat.h"
  22. #if !defined(CONFIG_NOCUTILS)
  23. /**
  24. * Return TRUE if val is a prefix of str. If it returns TRUE, ptr is
  25. * set to the next character in 'str' after the prefix.
  26. *
  27. * @param str input string
  28. * @param pfx prefix to test
  29. * @param ptr updated after the prefix in str in there is a match
  30. * @return TRUE if there is a match
  31. */
  32. int strstart(const char *str, const char *pfx, const char **ptr)
  33. {
  34. while (*pfx && *pfx++ == *str++);
  35. if (!*pfx && ptr)
  36. *ptr = str;
  37. return !*pfx;
  38. }
  39. /**
  40. * Return TRUE if val is a prefix of str (case independent). If it
  41. * returns TRUE, ptr is set to the next character in 'str' after the
  42. * prefix.
  43. *
  44. * @param str input string
  45. * @param pfx prefix to test
  46. * @param ptr updated after the prefix in str in there is a match
  47. * @return TRUE if there is a match */
  48. int stristart(const char *str, const char *pfx, const char **ptr)
  49. {
  50. while (*pfx && toupper((unsigned)*pfx++) == toupper((unsigned)*str++));
  51. if (!*pfx && ptr)
  52. *ptr = str;
  53. return !*pfx;
  54. }
  55. /**
  56. * Copy the string str to buf. If str length is bigger than buf_size -
  57. * 1 then it is clamped to buf_size - 1.
  58. * NOTE: this function does what strncpy should have done to be
  59. * useful. NEVER use strncpy.
  60. *
  61. * @param buf destination buffer
  62. * @param buf_size size of destination buffer
  63. * @param str source string
  64. */
  65. void pstrcpy(char *buf, int buf_size, const char *str)
  66. {
  67. if (buf_size <= 0)
  68. return;
  69. while (buf_size-- > 1 && *str)
  70. *buf++ = *str++;
  71. *buf = 0;
  72. }
  73. /* strcat and truncate. */
  74. char *pstrcat(char *buf, int buf_size, const char *s)
  75. {
  76. int len = strlen(buf);
  77. if (len < buf_size)
  78. pstrcpy(buf + len, buf_size - len, s);
  79. return buf;
  80. }
  81. #endif
  82. /* add one element to a dynamic array */
  83. void __dynarray_add(unsigned long **tab_ptr, int *nb_ptr, unsigned long elem)
  84. {
  85. int nb, nb_alloc;
  86. unsigned long *tab;
  87. nb = *nb_ptr;
  88. tab = *tab_ptr;
  89. if ((nb & (nb - 1)) == 0) {
  90. if (nb == 0)
  91. nb_alloc = 1;
  92. else
  93. nb_alloc = nb * 2;
  94. tab = av_realloc(tab, nb_alloc * sizeof(unsigned long));
  95. *tab_ptr = tab;
  96. }
  97. tab[nb++] = elem;
  98. *nb_ptr = nb;
  99. }
  100. time_t mktimegm(struct tm *tm)
  101. {
  102. time_t t;
  103. int y = tm->tm_year + 1900, m = tm->tm_mon + 1, d = tm->tm_mday;
  104. if (m < 3) {
  105. m += 12;
  106. y--;
  107. }
  108. t = 86400 *
  109. (d + (153 * m - 457) / 5 + 365 * y + y / 4 - y / 100 + y / 400 - 719469);
  110. t += 3600 * tm->tm_hour + 60 * tm->tm_min + tm->tm_sec;
  111. return t;
  112. }
  113. #define ISLEAP(y) (((y) % 4 == 0) && (((y) % 100) != 0 || ((y) % 400) == 0))
  114. #define LEAPS_COUNT(y) ((y)/4 - (y)/100 + (y)/400)
  115. /* this is our own gmtime_r. it differs from its POSIX counterpart in a
  116. couple of places, though. */
  117. struct tm *brktimegm(time_t secs, struct tm *tm)
  118. {
  119. int days, y, ny, m;
  120. int md[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
  121. days = secs / 86400;
  122. secs %= 86400;
  123. tm->tm_hour = secs / 3600;
  124. tm->tm_min = (secs % 3600) / 60;
  125. tm->tm_sec = secs % 60;
  126. /* oh well, may be someone some day will invent a formula for this stuff */
  127. y = 1970; /* start "guessing" */
  128. while (days >= (ISLEAP(y)?366:365)) {
  129. ny = (y + days/366);
  130. days -= (ny - y) * 365 + LEAPS_COUNT(ny - 1) - LEAPS_COUNT(y - 1);
  131. y = ny;
  132. }
  133. md[1] = ISLEAP(y)?29:28;
  134. for (m=0; days >= md[m]; m++)
  135. days -= md[m];
  136. tm->tm_year = y; /* unlike gmtime_r we store complete year here */
  137. tm->tm_mon = m+1; /* unlike gmtime_r tm_mon is from 1 to 12 */
  138. tm->tm_mday = days+1;
  139. return tm;
  140. }
  141. /* get a positive number between n_min and n_max, for a maximum length
  142. of len_max. Return -1 if error. */
  143. static int date_get_num(const char **pp,
  144. int n_min, int n_max, int len_max)
  145. {
  146. int i, val, c;
  147. const char *p;
  148. p = *pp;
  149. val = 0;
  150. for(i = 0; i < len_max; i++) {
  151. c = *p;
  152. if (!isdigit(c))
  153. break;
  154. val = (val * 10) + c - '0';
  155. p++;
  156. }
  157. /* no number read ? */
  158. if (p == *pp)
  159. return -1;
  160. if (val < n_min || val > n_max)
  161. return -1;
  162. *pp = p;
  163. return val;
  164. }
  165. /* small strptime for ffmpeg */
  166. const char *small_strptime(const char *p, const char *fmt,
  167. struct tm *dt)
  168. {
  169. int c, val;
  170. for(;;) {
  171. c = *fmt++;
  172. if (c == '\0') {
  173. return p;
  174. } else if (c == '%') {
  175. c = *fmt++;
  176. switch(c) {
  177. case 'H':
  178. val = date_get_num(&p, 0, 23, 2);
  179. if (val == -1)
  180. return NULL;
  181. dt->tm_hour = val;
  182. break;
  183. case 'M':
  184. val = date_get_num(&p, 0, 59, 2);
  185. if (val == -1)
  186. return NULL;
  187. dt->tm_min = val;
  188. break;
  189. case 'S':
  190. val = date_get_num(&p, 0, 59, 2);
  191. if (val == -1)
  192. return NULL;
  193. dt->tm_sec = val;
  194. break;
  195. case 'Y':
  196. val = date_get_num(&p, 0, 9999, 4);
  197. if (val == -1)
  198. return NULL;
  199. dt->tm_year = val - 1900;
  200. break;
  201. case 'm':
  202. val = date_get_num(&p, 1, 12, 2);
  203. if (val == -1)
  204. return NULL;
  205. dt->tm_mon = val - 1;
  206. break;
  207. case 'd':
  208. val = date_get_num(&p, 1, 31, 2);
  209. if (val == -1)
  210. return NULL;
  211. dt->tm_mday = val;
  212. break;
  213. case '%':
  214. goto match;
  215. default:
  216. return NULL;
  217. }
  218. } else {
  219. match:
  220. if (c != *p)
  221. return NULL;
  222. p++;
  223. }
  224. }
  225. return p;
  226. }