Libav
|
00001 /* 00002 * Various simple utilities for ffmpeg system 00003 * Copyright (c) 2000, 2001, 2002 Fabrice Bellard 00004 * 00005 * This file is part of FFmpeg. 00006 * 00007 * FFmpeg is free software; you can redistribute it and/or 00008 * modify it under the terms of the GNU Lesser General Public 00009 * License as published by the Free Software Foundation; either 00010 * version 2.1 of the License, or (at your option) any later version. 00011 * 00012 * FFmpeg is distributed in the hope that it will be useful, 00013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00015 * Lesser General Public License for more details. 00016 * 00017 * You should have received a copy of the GNU Lesser General Public 00018 * License along with FFmpeg; if not, write to the Free Software 00019 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 00020 */ 00021 #include "avformat.h" 00022 #include "internal.h" 00023 00024 /* add one element to a dynamic array */ 00025 void ff_dynarray_add(intptr_t **tab_ptr, int *nb_ptr, intptr_t elem) 00026 { 00027 int nb, nb_alloc; 00028 intptr_t *tab; 00029 00030 nb = *nb_ptr; 00031 tab = *tab_ptr; 00032 if ((nb & (nb - 1)) == 0) { 00033 if (nb == 0) 00034 nb_alloc = 1; 00035 else 00036 nb_alloc = nb * 2; 00037 tab = av_realloc(tab, nb_alloc * sizeof(intptr_t)); 00038 *tab_ptr = tab; 00039 } 00040 tab[nb++] = elem; 00041 *nb_ptr = nb; 00042 } 00043 00044 time_t mktimegm(struct tm *tm) 00045 { 00046 time_t t; 00047 00048 int y = tm->tm_year + 1900, m = tm->tm_mon + 1, d = tm->tm_mday; 00049 00050 if (m < 3) { 00051 m += 12; 00052 y--; 00053 } 00054 00055 t = 86400 * 00056 (d + (153 * m - 457) / 5 + 365 * y + y / 4 - y / 100 + y / 400 - 719469); 00057 00058 t += 3600 * tm->tm_hour + 60 * tm->tm_min + tm->tm_sec; 00059 00060 return t; 00061 } 00062 00063 #define ISLEAP(y) (((y) % 4 == 0) && (((y) % 100) != 0 || ((y) % 400) == 0)) 00064 #define LEAPS_COUNT(y) ((y)/4 - (y)/100 + (y)/400) 00065 00066 /* This is our own gmtime_r. It differs from its POSIX counterpart in a 00067 couple of places, though. */ 00068 struct tm *brktimegm(time_t secs, struct tm *tm) 00069 { 00070 int days, y, ny, m; 00071 int md[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; 00072 00073 days = secs / 86400; 00074 secs %= 86400; 00075 tm->tm_hour = secs / 3600; 00076 tm->tm_min = (secs % 3600) / 60; 00077 tm->tm_sec = secs % 60; 00078 00079 /* oh well, may be someone some day will invent a formula for this stuff */ 00080 y = 1970; /* start "guessing" */ 00081 while (days > 365) { 00082 ny = (y + days/366); 00083 days -= (ny - y) * 365 + LEAPS_COUNT(ny - 1) - LEAPS_COUNT(y - 1); 00084 y = ny; 00085 } 00086 if (days==365 && !ISLEAP(y)) { days=0; y++; } 00087 md[1] = ISLEAP(y)?29:28; 00088 for (m=0; days >= md[m]; m++) 00089 days -= md[m]; 00090 00091 tm->tm_year = y; /* unlike gmtime_r we store complete year here */ 00092 tm->tm_mon = m+1; /* unlike gmtime_r tm_mon is from 1 to 12 */ 00093 tm->tm_mday = days+1; 00094 00095 return tm; 00096 } 00097 00098 /* get a positive number between n_min and n_max, for a maximum length 00099 of len_max. Return -1 if error. */ 00100 static int date_get_num(const char **pp, 00101 int n_min, int n_max, int len_max) 00102 { 00103 int i, val, c; 00104 const char *p; 00105 00106 p = *pp; 00107 val = 0; 00108 for(i = 0; i < len_max; i++) { 00109 c = *p; 00110 if (!isdigit(c)) 00111 break; 00112 val = (val * 10) + c - '0'; 00113 p++; 00114 } 00115 /* no number read ? */ 00116 if (p == *pp) 00117 return -1; 00118 if (val < n_min || val > n_max) 00119 return -1; 00120 *pp = p; 00121 return val; 00122 } 00123 00124 /* small strptime for ffmpeg */ 00125 const char *small_strptime(const char *p, const char *fmt, 00126 struct tm *dt) 00127 { 00128 int c, val; 00129 00130 for(;;) { 00131 c = *fmt++; 00132 if (c == '\0') { 00133 return p; 00134 } else if (c == '%') { 00135 c = *fmt++; 00136 switch(c) { 00137 case 'H': 00138 val = date_get_num(&p, 0, 23, 2); 00139 if (val == -1) 00140 return NULL; 00141 dt->tm_hour = val; 00142 break; 00143 case 'M': 00144 val = date_get_num(&p, 0, 59, 2); 00145 if (val == -1) 00146 return NULL; 00147 dt->tm_min = val; 00148 break; 00149 case 'S': 00150 val = date_get_num(&p, 0, 59, 2); 00151 if (val == -1) 00152 return NULL; 00153 dt->tm_sec = val; 00154 break; 00155 case 'Y': 00156 val = date_get_num(&p, 0, 9999, 4); 00157 if (val == -1) 00158 return NULL; 00159 dt->tm_year = val - 1900; 00160 break; 00161 case 'm': 00162 val = date_get_num(&p, 1, 12, 2); 00163 if (val == -1) 00164 return NULL; 00165 dt->tm_mon = val - 1; 00166 break; 00167 case 'd': 00168 val = date_get_num(&p, 1, 31, 2); 00169 if (val == -1) 00170 return NULL; 00171 dt->tm_mday = val; 00172 break; 00173 case '%': 00174 goto match; 00175 default: 00176 return NULL; 00177 } 00178 } else { 00179 match: 00180 if (c != *p) 00181 return NULL; 00182 p++; 00183 } 00184 } 00185 return p; 00186 } 00187