Statistics
| Branch: | Revision:

ffmpeg / libavformat / cutils.c @ b973f9ad

History | View | Annotate | Download (4.97 KB)

1
/*
2
 * Various simple utilities for ffmpeg system
3
 * Copyright (c) 2000, 2001, 2002 Fabrice Bellard
4
 *
5
 * This file is part of FFmpeg.
6
 *
7
 * FFmpeg is free software; you can redistribute it and/or
8
 * modify it under the terms of the GNU Lesser General Public
9
 * License as published by the Free Software Foundation; either
10
 * version 2.1 of the License, or (at your option) any later version.
11
 *
12
 * FFmpeg is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
 * Lesser General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Lesser General Public
18
 * License along with FFmpeg; if not, write to the Free Software
19
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20
 */
21
#include "avformat.h"
22

    
23
/* add one element to a dynamic array */
24
void __dynarray_add(unsigned long **tab_ptr, int *nb_ptr, unsigned long elem)
25
{
26
    int nb, nb_alloc;
27
    unsigned long *tab;
28

    
29
    nb = *nb_ptr;
30
    tab = *tab_ptr;
31
    if ((nb & (nb - 1)) == 0) {
32
        if (nb == 0)
33
            nb_alloc = 1;
34
        else
35
            nb_alloc = nb * 2;
36
        tab = av_realloc(tab, nb_alloc * sizeof(unsigned long));
37
        *tab_ptr = tab;
38
    }
39
    tab[nb++] = elem;
40
    *nb_ptr = nb;
41
}
42

    
43
time_t mktimegm(struct tm *tm)
44
{
45
    time_t t;
46

    
47
    int y = tm->tm_year + 1900, m = tm->tm_mon + 1, d = tm->tm_mday;
48

    
49
    if (m < 3) {
50
        m += 12;
51
        y--;
52
    }
53

    
54
    t = 86400 *
55
        (d + (153 * m - 457) / 5 + 365 * y + y / 4 - y / 100 + y / 400 - 719469);
56

    
57
    t += 3600 * tm->tm_hour + 60 * tm->tm_min + tm->tm_sec;
58

    
59
    return t;
60
}
61

    
62
#define ISLEAP(y) (((y) % 4 == 0) && (((y) % 100) != 0 || ((y) % 400) == 0))
63
#define LEAPS_COUNT(y) ((y)/4 - (y)/100 + (y)/400)
64

    
65
/* This is our own gmtime_r. It differs from its POSIX counterpart in a
66
   couple of places, though. */
67
struct tm *brktimegm(time_t secs, struct tm *tm)
68
{
69
    int days, y, ny, m;
70
    int md[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
71

    
72
    days = secs / 86400;
73
    secs %= 86400;
74
    tm->tm_hour = secs / 3600;
75
    tm->tm_min = (secs % 3600) / 60;
76
    tm->tm_sec =  secs % 60;
77

    
78
    /* oh well, may be someone some day will invent a formula for this stuff */
79
    y = 1970; /* start "guessing" */
80
    while (days >= (ISLEAP(y)?366:365)) {
81
        ny = (y + days/366);
82
        days -= (ny - y) * 365 + LEAPS_COUNT(ny - 1) - LEAPS_COUNT(y - 1);
83
        y = ny;
84
    }
85
    md[1] = ISLEAP(y)?29:28;
86
    for (m=0; days >= md[m]; m++)
87
         days -= md[m];
88

    
89
    tm->tm_year = y;  /* unlike gmtime_r we store complete year here */
90
    tm->tm_mon = m+1; /* unlike gmtime_r tm_mon is from 1 to 12 */
91
    tm->tm_mday = days+1;
92

    
93
    return tm;
94
}
95

    
96
/* get a positive number between n_min and n_max, for a maximum length
97
   of len_max. Return -1 if error. */
98
static int date_get_num(const char **pp,
99
                        int n_min, int n_max, int len_max)
100
{
101
    int i, val, c;
102
    const char *p;
103

    
104
    p = *pp;
105
    val = 0;
106
    for(i = 0; i < len_max; i++) {
107
        c = *p;
108
        if (!isdigit(c))
109
            break;
110
        val = (val * 10) + c - '0';
111
        p++;
112
    }
113
    /* no number read ? */
114
    if (p == *pp)
115
        return -1;
116
    if (val < n_min || val > n_max)
117
        return -1;
118
    *pp = p;
119
    return val;
120
}
121

    
122
/* small strptime for ffmpeg */
123
const char *small_strptime(const char *p, const char *fmt,
124
                           struct tm *dt)
125
{
126
    int c, val;
127

    
128
    for(;;) {
129
        c = *fmt++;
130
        if (c == '\0') {
131
            return p;
132
        } else if (c == '%') {
133
            c = *fmt++;
134
            switch(c) {
135
            case 'H':
136
                val = date_get_num(&p, 0, 23, 2);
137
                if (val == -1)
138
                    return NULL;
139
                dt->tm_hour = val;
140
                break;
141
            case 'M':
142
                val = date_get_num(&p, 0, 59, 2);
143
                if (val == -1)
144
                    return NULL;
145
                dt->tm_min = val;
146
                break;
147
            case 'S':
148
                val = date_get_num(&p, 0, 59, 2);
149
                if (val == -1)
150
                    return NULL;
151
                dt->tm_sec = val;
152
                break;
153
            case 'Y':
154
                val = date_get_num(&p, 0, 9999, 4);
155
                if (val == -1)
156
                    return NULL;
157
                dt->tm_year = val - 1900;
158
                break;
159
            case 'm':
160
                val = date_get_num(&p, 1, 12, 2);
161
                if (val == -1)
162
                    return NULL;
163
                dt->tm_mon = val - 1;
164
                break;
165
            case 'd':
166
                val = date_get_num(&p, 1, 31, 2);
167
                if (val == -1)
168
                    return NULL;
169
                dt->tm_mday = val;
170
                break;
171
            case '%':
172
                goto match;
173
            default:
174
                return NULL;
175
            }
176
        } else {
177
        match:
178
            if (c != *p)
179
                return NULL;
180
            p++;
181
        }
182
    }
183
    return p;
184
}
185