• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • File List
  • Globals

libavformat/cutils.c

Go to the documentation of this file.
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 
00023 /* add one element to a dynamic array */
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 /* This is our own gmtime_r. It differs from its POSIX counterpart in a
00066    couple of places, though. */
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     /* oh well, may be someone some day will invent a formula for this stuff */
00079     y = 1970; /* start "guessing" */
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;  /* unlike gmtime_r we store complete year here */
00091     tm->tm_mon = m+1; /* unlike gmtime_r tm_mon is from 1 to 12 */
00092     tm->tm_mday = days+1;
00093 
00094     return tm;
00095 }
00096 
00097 /* get a positive number between n_min and n_max, for a maximum length
00098    of len_max. Return -1 if error. */
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     /* no number read ? */
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 /* small strptime for ffmpeg */
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 

Generated on Tue Nov 4 2014 12:59:23 for ffmpeg by  doxygen 1.7.1