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.

186 lines
5.0KB

  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. /* add one element to a dynamic array */
  23. void __dynarray_add(unsigned long **tab_ptr, int *nb_ptr, unsigned long elem)
  24. {
  25. int nb, nb_alloc;
  26. unsigned long *tab;
  27. nb = *nb_ptr;
  28. tab = *tab_ptr;
  29. if ((nb & (nb - 1)) == 0) {
  30. if (nb == 0)
  31. nb_alloc = 1;
  32. else
  33. nb_alloc = nb * 2;
  34. tab = av_realloc(tab, nb_alloc * sizeof(unsigned long));
  35. *tab_ptr = tab;
  36. }
  37. tab[nb++] = elem;
  38. *nb_ptr = nb;
  39. }
  40. time_t mktimegm(struct tm *tm)
  41. {
  42. time_t t;
  43. int y = tm->tm_year + 1900, m = tm->tm_mon + 1, d = tm->tm_mday;
  44. if (m < 3) {
  45. m += 12;
  46. y--;
  47. }
  48. t = 86400 *
  49. (d + (153 * m - 457) / 5 + 365 * y + y / 4 - y / 100 + y / 400 - 719469);
  50. t += 3600 * tm->tm_hour + 60 * tm->tm_min + tm->tm_sec;
  51. return t;
  52. }
  53. #define ISLEAP(y) (((y) % 4 == 0) && (((y) % 100) != 0 || ((y) % 400) == 0))
  54. #define LEAPS_COUNT(y) ((y)/4 - (y)/100 + (y)/400)
  55. /* This is our own gmtime_r. It differs from its POSIX counterpart in a
  56. couple of places, though. */
  57. struct tm *brktimegm(time_t secs, struct tm *tm)
  58. {
  59. int days, y, ny, m;
  60. int md[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
  61. days = secs / 86400;
  62. secs %= 86400;
  63. tm->tm_hour = secs / 3600;
  64. tm->tm_min = (secs % 3600) / 60;
  65. tm->tm_sec = secs % 60;
  66. /* oh well, may be someone some day will invent a formula for this stuff */
  67. y = 1970; /* start "guessing" */
  68. while (days >= (ISLEAP(y)?366:365)) {
  69. ny = (y + days/366);
  70. days -= (ny - y) * 365 + LEAPS_COUNT(ny - 1) - LEAPS_COUNT(y - 1);
  71. y = ny;
  72. }
  73. md[1] = ISLEAP(y)?29:28;
  74. for (m=0; days >= md[m]; m++)
  75. days -= md[m];
  76. tm->tm_year = y; /* unlike gmtime_r we store complete year here */
  77. tm->tm_mon = m+1; /* unlike gmtime_r tm_mon is from 1 to 12 */
  78. tm->tm_mday = days+1;
  79. return tm;
  80. }
  81. /* get a positive number between n_min and n_max, for a maximum length
  82. of len_max. Return -1 if error. */
  83. static int date_get_num(const char **pp,
  84. int n_min, int n_max, int len_max)
  85. {
  86. int i, val, c;
  87. const char *p;
  88. p = *pp;
  89. val = 0;
  90. for(i = 0; i < len_max; i++) {
  91. c = *p;
  92. if (!isdigit(c))
  93. break;
  94. val = (val * 10) + c - '0';
  95. p++;
  96. }
  97. /* no number read ? */
  98. if (p == *pp)
  99. return -1;
  100. if (val < n_min || val > n_max)
  101. return -1;
  102. *pp = p;
  103. return val;
  104. }
  105. /* small strptime for ffmpeg */
  106. const char *small_strptime(const char *p, const char *fmt,
  107. struct tm *dt)
  108. {
  109. int c, val;
  110. for(;;) {
  111. c = *fmt++;
  112. if (c == '\0') {
  113. return p;
  114. } else if (c == '%') {
  115. c = *fmt++;
  116. switch(c) {
  117. case 'H':
  118. val = date_get_num(&p, 0, 23, 2);
  119. if (val == -1)
  120. return NULL;
  121. dt->tm_hour = val;
  122. break;
  123. case 'M':
  124. val = date_get_num(&p, 0, 59, 2);
  125. if (val == -1)
  126. return NULL;
  127. dt->tm_min = val;
  128. break;
  129. case 'S':
  130. val = date_get_num(&p, 0, 59, 2);
  131. if (val == -1)
  132. return NULL;
  133. dt->tm_sec = val;
  134. break;
  135. case 'Y':
  136. val = date_get_num(&p, 0, 9999, 4);
  137. if (val == -1)
  138. return NULL;
  139. dt->tm_year = val - 1900;
  140. break;
  141. case 'm':
  142. val = date_get_num(&p, 1, 12, 2);
  143. if (val == -1)
  144. return NULL;
  145. dt->tm_mon = val - 1;
  146. break;
  147. case 'd':
  148. val = date_get_num(&p, 1, 31, 2);
  149. if (val == -1)
  150. return NULL;
  151. dt->tm_mday = val;
  152. break;
  153. case '%':
  154. goto match;
  155. default:
  156. return NULL;
  157. }
  158. } else {
  159. match:
  160. if (c != *p)
  161. return NULL;
  162. p++;
  163. }
  164. }
  165. return p;
  166. }