878
|
1 /*
|
|
2 * Various simple utilities for ffmpeg system
|
|
3 * Copyright (c) 2000, 2001, 2002 Fabrice Bellard
|
|
4 *
|
|
5 * This library is free software; you can redistribute it and/or
|
|
6 * modify it under the terms of the GNU Lesser General Public
|
|
7 * License as published by the Free Software Foundation; either
|
|
8 * version 2 of the License, or (at your option) any later version.
|
|
9 *
|
|
10 * This library is distributed in the hope that it will be useful,
|
|
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
13 * Lesser General Public License for more details.
|
|
14 *
|
|
15 * You should have received a copy of the GNU Lesser General Public
|
|
16 * License along with this library; if not, write to the Free Software
|
|
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
18 */
|
|
19 #include "avformat.h"
|
|
20
|
|
21 #if !defined(CONFIG_NOCUTILS)
|
|
22 /**
|
|
23 * Return TRUE if val is a prefix of str. If it returns TRUE, ptr is
|
|
24 * set to the next character in 'str' after the prefix.
|
|
25 *
|
|
26 * @param str input string
|
|
27 * @param val prefix to test
|
|
28 * @param ptr updated after the prefix in str in there is a match
|
|
29 * @return TRUE if there is a match
|
|
30 */
|
|
31 int strstart(const char *str, const char *val, const char **ptr)
|
|
32 {
|
|
33 const char *p, *q;
|
|
34 p = str;
|
|
35 q = val;
|
|
36 while (*q != '\0') {
|
|
37 if (*p != *q)
|
|
38 return 0;
|
|
39 p++;
|
|
40 q++;
|
|
41 }
|
|
42 if (ptr)
|
|
43 *ptr = p;
|
|
44 return 1;
|
|
45 }
|
|
46
|
|
47 /**
|
|
48 * Return TRUE if val is a prefix of str (case independent). If it
|
|
49 * returns TRUE, ptr is set to the next character in 'str' after the
|
|
50 * prefix.
|
|
51 *
|
|
52 * @param str input string
|
|
53 * @param val prefix to test
|
|
54 * @param ptr updated after the prefix in str in there is a match
|
|
55 * @return TRUE if there is a match */
|
|
56 int stristart(const char *str, const char *val, const char **ptr)
|
|
57 {
|
|
58 const char *p, *q;
|
|
59 p = str;
|
|
60 q = val;
|
|
61 while (*q != '\0') {
|
|
62 if (toupper(*(const unsigned char *)p) != toupper(*(const unsigned char *)q))
|
|
63 return 0;
|
|
64 p++;
|
|
65 q++;
|
|
66 }
|
|
67 if (ptr)
|
|
68 *ptr = p;
|
|
69 return 1;
|
|
70 }
|
|
71
|
|
72 /**
|
|
73 * Copy the string str to buf. If str length is bigger than buf_size -
|
|
74 * 1 then it is clamped to buf_size - 1.
|
|
75 * NOTE: this function does what strncpy should have done to be
|
|
76 * useful. NEVER use strncpy.
|
|
77 *
|
|
78 * @param buf destination buffer
|
|
79 * @param buf_size size of destination buffer
|
|
80 * @param str source string
|
|
81 */
|
|
82 void pstrcpy(char *buf, int buf_size, const char *str)
|
|
83 {
|
|
84 int c;
|
|
85 char *q = buf;
|
|
86
|
|
87 if (buf_size <= 0)
|
|
88 return;
|
|
89
|
|
90 for(;;) {
|
|
91 c = *str++;
|
|
92 if (c == 0 || q >= buf + buf_size - 1)
|
|
93 break;
|
|
94 *q++ = c;
|
|
95 }
|
|
96 *q = '\0';
|
|
97 }
|
|
98
|
|
99 /* strcat and truncate. */
|
|
100 char *pstrcat(char *buf, int buf_size, const char *s)
|
|
101 {
|
|
102 int len;
|
|
103 len = strlen(buf);
|
|
104 if (len < buf_size)
|
|
105 pstrcpy(buf + len, buf_size - len, s);
|
|
106 return buf;
|
|
107 }
|
|
108
|
|
109 #endif
|
|
110
|
|
111 /* add one element to a dynamic array */
|
|
112 void __dynarray_add(unsigned long **tab_ptr, int *nb_ptr, unsigned long elem)
|
|
113 {
|
|
114 int nb, nb_alloc;
|
|
115 unsigned long *tab;
|
|
116
|
|
117 nb = *nb_ptr;
|
|
118 tab = *tab_ptr;
|
|
119 if ((nb & (nb - 1)) == 0) {
|
|
120 if (nb == 0)
|
|
121 nb_alloc = 1;
|
|
122 else
|
|
123 nb_alloc = nb * 2;
|
|
124 tab = realloc(tab, nb_alloc * sizeof(unsigned long));
|
|
125 *tab_ptr = tab;
|
|
126 }
|
|
127 tab[nb++] = elem;
|
|
128 *nb_ptr = nb;
|
|
129 }
|
|
130
|
|
131 time_t mktimegm(struct tm *tm)
|
|
132 {
|
|
133 time_t t;
|
|
134
|
|
135 int y = tm->tm_year + 1900, m = tm->tm_mon + 1, d = tm->tm_mday;
|
|
136
|
|
137 if (m < 3) {
|
|
138 m += 12;
|
|
139 y--;
|
|
140 }
|
|
141
|
|
142 t = 86400 *
|
|
143 (d + (153 * m - 457) / 5 + 365 * y + y / 4 - y / 100 + y / 400 - 719469);
|
|
144
|
|
145 t += 3600 * tm->tm_hour + 60 * tm->tm_min + tm->tm_sec;
|
|
146
|
|
147 return t;
|
|
148 }
|
|
149
|
|
150 /* get a positive number between n_min and n_max, for a maximum length
|
|
151 of len_max. Return -1 if error. */
|
|
152 static int date_get_num(const char **pp,
|
|
153 int n_min, int n_max, int len_max)
|
|
154 {
|
|
155 int i, val, c;
|
|
156 const char *p;
|
|
157
|
|
158 p = *pp;
|
|
159 val = 0;
|
|
160 for(i = 0; i < len_max; i++) {
|
|
161 c = *p;
|
|
162 if (!isdigit(c))
|
|
163 break;
|
|
164 val = (val * 10) + c - '0';
|
|
165 p++;
|
|
166 }
|
|
167 /* no number read ? */
|
|
168 if (p == *pp)
|
|
169 return -1;
|
|
170 if (val < n_min || val > n_max)
|
|
171 return -1;
|
|
172 *pp = p;
|
|
173 return val;
|
|
174 }
|
|
175
|
|
176 /* small strptime for ffmpeg */
|
|
177 const char *small_strptime(const char *p, const char *fmt,
|
|
178 struct tm *dt)
|
|
179 {
|
|
180 int c, val;
|
|
181
|
|
182 for(;;) {
|
|
183 c = *fmt++;
|
|
184 if (c == '\0') {
|
|
185 return p;
|
|
186 } else if (c == '%') {
|
|
187 c = *fmt++;
|
|
188 switch(c) {
|
|
189 case 'H':
|
|
190 val = date_get_num(&p, 0, 23, 2);
|
|
191 if (val == -1)
|
|
192 return NULL;
|
|
193 dt->tm_hour = val;
|
|
194 break;
|
|
195 case 'M':
|
|
196 val = date_get_num(&p, 0, 59, 2);
|
|
197 if (val == -1)
|
|
198 return NULL;
|
|
199 dt->tm_min = val;
|
|
200 break;
|
|
201 case 'S':
|
|
202 val = date_get_num(&p, 0, 59, 2);
|
|
203 if (val == -1)
|
|
204 return NULL;
|
|
205 dt->tm_sec = val;
|
|
206 break;
|
|
207 case 'Y':
|
|
208 val = date_get_num(&p, 0, 9999, 4);
|
|
209 if (val == -1)
|
|
210 return NULL;
|
|
211 dt->tm_year = val - 1900;
|
|
212 break;
|
|
213 case 'm':
|
|
214 val = date_get_num(&p, 1, 12, 2);
|
|
215 if (val == -1)
|
|
216 return NULL;
|
|
217 dt->tm_mon = val - 1;
|
|
218 break;
|
|
219 case 'd':
|
|
220 val = date_get_num(&p, 1, 31, 2);
|
|
221 if (val == -1)
|
|
222 return NULL;
|
|
223 dt->tm_mday = val;
|
|
224 break;
|
|
225 case '%':
|
|
226 goto match;
|
|
227 default:
|
|
228 return NULL;
|
|
229 }
|
|
230 } else {
|
|
231 match:
|
|
232 if (c != *p)
|
|
233 return NULL;
|
|
234 p++;
|
|
235 }
|
|
236 }
|
|
237 return p;
|
|
238 }
|
|
239
|