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