2 * Various simple utilities for ffmpeg system
3 * Copyright (c) 2000, 2001, 2002 Fabrice Bellard
5 * This file is part of FFmpeg.
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.
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.
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
23 #if !defined(CONFIG_NOCUTILS)
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.
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
33 int strstart(const char *str
, const char *pfx
, const char **ptr
)
35 while (*pfx
&& *pfx
++ == *str
++);
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
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
)
52 while (*pfx
&& toupper((unsigned)*pfx
++) == toupper((unsigned)*str
++));
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.
64 * @param buf destination buffer
65 * @param buf_size size of destination buffer
66 * @param str source string
68 void pstrcpy(char *buf
, int buf_size
, const char *str
)
73 while (buf_size
-- > 1 && *str
)
78 /* strcat and truncate. */
79 char *pstrcat(char *buf
, int buf_size
, const char *s
)
81 int len
= strlen(buf
);
83 pstrcpy(buf
+ len
, buf_size
- len
, s
);
89 /* add one element to a dynamic array */
90 void __dynarray_add(unsigned long **tab_ptr
, int *nb_ptr
, unsigned long elem
)
97 if ((nb
& (nb
- 1)) == 0) {
102 tab
= av_realloc(tab
, nb_alloc
* sizeof(unsigned long));
109 time_t mktimegm(struct tm
*tm
)
113 int y
= tm
->tm_year
+ 1900, m
= tm
->tm_mon
+ 1, d
= tm
->tm_mday
;
121 (d
+ (153 * m
- 457) / 5 + 365 * y
+ y
/ 4 - y
/ 100 + y
/ 400 - 719469);
123 t
+= 3600 * tm
->tm_hour
+ 60 * tm
->tm_min
+ tm
->tm_sec
;
128 #define ISLEAP(y) (((y) % 4 == 0) && (((y) % 100) != 0 || ((y) % 400) == 0))
129 #define LEAPS_COUNT(y) ((y)/4 - (y)/100 + (y)/400)
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
)
136 int md
[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
140 tm
->tm_hour
= secs
/ 3600;
141 tm
->tm_min
= (secs
% 3600) / 60;
142 tm
->tm_sec
= secs
% 60;
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)) {
148 days
-= (ny
- y
) * 365 + LEAPS_COUNT(ny
- 1) - LEAPS_COUNT(y
- 1);
151 md
[1] = ISLEAP(y
)?
29:28;
152 for (m
=0; days
>= md
[m
]; m
++)
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;
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
)
172 for(i
= 0; i
< len_max
; i
++) {
176 val
= (val
* 10) + c
- '0';
179 /* no number read ? */
182 if (val
< n_min
|| val
> n_max
)
188 /* small strptime for ffmpeg */
189 const char *small_strptime(const char *p
, const char *fmt
,
198 } else if (c
== '%') {
202 val
= date_get_num(&p
, 0, 23, 2);
208 val
= date_get_num(&p
, 0, 59, 2);
214 val
= date_get_num(&p
, 0, 59, 2);
220 val
= date_get_num(&p
, 0, 9999, 4);
223 dt
->tm_year
= val
- 1900;
226 val
= date_get_num(&p
, 1, 12, 2);
229 dt
->tm_mon
= val
- 1;
232 val
= date_get_num(&p
, 1, 31, 2);