yading@11: /* yading@11: * Copyright (c) 2000, 2001, 2002 Fabrice Bellard yading@11: * Copyright (c) 2007 Mans Rullgard yading@11: * yading@11: * This file is part of FFmpeg. yading@11: * yading@11: * FFmpeg is free software; you can redistribute it and/or yading@11: * modify it under the terms of the GNU Lesser General Public yading@11: * License as published by the Free Software Foundation; either yading@11: * version 2.1 of the License, or (at your option) any later version. yading@11: * yading@11: * FFmpeg is distributed in the hope that it will be useful, yading@11: * but WITHOUT ANY WARRANTY; without even the implied warranty of yading@11: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU yading@11: * Lesser General Public License for more details. yading@11: * yading@11: * You should have received a copy of the GNU Lesser General Public yading@11: * License along with FFmpeg; if not, write to the Free Software yading@11: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA yading@11: */ yading@11: yading@11: #include yading@11: #include yading@11: #include yading@11: #include yading@11: yading@11: #include "config.h" yading@11: #include "common.h" yading@11: #include "mem.h" yading@11: #include "avstring.h" yading@11: #include "bprint.h" yading@11: yading@11: int av_strstart(const char *str, const char *pfx, const char **ptr) yading@11: { yading@11: while (*pfx && *pfx == *str) { yading@11: pfx++; yading@11: str++; yading@11: } yading@11: if (!*pfx && ptr) yading@11: *ptr = str; yading@11: return !*pfx; yading@11: } yading@11: yading@11: int av_stristart(const char *str, const char *pfx, const char **ptr) yading@11: { yading@11: while (*pfx && av_toupper((unsigned)*pfx) == av_toupper((unsigned)*str)) { yading@11: pfx++; yading@11: str++; yading@11: } yading@11: if (!*pfx && ptr) yading@11: *ptr = str; yading@11: return !*pfx; yading@11: } yading@11: yading@11: char *av_stristr(const char *s1, const char *s2) yading@11: { yading@11: if (!*s2) yading@11: return (char*)(intptr_t)s1; yading@11: yading@11: do yading@11: if (av_stristart(s1, s2, NULL)) yading@11: return (char*)(intptr_t)s1; yading@11: while (*s1++); yading@11: yading@11: return NULL; yading@11: } yading@11: yading@11: char *av_strnstr(const char *haystack, const char *needle, size_t hay_length) yading@11: { yading@11: size_t needle_len = strlen(needle); yading@11: if (!needle_len) yading@11: return (char*)haystack; yading@11: while (hay_length >= needle_len) { yading@11: hay_length--; yading@11: if (!memcmp(haystack, needle, needle_len)) yading@11: return (char*)haystack; yading@11: haystack++; yading@11: } yading@11: return NULL; yading@11: } yading@11: yading@11: size_t av_strlcpy(char *dst, const char *src, size_t size) yading@11: { yading@11: size_t len = 0; yading@11: while (++len < size && *src) yading@11: *dst++ = *src++; yading@11: if (len <= size) yading@11: *dst = 0; yading@11: return len + strlen(src) - 1; yading@11: } yading@11: yading@11: size_t av_strlcat(char *dst, const char *src, size_t size) yading@11: { yading@11: size_t len = strlen(dst); yading@11: if (size <= len + 1) yading@11: return len + strlen(src); yading@11: return len + av_strlcpy(dst + len, src, size - len); yading@11: } yading@11: yading@11: size_t av_strlcatf(char *dst, size_t size, const char *fmt, ...) yading@11: { yading@11: int len = strlen(dst); yading@11: va_list vl; yading@11: yading@11: va_start(vl, fmt); yading@11: len += vsnprintf(dst + len, size > len ? size - len : 0, fmt, vl); yading@11: va_end(vl); yading@11: yading@11: return len; yading@11: } yading@11: yading@11: char *av_asprintf(const char *fmt, ...) yading@11: { yading@11: char *p = NULL; yading@11: va_list va; yading@11: int len; yading@11: yading@11: va_start(va, fmt); yading@11: len = vsnprintf(NULL, 0, fmt, va); yading@11: va_end(va); yading@11: if (len < 0) yading@11: goto end; yading@11: yading@11: p = av_malloc(len + 1); yading@11: if (!p) yading@11: goto end; yading@11: yading@11: va_start(va, fmt); yading@11: len = vsnprintf(p, len + 1, fmt, va); yading@11: va_end(va); yading@11: if (len < 0) yading@11: av_freep(&p); yading@11: yading@11: end: yading@11: return p; yading@11: } yading@11: yading@11: char *av_d2str(double d) yading@11: { yading@11: char *str = av_malloc(16); yading@11: if (str) yading@11: snprintf(str, 16, "%f", d); yading@11: return str; yading@11: } yading@11: yading@11: #define WHITESPACES " \n\t" yading@11: yading@11: char *av_get_token(const char **buf, const char *term) yading@11: { yading@11: char *out = av_malloc(strlen(*buf) + 1); yading@11: char *ret = out, *end = out; yading@11: const char *p = *buf; yading@11: if (!out) yading@11: return NULL; yading@11: p += strspn(p, WHITESPACES); yading@11: yading@11: while (*p && !strspn(p, term)) { yading@11: char c = *p++; yading@11: if (c == '\\' && *p) { yading@11: *out++ = *p++; yading@11: end = out; yading@11: } else if (c == '\'') { yading@11: while (*p && *p != '\'') yading@11: *out++ = *p++; yading@11: if (*p) { yading@11: p++; yading@11: end = out; yading@11: } yading@11: } else { yading@11: *out++ = c; yading@11: } yading@11: } yading@11: yading@11: do yading@11: *out-- = 0; yading@11: while (out >= end && strspn(out, WHITESPACES)); yading@11: yading@11: *buf = p; yading@11: yading@11: return ret; yading@11: } yading@11: yading@11: char *av_strtok(char *s, const char *delim, char **saveptr) yading@11: { yading@11: char *tok; yading@11: yading@11: if (!s && !(s = *saveptr)) yading@11: return NULL; yading@11: yading@11: /* skip leading delimiters */ yading@11: s += strspn(s, delim); yading@11: yading@11: /* s now points to the first non delimiter char, or to the end of the string */ yading@11: if (!*s) { yading@11: *saveptr = NULL; yading@11: return NULL; yading@11: } yading@11: tok = s++; yading@11: yading@11: /* skip non delimiters */ yading@11: s += strcspn(s, delim); yading@11: if (*s) { yading@11: *s = 0; yading@11: *saveptr = s+1; yading@11: } else { yading@11: *saveptr = NULL; yading@11: } yading@11: yading@11: return tok; yading@11: } yading@11: yading@11: int av_strcasecmp(const char *a, const char *b) yading@11: { yading@11: uint8_t c1, c2; yading@11: do { yading@11: c1 = av_tolower(*a++); yading@11: c2 = av_tolower(*b++); yading@11: } while (c1 && c1 == c2); yading@11: return c1 - c2; yading@11: } yading@11: yading@11: int av_strncasecmp(const char *a, const char *b, size_t n) yading@11: { yading@11: const char *end = a + n; yading@11: uint8_t c1, c2; yading@11: do { yading@11: c1 = av_tolower(*a++); yading@11: c2 = av_tolower(*b++); yading@11: } while (a < end && c1 && c1 == c2); yading@11: return c1 - c2; yading@11: } yading@11: yading@11: const char *av_basename(const char *path) yading@11: { yading@11: char *p = strrchr(path, '/'); yading@11: yading@11: #if HAVE_DOS_PATHS yading@11: char *q = strrchr(path, '\\'); yading@11: char *d = strchr(path, ':'); yading@11: yading@11: p = FFMAX3(p, q, d); yading@11: #endif yading@11: yading@11: if (!p) yading@11: return path; yading@11: yading@11: return p + 1; yading@11: } yading@11: yading@11: const char *av_dirname(char *path) yading@11: { yading@11: char *p = strrchr(path, '/'); yading@11: yading@11: #if HAVE_DOS_PATHS yading@11: char *q = strrchr(path, '\\'); yading@11: char *d = strchr(path, ':'); yading@11: yading@11: d = d ? d + 1 : d; yading@11: yading@11: p = FFMAX3(p, q, d); yading@11: #endif yading@11: yading@11: if (!p) yading@11: return "."; yading@11: yading@11: *p = '\0'; yading@11: yading@11: return path; yading@11: } yading@11: yading@11: int av_escape(char **dst, const char *src, const char *special_chars, yading@11: enum AVEscapeMode mode, int flags) yading@11: { yading@11: AVBPrint dstbuf; yading@11: yading@11: av_bprint_init(&dstbuf, 1, AV_BPRINT_SIZE_UNLIMITED); yading@11: av_bprint_escape(&dstbuf, src, special_chars, mode, flags); yading@11: yading@11: if (!av_bprint_is_complete(&dstbuf)) { yading@11: av_bprint_finalize(&dstbuf, NULL); yading@11: return AVERROR(ENOMEM); yading@11: } else { yading@11: av_bprint_finalize(&dstbuf, dst); yading@11: return dstbuf.len; yading@11: } yading@11: } yading@11: yading@11: int av_isdigit(int c) yading@11: { yading@11: return c >= '0' && c <= '9'; yading@11: } yading@11: yading@11: int av_isgraph(int c) yading@11: { yading@11: return c > 32 && c < 127; yading@11: } yading@11: yading@11: int av_isspace(int c) yading@11: { yading@11: return c == ' ' || c == '\f' || c == '\n' || c == '\r' || c == '\t' || yading@11: c == '\v'; yading@11: } yading@11: yading@11: int av_isxdigit(int c) yading@11: { yading@11: c = av_tolower(c); yading@11: return av_isdigit(c) || (c >= 'a' && c <= 'f'); yading@11: } yading@11: yading@11: #ifdef TEST yading@11: yading@11: int main(void) yading@11: { yading@11: int i; yading@11: const char *strings[] = { yading@11: "''", yading@11: "", yading@11: ":", yading@11: "\\", yading@11: "'", yading@11: " '' :", yading@11: " '' '' :", yading@11: "foo '' :", yading@11: "'foo'", yading@11: "foo ", yading@11: " ' foo ' ", yading@11: "foo\\", yading@11: "foo': blah:blah", yading@11: "foo\\: blah:blah", yading@11: "foo\'", yading@11: "'foo : ' :blahblah", yading@11: "\\ :blah", yading@11: " foo", yading@11: " foo ", yading@11: " foo \\ ", yading@11: "foo ':blah", yading@11: " foo bar : blahblah", yading@11: "\\f\\o\\o", yading@11: "'foo : \\ \\ ' : blahblah", yading@11: "'\\fo\\o:': blahblah", yading@11: "\\'fo\\o\\:': foo ' :blahblah" yading@11: }; yading@11: yading@11: printf("Testing av_get_token()\n"); yading@11: for (i = 0; i < FF_ARRAY_ELEMS(strings); i++) { yading@11: const char *p = strings[i]; yading@11: char *q; yading@11: printf("|%s|", p); yading@11: q = av_get_token(&p, ":"); yading@11: printf(" -> |%s|", q); yading@11: printf(" + |%s|\n", p); yading@11: av_free(q); yading@11: } yading@11: yading@11: return 0; yading@11: } yading@11: yading@11: #endif /* TEST */