cutils.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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 ff_dynarray_add(intptr_t **tab_ptr, int *nb_ptr, intptr_t elem)
  24. {
  25. int nb, nb_alloc;
  26. intptr_t *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(intptr_t));
  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 > 365) {
  69. ny = (y + days/366);
  70. days -= (ny - y) * 365 + LEAPS_COUNT(ny - 1) - LEAPS_COUNT(y - 1);
  71. y = ny;
  72. }
  73. if (days==365 && !ISLEAP(y)) { days=0; y++; }
  74. md[1] = ISLEAP(y)?29:28;
  75. for (m=0; days >= md[m]; m++)
  76. days -= md[m];
  77. tm->tm_year = y; /* unlike gmtime_r we store complete year here */
  78. tm->tm_mon = m+1; /* unlike gmtime_r tm_mon is from 1 to 12 */
  79. tm->tm_mday = days+1;
  80. return tm;
  81. }
  82. /* get a positive number between n_min and n_max, for a maximum length
  83. of len_max. Return -1 if error. */
  84. static int date_get_num(const char **pp,
  85. int n_min, int n_max, int len_max)
  86. {
  87. int i, val, c;
  88. const char *p;
  89. p = *pp;
  90. val = 0;
  91. for(i = 0; i < len_max; i++) {
  92. c = *p;
  93. if (!isdigit(c))
  94. break;
  95. val = (val * 10) + c - '0';
  96. p++;
  97. }
  98. /* no number read ? */
  99. if (p == *pp)
  100. return -1;
  101. if (val < n_min || val > n_max)
  102. return -1;
  103. *pp = p;
  104. return val;
  105. }
  106. /* small strptime for ffmpeg */
  107. const char *small_strptime(const char *p, const char *fmt,
  108. struct tm *dt)
  109. {
  110. int c, val;
  111. for(;;) {
  112. c = *fmt++;
  113. if (c == '\0') {
  114. return p;
  115. } else if (c == '%') {
  116. c = *fmt++;
  117. switch(c) {
  118. case 'H':
  119. val = date_get_num(&p, 0, 23, 2);
  120. if (val == -1)
  121. return NULL;
  122. dt->tm_hour = val;
  123. break;
  124. case 'M':
  125. val = date_get_num(&p, 0, 59, 2);
  126. if (val == -1)
  127. return NULL;
  128. dt->tm_min = val;
  129. break;
  130. case 'S':
  131. val = date_get_num(&p, 0, 59, 2);
  132. if (val == -1)
  133. return NULL;
  134. dt->tm_sec = val;
  135. break;
  136. case 'Y':
  137. val = date_get_num(&p, 0, 9999, 4);
  138. if (val == -1)
  139. return NULL;
  140. dt->tm_year = val - 1900;
  141. break;
  142. case 'm':
  143. val = date_get_num(&p, 1, 12, 2);
  144. if (val == -1)
  145. return NULL;
  146. dt->tm_mon = val - 1;
  147. break;
  148. case 'd':
  149. val = date_get_num(&p, 1, 31, 2);
  150. if (val == -1)
  151. return NULL;
  152. dt->tm_mday = val;
  153. break;
  154. case '%':
  155. goto match;
  156. default:
  157. return NULL;
  158. }
  159. } else {
  160. match:
  161. if (c != *p)
  162. return NULL;
  163. p++;
  164. }
  165. }
  166. return p;
  167. }