00001
00009
00010
00011
00012
00013
00014
00015
00016 #ifndef VL_GENERIC_DRIVER
00017 #define VL_GENERIC_DRIVER
00018
00019 #include <vl/generic.h>
00020 #include <vl/stringop.h>
00021
00022 #include <stdio.h>
00023 #include <assert.h>
00024
00027 struct _VlFileMeta
00028 {
00029 vl_bool active ;
00030 char pattern [1024] ;
00031 int protocol ;
00033 char name [1024] ;
00034 FILE * file ;
00035 } ;
00036
00040 typedef struct _VlFileMeta VlFileMeta ;
00041
00042
00060 static int
00061 vl_file_meta_parse (VlFileMeta * fm, char const * optarg)
00062 {
00063 int q ;
00064
00065 fm -> active = 1 ;
00066
00067 if (optarg) {
00068 int protocol ;
00069 char const * arg = vl_string_parse_protocol (optarg, &protocol) ;
00070
00071
00072 switch (protocol) {
00073 case VL_PROT_UNKNOWN :
00074 return VL_ERR_BAD_ARG ;
00075
00076 case VL_PROT_ASCII :
00077 case VL_PROT_BINARY :
00078 fm -> protocol = protocol ;
00079 break ;
00080
00081 case VL_PROT_NONE :
00082 break ;
00083 }
00084
00085 if (vl_string_length (arg) > 0) {
00086 q = vl_string_copy
00087 (fm -> pattern, sizeof (fm -> pattern), arg) ;
00088
00089 if (q >= sizeof (fm -> pattern)) {
00090 return VL_ERR_OVERFLOW ;
00091 }
00092 }
00093
00094 }
00095 return VL_ERR_OK ;
00096 }
00097
00098
00110 static int
00111 vl_file_meta_open (VlFileMeta * fm, char const * basename, char const * mode)
00112 {
00113 int q ;
00114
00115 if (! fm -> active) {
00116 return VL_ERR_OK ;
00117 }
00118
00119 q = vl_string_replace_wildcard (fm -> name,
00120 sizeof(fm -> name),
00121 fm -> pattern,
00122 '%', '\0',
00123 basename) ;
00124
00125 if (q >= sizeof(fm -> name)) {
00126 vl_err_no = VL_ERR_OVERFLOW ;
00127 return -1 ;
00128 }
00129
00130 if (fm -> active) {
00131 fm -> file = fopen (fm -> name, mode) ;
00132 if (! fm -> file) {
00133 vl_err_no = VL_ERR_IO ;
00134 return -1 ;
00135 }
00136 }
00137 return 0 ;
00138 }
00139
00140
00145 static void
00146 vl_file_meta_close (VlFileMeta * fm)
00147 {
00148 if (fm -> file) {
00149 fclose (fm -> file) ;
00150 fm -> file = 0 ;
00151 }
00152 }
00153
00154
00164 VL_INLINE int
00165 vl_file_meta_put_double (VlFileMeta * fm, double x)
00166 {
00167 int err ;
00168 size_t n ;
00169 double y ;
00170
00171 switch (fm -> protocol) {
00172
00173 case VL_PROT_ASCII :
00174 err = fprintf (fm -> file, "%g ", x) ;
00175 break ;
00176
00177 case VL_PROT_BINARY :
00178 vl_swap_host_big_endianness_8 (&y, &x) ;
00179 n = fwrite (&y, sizeof(double), 1, fm -> file) ;
00180 err = n < 1 ;
00181 break ;
00182
00183 default :
00184 assert (0) ;
00185 break ;
00186 }
00187
00188 return err ? VL_ERR_ALLOC : VL_ERR_OK ;
00189 }
00190
00191
00201 VL_INLINE int
00202 vl_file_meta_put_uint8 (VlFileMeta *fm, vl_uint8 x)
00203 {
00204 size_t n ;
00205 int err ;
00206
00207 switch (fm -> protocol) {
00208
00209 case VL_PROT_ASCII :
00210 err = fprintf (fm -> file, "%d ", x) ;
00211 if (err) return VL_ERR_ALLOC ;
00212 break ;
00213
00214 case VL_PROT_BINARY :
00215 n = fwrite (&x, sizeof(vl_uint8), 1, fm -> file) ;
00216 if (n < 1) return VL_ERR_ALLOC ;
00217 break ;
00218
00219 default :
00220 assert (0) ;
00221 break ;
00222 }
00223
00224 return VL_ERR_OK ;
00225 }
00226
00227
00238 VL_INLINE int
00239 vl_file_meta_get_double (VlFileMeta *fm, double *x)
00240 {
00241 int err ;
00242 size_t n ;
00243 double y ;
00244
00245 switch (fm -> protocol) {
00246
00247 case VL_PROT_ASCII :
00248 fscanf (fm -> file, " ") ;
00249 err = fscanf (fm -> file, "%lg", x) ;
00250 if (err == EOF) return VL_ERR_EOF ;
00251 if (err < 1 ) return VL_ERR_BAD_ARG ;
00252 break ;
00253
00254 case VL_PROT_BINARY :
00255 n = fread (&y, sizeof(double), 1, fm -> file) ;
00256 if (n < 1) return VL_ERR_BAD_ARG ;
00257 vl_swap_host_big_endianness_8 (x, &y) ;
00258 break ;
00259
00260 default :
00261 assert (0) ;
00262 break ;
00263 }
00264
00265 return VL_ERR_OK ;
00266 }
00267
00268
00269
00270
00271 #endif