simplify strstart()
[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{
78 int c;
79 char *q = buf;
80
81 if (buf_size <= 0)
82 return;
83
84 for(;;) {
85 c = *str++;
86 if (c == 0 || q >= buf + buf_size - 1)
87 break;
88 *q++ = c;
89 }
90 *q = '\0';
91}
92
93/* strcat and truncate. */
94char *pstrcat(char *buf, int buf_size, const char *s)
95{
96 int len;
97 len = strlen(buf);
115329f1 98 if (len < buf_size)
2302dd13
FB
99 pstrcpy(buf + len, buf_size - len, s);
100 return buf;
101}
102
103#endif
39f472c3
FB
104
105/* add one element to a dynamic array */
106void __dynarray_add(unsigned long **tab_ptr, int *nb_ptr, unsigned long elem)
107{
108 int nb, nb_alloc;
109 unsigned long *tab;
110
111 nb = *nb_ptr;
112 tab = *tab_ptr;
113 if ((nb & (nb - 1)) == 0) {
114 if (nb == 0)
115 nb_alloc = 1;
116 else
117 nb_alloc = nb * 2;
118 tab = av_realloc(tab, nb_alloc * sizeof(unsigned long));
119 *tab_ptr = tab;
120 }
121 tab[nb++] = elem;
122 *nb_ptr = nb;
123}
124
f71869a4
FB
125time_t mktimegm(struct tm *tm)
126{
127 time_t t;
128
129 int y = tm->tm_year + 1900, m = tm->tm_mon + 1, d = tm->tm_mday;
130
131 if (m < 3) {
132 m += 12;
133 y--;
134 }
135
115329f1 136 t = 86400 *
f71869a4
FB
137 (d + (153 * m - 457) / 5 + 365 * y + y / 4 - y / 100 + y / 400 - 719469);
138
139 t += 3600 * tm->tm_hour + 60 * tm->tm_min + tm->tm_sec;
140
141 return t;
142}
143
0c9fc6e1
RS
144#define ISLEAP(y) (((y) % 4 == 0) && (((y) % 100) != 0 || ((y) % 400) == 0))
145#define LEAPS_COUNT(y) ((y)/4 - (y)/100 + (y)/400)
146
115329f1 147/* this is our own gmtime_r. it differs from its POSIX counterpart in a
0c9fc6e1
RS
148 couple of places, though. */
149struct tm *brktimegm(time_t secs, struct tm *tm)
115329f1 150{
0c9fc6e1
RS
151 int days, y, ny, m;
152 int md[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
153
154 days = secs / 86400;
155 secs %= 86400;
156 tm->tm_hour = secs / 3600;
157 tm->tm_min = (secs % 3600) / 60;
158 tm->tm_sec = secs % 60;
159
160 /* oh well, may be someone some day will invent a formula for this stuff */
161 y = 1970; /* start "guessing" */
162 while (days >= (ISLEAP(y)?366:365)) {
bb270c08
DB
163 ny = (y + days/366);
164 days -= (ny - y) * 365 + LEAPS_COUNT(ny - 1) - LEAPS_COUNT(y - 1);
165 y = ny;
0c9fc6e1
RS
166 }
167 md[1] = ISLEAP(y)?29:28;
168 for (m=0; days >= md[m]; m++)
bb270c08 169 days -= md[m];
0c9fc6e1
RS
170
171 tm->tm_year = y; /* unlike gmtime_r we store complete year here */
172 tm->tm_mon = m+1; /* unlike gmtime_r tm_mon is from 1 to 12 */
173 tm->tm_mday = days+1;
174
175 return tm;
176}
177
f71869a4
FB
178/* get a positive number between n_min and n_max, for a maximum length
179 of len_max. Return -1 if error. */
180static int date_get_num(const char **pp,
181 int n_min, int n_max, int len_max)
182{
183 int i, val, c;
184 const char *p;
185
186 p = *pp;
187 val = 0;
188 for(i = 0; i < len_max; i++) {
189 c = *p;
190 if (!isdigit(c))
191 break;
192 val = (val * 10) + c - '0';
193 p++;
194 }
195 /* no number read ? */
196 if (p == *pp)
197 return -1;
198 if (val < n_min || val > n_max)
199 return -1;
200 *pp = p;
201 return val;
202}
203
204/* small strptime for ffmpeg */
115329f1 205const char *small_strptime(const char *p, const char *fmt,
f71869a4
FB
206 struct tm *dt)
207{
208 int c, val;
209
210 for(;;) {
211 c = *fmt++;
212 if (c == '\0') {
213 return p;
214 } else if (c == '%') {
215 c = *fmt++;
216 switch(c) {
217 case 'H':
218 val = date_get_num(&p, 0, 23, 2);
219 if (val == -1)
220 return NULL;
221 dt->tm_hour = val;
222 break;
223 case 'M':
224 val = date_get_num(&p, 0, 59, 2);
225 if (val == -1)
226 return NULL;
227 dt->tm_min = val;
228 break;
229 case 'S':
230 val = date_get_num(&p, 0, 59, 2);
231 if (val == -1)
232 return NULL;
233 dt->tm_sec = val;
234 break;
235 case 'Y':
236 val = date_get_num(&p, 0, 9999, 4);
237 if (val == -1)
238 return NULL;
239 dt->tm_year = val - 1900;
240 break;
241 case 'm':
242 val = date_get_num(&p, 1, 12, 2);
243 if (val == -1)
244 return NULL;
245 dt->tm_mon = val - 1;
246 break;
247 case 'd':
248 val = date_get_num(&p, 1, 31, 2);
249 if (val == -1)
250 return NULL;
251 dt->tm_mday = val;
252 break;
253 case '%':
254 goto match;
255 default:
256 return NULL;
257 }
258 } else {
259 match:
260 if (c != *p)
261 return NULL;
262 p++;
263 }
264 }
265 return p;
266}
267