HLMSG ;ALB/CJM-HL7 - APIs for files 772/773 ;02/04/2004
 ;;1.6;HEALTH LEVEL SEVEN;**118**;Oct 13, 1995
 ;
GETMSG(IEN,MSG) ;
 ;Description: given the message ien=MSGIEN (required), it returns the MSG array containing information about the message, defined below.
 ;Input:
 ;  IEN - the ien of the message in file 773
 ;Output:
 ;  Function returns 1 on success, 0 on failure
 ;  MSG (pass by reference, required) These are the subscripts returned:
 ;    "BATCH"  = 1 if this is a batch message, 0  if not
 ;       "CURRENT MESSAGE" - defined only for batch messages -  a counter used during building and parsing messages to indicate the current message.  It will be set to 0 initially.
 ;  "BODY" - ptr to file 772 which contains the body of the message.
 ;  "CURRENT LINE" -  a counter used during building and parsing of
 ;     messages to indicate the current line within the message. For
 ;     batch messages where each message within the batch is stored
 ;     separately, this field indicates the position within the current
 ;     individual message
 ;  "HDR" - the header segment, NOT parsed, as a sequence of lines HDR(i)
 ;  "IEN" - ien, file 773
 ;
 K MSG
 Q:'$G(IEN) 0
 N I
 S MSG("IEN")=IEN
 S MSG("BODY")=$P($G(^HLMA(IEN,0)),"^")
 Q:'MSG("BODY") 0
 ;
 S MSG("BATCH")=$S($P(^HL(772,MSG("BODY"),0),"^",14)="B":1,1:0)
 I MSG("BATCH") S MSG("BATCH","CURRENT MESSAGE")=0
 S MSG("CURRENT LINE")=0
 S I=0
 F  S I=$O(^HLMA(IEN,"MSH",I)) Q:'I  S MSG("HDR",I)=$G(^HLMA(IEN,"MSH",I,0))
 Q 1
 ;
NEXTSEG(MSG,SEG) ;
 ;Description:  Returns the next segment as a set of lines stored in SEG.
 ;Input:
 ;  MSG (pass by reference, required)
 ;Output:
 ;  Function returns 1 on success, 0 on failure (no more segments)
 ;  SEG (pass by reference, required)
 ;
 K SEG
 Q:MSG("CURRENT LINE")=-1 0
 I 'MSG("BATCH") D
 .N I,J
 .S J=1,I=MSG("CURRENT LINE")
 .F  S I=$O(^HL(772,MSG("BODY"),"IN",I)) Q:'I  Q:$G(^HL(772,MSG("BODY"),"IN",I,0))=""  S SEG(J)=^HL(772,MSG("BODY"),"IN",I,0),J=J+1
 .I 'I S MSG("CURRENT LINE")=-1
 .I I S MSG("CURRENT LINE")=I
 I MSG("BATCH") D
 .N I,J
 .S I=MSG("CURRENT LINE")
 .F  S I=$O(^HL(772,MSG("BODY"),"IN",I)) Q:'I  I $G(^HL(772,MSG("BODY"),"IN",I,0))'="" D  Q
 ..Q:$E($G(^HL(772,MSG("BODY"),"IN",I,0)),1,3)="MSH"
 ..I $E($G(^HL(772,MSG("BODY"),"IN",I,0)),1,3)="BTS" S MSG("CURRENT LINE")=-1,MSG("BATCH","CURRENT MESSAGE")=-1 Q
 ..S SEG(1)=$G(^HL(772,MSG("BODY"),"IN",I,0))
 ..S J=2
 ..F  S I=$O(^HL(772,MSG("BODY"),"IN",I)) Q:'I  Q:$G(^HL(772,MSG("BODY"),"IN",I,0))=""  S SEG(J)=^HL(772,MSG("BODY"),"IN",I,0),J=J+1
 .I 'I S MSG("CURRENT LINE")=-1
 .I I S MSG("CURRENT LINE")=I-1
 Q $S($D(SEG):1,1:0)
 ;
