generic-driver.h
Go to the documentation of this file.
00001 
00009 /*
00010 Copyright (C) 2007-12 Andrea Vedaldi and Brian Fulkerson.
00011 Copyright (C) 2013 Andrea Vedaldi.
00012 All rights reserved.
00013 
00014 This file is part of the VLFeat library and is made available under
00015 the terms of the BSD license (see the COPYING file).
00016 */
00017 
00018 #ifndef VL_GENERIC_DRIVER
00019 #define VL_GENERIC_DRIVER
00020 
00021 #include <vl/generic.h>
00022 #include <vl/stringop.h>
00023 
00024 #include <stdio.h>
00025 #include <assert.h>
00026 
00029 struct _VlFileMeta
00030 {
00031   vl_bool active ;          
00032   char    pattern [1024] ;  
00033   int     protocol ;        
00035   char    name [1024] ;     
00036   FILE *  file ;            
00037 } ;
00038 
00042 typedef struct _VlFileMeta VlFileMeta ;
00043 
00044 /* ----------------------------------------------------------------- */
00062 static int
00063 vl_file_meta_parse (VlFileMeta * self, char const * optarg)
00064 {
00065   vl_size q ;
00066   self->active = 1 ;
00067 
00068   if (optarg) {
00069     int protocol ;
00070     char const * arg = vl_string_parse_protocol (optarg, &protocol) ;
00071 
00072     /* parse the (optional) protocol part */
00073     switch (protocol) {
00074     case VL_PROT_UNKNOWN :
00075       return VL_ERR_BAD_ARG  ;
00076 
00077     case VL_PROT_ASCII  :
00078     case VL_PROT_BINARY :
00079       self->protocol = protocol ;
00080       break ;
00081 
00082     case VL_PROT_NONE :
00083       break ;
00084     }
00085 
00086     if (vl_string_length (arg) > 0) {
00087       q = vl_string_copy
00088         (self->pattern, sizeof (self->pattern), arg) ;
00089 
00090       if (q >= sizeof(self->pattern)) {
00091         return VL_ERR_OVERFLOW ;
00092       }
00093     }
00094 
00095   }
00096   return VL_ERR_OK ;
00097 }
00098 
00099 /* ----------------------------------------------------------------- */
00111 static int
00112 vl_file_meta_open (VlFileMeta * self, char const * basename, char const * mode)
00113 {
00114   vl_size q ;
00115 
00116   if (! self->active) {
00117     return VL_ERR_OK ;
00118   }
00119 
00120   q = vl_string_replace_wildcard (self->name,
00121                                   sizeof(self->name),
00122                                   self -> pattern,
00123                                   '%', '\0',
00124                                   basename) ;
00125 
00126   if (q >= sizeof(self->name)) {
00127     return vl_set_last_error (VL_ERR_OVERFLOW, NULL) ;
00128   }
00129 
00130   if (self->active) {
00131     self->file = fopen (self->name, mode) ;
00132     if (! self->file) {
00133       return vl_set_last_error(VL_ERR_IO, NULL) ;
00134     }
00135   }
00136   return 0 ;
00137 }
00138 
00139 /* ----------------------------------------------------------------- */
00144 static void
00145 vl_file_meta_close (VlFileMeta * self)
00146 {
00147   if (self -> file) {
00148     fclose (self -> file) ;
00149     self -> file = 0 ;
00150   }
00151 }
00152 
00153 /* ----------------------------------------------------------------- */
00163 VL_INLINE int
00164 vl_file_meta_put_double (VlFileMeta * self, double x)
00165 {
00166   int err ;
00167   size_t n ;
00168   double y ;
00169 
00170   switch (self -> protocol) {
00171 
00172   case VL_PROT_ASCII :
00173     err = fprintf (self -> file, "%g ", x) ;
00174     break ;
00175 
00176   case VL_PROT_BINARY :
00177     vl_swap_host_big_endianness_8 (&y, &x) ;
00178     n = fwrite (&y, sizeof(double), 1, self -> file) ;
00179     err = n < 1 ;
00180     break ;
00181 
00182   default :
00183     abort() ;
00184   }
00185 
00186   return err ? VL_ERR_ALLOC : VL_ERR_OK ;
00187 }
00188 
00189 /* ----------------------------------------------------------------- */
00199 VL_INLINE int
00200 vl_file_meta_put_uint8 (VlFileMeta *self, vl_uint8 x)
00201 {
00202   size_t n ;
00203   int err ;
00204 
00205   switch (self -> protocol) {
00206 
00207   case VL_PROT_ASCII :
00208     err = fprintf (self -> file, "%d ", x) ;
00209     if (err) return VL_ERR_ALLOC ;
00210     break ;
00211 
00212   case VL_PROT_BINARY :
00213     n = fwrite (&x, sizeof(vl_uint8), 1, self -> file) ;
00214     if (n < 1) return VL_ERR_ALLOC ;
00215     break ;
00216 
00217   default :
00218     abort() ;
00219   }
00220 
00221   return VL_ERR_OK ;
00222 }
00223 
00224 /* ----------------------------------------------------------------- */
00235 VL_INLINE int
00236 vl_file_meta_get_double (VlFileMeta *self, double *x)
00237 {
00238   int err ;
00239   size_t n ;
00240   double y ;
00241 
00242   switch (self -> protocol) {
00243 
00244   case VL_PROT_ASCII :
00245     err = fscanf (self -> file, " ") ;
00246     if (err == EOF) return VL_ERR_EOF ;
00247     err = fscanf (self -> file, "%lg", x) ;
00248     if (err == EOF) return VL_ERR_EOF ;
00249     if (err <  1  ) return VL_ERR_BAD_ARG ;
00250     break ;
00251 
00252   case VL_PROT_BINARY :
00253     n = fread (&y, sizeof(double), 1, self -> file) ;
00254     if (n < 1) return VL_ERR_BAD_ARG ;
00255     vl_swap_host_big_endianness_8 (x, &y) ;
00256     break ;
00257 
00258   default :
00259     abort() ;
00260   }
00261 
00262   return VL_ERR_OK ;
00263 }
00264 
00265 
00266 
00267 /* VL_GENERIC_DRIVER */
00268 #endif


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