removed ctype.h header
[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
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 = av_realloc(tab, nb_alloc * sizeof(unsigned long));
125 *tab_ptr = tab;
126 }
127 tab[nb++] = elem;
128 *nb_ptr = nb;
129 }
130