• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • File List
  • Globals

libavutil/avstring.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2000, 2001, 2002 Fabrice Bellard
00003  * Copyright (c) 2007 Mans Rullgard
00004  *
00005  * This file is part of FFmpeg.
00006  *
00007  * FFmpeg is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2.1 of the License, or (at your option) any later version.
00011  *
00012  * FFmpeg is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with FFmpeg; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00020  */
00021 
00022 #include <stdarg.h>
00023 #include <stdio.h>
00024 #include <string.h>
00025 #include <ctype.h>
00026 #include "avstring.h"
00027 #include "mem.h"
00028 
00029 int av_strstart(const char *str, const char *pfx, const char **ptr)
00030 {
00031     while (*pfx && *pfx == *str) {
00032         pfx++;
00033         str++;
00034     }
00035     if (!*pfx && ptr)
00036         *ptr = str;
00037     return !*pfx;
00038 }
00039 
00040 int av_stristart(const char *str, const char *pfx, const char **ptr)
00041 {
00042     while (*pfx && toupper((unsigned)*pfx) == toupper((unsigned)*str)) {
00043         pfx++;
00044         str++;
00045     }
00046     if (!*pfx && ptr)
00047         *ptr = str;
00048     return !*pfx;
00049 }
00050 
00051 char *av_stristr(const char *s1, const char *s2)
00052 {
00053     if (!*s2)
00054         return (char*)(intptr_t)s1;
00055 
00056     do {
00057         if (av_stristart(s1, s2, NULL))
00058             return (char*)(intptr_t)s1;
00059     } while (*s1++);
00060 
00061     return NULL;
00062 }
00063 
00064 size_t av_strlcpy(char *dst, const char *src, size_t size)
00065 {
00066     size_t len = 0;
00067     while (++len < size && *src)
00068         *dst++ = *src++;
00069     if (len <= size)
00070         *dst = 0;
00071     return len + strlen(src) - 1;
00072 }
00073 
00074 size_t av_strlcat(char *dst, const char *src, size_t size)
00075 {
00076     size_t len = strlen(dst);
00077     if (size <= len + 1)
00078         return len + strlen(src);
00079     return len + av_strlcpy(dst + len, src, size - len);
00080 }
00081 
00082 size_t av_strlcatf(char *dst, size_t size, const char *fmt, ...)
00083 {
00084     int len = strlen(dst);
00085     va_list vl;
00086 
00087     va_start(vl, fmt);
00088     len += vsnprintf(dst + len, size > len ? size - len : 0, fmt, vl);
00089     va_end(vl);
00090 
00091     return len;
00092 }
00093 
00094 char *av_asprintf(const char *fmt, ...)
00095 {
00096     char *p = NULL;
00097     va_list va;
00098     int len;
00099 
00100     va_start(va, fmt);
00101     len = vsnprintf(NULL, 0, fmt, va);
00102     va_end(va);
00103     if (len < 0)
00104         goto end;
00105 
00106     p = av_malloc(len + 1);
00107     if (!p)
00108         goto end;
00109 
00110     va_start(va, fmt);
00111     len = vsnprintf(p, len + 1, fmt, va);
00112     va_end(va);
00113     if (len < 0)
00114         av_freep(&p);
00115 
00116 end:
00117     return p;
00118 }
00119 
00120 char *av_d2str(double d)
00121 {
00122     char *str= av_malloc(16);
00123     if(str) snprintf(str, 16, "%f", d);
00124     return str;
00125 }
00126 
00127 #define WHITESPACES " \n\t"
00128 
00129 char *av_get_token(const char **buf, const char *term)
00130 {
00131     char *out = av_malloc(strlen(*buf) + 1);
00132     char *ret= out, *end= out;
00133     const char *p = *buf;
00134     if (!out) return NULL;
00135     p += strspn(p, WHITESPACES);
00136 
00137     while(*p && !strspn(p, term)) {
00138         char c = *p++;
00139         if(c == '\\' && *p){
00140             *out++ = *p++;
00141             end= out;
00142         }else if(c == '\''){
00143             while(*p && *p != '\'')
00144                 *out++ = *p++;
00145             if(*p){
00146                 p++;
00147                 end= out;
00148             }
00149         }else{
00150             *out++ = c;
00151         }
00152     }
00153 
00154     do{
00155         *out-- = 0;
00156     }while(out >= end && strspn(out, WHITESPACES));
00157 
00158     *buf = p;
00159 
00160     return ret;
00161 }
00162 
00163 char *av_strtok(char *s, const char *delim, char **saveptr)
00164 {
00165     char *tok;
00166 
00167     if (!s && !(s = *saveptr))
00168         return NULL;
00169 
00170     /* skip leading delimiters */
00171     s += strspn(s, delim);
00172 
00173     /* s now points to the first non delimiter char, or to the end of the string */
00174     if (!*s) {
00175         *saveptr = NULL;
00176         return NULL;
00177     }
00178     tok = s++;
00179 
00180     /* skip non delimiters */
00181     s += strcspn(s, delim);
00182     if (*s) {
00183         *s = 0;
00184         *saveptr = s+1;
00185     } else {
00186         *saveptr = NULL;
00187     }
00188 
00189     return tok;
00190 }
00191 
00192 int av_strcasecmp(const char *a, const char *b)
00193 {
00194     uint8_t c1, c2;
00195     do {
00196         c1 = av_tolower(*a++);
00197         c2 = av_tolower(*b++);
00198     } while (c1 && c1 == c2);
00199     return c1 - c2;
00200 }
00201 
00202 int av_strncasecmp(const char *a, const char *b, size_t n)
00203 {
00204     const char *end = a + n;
00205     uint8_t c1, c2;
00206     do {
00207         c1 = av_tolower(*a++);
00208         c2 = av_tolower(*b++);
00209     } while (a < end && c1 && c1 == c2);
00210     return c1 - c2;
00211 }
00212 
00213 #ifdef TEST
00214 
00215 #undef printf
00216 
00217 int main(void)
00218 {
00219     int i;
00220 
00221     printf("Testing av_get_token()\n");
00222     {
00223         const char *strings[] = {
00224             "''",
00225             "",
00226             ":",
00227             "\\",
00228             "'",
00229             "    ''    :",
00230             "    ''  ''  :",
00231             "foo   '' :",
00232             "'foo'",
00233             "foo     ",
00234             "  '  foo  '  ",
00235             "foo\\",
00236             "foo':  blah:blah",
00237             "foo\\:  blah:blah",
00238             "foo\'",
00239             "'foo :  '  :blahblah",
00240             "\\ :blah",
00241             "     foo",
00242             "      foo       ",
00243             "      foo     \\ ",
00244             "foo ':blah",
00245             " foo   bar    :   blahblah",
00246             "\\f\\o\\o",
00247             "'foo : \\ \\  '   : blahblah",
00248             "'\\fo\\o:': blahblah",
00249             "\\'fo\\o\\:':  foo  '  :blahblah"
00250         };
00251 
00252         for (i=0; i < FF_ARRAY_ELEMS(strings); i++) {
00253             const char *p= strings[i];
00254             printf("|%s|", p);
00255             printf(" -> |%s|", av_get_token(&p, ":"));
00256             printf(" + |%s|\n", p);
00257         }
00258     }
00259 
00260     return 0;
00261 }
00262 
00263 #endif /* TEST */
Generated on Fri Feb 1 2013 14:34:55 for FFmpeg by doxygen 1.7.1