• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • Examples
  • 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 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 int av_strcasecmp(const char *a, const char *b)
00138 {
00139     uint8_t c1, c2;
00140     do {
00141         c1 = av_tolower(*a++);
00142         c2 = av_tolower(*b++);
00143     } while (c1 && c1 == c2);
00144     return c1 - c2;
00145 }
00146 
00147 int av_strncasecmp(const char *a, const char *b, size_t n)
00148 {
00149     const char *end = a + n;
00150     uint8_t c1, c2;
00151     do {
00152         c1 = av_tolower(*a++);
00153         c2 = av_tolower(*b++);
00154     } while (a < end && c1 && c1 == c2);
00155     return c1 - c2;
00156 }
00157 
00158 #ifdef TEST
00159 
00160 #undef printf
00161 
00162 int main(void)
00163 {
00164     int i;
00165 
00166     printf("Testing av_get_token()\n");
00167     {
00168         const char *strings[] = {
00169             "''",
00170             "",
00171             ":",
00172             "\\",
00173             "'",
00174             "    ''    :",
00175             "    ''  ''  :",
00176             "foo   '' :",
00177             "'foo'",
00178             "foo     ",
00179             "  '  foo  '  ",
00180             "foo\\",
00181             "foo':  blah:blah",
00182             "foo\\:  blah:blah",
00183             "foo\'",
00184             "'foo :  '  :blahblah",
00185             "\\ :blah",
00186             "     foo",
00187             "      foo       ",
00188             "      foo     \\ ",
00189             "foo ':blah",
00190             " foo   bar    :   blahblah",
00191             "\\f\\o\\o",
00192             "'foo : \\ \\  '   : blahblah",
00193             "'\\fo\\o:': blahblah",
00194             "\\'fo\\o\\:':  foo  '  :blahblah"
00195         };
00196 
00197         for (i=0; i < FF_ARRAY_ELEMS(strings); i++) {
00198             const char *p= strings[i];
00199             printf("|%s|", p);
00200             printf(" -> |%s|", av_get_token(&p, ":"));
00201             printf(" + |%s|\n", p);
00202         }
00203     }
00204 
00205     return 0;
00206 }
00207 
00208 #endif /* TEST */
Generated on Sun Apr 22 2012 21:54:09 for Libav by doxygen 1.7.1