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