VLFeat.org

API docs

  • Home
    • Download and Install
    • API docs
    • Matlab docs
    • About VLFeat
  • Tutorials
    • SIFT
    • MSER
    • IKM
    • HIKM
    • AIB
    • Utils
  • Main Page
  • Related Pages
  • Data Structures
  • Files
  • Examples

stringop.c

Go to the documentation of this file.
00001 
00006 /* AUTORIGHTS
00007 Copyright 2007 (c) Andrea Vedaldi and Brian Fulkerson
00008 
00009 This file is part of VLFeat, available in the terms of the GNU
00010 General Public License version 2.
00011 */
00012 
00039 #include "stringop.h"
00040 
00041 #include <string.h>
00042 #include <ctype.h>
00043 
00061 VL_EXPORT
00062 char*  
00063 vl_string_parse_protocol (char const *str, int *prot)
00064 {
00065   char const* cpt ;
00066   int dummy ;
00067   
00068   /* handle the case prot = 0 */
00069   if (prot == 0) 
00070     prot = &dummy ;
00071 
00072   /* look for :// */
00073   cpt = strstr(str, "://") ;
00074 
00075   if (cpt == 0) {
00076     *prot = VL_PROT_NONE ;
00077     cpt = str ;
00078   } 
00079   else {
00080     if      (strncmp(str, "ascii", cpt - str) == 0) {
00081       *prot = VL_PROT_ASCII ;
00082     } 
00083     else if (strncmp(str, "bin",   cpt - str) == 0) {
00084       *prot = VL_PROT_BINARY ;
00085     }
00086     else {
00087       *prot = VL_PROT_UNKNOWN ;
00088     }
00089     cpt += 3 ;
00090   }
00091   return (char*) cpt ;
00092 }
00093 
00106 VL_EXPORT
00107 char const*  
00108 vl_string_protocol_name (int prot)
00109 {
00110   switch (prot) {
00111   case VL_PROT_ASCII: 
00112     return "ascii" ;
00113   case VL_PROT_BINARY:
00114     return "bin" ;
00115   case VL_PROT_NONE :
00116     return "" ;
00117   default:
00118     return 0 ;
00119   }
00120 }
00121 
00122 
00143 VL_EXPORT
00144 int
00145 vl_string_basename (char *dst, int n, char const *src, int n_ext)
00146 {
00147 
00148   char c ;
00149   int  k = 0 ;
00150   int  beg, end ;
00151   
00152   /* find beginning */
00153   beg = 0 ;
00154   for (k = 0 ; (c = src[k]) ; ++ k) {
00155     if (c == '\\' || c == '/') beg = k + 1 ;
00156   }
00157   
00158   /* find ending */
00159   end = strlen(src) ;
00160   for (k = end ; k > beg ; --k) {
00161     if (src [k - 1] == '.' && n_ext > 0) {
00162       -- n_ext ;
00163       end = k - 1 ;
00164     }
00165   }
00166   
00167   return vl_string_copy_sub (dst, n, src + beg, src + end) ; 
00168 }
00169 
00193 VL_EXPORT
00194 int
00195 vl_string_replace_wildcard (char *dst, int n, 
00196                             char const *src, 
00197                             char wild, 
00198                             char esc,
00199                             char const *repl)
00200 {
00201   char    c ;
00202   int     k = 0 ;
00203   vl_bool escape = 0 ;
00204 
00205   while (( c = *src++ )) {
00206     
00207     /* enter escape mode ? */
00208     if (! escape && c == esc) {
00209       escape = 1 ;
00210       continue ;
00211     }
00212 
00213     /* wildcard or regular? */
00214     if (! escape && c == wild) {
00215       char const *repl_ = repl ;
00216       while (( c = *repl_++ )) {
00217         if (dst && k < n - 1) dst [k]  = c ;
00218         ++ k ;
00219       }
00220     }
00221     /* regular character */
00222     else {
00223       if (dst && k < n - 1) dst [k] = c ;
00224       ++ k ;
00225     }
00226 
00227     escape = 0 ;
00228   }
00229 
00230   /* add trailing 0 */
00231   if (n > 0) dst [VL_MIN(k, n - 1)] = 0 ;
00232   return  k ;
00233 }
00234 
00249 VL_EXPORT
00250 int
00251 vl_string_copy (char *dst, int n, char const *src)
00252 {
00253 
00254   char c ;
00255   int  k = 0 ;
00256     
00257   while (( c = *src++ )) {
00258     if (dst && k < n - 1) dst [k] = c ;
00259     ++ k ;
00260   }
00261 
00262   /* finalize */
00263   if (n > 0) dst [VL_MIN(k, n - 1)] = 0 ;
00264   return  k ;
00265 }
00266 
00284 VL_EXPORT
00285 int
00286 vl_string_copy_sub (char *dst, int n, 
00287                     char const *beg,
00288                     char const *end)
00289 {
00290   char c ;
00291   int  k = 0 ;
00292 
00293   while (beg < end && (c = *beg++)) {
00294     if (dst && k < n - 1) dst [k] = c ;
00295     ++ k ;
00296   }
00297 
00298   /* finalize */
00299   if (n > 0) dst [VL_MIN(k, n - 1)] = 0 ;
00300   return  k ;
00301 }
00302 
00316 VL_EXPORT
00317 char *
00318 vl_string_find_char_rev (char const *beg, char const* end, char c)
00319 {
00320   while(end -- != beg) {
00321     if(*end == 'c')
00322       return (char*) end ;
00323   }
00324   return 0 ;
00325 }
00326 
00327 
00335 VL_EXPORT
00336 int
00337 vl_string_length (char const *str)
00338 {
00339   int i ;
00340   for(i = 0 ; str [i] ; ++i) ;
00341   return i ;
00342 }
00343 
00354 VL_EXPORT
00355 int
00356 vl_string_casei_cmp (const char *s1, const char *s2)
00357 {
00358   while (tolower((unsigned char)*s1) == 
00359          tolower((unsigned char)*s2))
00360   {
00361     if (*s1 == 0)
00362       return 0;
00363     s1++;
00364     s2++;
00365   }
00366   return 
00367     (int)tolower((unsigned char)*s1) - 
00368     (int)tolower((unsigned char)*s2) ;
00369 }
Copyright © 2008 Andrea Vedaldi and Brian Fulkerson