str2hl7 函数:将字符串转换为 HL7 消息
struct hl7_part_t *str2hl7 (char *message_string) //str2hl7:将字符串转换为HL7消息。
{
struct hl7_part_t *message; /* 正在处理的消息结构 */
char delimiters[6], /* 消息中的分隔符列表 */
separators[7], /* 正确顺序的分隔符列表 */
temp[6]; /* 用于在最终消息结构中设置正确的分隔符 */
/* 可能应该检查消息是否以MSH开头,并且长度是否大于最小长度... */
if (!(message = (struct hl7_part_t *) malloc (sizeof (struct hl7_part_t)))){ // 如果没有足够的内存,就退出。
/* 这有些突兀,需要更优雅地处理。 */
exit (1);
}
/* 在清洁的新结构中放入一些合理的值 */
message->lower=(struct hl7_part_t *)0;
message->next=(struct hl7_part_t *)0;
/* 将hl7_string的副本放入消息结构的正确位置 */
message->data = strset (message_string);
/* 将hl7分隔符复制到字符串中以保留它们以供处理 */
strncpy (delimiters, &message->data[3], 5);
/* 使用终止符关闭字符串的末尾,以防止出现核心转储 */
delimiters[5] = '\000';
/* 将分隔符重新排序为分离的正确顺序 */
separators[0] = '\n';
separators[1] = '\r';
separators[2] = delimiters[0];
separators[3] = delimiters[1];
separators[4] = delimiters[2];
separators[5] = delimiters[4];
separators[6] = '\000';
/* 从消息中删除分隔符,以免混淆 */
message->data[4] = 'X';
message->data[5] = '|';
message->data[6] = 'Z';
message->data[7] = 'K';
/* 递归调用split,在每个分隔符上拆分消息 */
message->lower = split (message, separators);
/* 将分隔符放入消息结构的正确位置 */
/* 尽快用hl7set替换它。 */
free (message->lower->lower->lower->next->lower->lower->lower->data);
strncpy (temp, &delimiters[0], 1);
temp[1] = '\000';
message->lower->lower->lower->next->lower->lower->lower->data = strset (temp);
free (message->lower->lower->lower->next->next->lower->lower->lower->data);
strncpy (temp, &delimiters[1], 4);
temp[4] = '\000';
message->lower->lower->lower->next->next->lower->lower->lower->data = strset (temp);
/* 完成后返回结果 */
return message;
} /* end str2hl7 */
/*********************************************************************
*********************************************************************/
void hl7printlocation(struct hl7_location_t *location){ //hl7printlocation:打印HL7消息中特定位置的信息。
for(; location->next; location=location->next) {
printf("%d", location->count);
if (location->next->next){
printf(".");
}
}
} /* end hl7printlocation */
/*********************************************************************
*********************************************************************/
void hl7rawprint(struct hl7_part_t *hl7_part, struct hl7_location_t *location, //hl7rawprint:打印HL7消息的层次结构。
struct hl7_location_t * current, int state)
{
/* 递归遍历树,跟踪您的位置并打印数据 */
if (!(current->next)){
if (!(current->next = (struct hl7_location_t *) malloc(sizeof(struct hl7_location_t)))){
exit (1);
}
current->next->next = (struct hl7_location_t *) 0;
current->next->count = 0;
}
if ((hl7_part->data) &&(strlen(hl7_part->data))){
hl7printlocation(location);
printf(" >%s<
", hl7_part->data);
}
if (hl7_part->lower){
hl7rawprint(hl7_part->lower, location, current->next, LOWER);
}
if (hl7_part->next){
current->count++;
hl7rawprint(hl7_part->next, location, current, NEXT);
}
if (state==LOWER)
current->count=0;
return;
} /* end hl7rawprint */
/*********************************************************************
*********************************************************************/
void hl7print (struct hl7_part_t *message) //hl7print:打印HL7消息的内容
{
struct hl7_location_t *location;
/* 设置一些数据结构并调用真正的函数 */
if (!(location = (struct hl7_location_t *) malloc(sizeof(struct hl7_location_t)))){
exit (1);
}
location->count = 0;
location->next = (struct hl7_location_t *) 0;
hl7rawprint(message->lower, location, location, BEGIN);
hl7freelocation(location);
/* 需要释放为位置分配的内存! */
} /* end hl7print */
原文地址: https://www.cveoy.top/t/topic/npbD 著作权归作者所有。请勿转载和采集!