NEXTMSG(MSG,HDR) ;
 ;Advances to the next message in the batch
 ;Input:
 ;  MSG (pass by reference, required) - defined by $$GETMSG()
 ;Output:
 ;  Function returns 1 on success, 0 if no more messages
 ;  MSH - updated with current position in the message
 ;  HDR (pass by reference, required) returns the header as an array of 2lines HDR(1),HDR(2)
 ;
 K HDR
 N ARY,I,J
 S ARY="^HL(772,"_MSG("BODY")_",""IN"")"
 S I=MSG("CURRENT LINE")
 F  S I=$O(@ARY@(I)) Q:'I  D:$G(@ARY@(I,0))'=""  Q:$D(HDR)  Q:MSG("CURRENT LINE")=-1
 .I $E($G(@ARY@(I,0)),1,3)="BTS" S MSG("CURRENT LINE")=-1,MSG("BATCH","CURRENT MESSAGE")=-1 Q
 .I $E($G(@ARY@(I,0)),1,3)="MSH" D
 ..S J=1
 ..S HDR(J)=$G(@ARY@(I,0)),MSG("CURRENT LINE")=I,MSG("BATCH","CURRENT MESSAGE")=$G(MSG("BATCH","CURRENT MESSAGE"))+1
 ..F  S I=$O(@ARY@(I)) Q:'I  Q:$G(@ARY@(I,0))=""  S J=J+1,HDR(J)=$G(@ARY@(I,0))
 .E  D
 ..F  S I=$O(@ARY@(I)) Q:'I  Q:$G(@ARY@(I,0))=""
 Q $S($D(HDR):1,1:0)
 
