Mercurial > libavutil.hg
comparison avstring.c @ 1027:7d79d41d152e libavutil
Move av_get_token() from libavfilter to libavutil.
| author | stefano |
|---|---|
| date | Mon, 27 Sep 2010 16:23:43 +0000 |
| parents | 536c2e94defa |
| children |
comparison
equal
deleted
inserted
replaced
| 1026:580d47a2f015 | 1027:7d79d41d152e |
|---|---|
| 95 { | 95 { |
| 96 char *str= av_malloc(16); | 96 char *str= av_malloc(16); |
| 97 if(str) snprintf(str, 16, "%f", d); | 97 if(str) snprintf(str, 16, "%f", d); |
| 98 return str; | 98 return str; |
| 99 } | 99 } |
| 100 | |
| 101 #define WHITESPACES " \n\t" | |
| 102 | |
| 103 char *av_get_token(const char **buf, const char *term) | |
| 104 { | |
| 105 char *out = av_malloc(strlen(*buf) + 1); | |
| 106 char *ret= out, *end= out; | |
| 107 const char *p = *buf; | |
| 108 if (!out) return NULL; | |
| 109 p += strspn(p, WHITESPACES); | |
| 110 | |
| 111 while(*p && !strspn(p, term)) { | |
| 112 char c = *p++; | |
| 113 if(c == '\\' && *p){ | |
| 114 *out++ = *p++; | |
| 115 end= out; | |
| 116 }else if(c == '\''){ | |
| 117 while(*p && *p != '\'') | |
| 118 *out++ = *p++; | |
| 119 if(*p){ | |
| 120 p++; | |
| 121 end= out; | |
| 122 } | |
| 123 }else{ | |
| 124 *out++ = c; | |
| 125 } | |
| 126 } | |
| 127 | |
| 128 do{ | |
| 129 *out-- = 0; | |
| 130 }while(out >= end && strspn(out, WHITESPACES)); | |
| 131 | |
| 132 *buf = p; | |
| 133 | |
| 134 return ret; | |
| 135 } | |
| 136 | |
| 137 #ifdef TEST | |
| 138 | |
| 139 #undef printf | |
| 140 | |
| 141 int main(void) | |
| 142 { | |
| 143 int i; | |
| 144 | |
| 145 printf("Testing av_get_token()\n"); | |
| 146 { | |
| 147 const char *strings[] = { | |
| 148 "''", | |
| 149 "", | |
| 150 ":", | |
| 151 "\\", | |
| 152 "'", | |
| 153 " '' :", | |
| 154 " '' '' :", | |
| 155 "foo '' :", | |
| 156 "'foo'", | |
| 157 "foo ", | |
| 158 " ' foo ' ", | |
| 159 "foo\\", | |
| 160 "foo': blah:blah", | |
| 161 "foo\\: blah:blah", | |
| 162 "foo\'", | |
| 163 "'foo : ' :blahblah", | |
| 164 "\\ :blah", | |
| 165 " foo", | |
| 166 " foo ", | |
| 167 " foo \\ ", | |
| 168 "foo ':blah", | |
| 169 " foo bar : blahblah", | |
| 170 "\\f\\o\\o", | |
| 171 "'foo : \\ \\ ' : blahblah", | |
| 172 "'\\fo\\o:': blahblah", | |
| 173 "\\'fo\\o\\:': foo ' :blahblah" | |
| 174 }; | |
| 175 | |
| 176 for (i=0; i < FF_ARRAY_ELEMS(strings); i++) { | |
| 177 const char *p= strings[i]; | |
| 178 printf("|%s|", p); | |
| 179 printf(" -> |%s|", av_get_token(&p, ":")); | |
| 180 printf(" + |%s|\n", p); | |
| 181 } | |
| 182 } | |
| 183 | |
| 184 return 0; | |
| 185 } | |
| 186 | |
| 187 #endif /* TEST */ |
