Libav 0.7.1
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 Libav.
00006  *
00007  * Libav 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  * Libav 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 Libav; 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 s1;
00055 
00056     do {
00057         if (av_stristart(s1, s2, NULL))
00058             return 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_d2str(double d)
00095 {
00096     char *str= av_malloc(16);
00097     if(str) snprintf(str, 16, "%f", d);
00098     return str;
00099 }
00100 
00101 #define WHITESPACES " \n\t"
00102 
00103 char *av_get_token(const char **buf, const char *term)
00104 {
00105     char *out = av_malloc(strlen(*buf) + 1);
00106     char *ret= out, *end= out;
00107     const char *p = *buf;
00108     if (!out) return NULL;
00109     p += strspn(p, WHITESPACES);
00110 
00111     while(*p && !strspn(p, term)) {
00112         char c = *p++;
00113         if(c == '\\' && *p){
00114             *out++ = *p++;
00115             end= out;
00116         }else if(c == '\''){
00117             while(*p && *p != '\'')
00118                 *out++ = *p++;
00119             if(*p){
00120                 p++;
00121                 end= out;
00122             }
00123         }else{
00124             *out++ = c;
00125         }
00126     }
00127 
00128     do{
00129         *out-- = 0;
00130     }while(out >= end && strspn(out, WHITESPACES));
00131 
00132     *buf = p;
00133 
00134     return ret;
00135 }
00136 
00137 #ifdef TEST
00138 
00139 #undef printf
00140 
00141 int main(void)
00142 {
00143     int i;
00144 
00145     printf("Testing av_get_token()\n");
00146     {
00147         const char *strings[] = {
00148             "''",
00149             "",
00150             ":",
00151             "\\",
00152             "'",
00153             "    ''    :",
00154             "    ''  ''  :",
00155             "foo   '' :",
00156             "'foo'",
00157             "foo     ",
00158             "  '  foo  '  ",
00159             "foo\\",
00160             "foo':  blah:blah",
00161             "foo\\:  blah:blah",
00162             "foo\'",
00163             "'foo :  '  :blahblah",
00164             "\\ :blah",
00165             "     foo",
00166             "      foo       ",
00167             "      foo     \\ ",
00168             "foo ':blah",
00169             " foo   bar    :   blahblah",
00170             "\\f\\o\\o",
00171             "'foo : \\ \\  '   : blahblah",
00172             "'\\fo\\o:': blahblah",
00173             "\\'fo\\o\\:':  foo  '  :blahblah"
00174         };
00175 
00176         for (i=0; i < FF_ARRAY_ELEMS(strings); i++) {
00177             const char *p= strings[i];
00178             printf("|%s|", p);
00179             printf(" -> |%s|", av_get_token(&p, ":"));
00180             printf(" + |%s|\n", p);
00181         }
00182     }
00183 
00184     return 0;
00185 }
00186 
00187 #endif /* TEST */