28f40b0e2f2e181dbad1b2b9038d1637cd7c8108
[libav.git] / libavformat / cutils.c
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19 #include "avformat.h"
20 #include <ctype.h>
21
22 #if !defined(CONFIG_NOCUTILS)
23 /**
24 * Return TRUE if val is a prefix of str. If it returns TRUE, ptr is
25 * set to the next character in 'str' after the prefix.
26 *
27 * @param str input string
28 * @param val prefix to test
29 * @param ptr updated after the prefix in str in there is a match
30 * @return TRUE if there is a match
31 */
32 int strstart(const char *str, const char *val, const char **ptr)
33 {
34 const char *p, *q;
35 p = str;
36 q = val;
37 while (*q != '\0') {
38 if (*p != *q)
39 return 0;
40 p++;
41 q++;
42 }
43 if (ptr)
44 *ptr = p;
45 return 1;
46 }
47
48 /**
49 * Return TRUE if val is a prefix of str (case independent). If it
50 * returns TRUE, ptr is set to the next character in 'str' after the
51 * prefix.
52 *
53 * @param str input string
54 * @param val prefix to test
55 * @param ptr updated after the prefix in str in there is a match
56 * @return TRUE if there is a match */
57 int stristart(const char *str, const char *val, const char **ptr)
58 {
59 const char *p, *q;
60 p = str;
61 q = val;
62 while (*q != '\0') {
63 if (toupper(*(const unsigned char *)p) != toupper(*(const unsigned char *)q))
64 return 0;
65 p++;
66 q++;
67 }
68 if (ptr)
69 *ptr = p;
70 return 1;
71 }
72
73 /**
74 * Copy the string str to buf. If str length is bigger than buf_size -
75 * 1 then it is clamped to buf_size - 1.
76 * NOTE: this function does what strncpy should have done to be
77 * useful. NEVER use strncpy.
78 *
79 * @param buf destination buffer
80 * @param buf_size size of destination buffer
81 * @param str source string
82 */
83 void pstrcpy(char *buf, int buf_size, const char *str)
84 {
85 int c;
86 char *q = buf;
87
88 if (buf_size <= 0)
89 return;
90
91 for(;;) {
92 c = *str++;
93 if (c == 0 || q >= buf + buf_size - 1)
94 break;
95 *q++ = c;
96 }
97 *q = '\0';
98 }
99
100 /* strcat and truncate. */
101 char *pstrcat(char *buf, int buf_size, const char *s)
102 {
103 int len;
104 len = strlen(buf);
105 if (len < buf_size)
106 pstrcpy(buf + len, buf_size - len, s);
107 return buf;
108 }
109
110 #endif
111
112 /* add one element to a dynamic array */
113 void __dynarray_add(unsigned long **tab_ptr, int *nb_ptr, unsigned long elem)
114 {
115 int nb, nb_alloc;
116 unsigned long *tab;
117
118 nb = *nb_ptr;
119 tab = *tab_ptr;
120 if ((nb & (nb - 1)) == 0) {
121 if (nb == 0)
122 nb_alloc = 1;
123 else
124 nb_alloc = nb * 2;
125 tab = av_realloc(tab, nb_alloc * sizeof(unsigned long));
126 *tab_ptr = tab;
127 }
128 tab[nb++] = elem;
129 *nb_ptr = nb;
130 }
131