00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "avformat.h"
00022 #include "internal.h"
00023
00024
00025 void ff_dynarray_add(intptr_t **tab_ptr, int *nb_ptr, intptr_t elem)
00026 {
00027
00028 int nb, nb_alloc;
00029 intptr_t *tab;
00030
00031 nb = *nb_ptr;
00032 tab = *tab_ptr;
00033 if ((nb & (nb - 1)) == 0) {
00034 if (nb == 0)
00035 nb_alloc = 1;
00036 else
00037 nb_alloc = nb * 2;
00038 tab = av_realloc(tab, nb_alloc * sizeof(intptr_t));
00039 *tab_ptr = tab;
00040 }
00041 tab[nb++] = elem;
00042 *nb_ptr = nb;
00043 }
00044
00045 time_t mktimegm(struct tm *tm)
00046 {
00047 time_t t;
00048
00049 int y = tm->tm_year + 1900, m = tm->tm_mon + 1, d = tm->tm_mday;
00050
00051 if (m < 3) {
00052 m += 12;
00053 y--;
00054 }
00055
00056 t = 86400 *
00057 (d + (153 * m - 457) / 5 + 365 * y + y / 4 - y / 100 + y / 400 - 719469);
00058
00059 t += 3600 * tm->tm_hour + 60 * tm->tm_min + tm->tm_sec;
00060
00061 return t;
00062 }
00063
00064 #define ISLEAP(y) (((y) % 4 == 0) && (((y) % 100) != 0 || ((y) % 400) == 0))
00065 #define LEAPS_COUNT(y) ((y)/4 - (y)/100 + (y)/400)
00066
00067
00068
00069 struct tm *brktimegm(time_t secs, struct tm *tm)
00070 {
00071 int days, y, ny, m;
00072 int md[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
00073
00074 days = secs / 86400;
00075 secs %= 86400;
00076 tm->tm_hour = secs / 3600;
00077 tm->tm_min = (secs % 3600) / 60;
00078 tm->tm_sec = secs % 60;
00079
00080
00081 y = 1970;
00082 while (days > 365) {
00083 ny = (y + days/366);
00084 days -= (ny - y) * 365 + LEAPS_COUNT(ny - 1) - LEAPS_COUNT(y - 1);
00085 y = ny;
00086 }
00087 if (days==365 && !ISLEAP(y)) { days=0; y++; }
00088 md[1] = ISLEAP(y)?29:28;
00089 for (m=0; days >= md[m]; m++)
00090 days -= md[m];
00091
00092 tm->tm_year = y;
00093 tm->tm_mon = m+1;
00094 tm->tm_mday = days+1;
00095
00096 return tm;
00097 }
00098
00099
00100
00101 static int date_get_num(const char **pp,
00102 int n_min, int n_max, int len_max)
00103 {
00104 int i, val, c;
00105 const char *p;
00106
00107 p = *pp;
00108 val = 0;
00109 for(i = 0; i < len_max; i++) {
00110 c = *p;
00111 if (!isdigit(c))
00112 break;
00113 val = (val * 10) + c - '0';
00114 p++;
00115 }
00116
00117 if (p == *pp)
00118 return -1;
00119 if (val < n_min || val > n_max)
00120 return -1;
00121 *pp = p;
00122 return val;
00123 }
00124
00125
00126 const char *small_strptime(const char *p, const char *fmt,
00127 struct tm *dt)
00128 {
00129 int c, val;
00130
00131 for(;;) {
00132 c = *fmt++;
00133 if (c == '\0') {
00134 return p;
00135 } else if (c == '%') {
00136 c = *fmt++;
00137 switch(c) {
00138 case 'H':
00139 val = date_get_num(&p, 0, 23, 2);
00140 if (val == -1)
00141 return NULL;
00142 dt->tm_hour = val;
00143 break;
00144 case 'M':
00145 val = date_get_num(&p, 0, 59, 2);
00146 if (val == -1)
00147 return NULL;
00148 dt->tm_min = val;
00149 break;
00150 case 'S':
00151 val = date_get_num(&p, 0, 59, 2);
00152 if (val == -1)
00153 return NULL;
00154 dt->tm_sec = val;
00155 break;
00156 case 'Y':
00157 val = date_get_num(&p, 0, 9999, 4);
00158 if (val == -1)
00159 return NULL;
00160 dt->tm_year = val - 1900;
00161 break;
00162 case 'm':
00163 val = date_get_num(&p, 1, 12, 2);
00164 if (val == -1)
00165 return NULL;
00166 dt->tm_mon = val - 1;
00167 break;
00168 case 'd':
00169 val = date_get_num(&p, 1, 31, 2);
00170 if (val == -1)
00171 return NULL;
00172 dt->tm_mday = val;
00173 break;
00174 case '%':
00175 goto match;
00176 default:
00177 return NULL;
00178 }
00179 } else {
00180 match:
00181 if (c != *p)
00182 return NULL;
00183 p++;
00184 }
00185 }
00186 return p;
00187 }
00188