avstring.c 5.77 KB
Newer Older
1 2 3 4
/*
 * Copyright (c) 2000, 2001, 2002 Fabrice Bellard
 * Copyright (c) 2007 Mans Rullgard
 *
5
 * This file is part of Libav.
6
 *
7
 * Libav is free software; you can redistribute it and/or
8 9 10 11
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
12
 * Libav is distributed in the hope that it will be useful,
13 14 15 16 17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
18
 * License along with Libav; if not, write to the Free Software
19 20 21
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

22
#include <stdarg.h>
23
#include <stdint.h>
24
#include <stdio.h>
25
#include <string.h>
26

27 28
#include "config.h"
#include "common.h"
29
#include "mem.h"
30
#include "avstring.h"
31 32 33

int av_strstart(const char *str, const char *pfx, const char **ptr)
{
34 35 36 37
    while (*pfx && *pfx == *str) {
        pfx++;
        str++;
    }
38 39 40 41 42 43 44
    if (!*pfx && ptr)
        *ptr = str;
    return !*pfx;
}

int av_stristart(const char *str, const char *pfx, const char **ptr)
{
45
    while (*pfx && av_toupper((unsigned)*pfx) == av_toupper((unsigned)*str)) {
46 47 48
        pfx++;
        str++;
    }
49 50 51 52 53
    if (!*pfx && ptr)
        *ptr = str;
    return !*pfx;
}

54 55 56 57 58
char *av_stristr(const char *s1, const char *s2)
{
    if (!*s2)
        return s1;

59
    do
60 61
        if (av_stristart(s1, s2, NULL))
            return s1;
62
    while (*s1++);
63 64 65 66

    return NULL;
}

67 68 69 70 71 72 73 74 75 76 77 78 79 80
char *av_strnstr(const char *haystack, const char *needle, size_t hay_length)
{
    size_t needle_len = strlen(needle);
    if (!needle_len)
        return haystack;
    while (hay_length >= needle_len) {
        hay_length--;
        if (!memcmp(haystack, needle, needle_len))
            return haystack;
        haystack++;
    }
    return NULL;
}

81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
size_t av_strlcpy(char *dst, const char *src, size_t size)
{
    size_t len = 0;
    while (++len < size && *src)
        *dst++ = *src++;
    if (len <= size)
        *dst = 0;
    return len + strlen(src) - 1;
}

size_t av_strlcat(char *dst, const char *src, size_t size)
{
    size_t len = strlen(dst);
    if (size <= len + 1)
        return len + strlen(src);
    return len + av_strlcpy(dst + len, src, size - len);
}
98 99 100 101 102 103 104 105 106 107 108 109

size_t av_strlcatf(char *dst, size_t size, const char *fmt, ...)
{
    int len = strlen(dst);
    va_list vl;

    va_start(vl, fmt);
    len += vsnprintf(dst + len, size > len ? size - len : 0, fmt, vl);
    va_end(vl);

    return len;
}
110

111 112
char *av_d2str(double d)
{
113 114 115
    char *str = av_malloc(16);
    if (str)
        snprintf(str, 16, "%f", d);
116 117
    return str;
}
118 119 120 121 122

#define WHITESPACES " \n\t"

char *av_get_token(const char **buf, const char *term)
{
123 124
    char *out     = av_malloc(strlen(*buf) + 1);
    char *ret     = out, *end = out;
125
    const char *p = *buf;
126 127
    if (!out)
        return NULL;
128 129
    p += strspn(p, WHITESPACES);

130
    while (*p && !strspn(p, term)) {
131
        char c = *p++;
132
        if (c == '\\' && *p) {
133
            *out++ = *p++;
134 135 136
            end    = out;
        } else if (c == '\'') {
            while (*p && *p != '\'')
137
                *out++ = *p++;
138
            if (*p) {
139
                p++;
140
                end = out;
141
            }
142
        } else {
143 144 145 146
            *out++ = c;
        }
    }

147
    do
148
        *out-- = 0;
149
    while (out >= end && strspn(out, WHITESPACES));
150 151 152 153 154 155

    *buf = p;

    return ret;
}

156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
int av_strcasecmp(const char *a, const char *b)
{
    uint8_t c1, c2;
    do {
        c1 = av_tolower(*a++);
        c2 = av_tolower(*b++);
    } while (c1 && c1 == c2);
    return c1 - c2;
}

int av_strncasecmp(const char *a, const char *b, size_t n)
{
    const char *end = a + n;
    uint8_t c1, c2;
    do {
        c1 = av_tolower(*a++);
        c2 = av_tolower(*b++);
    } while (a < end && c1 && c1 == c2);
    return c1 - c2;
}

177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
const char *av_basename(const char *path)
{
    char *p = strrchr(path, '/');

#if HAVE_DOS_PATHS
    char *q = strrchr(path, '\\');
    char *d = strchr(path, ':');

    p = FFMAX3(p, q, d);
#endif

    if (!p)
        return path;

    return p + 1;
}

const char *av_dirname(char *path)
{
    char *p = strrchr(path, '/');

#if HAVE_DOS_PATHS
    char *q = strrchr(path, '\\');
    char *d = strchr(path, ':');

    d = d ? d + 1 : d;

    p = FFMAX3(p, q, d);
#endif

    if (!p)
        return ".";

    *p = '\0';

    return path;
}

215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
int av_isdigit(int c)
{
    return c >= '0' && c <= '9';
}

int av_isgraph(int c)
{
    return c > 32 && c < 127;
}

int av_isspace(int c)
{
    return c == ' ' || c == '\f' || c == '\n' || c == '\r' || c == '\t' ||
           c == '\v';
}

int av_isxdigit(int c)
{
    c = av_tolower(c);
234
    return av_isdigit(c) || (c >= 'a' && c <= 'f');
235 236
}

237 238 239 240 241
#ifdef TEST

int main(void)
{
    int i;
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
    const char *strings[] = {
        "''",
        "",
        ":",
        "\\",
        "'",
        "    ''    :",
        "    ''  ''  :",
        "foo   '' :",
        "'foo'",
        "foo     ",
        "  '  foo  '  ",
        "foo\\",
        "foo':  blah:blah",
        "foo\\:  blah:blah",
        "foo\'",
        "'foo :  '  :blahblah",
        "\\ :blah",
        "     foo",
        "      foo       ",
        "      foo     \\ ",
        "foo ':blah",
        " foo   bar    :   blahblah",
        "\\f\\o\\o",
        "'foo : \\ \\  '   : blahblah",
        "'\\fo\\o:': blahblah",
        "\\'fo\\o\\:':  foo  '  :blahblah"
    };
270 271

    printf("Testing av_get_token()\n");
272 273 274 275 276 277 278
    for (i = 0; i < FF_ARRAY_ELEMS(strings); i++) {
        const char *p = strings[i], *q;
        printf("|%s|", p);
        q = av_get_token(&p, ":");
        printf(" -> |%s|", q);
        printf(" + |%s|\n", p);
        av_free(q);
279 280 281 282 283 284
    }

    return 0;
}

#endif /* TEST */