--- Routine Detail   --- with STRUCTURED ROUTINE LISTING ---[H[J[2J[HHLMSG   3647     printed  Sep 23, 2025@19:34:27                                                                                                                                                                                                       Page 2
HLMSG     ;ALB/CJM-HL7 - APIs for files 772/773 ;02/04/2004
 +1       ;;1.6;HEALTH LEVEL SEVEN;**118**;Oct 13, 1995
 +2       ;
GETMSG(IEN,MSG) ;
 +1       ;Description: given the message ien=MSGIEN (required), it returns the MSG array containing information about the message, defined below.
 +2       ;Input:
 +3       ;  IEN - the ien of the message in file 773
 +4       ;Output:
 +5       ;  Function returns 1 on success, 0 on failure
 +6       ;  MSG (pass by reference, required) These are the subscripts returned:
 +7       ;    "BATCH"  = 1 if this is a batch message, 0  if not
 +8       ;       "CURRENT MESSAGE" - defined only for batch messages -  a counter used during building and parsing messages to indicate the current message.  It will be set to 0 initially.
 +9       ;  "BODY" - ptr to file 772 which contains the body of the message.
 +10      ;  "CURRENT LINE" -  a counter used during building and parsing of
 +11      ;     messages to indicate the current line within the message. For
 +12      ;     batch messages where each message within the batch is stored
 +13      ;     separately, this field indicates the position within the current
 +14      ;     individual message
 +15      ;  "HDR" - the header segment, NOT parsed, as a sequence of lines HDR(i)
 +16      ;  "IEN" - ien, file 773
 +17      ;
 +18       KILL MSG
 +19       if '$GET(IEN)
               QUIT 0
 +20       NEW I
 +21       SET MSG("IEN")=IEN
 +22       SET MSG("BODY")=$PIECE($GET(^HLMA(IEN,0)),"^")
 +23       if 'MSG("BODY")
               QUIT 0
 +24      ;
 +25       SET MSG("BATCH")=$SELECT($PIECE(^HL(772,MSG("BODY"),0),"^",14)="B":1,1:0)
 +26       IF MSG("BATCH")
               SET MSG("BATCH","CURRENT MESSAGE")=0
 +27       SET MSG("CURRENT LINE")=0
 +28       SET I=0
 +29       FOR 
               SET I=$ORDER(^HLMA(IEN,"MSH",I))
               if 'I
                   QUIT 
               SET MSG("HDR",I)=$GET(^HLMA(IEN,"MSH",I,0))
 +30       QUIT 1
 +31      ;
NEXTSEG(MSG,SEG) ;
 +1       ;Description:  Returns the next segment as a set of lines stored in SEG.
 +2       ;Input:
 +3       ;  MSG (pass by reference, required)
 +4       ;Output:
 +5       ;  Function returns 1 on success, 0 on failure (no more segments)
 +6       ;  SEG (pass by reference, required)
 +7       ;
 +8        KILL SEG
 +9        if MSG("CURRENT LINE")=-1
               QUIT 0
 +10       IF 'MSG("BATCH")
               Begin DoDot:1
 +11               NEW I,J
 +12               SET J=1
                   SET I=MSG("CURRENT LINE")
 +13               FOR 
                       SET I=$ORDER(^HL(772,MSG("BODY"),"IN",I))
                       if 'I
                           QUIT 
                       if $GET(^HL(772,MSG("BODY"),"IN",I,0))=""
                           QUIT 
                       SET SEG(J)=^HL(772,MSG("BODY"),"IN",I,0)
                       SET J=J+1
 +14               IF 'I
                       SET MSG("CURRENT LINE")=-1
 +15               IF I
                       SET MSG("CURRENT LINE")=I
               End DoDot:1
 +16       IF MSG("BATCH")
               Begin DoDot:1
 +17               NEW I,J
 +18               SET I=MSG("CURRENT LINE")
 +19               FOR 
                       SET I=$ORDER(^HL(772,MSG("BODY"),"IN",I))
                       if 'I
                           QUIT 
                       IF $GET(^HL(772,MSG("BODY"),"IN",I,0))'=""
                           Begin DoDot:2
 +20                           if $EXTRACT($GET(^HL(772,MSG("BODY"),"IN",I,0)),1,3)="MSH"
                                   QUIT 
 +21                           IF $EXTRACT($GET(^HL(772,MSG("BODY"),"IN",I,0)),1,3)="BTS"
                                   SET MSG("CURRENT LINE")=-1
                                   SET MSG("BATCH","CURRENT MESSAGE")=-1
                                   QUIT 
 +22                           SET SEG(1)=$GET(^HL(772,MSG("BODY"),"IN",I,0))
 +23                           SET J=2
 +24                           FOR 
                                   SET I=$ORDER(^HL(772,MSG("BODY"),"IN",I))
                                   if 'I
                                       QUIT 
                                   if $GET(^HL(772,MSG("BODY"),"IN",I,0))=""
                                       QUIT 
                                   SET SEG(J)=^HL(772,MSG("BODY"),"IN",I,0)
                                   SET J=J+1
                           End DoDot:2
                           QUIT 
 +25               IF 'I
                       SET MSG("CURRENT LINE")=-1
 +26               IF I
                       SET MSG("CURRENT LINE")=I-1
               End DoDot:1
 +27       QUIT $SELECT($DATA(SEG):1,1:0)
 +28      ;
NEXTMSG(MSG,HDR) ;
 +1       ;Advances to the next message in the batch
 +2       ;Input:
 +3       ;  MSG (pass by reference, required) - defined by $$GETMSG()
 +4       ;Output:
 +5       ;  Function returns 1 on success, 0 if no more messages
 +6       ;  MSH - updated with current position in the message
 +7       ;  HDR (pass by reference, required) returns the header as an array of 2lines HDR(1),HDR(2)
 +8       ;
 +9        KILL HDR
 +10       NEW ARY,I,J
 +11       SET ARY="^HL(772,"_MSG("BODY")_",""IN"")"
 +12       SET I=MSG("CURRENT LINE")
 +13       FOR 
               SET I=$ORDER(@ARY@(I))
               if 'I
                   QUIT 
               if $GET(@ARY@(I,0))'=""
                   Begin DoDot:1
 +14                   IF $EXTRACT($GET(@ARY@(I,0)),1,3)="BTS"
                           SET MSG("CURRENT LINE")=-1
                           SET MSG("BATCH","CURRENT MESSAGE")=-1
                           QUIT 
 +15                   IF $EXTRACT($GET(@ARY@(I,0)),1,3)="MSH"
                           Begin DoDot:2
 +16                           SET J=1
 +17                           SET HDR(J)=$GET(@ARY@(I,0))
                               SET MSG("CURRENT LINE")=I
                               SET MSG("BATCH","CURRENT MESSAGE")=$GET(MSG("BATCH","CURRENT MESSAGE"))+1
 +18                           FOR 
                                   SET I=$ORDER(@ARY@(I))
                                   if 'I
                                       QUIT 
                                   if $GET(@ARY@(I,0))=""
                                       QUIT 
                                   SET J=J+1
                                   SET HDR(J)=$GET(@ARY@(I,0))
                           End DoDot:2
 +19                  IF '$TEST
                           Begin DoDot:2
 +20                           FOR 
                                   SET I=$ORDER(@ARY@(I))
                                   if 'I
                                       QUIT 
                                   if $GET(@ARY@(I,0))=""
                                       QUIT 
                           End DoDot:2
                   End DoDot:1
               if $DATA(HDR)
                   QUIT 
               if MSG("CURRENT LINE")=-1
                   QUIT 
 +21       QUIT $SELECT($DATA(HDR):1,1:0)