55257a5a01fa7c8a5a9d16275ebcddb5286a1d63
[libav.git] / libavformat / cutils.c
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 #if !defined(CONFIG_NOCUTILS)
24 /**
25 * Return TRUE if val is a prefix of str. If it returns TRUE, ptr is
26 * set to the next character in 'str' after the prefix.
27 *
28 * @param str input string
29 * @param pfx prefix to test
30 * @param ptr updated after the prefix in str in there is a match
31 * @return TRUE if there is a match
32 */
33 int strstart(const char *str, const char *pfx, const char **ptr)
34 {
35 while (*pfx && *pfx++ == *str++);
36 if (!*pfx && ptr)
37 *ptr = str;
38 return !*pfx;
39 }
40
41 /**
42 * Return TRUE if val is a prefix of str (case independent). If it
43 * returns TRUE, ptr is set to the next character in 'str' after the
44 * prefix.
45 *
46 * @param str input string
47 * @param val prefix to test
48 * @param ptr updated after the prefix in str in there is a match
49 * @return TRUE if there is a match */
50 int stristart(const char *str, const char *val, const char **ptr)
51 {
52 const char *p, *q;
53 p = str;
54 q = val;
55 while (*q != '\0') {
56 if (toupper(*(const unsigned char *)p) != toupper(*(const unsigned char *)q))
57 return 0;
58 p++;
59 q++;
60 }
61 if (ptr)
62 *ptr = p;
63 return 1;
64 }
65
66 /**
67 * Copy the string str to buf. If str length is bigger than buf_size -
68 * 1 then it is clamped to buf_size - 1.
69 * NOTE: this function does what strncpy should have done to be
70 * useful. NEVER use strncpy.
71 *
72 * @param buf destination buffer
73 * @param buf_size size of destination buffer
74 * @param str source string
75 */
76 void pstrcpy(char *buf, int buf_size, const char *str)
77 {
78 if (buf_size <= 0)
79 return;
80
81 while (buf_size-- > 1 && *str)
82 *buf++ = *str++;
83 *buf = 0;
84 }
85
86 /* strcat and truncate. */
87 char *pstrcat(char *buf, int buf_size, const char *s)
88 {
89 int len;
90 len = strlen(buf);
91 if (len < buf_size)
92 pstrcpy(buf + len, buf_size - len, s);
93 return buf;
94 }
95
96 #endif
97
98 /* add one element to a dynamic array */
99 void __dynarray_add(unsigned long **tab_ptr, int *nb_ptr, unsigned long elem)
100 {
101 int nb, nb_alloc;
102 unsigned long *tab;
103
104 nb = *nb_ptr;
105 tab = *tab_ptr;
106 if ((nb & (nb - 1)) == 0) {
107 if (nb == 0)
108 nb_alloc = 1;
109 else
110 nb_alloc = nb * 2;
111 tab = av_realloc(tab, nb_alloc * sizeof(unsigned long));
112 *tab_ptr = tab;
113 }
114 tab[nb++] = elem;
115 *nb_ptr = nb;
116 }
117
118 time_t mktimegm(struct tm *tm)
119 {
120 time_t t;
121
122 int y = tm->tm_year + 1900, m = tm->tm_mon + 1, d = tm->tm_mday;
123
124 if (m < 3) {
125 m += 12;
126 y--;
127 }
128
129 t = 86400 *
130 (d + (153 * m - 457) / 5 + 365 * y + y / 4 - y / 100 + y / 400 - 719469);
131
132 t += 3600 * tm->tm_hour + 60 * tm->tm_min + tm->tm_sec;
133
134 return t;
135 }
136
137 #define ISLEAP(y) (((y) % 4 == 0) && (((y) % 100) != 0 || ((y) % 400) == 0))
138 #define LEAPS_COUNT(y) ((y)/4 - (y)/100 + (y)/400)
139
140 /* this is our own gmtime_r. it differs from its POSIX counterpart in a
141 couple of places, though. */
142 struct tm *brktimegm(time_t secs, struct tm *tm)
143 {
144 int days, y, ny, m;
145 int md[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
146
147 days = secs / 86400;
148 secs %= 86400;
149 tm->tm_hour = secs / 3600;
150 tm->tm_min = (secs % 3600) / 60;
151 tm->tm_sec = secs % 60;
152
153 /* oh well, may be someone some day will invent a formula for this stuff */
154 y = 1970; /* start "guessing" */
155 while (days >= (ISLEAP(y)?366:365)) {
156 ny = (y + days/366);
157 days -= (ny - y) * 365 + LEAPS_COUNT(ny - 1) - LEAPS_COUNT(y - 1);
158 y = ny;
159 }
160 md[1] = ISLEAP(y)?29:28;
161 for (m=0; days >= md[m]; m++)
162 days -= md[m];
163
164 tm->tm_year = y; /* unlike gmtime_r we store complete year here */
165 tm->tm_mon = m+1; /* unlike gmtime_r tm_mon is from 1 to 12 */
166 tm->tm_mday = days+1;
167
168 return tm;
169 }
170
171 /* get a positive number between n_min and n_max, for a maximum length
172 of len_max. Return -1 if error. */
173 static int date_get_num(const char **pp,
174 int n_min, int n_max, int len_max)
175 {
176 int i, val, c;
177 const char *p;
178
179 p = *pp;
180 val = 0;
181 for(i = 0; i < len_max; i++) {
182 c = *p;
183 if (!isdigit(c))
184 break;
185 val = (val * 10) + c - '0';
186 p++;
187 }
188 /* no number read ? */
189 if (p == *pp)
190 return -1;
191 if (val < n_min || val > n_max)
192 return -1;
193 *pp = p;
194 return val;
195 }
196
197 /* small strptime for ffmpeg */
198 const char *small_strptime(const char *p, const char *fmt,
199 struct tm *dt)
200 {
201 int c, val;
202
203 for(;;) {
204 c = *fmt++;
205 if (c == '\0') {
206 return p;
207 } else if (c == '%') {
208 c = *fmt++;
209 switch(c) {
210 case 'H':
211 val = date_get_num(&p, 0, 23, 2);
212 if (val == -1)
213 return NULL;
214 dt->tm_hour = val;
215 break;
216 case 'M':
217 val = date_get_num(&p, 0, 59, 2);
218 if (val == -1)
219 return NULL;
220 dt->tm_min = val;
221 break;
222 case 'S':
223 val = date_get_num(&p, 0, 59, 2);
224 if (val == -1)
225 return NULL;
226 dt->tm_sec = val;
227 break;
228 case 'Y':
229 val = date_get_num(&p, 0, 9999, 4);
230 if (val == -1)
231 return NULL;
232 dt->tm_year = val - 1900;
233 break;
234 case 'm':
235 val = date_get_num(&p, 1, 12, 2);
236 if (val == -1)
237 return NULL;
238 dt->tm_mon = val - 1;
239 break;
240 case 'd':
241 val = date_get_num(&p, 1, 31, 2);
242 if (val == -1)
243 return NULL;
244 dt->tm_mday = val;
245 break;
246 case '%':
247 goto match;
248 default:
249 return NULL;
250 }
251 } else {
252 match:
253 if (c != *p)
254 return NULL;
255 p++;
256 }
257 }
258 return p;
259 }
260