| 1 |
/** @file stringop.c |
| 2 |
** @brief String operations - Definition |
| 3 |
** @author Andrea Vedaldi |
| 4 |
**/ |
| 5 |
|
| 6 |
/* |
| 7 |
Copyright (C) 2007-12 Andrea Vedaldi and Brian Fulkerson. |
| 8 |
All rights reserved. |
| 9 |
|
| 10 |
This file is part of the VLFeat library and is made available under |
| 11 |
the terms of the BSD license (see the COPYING file). |
| 12 |
*/ |
| 13 |
|
| 14 |
/** |
| 15 |
@file stringop.h |
| 16 |
@brief String operations |
| 17 |
@author Andrea Vedaldi |
| 18 |
@tableofcontents |
| 19 |
|
| 20 |
@ref stringop.h implements basic string operations. All functions that |
| 21 |
write to strings use range checking, which makes them safer than some |
| 22 |
standard POSIX equivalent (see @ref vl-stringop-err). |
| 23 |
|
| 24 |
@section vl-stringop-enumeration Enumerations |
| 25 |
|
| 26 |
@ref stringop.h defines a simple enumeration data type. This is given |
| 27 |
by an array of enumeration members, represented by |
| 28 |
instances of the ::VlEnumerator strucutre, each storing a |
| 29 |
name-value pair. The enumeration must end by a member whose |
| 30 |
name is set to @c NULL. |
| 31 |
|
| 32 |
Use ::vl_enumeration_get and ::vl_enumeration_get_casei |
| 33 |
to retrieve an enumeration member by name. |
| 34 |
|
| 35 |
@section vl-stringop-file-protocols File protocols |
| 36 |
|
| 37 |
@ref stringop.h defines a few file "protocols" and helps parsing them |
| 38 |
from URL-like formatted strings. The supported protocols are: |
| 39 |
|
| 40 |
<table> |
| 41 |
<caption>File protocols</caption> |
| 42 |
<tr><td>Protocol</td><td>Code</td><td>URL prefix</td></tr> |
| 43 |
<tr><td>ASCII</td><td>::VL_PROT_ASCII</td><td><code>ascii://</code></td></tr> |
| 44 |
<tr><td>BINARY</td><td>::VL_PROT_BINARY</td><td><code>binary://</code></td></tr> |
| 45 |
</table> |
| 46 |
|
| 47 |
@section vl-stringop-err Detecting overflow |
| 48 |
|
| 49 |
@ref stringop.h functions that write a string to a character buffer take |
| 50 |
both the buffer and its size @c n as input. If @c n is not large |
| 51 |
enough, the output may be truncated but it is always a null terminated |
| 52 |
string (provided that @c n >= 1). Such functions also return the |
| 53 |
length of the string that would have been written @c r (which does not |
| 54 |
include the terminating null character) had the buffer been large |
| 55 |
enough. Hence an <em>overflow</em> can be detected by testing if @c r |
| 56 |
>= @c n, @c r can be used to re-allocate a buffer large enough to |
| 57 |
contain the result, and the operation can be repeated. |
| 58 |
**/ |
| 59 |
|
| 60 |
#include "stringop.h" |
| 61 |
|
| 62 |
#include <string.h> |
| 63 |
#include <ctype.h> |
| 64 |
|
| 65 |
/** ------------------------------------------------------------------ |
| 66 |
** @brief Extract the protocol prefix from a string |
| 67 |
** @param string string. |
| 68 |
** @param protocol protocol code (output). |
| 69 |
** @return pointer to the first character after the protocol prefix. |
| 70 |
** |
| 71 |
** The function extracts the prefix of the string @a string |
| 72 |
** terminated by the first occurrence of the @c :// substring (if |
| 73 |
** any). It then matches the suffix terminated by @c :// to the |
| 74 |
** supported @ref vl-stringop-file-protocols protocols. If @c protocol is not |
| 75 |
** @c NULL, the corresponding protocol code is written to @a protocol |
| 76 |
** |
| 77 |
** The function writes to @a protocol the value ::VL_PROT_NONE if no |
| 78 |
** suffix is detected and ::VL_PROT_UNKNOWN if there is a suffix but |
| 79 |
** it cannot be matched to any of the supported protocols. |
| 80 |
**/ |
| 81 |
|
| 82 |
VL_EXPORT char * |
| 83 |
vl_string_parse_protocol (char const *string, int *protocol) |
| 84 |
{ |
| 85 |
char const * cpt ; |
| 86 |
int dummy ; |
| 87 |
|
| 88 |
/* handle the case prot = 0 */ |
| 89 |
if (protocol == 0) |
| 90 |
protocol = &dummy ; |
| 91 |
|
| 92 |
/* look for :// */ |
| 93 |
cpt = strstr(string, "://") ; |
| 94 |
|
| 95 |
if (cpt == 0) { |
| 96 |
*protocol = VL_PROT_NONE ; |
| 97 |
cpt = string ; |
| 98 |
} |
| 99 |
else { |
| 100 |
if (strncmp(string, "ascii", cpt - string) == 0) { |
| 101 |
*protocol = VL_PROT_ASCII ; |
| 102 |
} |
| 103 |
else if (strncmp(string, "bin", cpt - string) == 0) { |
| 104 |
*protocol = VL_PROT_BINARY ; |
| 105 |
} |
| 106 |
else { |
| 107 |
*protocol = VL_PROT_UNKNOWN ; |
| 108 |
} |
| 109 |
cpt += 3 ; |
| 110 |
} |
| 111 |
return (char*) cpt ; |
| 112 |
} |
| 113 |
|
| 114 |
/** ------------------------------------------------------------------ |
| 115 |
** @brief Get protocol name |
| 116 |
** @param protocol protocol code. |
| 117 |
** @return pointer protocol name string. |
| 118 |
** |
| 119 |
** The function returns a pointer to a string containing the name of |
| 120 |
** the protocol @a protocol (see the @a vl-file-protocols protocols |
| 121 |
** list). If the protocol is unknown the function returns the empty |
| 122 |
** string. |
| 123 |
**/ |
| 124 |
|
| 125 |
VL_EXPORT char const * |
| 126 |
vl_string_protocol_name (int protocol) |
| 127 |
{ |
| 128 |
switch (protocol) { |
| 129 |
case VL_PROT_ASCII: |
| 130 |
return "ascii" ; |
| 131 |
case VL_PROT_BINARY: |
| 132 |
return "bin" ; |
| 133 |
case VL_PROT_NONE : |
| 134 |
return "" ; |
| 135 |
default: |
| 136 |
return 0 ; |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
|
| 141 |
/** ------------------------------------------------------------------ |
| 142 |
** @brief Extract base of file name |
| 143 |
** @param destination destination buffer. |
| 144 |
** @param destinationSize size of destination buffer. |
| 145 |
** @param source input string. |
| 146 |
** @param maxNumStrippedExtensions maximum number of extensions to strip. |
| 147 |
** @return length of the destination string. |
| 148 |
** |
| 149 |
** The function removes the leading path and up to @c |
| 150 |
** maxNumStrippedExtensions trailing extensions from the string @a |
| 151 |
** source and writes the result to the buffer @a destination. |
| 152 |
** |
| 153 |
** The leading path is the longest suffix that ends with either the |
| 154 |
** @c \ or @c / characters. An extension is a string starting with |
| 155 |
** the <code>.</code> character not containing it. For instance, the string @c |
| 156 |
** file.png contains the extension <code>.png</code> and the string @c |
| 157 |
** file.tar.gz contains two extensions (<code>.tar</code> and @c <code>.gz</code>). |
| 158 |
** |
| 159 |
** @sa @ref vl-stringop-err. |
| 160 |
**/ |
| 161 |
|
| 162 |
VL_EXPORT vl_size |
| 163 |
vl_string_basename (char * destination, |
| 164 |
vl_size destinationSize, |
| 165 |
char const * source, |
| 166 |
vl_size maxNumStrippedExtensions) |
| 167 |
{ |
| 168 |
char c ; |
| 169 |
vl_uindex k = 0, beg, end ; |
| 170 |
|
| 171 |
/* find beginning */ |
| 172 |
beg = 0 ; |
| 173 |
for (k = 0 ; (c = source[k]) ; ++ k) { |
| 174 |
if (c == '\\' || c == '/') beg = k + 1 ; |
| 175 |
} |
| 176 |
|
| 177 |
/* find ending */ |
| 178 |
end = strlen (source) ; |
| 179 |
for (k = end ; k > beg ; --k) { |
| 180 |
if (source[k - 1] == '.' && maxNumStrippedExtensions > 0) { |
| 181 |
-- maxNumStrippedExtensions ; |
| 182 |
end = k - 1 ; |
| 183 |
} |
| 184 |
} |
| 185 |
|
| 186 |
return vl_string_copy_sub (destination, destinationSize, |
| 187 |
source + beg, source + end) ; |
| 188 |
} |
| 189 |
|
| 190 |
/** ------------------------------------------------------------------ |
| 191 |
** @brief Replace wildcard characters by a string |
| 192 |
** @param destination output buffer. |
| 193 |
** @param destinationSize size of the output buffer. |
| 194 |
** @param source input string. |
| 195 |
** @param wildcardChar wildcard character. |
| 196 |
** @param escapeChar escape character. |
| 197 |
** @param replacement replacement string. |
| 198 |
** |
| 199 |
** The function replaces the occurrence of the specified wildcard |
| 200 |
** character @a wildcardChar by the string @a replacement. The result |
| 201 |
** is written to the buffer @a destination of size @a |
| 202 |
** destinationSize. |
| 203 |
** |
| 204 |
** Wildcard characters may be escaped by preceding them by the @a esc |
| 205 |
** character. More in general, anything following an occurrence of @a |
| 206 |
** esc character is copied verbatim. To disable the escape characters |
| 207 |
** simply set @a esc to 0. |
| 208 |
** |
| 209 |
** @return length of the result. |
| 210 |
** @sa @ref vl-stringop-err. |
| 211 |
**/ |
| 212 |
|
| 213 |
VL_EXPORT vl_size |
| 214 |
vl_string_replace_wildcard (char * destination, |
| 215 |
vl_size destinationSize, |
| 216 |
char const * source, |
| 217 |
char wildcardChar, |
| 218 |
char escapeChar, |
| 219 |
char const * replacement) |
| 220 |
{ |
| 221 |
char c ; |
| 222 |
vl_uindex k = 0 ; |
| 223 |
vl_bool escape = 0 ; |
| 224 |
|
| 225 |
while ((c = *source++)) { |
| 226 |
|
| 227 |
/* enter escape mode ? */ |
| 228 |
if (! escape && c == escapeChar) { |
| 229 |
escape = 1 ; |
| 230 |
continue ; |
| 231 |
} |
| 232 |
|
| 233 |
/* wildcard or regular? */ |
| 234 |
if (! escape && c == wildcardChar) { |
| 235 |
char const * repl = replacement ; |
| 236 |
while ((c = *repl++)) { |
| 237 |
if (destination && k + 1 < destinationSize) { |
| 238 |
destination[k] = c ; |
| 239 |
} |
| 240 |
++ k ; |
| 241 |
} |
| 242 |
} |
| 243 |
/* regular character */ |
| 244 |
else { |
| 245 |
if (destination && k + 1 < destinationSize) { |
| 246 |
destination[k] = c ; |
| 247 |
} |
| 248 |
++ k ; |
| 249 |
} |
| 250 |
escape = 0 ; |
| 251 |
} |
| 252 |
|
| 253 |
/* add trailing 0 */ |
| 254 |
if (destinationSize > 0) { |
| 255 |
destination[VL_MIN(k, destinationSize - 1)] = 0 ; |
| 256 |
} |
| 257 |
return k ; |
| 258 |
} |
| 259 |
|
| 260 |
/** ------------------------------------------------------------------ |
| 261 |
** @brief Copy string |
| 262 |
** @param destination output buffer. |
| 263 |
** @param destinationSize size of the output buffer. |
| 264 |
** @param source string to copy. |
| 265 |
** @return length of the source string. |
| 266 |
** |
| 267 |
** The function copies the string @a source to the buffer @a |
| 268 |
** destination of size @a destinationSize. |
| 269 |
** |
| 270 |
** @sa @ref vl-stringop-err. |
| 271 |
**/ |
| 272 |
|
| 273 |
VL_EXPORT vl_size |
| 274 |
vl_string_copy (char * destination, vl_size destinationSize, |
| 275 |
char const * source) |
| 276 |
{ |
| 277 |
char c ; |
| 278 |
vl_uindex k = 0 ; |
| 279 |
|
| 280 |
while ((c = *source++)) { |
| 281 |
if (destination && k + 1 < destinationSize) { |
| 282 |
destination[k] = c ; |
| 283 |
} |
| 284 |
++ k ; |
| 285 |
} |
| 286 |
|
| 287 |
/* finalize */ |
| 288 |
if (destinationSize > 0) { |
| 289 |
destination[VL_MIN(k, destinationSize - 1)] = 0 ; |
| 290 |
} |
| 291 |
return k ; |
| 292 |
} |
| 293 |
|
| 294 |
/** ------------------------------------------------------------------ |
| 295 |
** @brief Copy substring |
| 296 |
** @param destination output buffer. |
| 297 |
** @param destinationSize size of output buffer. |
| 298 |
** @param beginning start of the substring. |
| 299 |
** @param end end of the substring. |
| 300 |
** @return length of the destination string. |
| 301 |
** |
| 302 |
** The function copies the substring from at @a beginning to @a end |
| 303 |
** (not included) to the buffer @a destination of size @a |
| 304 |
** destinationSize. If, however, the null character is found before |
| 305 |
** @a end, the substring terminates there. |
| 306 |
** |
| 307 |
** @sa @ref vl-stringop-err. |
| 308 |
**/ |
| 309 |
|
| 310 |
VL_EXPORT vl_size |
| 311 |
vl_string_copy_sub (char * destination, |
| 312 |
vl_size destinationSize, |
| 313 |
char const * beginning, |
| 314 |
char const * end) |
| 315 |
{ |
| 316 |
char c ; |
| 317 |
vl_uindex k = 0 ; |
| 318 |
|
| 319 |
while (beginning < end && (c = *beginning++)) { |
| 320 |
if (destination && k + 1 < destinationSize) { |
| 321 |
destination[k] = c ; |
| 322 |
} |
| 323 |
++ k ; |
| 324 |
} |
| 325 |
|
| 326 |
/* finalize */ |
| 327 |
if (destinationSize > 0) { |
| 328 |
destination[VL_MIN(k, destinationSize - 1)] = 0 ; |
| 329 |
} |
| 330 |
return k ; |
| 331 |
} |
| 332 |
|
| 333 |
/** ------------------------------------------------------------------ |
| 334 |
** @brief Search character in reversed order |
| 335 |
** @param beginning pointer to the substring beginning. |
| 336 |
** @param end pointer to the substring end. |
| 337 |
** @param c character to search for. |
| 338 |
** @return pointer to last occurrence of @a c, or 0 if none. |
| 339 |
** |
| 340 |
** The function searches for the last occurrence of the character @a c |
| 341 |
** in the substring from @a beg to @a end (the latter not being included). |
| 342 |
**/ |
| 343 |
|
| 344 |
VL_EXPORT char * |
| 345 |
vl_string_find_char_rev (char const *beginning, char const* end, char c) |
| 346 |
{ |
| 347 |
while (end -- != beginning) { |
| 348 |
if (*end == c) { |
| 349 |
return (char*) end ; |
| 350 |
} |
| 351 |
} |
| 352 |
return 0 ; |
| 353 |
} |
| 354 |
|
| 355 |
/** ------------------------------------------------------------------ |
| 356 |
** @brief Calculate string length |
| 357 |
** @param string string. |
| 358 |
** @return string length. |
| 359 |
**/ |
| 360 |
|
| 361 |
VL_EXPORT vl_size |
| 362 |
vl_string_length (char const *string) |
| 363 |
{ |
| 364 |
vl_uindex i ; |
| 365 |
for (i = 0 ; string[i] ; ++i) ; |
| 366 |
return i ; |
| 367 |
} |
| 368 |
|
| 369 |
/** ------------------------------------------------------------------ |
| 370 |
** @brief Compare strings case-insensitive |
| 371 |
** @param string1 fisrt string. |
| 372 |
** @param string2 second string. |
| 373 |
** @return an integer =,<,> 0 if @c string1 =,<,> @c string2 |
| 374 |
**/ |
| 375 |
|
| 376 |
VL_EXPORT int |
| 377 |
vl_string_casei_cmp (const char * string1, const char * string2) |
| 378 |
{ |
| 379 |
while (tolower((char unsigned)*string1) == |
| 380 |
tolower((char unsigned)*string2)) |
| 381 |
{ |
| 382 |
if (*string1 == 0) { |
| 383 |
return 0 ; |
| 384 |
} |
| 385 |
string1 ++ ; |
| 386 |
string2 ++ ; |
| 387 |
} |
| 388 |
return |
| 389 |
(int)tolower((char unsigned)*string1) - |
| 390 |
(int)tolower((char unsigned)*string2) ; |
| 391 |
} |
| 392 |
|
| 393 |
/* ------------------------------------------------------------------- |
| 394 |
* VlEnumeration |
| 395 |
* ---------------------------------------------------------------- */ |
| 396 |
|
| 397 |
/** @brief Get a member of an enumeration by name |
| 398 |
** @param enumeration array of ::VlEnumerator objects. |
| 399 |
** @param name the name of the desired member. |
| 400 |
** @return enumerator matching @a name. |
| 401 |
** |
| 402 |
** If @a name is not found in the enumeration, then the value |
| 403 |
** @c NULL is returned. |
| 404 |
** |
| 405 |
** @sa vl-stringop-enumeration |
| 406 |
**/ |
| 407 |
|
| 408 |
VL_EXPORT VlEnumerator * |
| 409 |
vl_enumeration_get (VlEnumerator const *enumeration, char const *name) |
| 410 |
{ |
| 411 |
assert(enumeration) ; |
| 412 |
while (enumeration->name) { |
| 413 |
if (strcmp(name, enumeration->name) == 0) return (VlEnumerator*)enumeration ; |
| 414 |
enumeration ++ ; |
| 415 |
} |
| 416 |
return NULL ; |
| 417 |
} |
| 418 |
|
| 419 |
/** @brief Get a member of an enumeration by name (case insensitive) |
| 420 |
** @param enumeration array of ::VlEnumerator objects. |
| 421 |
** @param name the name of the desired member. |
| 422 |
** @return enumerator matching @a name. |
| 423 |
** |
| 424 |
** If @a name is not found in the enumeration, then the value |
| 425 |
** @c NULL is returned. @a string is matched case insensitive. |
| 426 |
** |
| 427 |
** @sa vl-stringop-enumeration |
| 428 |
**/ |
| 429 |
|
| 430 |
VL_EXPORT VlEnumerator * |
| 431 |
vl_enumeration_get_casei (VlEnumerator const *enumeration, char const *name) |
| 432 |
{ |
| 433 |
assert(enumeration) ; |
| 434 |
while (enumeration->name) { |
| 435 |
if (vl_string_casei_cmp(name, enumeration->name) == 0) return (VlEnumerator*)enumeration ; |
| 436 |
enumeration ++ ; |
| 437 |
} |
| 438 |
return NULL ; |
| 439 |
} |
| 440 |
|
| 441 |
/** @brief Get a member of an enumeration by value |
| 442 |
** @param enumeration array of ::VlEnumerator objects. |
| 443 |
** @param value value of the desired member. |
| 444 |
** @return enumerator matching @a value. |
| 445 |
** |
| 446 |
** If @a value is not found in the enumeration, then the value |
| 447 |
** @c NULL is returned. |
| 448 |
** |
| 449 |
** @sa vl-stringop-enumeration |
| 450 |
**/ |
| 451 |
|
| 452 |
VL_EXPORT VlEnumerator * |
| 453 |
vl_enumeration_get_by_value (VlEnumerator const *enumeration, vl_index value) |
| 454 |
{ |
| 455 |
assert(enumeration) ; |
| 456 |
while (enumeration->name) { |
| 457 |
if (enumeration->value == value) return (VlEnumerator*)enumeration ; |
| 458 |
enumeration ++ ; |
| 459 |
} |
| 460 |
return NULL ; |
| 461 |
} |
| 462 |
|