cosmetic: combine declaration and initialisation
[libav.git] / libavformat / cutils.c
CommitLineData
2302dd13
FB
1/*
2 * Various simple utilities for ffmpeg system
3 * Copyright (c) 2000, 2001, 2002 Fabrice Bellard
4 *
b78e7197
DB
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
2302dd13
FB
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
b78e7197 10 * version 2.1 of the License, or (at your option) any later version.
2302dd13 11 *
b78e7197 12 * FFmpeg is distributed in the hope that it will be useful,
2302dd13
FB
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
b78e7197 18 * License along with FFmpeg; if not, write to the Free Software
5509bffa 19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
2302dd13
FB
20 */
21#include "avformat.h"
2302dd13
FB
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
12a6f289 29 * @param pfx prefix to test
2302dd13
FB
30 * @param ptr updated after the prefix in str in there is a match
31 * @return TRUE if there is a match
32 */
12a6f289 33int strstart(const char *str, const char *pfx, const char **ptr)
2302dd13 34{
12a6f289
MR
35 while (*pfx && *pfx++ == *str++);
36 if (!*pfx && ptr)
37 *ptr = str;
38 return !*pfx;
2302dd13
FB
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 */
50int 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') {
bb270c08 56 if (toupper(*(const unsigned char *)p) != toupper(*(const unsigned char *)q))
2302dd13
FB
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.
115329f1 71 *
2302dd13
FB
72 * @param buf destination buffer
73 * @param buf_size size of destination buffer
74 * @param str source string
75 */
76void pstrcpy(char *buf, int buf_size, const char *str)
77{
2302dd13
FB
78 if (buf_size <= 0)
79 return;
80
26301cb8
MR
81 while (buf_size-- > 1 && *str)
82 *buf++ = *str++;
83 *buf = 0;
2302dd13
FB
84}
85
86/* strcat and truncate. */
87char *pstrcat(char *buf, int buf_size, const char *s)
88{
315a2858 89 int len = strlen(buf);
115329f1 90 if (len < buf_size)
2302dd13
FB
91 pstrcpy(buf + len, buf_size - len, s);
92 return buf;
93}
94
95#endif
39f472c3
FB
96
97/* add one element to a dynamic array */
98void __dynarray_add(unsigned long **tab_ptr, int *nb_ptr, unsigned long elem)
99{
100 int nb, nb_alloc;
101 unsigned long *tab;
102
103 nb = *nb_ptr;
104 tab = *tab_ptr;
105 if ((nb & (nb - 1)) == 0) {
106 if (nb == 0)
107 nb_alloc = 1;
108 else
109 nb_alloc = nb * 2;
110 tab = av_realloc(tab, nb_alloc * sizeof(unsigned long));
111 *tab_ptr = tab;
112 }
113 tab[nb++] = elem;
114 *nb_ptr = nb;
115}
116
f71869a4
FB
117time_t mktimegm(struct tm *tm)
118{
119 time_t t;
120
121 int y = tm->tm_year + 1900, m = tm->tm_mon + 1, d = tm->tm_mday;
122
123 if (m < 3) {
124 m += 12;
125 y--;
126 }
127
115329f1 128 t = 86400 *
f71869a4
FB
129 (d + (153 * m - 457) / 5 + 365 * y + y / 4 - y / 100 + y / 400 - 719469);
130
131 t += 3600 * tm->tm_hour + 60 * tm->tm_min + tm->tm_sec;
132
133 return t;
134}
135
0c9fc6e1
RS
136#define ISLEAP(y) (((y) % 4 == 0) && (((y) % 100) != 0 || ((y) % 400) == 0))
137#define LEAPS_COUNT(y) ((y)/4 - (y)/100 + (y)/400)
138
115329f1 139/* this is our own gmtime_r. it differs from its POSIX counterpart in a
0c9fc6e1
RS
140 couple of places, though. */
141struct tm *brktimegm(time_t secs, struct tm *tm)
115329f1 142{
0c9fc6e1
RS
143 int days, y, ny, m;
144 int md[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
145
146 days = secs / 86400;
147 secs %= 86400;
148 tm->tm_hour = secs / 3600;
149 tm->tm_min = (secs % 3600) / 60;
150 tm->tm_sec = secs % 60;
151
152 /* oh well, may be someone some day will invent a formula for this stuff */
153 y = 1970; /* start "guessing" */
154 while (days >= (ISLEAP(y)?366:365)) {
bb270c08
DB
155 ny = (y + days/366);
156 days -= (ny - y) * 365 + LEAPS_COUNT(ny - 1) - LEAPS_COUNT(y - 1);
157 y = ny;
0c9fc6e1
RS
158 }
159 md[1] = ISLEAP(y)?29:28;
160 for (m=0; days >= md[m]; m++)
bb270c08 161 days -= md[m];
0c9fc6e1
RS
162
163 tm->tm_year = y; /* unlike gmtime_r we store complete year here */
164 tm->tm_mon = m+1; /* unlike gmtime_r tm_mon is from 1 to 12 */
165 tm->tm_mday = days+1;
166
167 return tm;
168}
169
f71869a4
FB
170/* get a positive number between n_min and n_max, for a maximum length
171 of len_max. Return -1 if error. */
172static int date_get_num(const char **pp,
173 int n_min, int n_max, int len_max)
174{
175 int i, val, c;
176 const char *p;
177
178 p = *pp;
179 val = 0;
180 for(i = 0; i < len_max; i++) {
181 c = *p;
182 if (!isdigit(c))
183 break;
184 val = (val * 10) + c - '0';
185 p++;
186 }
187 /* no number read ? */
188 if (p == *pp)
189 return -1;
190 if (val < n_min || val > n_max)
191 return -1;
192 *pp = p;
193 return val;
194}
195
196/* small strptime for ffmpeg */
115329f1 197const char *small_strptime(const char *p, const char *fmt,
f71869a4
FB
198 struct tm *dt)
199{
200 int c, val;
201
202 for(;;) {
203 c = *fmt++;
204 if (c == '\0') {
205 return p;
206 } else if (c == '%') {
207 c = *fmt++;
208 switch(c) {
209 case 'H':
210 val = date_get_num(&p, 0, 23, 2);
211 if (val == -1)
212 return NULL;
213 dt->tm_hour = val;
214 break;
215 case 'M':
216 val = date_get_num(&p, 0, 59, 2);
217 if (val == -1)
218 return NULL;
219 dt->tm_min = val;
220 break;
221 case 'S':
222 val = date_get_num(&p, 0, 59, 2);
223 if (val == -1)
224 return NULL;
225 dt->tm_sec = val;
226 break;
227 case 'Y':
228 val = date_get_num(&p, 0, 9999, 4);
229 if (val == -1)
230 return NULL;
231 dt->tm_year = val - 1900;
232 break;
233 case 'm':
234 val = date_get_num(&p, 1, 12, 2);
235 if (val == -1)
236 return NULL;
237 dt->tm_mon = val - 1;
238 break;
239 case 'd':
240 val = date_get_num(&p, 1, 31, 2);
241 if (val == -1)
242 return NULL;
243 dt->tm_mday = val;
244 break;
245 case '%':
246 goto match;
247 default:
248 return NULL;
249 }
250 } else {
251 match:
252 if (c != *p)
253 return NULL;
254 p++;
255 }
256 }
257 return p;
258}
259