HL7 消息处理库:解析、拼接、修改 HL7 消息
#include 'hl7.h' #include <string.h>
/********************************************************************* ********************************************************************/
char * concat(char *string1, char *string2){ // 将两个字符串连接起来。
int length1, length2; /* 存储字符串的长度 */
/* 获取第二个字符串的长度 */ length2 = strlen(string2);
/* 如果第二个字符串为空,则返回第一个字符串 */ if (length2==0) return string1;
/* 获取第一个字符串的长度 */ length1 = strlen(string1);
/* 扩展字符串以容纳额外的字符 */ if (string1 = (char *) realloc(string1, length1 + length2 +1)){
/* 将第二个字符串复制到第一个字符串的末尾 */
strncpy(&string1[length1], string2, length2);
string1[length1 + length2] = '\000'; // 将新字符串的末尾设置为 '\0'
/* 返回新字符串 */
return string1;
} else { /* 没有足够的内存,退出程序 */ exit(1); }
}
/********************************************************************* 为字符串分配内存,将字符串复制到内存中,然后返回指向内存的指针。 ********************************************************************/
char * strset (char *string) // 将一个字符串复制到一个新的内存区域中 {
char *pointer_to_string;
if (pointer_to_string = (char ) malloc (strlen (string)+1)) // 计算字符串的长度,然后为字符串分配一个包含该长度加一的空间,最后将该字符串复制到新分配的内存空间中。 { strcpy (pointer_to_string, string); return (pointer_to_string); } else { / 退出程序 / exit (1); } } / end strset */
/********************************************************************* ********************************************************************/
void hl7count (struct hl7_part_t *message, char *seg_location) { // 统计 HL7 消息中特定部分的数量。
} /* end hl7count */
/********************************************************************* ********************************************************************/
void hl7rawcount (struct hl7_part_t *message, char *location) { // 统计 HL7 消息中特定部分的数量,不考虑层次结构。
} /* end hl7rawcount */
/********************************************************************* ********************************************************************/
void hl7delete (struct hl7_part_t *message, char *seg_location, char *string){ // 删除 HL7 消息中特定部分的特定字符串。
} /* end hl7delete */
/********************************************************************* ********************************************************************/
void hl7insert (struct hl7_part_t *message, char *seg_location, char *string){ // 在 HL7 消息中特定部分插入特定字符串
} /* end hl7insert */
/********************************************************************* ********************************************************************/
void hl7rawdelete (struct hl7_part_t *message, struct hl7_location_t *location){ // 删除 HL7 消息中特定位置的节点。
} /* end hl7rawdelete */
/********************************************************************* ********************************************************************/
void hl7rawinsert (struct hl7_part_t *message, struct hl7_location_t *location, char *string){ // 在 HL7 消息中特定位置插入特定字符串
} /* end hl7rawinsert */
/********************************************************************* ********************************************************************/
void hl7set (struct hl7_part_t *message, char *seg_location, char *string){ // 设置 HL7 消息中特定部分的值。
} /* end hl7set */
/********************************************************************* ********************************************************************/
void hl7get (struct hl7_part_t *message, char *seg_location){ // 获取 HL7 消息中特定部分的值。
} /* end hl7get */
/********************************************************************* ********************************************************************/
void hl7rawset (struct hl7_part_t *message, struct hl7_location_t *location, char *string){ // 设置 HL7 消息中特定位置的值
/* 这个有点难,需要遍历字符串到指定位置,创建缺少的节点,然后将最终部分设置为给定的字符串。 */
} /* end hl7rawset */
/********************************************************************* hl7rawget 函数遍历消息结构以达到指定位置,并返回最能代表消息结构的字符串。
hl7rawget 接收一个指向消息结构的指针和一个位置,返回一个最能代表该位置消息结构的字符串。
该函数通过遍历消息结构直到在路径中达到 NULL 或指定位置来工作。
如果到达 NULL,则返回空字符串。
否则,它到达指定位置并根据 join 函数的要求传递正确的参数以检索正确的字符串。 ********************************************************************/
char *hl7rawget (struct hl7_part_t *message, struct hl7_location_t *given_location){ // 获取 HL7 消息中特定位置的值
struct hl7_part_t *hl7_part; struct hl7_location_t *location; char msg_string; / 指向消息字符串 / char separators[8]; / 以正确顺序列出分隔符的列表 */ int i, depth = 0;
/* 初始化返回值字符串的指针 */ msg_string = strset("");
/* 遍历树 */ hl7_part = message->lower; for (location = given_location ; location->next ; location=location->next){
/* 继续计数 */
for(i=location->count; i ; i--) {
if(hl7_part->next){
hl7_part=hl7_part->next;
} else {
return msg_string;
}
}
/* 降低每个层级 */
if(hl7_part->lower){
depth++;
hl7_part=hl7_part->lower;
} else {
/* return msg_string; */ } }
/* 这需要是一个子例程,因为我们刚刚做了两次 / / 构建分隔符字符串 */ separators[0] = '\n'; separators[1] = '\n'; separators[2] = '\r'; separators[3] = message->lower->lower->lower->next->lower->lower->lower->data[0]; separators[4] = message->lower->lower->lower->next->next->lower->lower->lower->data[3]; separators[5] = message->lower->lower->lower->next->next->lower->lower->lower->data[0]; separators[6] = message->lower->lower->lower->next->next->lower->lower->lower->data[1]; separators[7] = '\000';
printf("深度是 >%d< ", depth); hl7printlocation(given_location);
/* 调用 join 函数 */ msg_string=join(hl7_part, msg_string, &separators[depth+1]);
/* 返回我们找到的字符串 */ return msg_string;
} /* end hl7rawget */
/********************************************************************* join 函数使用递归来根据一组分隔符拆分消息。
该函数接收一个指向需要拆分的 hl7 消息部分的指针,一个分隔符字符的字符串指针和一个状态,并返回一个指向包含 hl7_message 的字符串的指针。
一些特殊编码正在使用,以确保所有段都以字段分隔符结尾。 ********************************************************************/
char *join (struct hl7_part_t *hl7_part, char *msg_string, char *separators){ //join:将 HL7 消息中的各个部分连接起来。
char current_separator[2];
if (separators=='�00') exit(1);
current_separator[0] = separators[0]; current_separator[1] = '\000';
if (hl7_part->data){ msg_string = concat(msg_string, hl7_part->data); }
if (hl7_part->lower){ msg_string = join(hl7_part->lower, msg_string, &separators[1]); }
if (hl7_part->next){ msg_string = concat(msg_string, current_separator); msg_string = join(hl7_part->next, msg_string, separators); }
return msg_string;
} /* end join */
/********************************************************************* The funtion hl72str will create an hl7 message string from a message structure.
hl72str is passed a message structure and returns a pointer to a string.
hl72str is really a wrapper around the function join. This function does some prep work and calls join with the proper arguments, returning the string that is returned by join. ********************************************************************/
char *hl72str (struct hl7_part_t *message){ //hl72str:将 HL7 消息转换为字符串。
char separators[8]; /* list of separators in proper order */ char msg_string; / points to the message string */ int length;
msg_string=strset("");
/* build the seperators string */ separators[0] = '\n'; separators[1] = '\n'; separators[2] = '\r'; separators[3] = message->lower->lower->lower->next->lower->lower->lower->data[0]; separators[4] = message->lower->lower->lower->next->next->lower->lower->lower->data[3]; separators[5] = message->lower->lower->lower->next->next->lower->lower->lower->data[0]; separators[6] = message->lower->lower->lower->next->next->lower->lower->lower->data[1]; separators[7] = '\000';
/* turn the message into a string */ msg_string = join(message, msg_string, separators);
/* fix the field separator field */ length=strlen(msg_string); strncpy(&msg_string[4], &msg_string[6], length-5); if (!(msg_string=(char *) realloc(msg_string, length-2))){
/* not enough memory, die */
exit(1);
}
return msg_string;
} /* end hl72str */
/********************************************************************* The function split uses recursion in order to split a message based on a set of seperators.
The function is given a pointer to an hl7 message part that needs to be split and a pointer to a string of seperators and returns a pointer to an the lower part of the message. *********************************************************************/
原文地址: https://www.cveoy.top/t/topic/no9M 著作权归作者所有。请勿转载和采集!