generic-driver.h

00001 
00009 /* AUTORIGHTS */
00010 
00011 #ifndef VL_GENERIC_DRIVER
00012 #define VL_GENERIC_DRIVER
00013 
00014 #include <vl/generic.h>
00015 #include <vl/stringop.h>
00016 
00017 #include <stdio.h>
00018 #include <assert.h>
00019 
00022 struct _VlFileMeta
00023 {
00024   vl_bool active ;          
00025   char    pattern [1024] ;  
00026   int     protocol ;        
00028   char    name [1024] ;     
00029   FILE *  file ;            
00030 } ;
00031 
00035 typedef struct _VlFileMeta VlFileMeta ;
00036 
00037 /* ----------------------------------------------------------------- */
00055 static int
00056 vl_file_meta_parse (VlFileMeta * fm, char const * optarg) 
00057 {
00058   int q ;
00059 
00060   fm -> active = 1 ;
00061 
00062   if (optarg) {
00063     int protocol ;
00064     char const * arg = vl_string_parse_protocol (optarg, &protocol) ;
00065         
00066     /* parse the (optional) protocol part */
00067     switch (protocol) {
00068     case VL_PROT_UNKNOWN :
00069       return VL_ERR_BAD_ARG  ;
00070 
00071     case VL_PROT_ASCII  :
00072     case VL_PROT_BINARY :
00073       fm -> protocol = protocol ;
00074       break ;
00075 
00076     case VL_PROT_NONE :
00077       break ;
00078     }
00079 
00080     if (vl_string_length (arg) > 0) {
00081       q = vl_string_copy 
00082         (fm -> pattern, sizeof (fm -> pattern), arg) ;
00083     
00084       if (q >= sizeof (fm -> pattern)) {
00085         return VL_ERR_OVERFLOW ;
00086       }
00087     }
00088     
00089   }
00090   return VL_ERR_OK ;
00091 }
00092 
00093 /* ----------------------------------------------------------------- */
00105 static int
00106 vl_file_meta_open (VlFileMeta * fm, char const * basename, char const * mode) 
00107 {
00108   int q ;
00109   
00110   if (! fm -> active) {
00111     return VL_ERR_OK ;
00112   }
00113 
00114   q = vl_string_replace_wildcard (fm -> name, 
00115                                   sizeof(fm -> name),
00116                                   fm -> pattern, 
00117                                   '%', '\0', 
00118                                   basename) ;
00119 
00120   if (q >= sizeof(fm -> name)) {
00121     vl_err_no = VL_ERR_OVERFLOW ;
00122     return -1 ;
00123   }
00124   
00125   if (fm -> active) {
00126     fm -> file = fopen (fm -> name, mode) ;
00127     if (! fm -> file) {
00128       vl_err_no = VL_ERR_IO ;
00129       return -1 ;
00130     }
00131   }
00132   return 0 ;
00133 }
00134 
00135 /* ----------------------------------------------------------------- */
00140 static void
00141 vl_file_meta_close (VlFileMeta * fm) 
00142 {
00143   if (fm -> file) {
00144     fclose (fm -> file) ;
00145     fm -> file = 0 ;
00146   }
00147 }
00148 
00149 /* ----------------------------------------------------------------- */
00159 VL_INLINE int
00160 vl_file_meta_put_double (VlFileMeta * fm, double x)
00161 {
00162   int err ;
00163   size_t n ;
00164   double y ;
00165 
00166   switch (fm -> protocol) {
00167 
00168   case VL_PROT_ASCII :
00169     err = fprintf (fm -> file, "%g ", x) ;
00170     break ;
00171     
00172   case VL_PROT_BINARY :
00173     vl_adapt_endianness_8 (&y, &x) ;
00174     n = fwrite (&y, sizeof(double), 1, fm -> file) ;
00175     err = n < 1 ;
00176     break ;
00177 
00178   default :
00179     assert (0) ;
00180     break ;
00181   }
00182 
00183   return err ? VL_ERR_ALLOC : VL_ERR_OK ;
00184 }
00185 
00186 /* ----------------------------------------------------------------- */
00196 VL_INLINE int
00197 vl_file_meta_put_uint8 (VlFileMeta *fm, vl_uint8 x)
00198 {
00199   size_t n ;
00200   int err ;
00201 
00202   switch (fm -> protocol) {
00203 
00204   case VL_PROT_ASCII :
00205     err = fprintf (fm -> file, "%d ", x) ;
00206     if (err) return VL_ERR_ALLOC ;
00207     break ;
00208     
00209   case VL_PROT_BINARY :
00210     n = fwrite (&x, sizeof(vl_uint8), 1, fm -> file) ;
00211     if (n < 1) return VL_ERR_ALLOC ;
00212     break ;
00213 
00214   default :
00215     assert (0) ;
00216     break ;
00217   }
00218 
00219   return VL_ERR_OK ;
00220 }
00221 
00222 /* ----------------------------------------------------------------- */
00233 VL_INLINE int
00234 vl_file_meta_get_double (VlFileMeta *fm, double *x)
00235 {
00236   int err ;
00237   size_t n ;
00238   double y ;
00239 
00240   switch (fm -> protocol) {
00241 
00242   case VL_PROT_ASCII :
00243     fscanf (fm -> file, " ") ;
00244     err = fscanf (fm -> file, "%lg", x) ;
00245     if (err == EOF) return VL_ERR_EOF ;
00246     if (err <  1  ) return VL_ERR_BAD_ARG ;
00247     break ;
00248   
00249   case VL_PROT_BINARY :
00250     n = fread (&y, sizeof(double), 1, fm -> file) ;
00251     if (n < 1) return VL_ERR_BAD_ARG ;
00252     vl_adapt_endianness_8 (x, &y) ;
00253     break ;
00254 
00255   default :
00256     assert (0) ;
00257     break ;
00258   }
00259 
00260   return VL_ERR_OK ;
00261 }
00262 
00263 
00264 
00265 /* VL_GENERIC_DRIVER */
00266 #endif

Generated on Mon Jan 21 17:43:32 2008 for vlfeat by  doxygen 1.5.4