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