hl7parse
|
Main datastructures for HL7 nodes and messages This file contains the main parser data structures and methods to read and parse hl7 files. More...
#include <stdio.h>
#include <stdlib.h>
#include "meta.h"
#include "address.h"
#include "message_state.h"
#include "logging.h"
Go to the source code of this file.
Classes | |
struct | raw_field_t |
structure to track delmiter fields in a fieldset More... | |
struct | node_t |
primary storage type of a delimited element More... | |
struct | message_t |
Macros | |
#define | NODE_PREALLOC_CHILDREN 5 |
number of children to pre-allocate when there is not room in children anymore | |
#define | MESSAGE_PREALLOC_CHILDREN 10 |
number of children to pre-allocate when there is not room in children anymore | |
#define | MAX_FIELDS 1000 |
maximum number of temporary elements to allocate in the parser buffer | |
Typedefs | |
typedef struct raw_field_t | raw_field_t |
structure to track delmiter fields in a fieldset More... | |
typedef enum node_type_t | node_type_t |
Node types. | |
typedef struct node_t | node_t |
primary storage type of a delimited element More... | |
typedef struct message_state_t | message_state_t |
hl7 message container More... | |
typedef struct message_t | message_t |
Enumerations | |
enum | node_type_t { MESSAGE = 1, SEGMENT = 2, FIELDLIST = 4, FIELD = 8, COMP = 16, SUBCOMP = 32, LEAF = 64 } |
Node types. More... | |
Functions | |
raw_field_t * | create_raw_field_t (void) |
create raw fied structure More... | |
void | free_raw_field (raw_field_t *raw_e) |
free raw fiel structure | |
node_t * | create_node_t (node_type_t type, unsigned char *data, size_t length, int pos) |
create a new node More... | |
void | free_node_t (node_t *node) |
cleanup all memory of a node More... | |
int | node_append (node_t **parent, node_t *node) |
append a child node More... | |
int | node_parent_child_pos (node_t *node) |
find the position in parent's children struct More... | |
node_t * | process_node (raw_field_t *raw_e, hl7_meta_t *meta, int start_pos) |
void | disply_raw_node (raw_field_t *raw_e) |
dump raw_e structure to stdout More... | |
node_t * | node_in_segment (node_t *segment, hl7_addr_t *addr) |
check if a node with given addres exists More... | |
const char * | node_type_to_string (node_type_t type) |
string representation of node_type_t More... | |
message_t * | create_message_t (hl7_meta_t *meta) |
initialize an empty messagte_t struct More... | |
void | free_message_t (message_t *message) |
free message_t an all it's child objects More... | |
int | message_append (message_t **parent, node_t *node) |
append a segment to the message More... | |
hl7_addr_t * | addr_from_node (node_t *node) |
generate an addr from any node in a message More... | |
Main datastructures for HL7 nodes and messages This file contains the main parser data structures and methods to read and parse hl7 files.
All api functions defined here are used by the parser to create a node_t structure. For advanced node capabilities (for example editing) see node_util.h experiemntal functions.
typedef struct message_state_t message_state_t |
hl7 message container
This struct holds data about the hl7 file (delimiter) in messgae_t.meta
as well as a tree structure of nodes, stored in message_t.segments.
This is your main structure returned by the parser.
Example:
primary storage type of a delimited element
Every hl7 element (regardless of place in the hirarchy) is represented by a node_t. The node_t.type defines where in the hirarchy the node is. Every node_t has a parent
node.
The top level node of a parsed HL7 message is always of type message_t. The Hirarchy looks like this:
node_t.type
== SEGMENT
this is the segment nodenode_t.type
== FIELDLIST
array of elements between |
, always one, may have multiple children delimited by ~
node_t.type
== FIELD
array of fields delimited by ~
, always at least onenode_t.type
== COMP
optional components delimited by ^
node_t.type
== SUBCOMP
optional sub components delimited by &
The delimiters shown above are the standard delimiters. The HL7 file may define different delimiters which are read and accounted for by the parser.
Every node has potential children indicated by node_t.num_children (0 means none) and has a parent (special case is message_t which is always the root of the structure and is the partent of node_type_t SEGMENT).
This also means, that some node types (node_t.type) need to be treated specially.
To create a valid node_t you need to know it's data and length.
Example:
node_t takes owenship of the pointer pointing to data
. be aware of the fact that when you free a node then all children and it's data will get freed.
Example:
You should use the api to manage relationships between children and parents. this will make sure you don't get any dangling nodes in your structure.
Example:
message_t has a very similar structure (lacking data but having file metadata). when accessing the parent of a SEGMENT
, then the parent should be casted into message_t*
.
typedef struct raw_field_t raw_field_t |
structure to track delmiter fields in a fieldset
This is a private data structure for parse_segment()
. It keeps track of all delimiters in a segment while looping over it byte by byte. process_node()
the uses it to calculate bounds and structure of the message while extracting the data and structure.
This is a parser internal structure used to keep track of delimiters.
parse_segment()
enum node_type_t |
Node types.
hl7_addr_t* addr_from_node | ( | node_t * | node | ) |
generate an addr from any node in a message
traverse up until message is reached. The structure must have a message type as top parent or it will return NULL
.
while traversing up through parents, if type is MESSAGE
and parent is NULL
, we have successfully reached the top and can produce a result.
node | the starting point from where to traverse up |
message_t* create_message_t | ( | hl7_meta_t * | meta | ) |
initialize an empty messagte_t struct
This function wil lsetup your message_t. If the first param is NULL
then default values for message_t.meta and message_t.meta.bom are set according to init_hl7_meta_t().
meta | or NULL |
node_t* create_node_t | ( | node_type_t | type, |
unsigned char * | data, | ||
size_t | length, | ||
int | pos | ||
) |
create a new node
you must make sure to properly free the node
my_node->parent = another_node;
if you just plan to append as a child,
parent
is taken care of.type | the node type |
data | byte array of data |
length | length including \0 delimiting byte if there is any |
pos | position of element in line (segment). the position marks the beginnign delimiter |
raw_field_t* create_raw_field_t | ( | void | ) |
create raw fied structure
void disply_raw_node | ( | raw_field_t * | raw_e | ) |
dump raw_e structure to stdout
raw_e | the node's delimiter information |
void free_message_t | ( | message_t * | message | ) |
free message_t an all it's child objects
This method will free messgate_t.meta and messagte_t.segments (recoursively).
message | the message to free |
void free_node_t | ( | node_t * | node | ) |
cleanup all memory of a node
All children and children's children are freed.
node | the node and it's children to be cleaned |
append a segment to the message
This method will dynamically allocate more memory for message_t.segments and store the number of allocated items in messgae_t._num_children_allocated.
parent | typically the root node, a message_t object |
node | the segment to append to message |
append a child node
Memory is allocated if _num_children_allocated is too small to hold an additional pointer in the children array. You must make sure to properly allocate memory for the child itself, use create_node_t() for that.
parent | the parent node to be associated, may be null (this is reserved for the root node) |
node | the node to append |
node_t* node_in_segment | ( | node_t * | segment, |
hl7_addr_t * | addr | ||
) |
check if a node with given addres exists
segment | node tree to search (must be of type segment) |
addr | the address to look up |
int node_parent_child_pos | ( | node_t * | node | ) |
find the position in parent's children struct
This is useful to find next/previous siblings.
-1
if there is no parent and -2
if the node is not found in parents children const char* node_type_to_string | ( | node_type_t | type | ) |
string representation of node_type_t
type | type |
node_t* process_node | ( | raw_field_t * | raw_e, |
hl7_meta_t * | meta, | ||
int | start_pos | ||
) |
Sub component parser
This is the sub component prser that takes care of braking down all elements in a field delimited by ^~&
.
creates a node structure for components and subcomponents. don't forget to set the parent after creation. This is an internal function of the parsers and is not useful anywhere else.
raw_e | raw field elements |
meta | hl7 meta data, containing delimiter |
start_pos | the position of the first delimiter beginning data of thes field in the segment |