| 1 | HLOPBLD ;ALB/CJM-HL7 - Building segments ;10/24/2006 | 
|---|
| 2 | ;;1.6;HEALTH LEVEL SEVEN;**126,132**;Oct 13, 1995;Build 6 | 
|---|
| 3 | ;Per VHA Directive 2004-038, this routine should not be modified. | 
|---|
| 4 | ; | 
|---|
| 5 | ; | 
|---|
| 6 | BUILDSEG(HLMSTATE,SEG,TOARY,ERROR) ;Builds the segment from the individual values | 
|---|
| 7 | ;Input: | 
|---|
| 8 | ;  HLMSTATE() - (pass by reference, required) Used to track the progress of the message. Uses these subscripts: | 
|---|
| 9 | ;    ("HDR","FIELD SEPARATOR") | 
|---|
| 10 | ;    ("HDR","ENCODING CHARACTERS") | 
|---|
| 11 | ;  SEG() - (pass by reference, required) Contains the data.  It must be built by calls to SET^HLOAPI prior to calling $$BUILDSEG. | 
|---|
| 12 | ; | 
|---|
| 13 | ;Note#1:  The '0' field must be a 3 character segment type | 
|---|
| 14 | ;Note#2: ***SEG is killed upon successfully adding the segment*** | 
|---|
| 15 | ; | 
|---|
| 16 | ;Output: | 
|---|
| 17 | ;  Function - returns 1 on success, 0 on failure | 
|---|
| 18 | ;  TOARY (pass by reference) This will return the segment in an array format TOARY(1),TOARY(2),... For segments that are shorter than the MUMPS maximum string length, there will be only TOARY(1) | 
|---|
| 19 | ;  ERROR (optional, pass by reference) - returns an error message on failure | 
|---|
| 20 | ; | 
|---|
| 21 | ; | 
|---|
| 22 | K ERROR,TOARY | 
|---|
| 23 | N TEMP1,TEMP2,LINE,LAST,SEQ,MAX,COUNT,SEGTYPE | 
|---|
| 24 | S COUNT=0 | 
|---|
| 25 | S MAX=HLMSTATE("SYSTEM","MAXSTRING")-1 ;save some room for the <CR> | 
|---|
| 26 | S SEGTYPE=$G(SEG(0,1,1,1)) | 
|---|
| 27 | S LAST=0,(TEMP1,TEMP2)="",LINE=SEGTYPE_HLMSTATE("HDR","FIELD SEPARATOR") | 
|---|
| 28 | F  S SEQ=$O(SEG(LAST)) Q:'SEQ  D | 
|---|
| 29 | .S TEMP2="",$P(TEMP2,HLMSTATE("HDR","FIELD SEPARATOR"),$S(LAST=0:SEQ,1:SEQ-LAST+1))="" | 
|---|
| 30 | .S TEMP1=TEMP2 | 
|---|
| 31 | .S LAST=SEQ | 
|---|
| 32 | .N REP,LAST | 
|---|
| 33 | .S LAST=0 | 
|---|
| 34 | .F  S REP=$O(SEG(SEQ,LAST)) Q:'REP  D | 
|---|
| 35 | ..S TEMP2="",$P(TEMP2,$E(HLMSTATE("HDR","ENCODING CHARACTERS"),2),$S(LAST=0:REP,1:REP-LAST+1))="" | 
|---|
| 36 | ..S TEMP1=TEMP1_TEMP2 | 
|---|
| 37 | ..S LAST=REP | 
|---|
| 38 | ..; | 
|---|
| 39 | ..N COMP,LAST | 
|---|
| 40 | ..S LAST=0 | 
|---|
| 41 | ..F  S COMP=$O(SEG(SEQ,REP,LAST)) Q:'COMP  D | 
|---|
| 42 | ...S TEMP2="",$P(TEMP2,$E(HLMSTATE("HDR","ENCODING CHARACTERS"),1),$S(LAST=0:COMP,1:COMP-LAST+1))="" | 
|---|
| 43 | ...S TEMP1=TEMP1_TEMP2 | 
|---|
| 44 | ...S LAST=COMP | 
|---|
| 45 | ...; | 
|---|
| 46 | ...N SUBCOMP,LAST | 
|---|
| 47 | ...S LAST=0 | 
|---|
| 48 | ...F  S SUBCOMP=$O(SEG(SEQ,REP,COMP,LAST)) Q:'SUBCOMP  D | 
|---|
| 49 | ....N VALUE | 
|---|
| 50 | ....S TEMP2="",$P(TEMP2,$E(HLMSTATE("HDR","ENCODING CHARACTERS"),4),$S(LAST=0:SUBCOMP,1:SUBCOMP-LAST+1))="" | 
|---|
| 51 | ....S VALUE=$G(SEG(SEQ,REP,COMP,SUBCOMP)) | 
|---|
| 52 | ....K SEG(SEQ,REP,COMP,SUBCOMP) | 
|---|
| 53 | ....S:((SEGTYPE'="MSH")&(SEGTYPE'="BHS"))!(SEQ'=2) VALUE=$$ESCAPE(.HLMSTATE,VALUE) | 
|---|
| 54 | ....S TEMP2=TEMP2_VALUE | 
|---|
| 55 | ....S TEMP1=TEMP1_TEMP2 | 
|---|
| 56 | ....I $L(LINE)+$L(TEMP1)<MAX D | 
|---|
| 57 | .....S LINE=LINE_TEMP1 | 
|---|
| 58 | ....E  D | 
|---|
| 59 | .....D ADDLINE(.TOARY,LINE_$E(TEMP1,1,MAX-$L(LINE)),.COUNT) | 
|---|
| 60 | .....S LINE=$E(TEMP1,MAX-$L(LINE)+1,MAX+100) | 
|---|
| 61 | ....S TEMP1="" | 
|---|
| 62 | ....S LAST=SUBCOMP | 
|---|
| 63 | I $L(LINE) D ADDLINE(.TOARY,LINE,.COUNT) | 
|---|
| 64 | K SEG | 
|---|
| 65 | Q 1 | 
|---|
| 66 | ; | 
|---|
| 67 | ADDLINE(TOARY,LINE,COUNT) ; | 
|---|
| 68 | S COUNT=COUNT+1 | 
|---|
| 69 | S TOARY(COUNT)=LINE | 
|---|
| 70 | Q | 
|---|
| 71 | ; | 
|---|
| 72 | ESCAPE(HLMSTATE,VALUE) ; | 
|---|
| 73 | ;Replaces the HL7 encoding characters with the corresponding escape sequences and returns the result as the function value | 
|---|
| 74 | ; | 
|---|
| 75 | N ESC,CHARS,I,NEWVALUE,LEN,CUR | 
|---|
| 76 | S CHARS=HLMSTATE("HDR","ENCODING CHARACTERS") | 
|---|
| 77 | S ESC=$E(CHARS,3) | 
|---|
| 78 | S NEWVALUE="",LEN=$L(VALUE) | 
|---|
| 79 | F I=1:1:LEN D | 
|---|
| 80 | .S CUR=$E(VALUE,I) | 
|---|
| 81 | .S NEWVALUE=NEWVALUE_$S(CUR=HLMSTATE("HDR","FIELD SEPARATOR"):ESC_"F"_ESC,CUR=ESC:ESC_"E"_ESC,CUR=$E(CHARS,1):ESC_"S"_ESC,CUR=$E(CHARS,4):ESC_"T"_ESC,CUR=$E(CHARS,2):ESC_"R"_ESC,1:CUR) | 
|---|
| 82 | Q NEWVALUE | 
|---|
| 83 | ; | 
|---|
| 84 | REPLACE(VALUE,CHAR,STRING) ; | 
|---|
| 85 | ;Takes the input string=VALUE and replaces each instance of the character | 
|---|
| 86 | ;=CHAR with the string=STRING and returns the resultant string | 
|---|
| 87 | ;as the function value | 
|---|
| 88 | ; | 
|---|
| 89 | N I,NEWVALUE,CURCHAR | 
|---|
| 90 | S NEWVALUE="" | 
|---|
| 91 | F I=1:1:$L(VALUE) D | 
|---|
| 92 | .S CURCHAR=$E(VALUE,I) | 
|---|
| 93 | .S NEWVALUE=NEWVALUE_$S(CURCHAR=CHAR:STRING,1:CURCHAR) | 
|---|
| 94 | Q NEWVALUE | 
|---|