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.

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