Library for structured binary data I/O
bdio.h
Go to the documentation of this file.
1 
59 #ifndef H_BDIO
60 #define H_BDIO 1
61 
62 /* record data formats */
66 #define BDIO_BIN_GENERIC 0x00
67 
70 #define BDIO_ASC_EXEC 0x01
71 
74 #define BDIO_BIN_INT32BE 0x02
75 
78 #define BDIO_BIN_INT32LE 0x03
79 
82 #define BDIO_BIN_INT64BE 0x04
83 
86 #define BDIO_BIN_INT64LE 0x05
87 
90 #define BDIO_BIN_F32BE 0x06
91 
94 #define BDIO_BIN_F32LE 0x07
95 
98 #define BDIO_BIN_F64BE 0x08
99 
102 #define BDIO_BIN_F64LE 0x09
103 
106 #define BDIO_ASC_GENERIC 0x0A
107 
110 #define BDIO_ASC_XML 0x0B
111 
115 #define BDIO_BIN_INT32 0xF0
116 
119 #define BDIO_BIN_INT64 0xF1
120 
123 #define BDIO_BIN_F32 0xF2
124 
127 #define BDIO_BIN_F64 0xF3
128 
129 /* I/O modes 'r','w','a' */
133 #define BDIO_R_MODE 0
134 
137 #define BDIO_W_MODE 1
138 
141 #define BDIO_A_MODE 2
142 
143 /* possible values for BDIO.state */
147 #define BDIO_H_STATE 1
148 
151 #define BDIO_R_STATE 2
152 
155 #define BDIO_N_STATE 3
156 
159 #define BDIO_E_STATE 4
160 
161 /* little endian, big endian */
165 #define BDIO_LEND 0
166 
169 #define BDIO_BEND 1
170 
171 
172 #include <stdint.h>
173 #include <stdio.h>
174 
175 /* data types */
181 typedef struct
182 {
183  int state;
184  int mode;
189  int endian;
193  FILE *fp;
195  FILE *msg;
196  int verbose;
197  char error[256];
198  char ferror[256];
199  int nerror;
200 
201  unsigned char *buf;
202 
204  int hcnt;
205 
206  /* information contained in last header */
207  int hmagic ;
208  int hversion;
209  int hdirinfo1;
210  int hdirinfo2;
213  int hcdate;
216  int hmdate;
217  char *hcuser;
218  char *hmuser;
219  char *hchost;
220  char *hmhost;
221  char *hpinfo;
222  int hssize;
223  uint64_t hstart;
224 
225  /* information about current record */
226  uint64_t rstart;
227  int rcnt;
228  uint64_t rlen;
229  char rlongrec;
231 
232  uint64_t ridx;
233  int rfmt;
234  int ruinfo;
235  int rdsize;
236  char rswap;
237 
238  /* information about the buffer */
239  uint64_t bufstart;
240  int bufidx;
241 } BDIO;
244 
245 
246 
247 /* prototypes */
258 void bdio_perror(const char *s, BDIO *fh);
259 
260 
271 void bdio_pferror(const char *s, BDIO *fh);
272 
311 BDIO *bdio_open(const char* file, const char* mode, char* protocol_info);
312 
313 
331 int bdio_close(BDIO *fh);
332 
333 
339 void bdio_set_dflt_msg(FILE *stream);
340 
341 
351 int bdio_set_msg(FILE *stream,BDIO *fh);
352 
353 
361 void bdio_set_dflt_verbose(int v);
362 
363 
372 int bdio_set_verbose(int v,BDIO *fh);
373 
374 
381 char* bdio_get_hchost(BDIO *fh);
382 
383 
390 char* bdio_get_hcuser(BDIO *fh);
391 
392 
399 char* bdio_get_hmhost(BDIO *fh);
400 
401 
408 char* bdio_get_hmuser(BDIO *fh);
409 
410 
417 char* bdio_get_hpinfo(BDIO *fh);
418 
419 
426 int bdio_get_hcdate(BDIO *fh);
427 
428 
435 int bdio_get_hmdate(BDIO *fh);
436 
443 int bdio_get_ruinfo(BDIO *fh);
444 
445 
452 int bdio_get_hversion(BDIO *fh);
453 
454 
462 int bdio_get_hcnt(BDIO *fh);
463 
464 
473 int bdio_get_rfmt(BDIO *fh);
474 
475 
485 uint64_t bdio_get_rlen(BDIO *fh);
486 
487 
488 
497 int bdio_get_rcnt(BDIO *fh);
498 
499 
506 int bdio_is_in_record(BDIO *fh);
507 
508 
515 int bdio_is_in_header(BDIO *fh);
516 
517 
529 int bdio_seek_record(BDIO *fh);
530 
531 
551 size_t bdio_read(void *buf, size_t nb, BDIO *fh);
552 
553 
571 size_t bdio_read_f32(float *buf, size_t nb, BDIO *fh);
572 
573 
591 size_t bdio_read_f64(double *buf, size_t nb, BDIO *fh);
592 
593 
594 
612 size_t bdio_read_int32(int32_t *buf, size_t nb, BDIO *fh);
613 
614 
632 size_t bdio_read_int64(int64_t *buf, size_t nb, BDIO *fh);
633 
634 
669 int bdio_start_record(int fmt, int uinfo, BDIO *fh);
670 
671 
705 int bdio_append_record(int fmt, int uinfo, BDIO *fh);
706 
707 
708 
732 size_t bdio_write(void *ptr, size_t nb, BDIO *fh);
733 
734 
735 
753 size_t bdio_write_f32(float *ptr, size_t nb, BDIO *fh);
754 
755 
756 
774 size_t bdio_write_f64(double *ptr, size_t nb, BDIO *fh);
775 
776 
777 
795 size_t bdio_write_int32(int32_t *ptr, size_t nb, BDIO *fh);
796 
797 
798 
816 size_t bdio_write_int64(int64_t *ptr, size_t nb, BDIO *fh);
817 
818 
819 
820 
843 int bdio_flush_record( BDIO *fh);
844 
845 #endif
char * hpinfo
protocol-info | schema | generator | spec of the last header
Definition: bdio.h:221
char * bdio_get_hmuser(BDIO *fh)
Get the user name who modified the last header most recently.
Definition: bdio.c:1148
size_t bdio_write_int64(int64_t *ptr, size_t nb, BDIO *fh)
Write nb bytes from ptr to fh.
Definition: bdio.c:2000
int bdio_get_hcdate(BDIO *fh)
Get the time of creation of the last header.
Definition: bdio.c:1184
uint64_t bufstart
offset in record where the buffer starts
Definition: bdio.h:239
int bdio_get_ruinfo(BDIO *fh)
Get the user info of the current record.
Definition: bdio.c:1202
char * hchost
host of first creation of the last header
Definition: bdio.h:219
uint64_t bdio_get_rlen(BDIO *fh)
Get length of data content of current record (written or read).
Definition: bdio.c:1226
void bdio_set_dflt_verbose(int v)
Change the behavior for reporting of errors.
Definition: bdio.c:1075
int hcnt
number of headers encountered (including current)
Definition: bdio.h:204
int bdio_start_record(int fmt, int uinfo, BDIO *fh)
Position bdio stream after the current record and start writing a new record with specified format an...
Definition: bdio.c:1596
int bdio_get_hmdate(BDIO *fh)
Get the time of the most recent modification of the last header.
Definition: bdio.c:1193
int bdio_is_in_record(BDIO *fh)
Returns 1 if fh is in a record and 0 otherwise.
Definition: bdio.c:1251
int ruinfo
user info of current record
Definition: bdio.h:234
size_t bdio_read_f32(float *buf, size_t nb, BDIO *fh)
brief Read nb bytes from fh into buf. nb must be a multiple of 4.
Definition: bdio.c:1536
int bdio_seek_record(BDIO *fh)
Position bdio stream to start of next record and read its header.
Definition: bdio.c:1339
size_t bdio_write_int32(int32_t *ptr, size_t nb, BDIO *fh)
Write nb bytes from ptr to fh.
Definition: bdio.c:1981
char rswap
1/0 = records data has/has not to be byte-swapped
Definition: bdio.h:236
uint64_t ridx
offset within current record or header
Definition: bdio.h:232
int bdio_set_verbose(int v, BDIO *fh)
Set bdio file fh to verbose (v>0) or silent (v==0)
Definition: bdio.c:1085
void bdio_pferror(const char *s, BDIO *fh)
Print an error string to BDIO.msg.
Definition: bdio.c:661
char * hmuser
user of last modification of the last header
Definition: bdio.h:218
int rdsize
size of a data-item in the current record e.g int32 -> 4
Definition: bdio.h:235
int hdirinfo1
Definition: bdio.h:209
size_t bdio_write_f64(double *ptr, size_t nb, BDIO *fh)
Write nb bytes from ptr to fh.
Definition: bdio.c:1963
char * bdio_get_hmhost(BDIO *fh)
Get the host on which the last header was most recently modified.
Definition: bdio.c:1139
int bdio_get_hcnt(BDIO *fh)
Get number of headers read or written so far (including current)
Definition: bdio.c:1175
size_t bdio_read_int64(int64_t *buf, size_t nb, BDIO *fh)
brief Read nb bytes from fh into buf. nb must be a multiple of 8.
Definition: bdio.c:1581
size_t bdio_read_f64(double *buf, size_t nb, BDIO *fh)
brief Read nb bytes from fh into buf. nb must be a multiple of 8.
Definition: bdio.c:1551
void bdio_set_dflt_msg(FILE *stream)
Set the default stream for error messages.
Definition: bdio.c:1069
int nerror
number of errors that occured so far
Definition: bdio.h:199
char * hmhost
host of last modification of the last header
Definition: bdio.h:220
unsigned char * buf
Definition: bdio.h:201
char * bdio_get_hchost(BDIO *fh)
Get the host on which the last header was created.
Definition: bdio.c:1121
int state
Definition: bdio.h:183
int hmdate
date of last modification of the last header
Definition: bdio.h:216
int verbose
0: silent mode, 1: print all errors, default: 0
Definition: bdio.h:196
size_t bdio_write(void *ptr, size_t nb, BDIO *fh)
Write nb bytes from ptr to fh.
Definition: bdio.c:1754
int bdio_is_in_header(BDIO *fh)
Returns 1 if fh is in a header and 0 otherwise.
Definition: bdio.c:1263
int rcnt
number of records encountered (including current)
Definition: bdio.h:227
int bufidx
Definition: bdio.h:240
bdio file descriptor
Definition: bdio.h:181
char * hcuser
user of first creation of the last header
Definition: bdio.h:217
int bdio_get_rfmt(BDIO *fh)
Get format of current record (written or read).
Definition: bdio.c:1214
uint64_t rstart
start position of current record or header
Definition: bdio.h:226
int bdio_flush_record(BDIO *fh)
Finalize the current record and set fh to BDIO_N_STATE.
Definition: bdio.c:2020
int bdio_get_hversion(BDIO *fh)
Get version number in last header.
Definition: bdio.c:1166
size_t bdio_read_int32(int32_t *buf, size_t nb, BDIO *fh)
brief Read nb bytes from fh into buf. nb must be a multiple of 4.
Definition: bdio.c:1566
size_t bdio_read(void *buf, size_t nb, BDIO *fh)
Read nb bytes from fh into buf.
Definition: bdio.c:1489
int bdio_get_rcnt(BDIO *fh)
Get number of records read or written so far (including current)
Definition: bdio.c:1242
uint64_t rlen
Definition: bdio.h:228
int bdio_close(BDIO *fh)
Close a bdio file.
Definition: bdio.c:1014
int hmagic
magic number of the last header
Definition: bdio.h:207
FILE * msg
file pointer to file for error messages (e.g. stderr)
Definition: bdio.h:195
int rfmt
format of current record
Definition: bdio.h:233
int hssize
combined length of user/host/pinfo strings of the last header
Definition: bdio.h:222
size_t bdio_write_f32(float *ptr, size_t nb, BDIO *fh)
Write nb bytes from ptr to fh.
Definition: bdio.c:1945
void bdio_perror(const char *s, BDIO *fh)
Print an error string to BDIO.msg.
Definition: bdio.c:650
BDIO * bdio_open(const char *file, const char *mode, char *protocol_info)
Open a bdio file in mode &#39;r&#39; (read), &#39;w&#39; (write) or &#39;a&#39; (append).
Definition: bdio.c:672
char * bdio_get_hpinfo(BDIO *fh)
Get the protocol info of the last header.
Definition: bdio.c:1157
int hversion
bdio version of the last header
Definition: bdio.h:208
char * bdio_get_hcuser(BDIO *fh)
Get the user name who created the last header.
Definition: bdio.c:1130
int bdio_set_msg(FILE *stream, BDIO *fh)
Set the stream for error messages in fh to stream.
Definition: bdio.c:1105
uint64_t hstart
starting position of last header record
Definition: bdio.h:223
int bdio_append_record(int fmt, int uinfo, BDIO *fh)
If the last item in the file was a record: position the stream at the end of it. Otherwise EOF is ret...
Definition: bdio.c:1278