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.

274 lines
6.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. #define ISLEAP(y) (((y) % 4 == 0) && (((y) % 100) != 0 || ((y) % 400) == 0))
  135. #define LEAPS_COUNT(y) ((y)/4 - (y)/100 + (y)/400)
  136. /* this is our own gmtime_r. it differs from its POSIX counterpart in a
  137. couple of places, though. */
  138. struct tm *brktimegm(time_t secs, struct tm *tm)
  139. {
  140. int days, y, ny, m;
  141. int md[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
  142. days = secs / 86400;
  143. secs %= 86400;
  144. tm->tm_hour = secs / 3600;
  145. tm->tm_min = (secs % 3600) / 60;
  146. tm->tm_sec = secs % 60;
  147. /* oh well, may be someone some day will invent a formula for this stuff */
  148. y = 1970; /* start "guessing" */
  149. while (days >= (ISLEAP(y)?366:365)) {
  150. ny = (y + days/366);
  151. days -= (ny - y) * 365 + LEAPS_COUNT(ny - 1) - LEAPS_COUNT(y - 1);
  152. y = ny;
  153. }
  154. md[1] = ISLEAP(y)?29:28;
  155. for (m=0; days >= md[m]; m++)
  156. days -= md[m];
  157. tm->tm_year = y; /* unlike gmtime_r we store complete year here */
  158. tm->tm_mon = m+1; /* unlike gmtime_r tm_mon is from 1 to 12 */
  159. tm->tm_mday = days+1;
  160. return tm;
  161. }
  162. /* get a positive number between n_min and n_max, for a maximum length
  163. of len_max. Return -1 if error. */
  164. static int date_get_num(const char **pp,
  165. int n_min, int n_max, int len_max)
  166. {
  167. int i, val, c;
  168. const char *p;
  169. p = *pp;
  170. val = 0;
  171. for(i = 0; i < len_max; i++) {
  172. c = *p;
  173. if (!isdigit(c))
  174. break;
  175. val = (val * 10) + c - '0';
  176. p++;
  177. }
  178. /* no number read ? */
  179. if (p == *pp)
  180. return -1;
  181. if (val < n_min || val > n_max)
  182. return -1;
  183. *pp = p;
  184. return val;
  185. }
  186. /* small strptime for ffmpeg */
  187. const char *small_strptime(const char *p, const char *fmt,
  188. struct tm *dt)
  189. {
  190. int c, val;
  191. for(;;) {
  192. c = *fmt++;
  193. if (c == '\0') {
  194. return p;
  195. } else if (c == '%') {
  196. c = *fmt++;
  197. switch(c) {
  198. case 'H':
  199. val = date_get_num(&p, 0, 23, 2);
  200. if (val == -1)
  201. return NULL;
  202. dt->tm_hour = val;
  203. break;
  204. case 'M':
  205. val = date_get_num(&p, 0, 59, 2);
  206. if (val == -1)
  207. return NULL;
  208. dt->tm_min = val;
  209. break;
  210. case 'S':
  211. val = date_get_num(&p, 0, 59, 2);
  212. if (val == -1)
  213. return NULL;
  214. dt->tm_sec = val;
  215. break;
  216. case 'Y':
  217. val = date_get_num(&p, 0, 9999, 4);
  218. if (val == -1)
  219. return NULL;
  220. dt->tm_year = val - 1900;
  221. break;
  222. case 'm':
  223. val = date_get_num(&p, 1, 12, 2);
  224. if (val == -1)
  225. return NULL;
  226. dt->tm_mon = val - 1;
  227. break;
  228. case 'd':
  229. val = date_get_num(&p, 1, 31, 2);
  230. if (val == -1)
  231. return NULL;
  232. dt->tm_mday = val;
  233. break;
  234. case '%':
  235. goto match;
  236. default:
  237. return NULL;
  238. }
  239. } else {
  240. match:
  241. if (c != *p)
  242. return NULL;
  243. p++;
  244. }
  245. }
  246. return p;
  247. }