hl7parse
Classes | Typedefs | Functions
message_state.h File Reference

private message structures More...

#include "node.h"
Include dependency graph for message_state.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

struct  message_state_t
 holds callbacks and associated variables More...
 

Typedefs

typedef struct message_t message_t
 
typedef struct message_state_t message_state_t
 holds callbacks and associated variables
 

Functions

message_state_tmessage_state_new ()
 initializes an message_state_t structure
 
void message_state_free (message_state_t *ms)
 frees an messate_state_t structure
 

Detailed Description

private message structures

This file describes a structure that is used to keep the state of the parser while parsing a hl7 file. It is primarely used for registering callback functions.

If you want to analyze parts of the document while the parser is still running or you are using it in a multi-threaded environment, then there is the possibility to add callback functions. The following callback hooks are available:

Also, you can controll how often the cb_progress() callback is fired, default is every 1% of progress (does not fire on files smaller than 100 bytes).

You may register callbacks as follows:

#include <stdio.h>
#include "decode.h" // or lib7.h which is the combined header for lib7.so
static void cb_progress(message_t *message, size_t total, size_t current) {
printf("Parsing: %02d%%\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b", (current*100/total)+1);
fflush(stdout);
}
static void cb_end(message_t *message, size_t max, size_t current, int exit_code) {
// clear progress line in terminal
printf("\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b");
if (exit_code != 0) {
printf("Parser failed with exit code: %02d\n", exit_code);
}
}
int main() {
char *filename "some_file.hl7";
FILE *fd = fopen(filename, "rb");
// example of a progress callback, it will show on very large files
size_t fd_pos = ftell(fd);
fseek(fd, 0, SEEK_END);
size_t file_size = ftell(fd);
fseek(fd, fd_pos, SEEK_SET); // reset
// register callback progress
root->state->cb_progress = cb_progress;
// call progress every 5%
root->state->progress_every = file_size / 20;
// add a callback at the end of paring, which will clear the progress line
root->state->cb_end = cb_end;
// start the parser
int ret = hl7_decode(fd, &root);
fclose(fd); // no need for fd anymore, all data in memory
if (ret != 0) {
// whoopsie
}
// do something with the data in root
// cleanup
}