stringop.c
Go to the documentation of this file.
00001 
00006 /*
00007 Copyright (C) 2007-12 Andrea Vedaldi and Brian Fulkerson.
00008 All rights reserved.
00009 
00010 This file is part of the VLFeat library and is made available under
00011 the terms of the BSD license (see the COPYING file).
00012 */
00013 
00060 #include "stringop.h"
00061 
00062 #include <string.h>
00063 #include <ctype.h>
00064 
00082 VL_EXPORT char *
00083 vl_string_parse_protocol (char const *string, int *protocol)
00084 {
00085   char const * cpt ;
00086   int dummy ;
00087 
00088   /* handle the case prot = 0 */
00089   if (protocol == 0)
00090     protocol = &dummy ;
00091 
00092   /* look for :// */
00093   cpt = strstr(string, "://") ;
00094 
00095   if (cpt == 0) {
00096     *protocol = VL_PROT_NONE ;
00097     cpt = string ;
00098   }
00099   else {
00100     if (strncmp(string, "ascii", cpt - string) == 0) {
00101       *protocol = VL_PROT_ASCII ;
00102     }
00103     else if (strncmp(string, "bin",   cpt - string) == 0) {
00104       *protocol = VL_PROT_BINARY ;
00105     }
00106     else {
00107       *protocol = VL_PROT_UNKNOWN ;
00108     }
00109     cpt += 3 ;
00110   }
00111   return (char*) cpt ;
00112 }
00113 
00125 VL_EXPORT char const *
00126 vl_string_protocol_name (int protocol)
00127 {
00128   switch (protocol) {
00129   case VL_PROT_ASCII:
00130     return "ascii" ;
00131   case VL_PROT_BINARY:
00132     return "bin" ;
00133   case VL_PROT_NONE :
00134     return "" ;
00135   default:
00136     return 0 ;
00137   }
00138 }
00139 
00140 
00162 VL_EXPORT vl_size
00163 vl_string_basename (char * destination,
00164                     vl_size destinationSize,
00165                     char const * source,
00166                     vl_size maxNumStrippedExtensions)
00167 {
00168   char c ;
00169   vl_uindex k = 0, beg, end ;
00170 
00171   /* find beginning */
00172   beg = 0 ;
00173   for (k = 0 ; (c = source[k]) ; ++ k) {
00174     if (c == '\\' || c == '/') beg = k + 1 ;
00175   }
00176 
00177   /* find ending */
00178   end = strlen (source) ;
00179   for (k = end ; k > beg ; --k) {
00180     if (source[k - 1] == '.' && maxNumStrippedExtensions > 0) {
00181       -- maxNumStrippedExtensions ;
00182       end = k - 1 ;
00183     }
00184   }
00185 
00186   return vl_string_copy_sub (destination, destinationSize,
00187                              source + beg, source + end) ;
00188 }
00189 
00213 VL_EXPORT vl_size
00214 vl_string_replace_wildcard (char * destination,
00215                             vl_size destinationSize,
00216                             char const * source,
00217                             char wildcardChar,
00218                             char escapeChar,
00219                             char const * replacement)
00220 {
00221   char c ;
00222   vl_uindex k = 0 ;
00223   vl_bool escape = 0 ;
00224 
00225   while ((c = *source++)) {
00226 
00227     /* enter escape mode ? */
00228     if (! escape && c == escapeChar) {
00229       escape = 1 ;
00230       continue ;
00231     }
00232 
00233     /* wildcard or regular? */
00234     if (! escape && c == wildcardChar) {
00235       char const * repl = replacement ;
00236       while ((c = *repl++)) {
00237         if (destination && k + 1 < destinationSize) {
00238           destination[k] = c ;
00239         }
00240         ++ k ;
00241       }
00242     }
00243     /* regular character */
00244     else {
00245       if (destination && k + 1 < destinationSize) {
00246         destination[k] = c ;
00247       }
00248       ++ k ;
00249     }
00250     escape = 0 ;
00251   }
00252 
00253   /* add trailing 0 */
00254   if (destinationSize > 0) {
00255     destination[VL_MIN(k, destinationSize - 1)] = 0 ;
00256   }
00257   return  k ;
00258 }
00259 
00273 VL_EXPORT vl_size
00274 vl_string_copy (char * destination, vl_size destinationSize,
00275                 char const * source)
00276 {
00277   char c ;
00278   vl_uindex k = 0 ;
00279 
00280   while ((c = *source++)) {
00281     if (destination && k + 1 < destinationSize) {
00282       destination[k] = c ;
00283     }
00284     ++ k ;
00285   }
00286 
00287   /* finalize */
00288   if (destinationSize > 0) {
00289     destination[VL_MIN(k, destinationSize - 1)] = 0 ;
00290   }
00291   return  k ;
00292 }
00293 
00310 VL_EXPORT vl_size
00311 vl_string_copy_sub (char * destination,
00312                     vl_size destinationSize,
00313                     char const * beginning,
00314                     char const * end)
00315 {
00316   char c ;
00317   vl_uindex k = 0 ;
00318 
00319   while (beginning < end && (c = *beginning++)) {
00320     if (destination && k + 1 < destinationSize) {
00321       destination[k] = c ;
00322     }
00323     ++ k ;
00324   }
00325 
00326   /* finalize */
00327   if (destinationSize > 0) {
00328     destination[VL_MIN(k, destinationSize - 1)] = 0 ;
00329   }
00330   return  k ;
00331 }
00332 
00344 VL_EXPORT char *
00345 vl_string_find_char_rev (char const *beginning, char const* end, char c)
00346 {
00347   while (end -- != beginning) {
00348     if (*end == c) {
00349       return (char*) end ;
00350     }
00351   }
00352   return 0 ;
00353 }
00354 
00361 VL_EXPORT vl_size
00362 vl_string_length (char const *string)
00363 {
00364   vl_uindex i ;
00365   for (i = 0 ; string[i] ; ++i) ;
00366   return i ;
00367 }
00368 
00376 VL_EXPORT int
00377 vl_string_casei_cmp (const char * string1, const char * string2)
00378 {
00379   while (tolower((char unsigned)*string1) ==
00380          tolower((char unsigned)*string2))
00381   {
00382     if (*string1 == 0) {
00383       return 0 ;
00384     }
00385     string1 ++ ;
00386     string2 ++ ;
00387   }
00388   return
00389     (int)tolower((char unsigned)*string1) -
00390     (int)tolower((char unsigned)*string2) ;
00391 }
00392 
00393 /* -------------------------------------------------------------------
00394  *                                                       VlEnumeration
00395  * ---------------------------------------------------------------- */
00396 
00408 VL_EXPORT VlEnumerator *
00409 vl_enumeration_get (VlEnumerator const *enumeration, char const *name)
00410 {
00411   assert(enumeration) ;
00412   while (enumeration->name) {
00413     if (strcmp(name, enumeration->name) == 0) return (VlEnumerator*)enumeration ;
00414     enumeration ++ ;
00415   }
00416   return NULL ;
00417 }
00418 
00430 VL_EXPORT VlEnumerator *
00431 vl_enumeration_get_casei (VlEnumerator const *enumeration, char const *name)
00432 {
00433   assert(enumeration) ;
00434   while (enumeration->name) {
00435     if (vl_string_casei_cmp(name, enumeration->name) == 0) return (VlEnumerator*)enumeration ;
00436     enumeration ++ ;
00437   }
00438   return NULL ;
00439 }
00440 
00452 VL_EXPORT VlEnumerator *
00453 vl_enumeration_get_by_value (VlEnumerator const *enumeration, vl_index value)
00454 {
00455   assert(enumeration) ;
00456   while (enumeration->name) {
00457     if (enumeration->value == value) return (VlEnumerator*)enumeration ;
00458     enumeration ++ ;
00459   }
00460   return NULL ;
00461 }
00462 


libvlfeat
Author(s): Andrea Vedaldi
autogenerated on Thu Jun 6 2019 20:25:51