hl7parse
|
primary hl7 parse methods More...
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include "logging.h"
#include "bom.h"
#include "meta.h"
#include "node.h"
#include "util.h"
Go to the source code of this file.
Functions | |
const char * | version_parser () |
parser version information More... | |
void | print_error (int e, char *additional) |
pretty print system errors More... | |
unsigned char * | extract_substr (int start, int length, unsigned char *buffer) |
extract a sub string from a string More... | |
int | parse_segment (FILE *fd, hl7_meta_t *meta, node_t **fieldlist_p, unsigned char **segment_name) |
parse one HL7 line More... | |
int | hl7_decode (FILE *fd, message_t **message_p) |
Entry point for the parser. More... | |
message_t * | decode (FILE *fd, hl7_meta_t *meta) |
parse hl7 file More... | |
FILE * | hl7_open (char *filename) |
open file More... | |
int | hl7_close (FILE *fd) |
close file More... | |
primary hl7 parse methods
high level interface to be exposed to bindings
message_t* decode | ( | FILE * | fd, |
hl7_meta_t * | meta | ||
) |
parse hl7 file
This method gives more control of the metadata than hl7_decode(). You may do your own delimiter detection and store the information in a meta_t struct.
Make sure to set meta_t.crlf to something other than -1
(default) otherwise the parser will try to detect the delimiters again from the first line it is fed.
fd | file pointer |
meta | prepared metadata with meta_t.clrf != -1 to prevent automagic meta detection by hl7_decode() |
unsigned char* extract_substr | ( | int | start, |
int | length, | ||
unsigned char * | buffer | ||
) |
extract a sub string from a string
This method expects the string's length without \0
character. It will allocate a new buffer (including delimiting \0
) and copy all data into the new buffer. The buffer wil lthen be delimited with \0
.
The user must take care to properly free() the newly allcoated string.
start | position of the substrings first character |
length | string length without terminating \0 |
buffer | the srouce string |
int hl7_close | ( | FILE * | fd | ) |
close file
Convencience function, mainly used for language bindings
fd | file pointer |
fclose()
int hl7_decode | ( | FILE * | fd, |
message_t ** | message_p | ||
) |
Entry point for the parser.
The parser has 2 modes:
\r
or \n
is found. Detirmine delimiters, update meta 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:
void (*cb_progress)(message_t *message, size_t total, size_t current);
void (*cb_start)(message_t *message)
void (*cb_end)(message_t *message, size_t max, size_t current, int exit_code)
void (*cb_segment)(message_t *message, size_t num, char name[3])
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).
fd | File descriptor, must be forwarded on the last byte of the BOM (if any) |
message_p | this must be an initialized message_t object. |
FILE* hl7_open | ( | char * | filename | ) |
open file
Convencience function, mainly used for language bindings
filename | path to file |
int parse_segment | ( | FILE * | fd, |
hl7_meta_t * | meta, | ||
node_t ** | fieldlist_p, | ||
unsigned char ** | segment_name | ||
) |
parse one HL7 line
Please use read_meta() before using this method on the first line of the HL7 file. It might work without (if the HL7 file uses default delimiters) but is not safe.
This method reads one line of the HL7 file and parses it into a segment structure for node_t's. Meta will be updated once we reach end of line. meta->crlf
and meta->sep_message
will be updated when the first \n
or \r
character is found if meta->crlf
is -1
(default value, no EOL
detection happened yet).
meta->crlf
and meta->sep_message
will only be updated on the first pass, then cached data will be used to speed up the parsing process.
Error codes:
1
: failed to allocate memory for line buffer2
: failed to allocate more memory for line buffer3
: EOF was reached unexpectedly4
: maximum delimiters per segment is reached, raise MAX_FIELDS5
: failed to allocate raw_field 6
: failed to allocate node_t 7
: failed to process sub field elements 8
: failed to append child 9
: failed to allocate memory for segment name10
: File did not start with 'MSH'11
: MSH-2 was not delimited by MSH-1, FIXME: these error codes may overlap where we use ret+10 now!12
: Segment name is longer than 5 bytesfd | File descriptor, must be forwarded on the last byte of the BOM (if any) |
meta | must be initialized at least with correct delimimters. this is typically done by parse_msh() |
fieldlist_p | this is the node tree of the segment |
segment_name | will hold the segment name, eg. "PID", "OBX", ... |
It is safest to first run read_meta() before calling parse_segment(). If you know that this is an MSH
segment (and only then) it is safe to skip read_meta() prior to running parse_segment().
If read_meta() is not run, then we we go in meta detection mode and expect the field separator at position 3 of this line.
Not sure if this is really a good idea? We assume that the segment name is always 3 characters which seems to be true most of the time (always?).
You may override this by first manually detecting all separators and feeding parse_segment with a complete metadata structure.
void print_error | ( | int | e, |
char * | additional | ||
) |
pretty print system errors
e | Error code |
additional | Extra string to print |
const char* version_parser | ( | ) |
parser version information