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 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
00067
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
00080 y = 1970;
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;
00092 tm->tm_mon = m+1;
00093 tm->tm_mday = days+1;
00094
00095 return tm;
00096 }
00097
00098
00099
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
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
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