cutils.c 5.0 KB

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