Kevin;

   You are working way too hard.  Do a buffered read and then use $ASCII to
take it apart one octet at a time.  The encoding is much easier than you
have made it.  Each octet will be something like this;

  N BUF,C,B1,B2,OBUF
  S OBUF=""
  R BUF#255
  F I=1:1:$L(BUF)  D
  .  S C=$ASCII(BUF,I)  ; Converts to the value of the character (0 to 255)
  .  S OBUF=OBUF_$$BYT2BIN(C)
  .QUIT

BYT2BIN(V) ; Take one BYTE and return HEX Values
  N HV,B1,B2
  S NV="0123456789ABCDEF"
  S B1=(V#16)+1  ; 0 to 15 becomes 1 to 16
  S B2=(V\16)+1
  QUIT $E(NV,B1)_$E(NV,B2)  ;  You figure out the byte order  1-2 or 2-1

The star reads are eating your lunch.  This will be much faster.


----- Original Message -----
From: "Kevin Toppenberg" <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Sunday, August 21, 2005 5:53 AM
Subject: [Hardhats-members] Re: Is $$GTF~%ZISH() binary friendly?


Here is the code.  I will also attach it incase wrapping ruins it here...


;"TMG BIN <-->GBL FUNCTION
;"Kevin Toppenberg MD
;"GNU General Public License (GPL) applies
;"8-20-2005

;"=======================================================================
;" API -- Public Functions.
;"=======================================================================
;"$$BIN2GBL^TMGBINF(path,filename,globalRef,incSubscr)
;"$$GBL2BIN^TMGBINF(globalRef,incSubscr,path,filename)

;"=======================================================================
;"PRIVATE API FUNCTIONS
;"=======================================================================



;"=======================================================================
BIN2GBL(path,filename,globalRef,incSubscr)
        ;"Purpose: To load a binary file from the host filesystem into
a WP field, storing
        ;"              the composit bytes as ascii hex codes.
        ;"Input: path --        full path, up to but not including the
filename (required)
        ;"         filename --  name of the file to open (required)
        ;"         globalRef-- Global reference to WRITE the host
binary file to, in fully resolved
        ;"                              (closed root) format.  This
function does not kill the global before
        ;"                              writing to it.  (required)
        ;"                           Note:
        ;"                           At least one subscript must be
numeric.  This will be the incrementing
        ;"                           subscript (i.e. the subscript
that $$BIN2WP^TMGBINWP will increment
        ;"                           to store each new global node).
This subscript need not be the final
        ;"                           subscript.  For example, to load
into a WORD PROCESSING field, the
        ;"                           incrementing node is the
second-to-last subscript; the final subscript
        ;"                           is always zero.
        ;"        incSubscr-- (required) Identifies the incrementing
subscript level.  For example, if you
        ;"                           pass ^TMP(115,1,1,0) as the
global_ref parameter and pass 3 as the
        ;"                           inc_subscr parameter, $$BIN2GBL
will increment the third subscript, such
        ;"                           as ^TMP(115,1,x), but will WRITE
notes at the full global reference, such
        ;"                           as ^TMP(115,1,x,0).
        ;"Result: 1=success, 0=failure
        ;"
        ;"Note: Each line of the global will contain up to 128 bytes
(256 characters)
        ;"               (2 ascii hex characters = 1 source byte)
        ;"Example:
        ;"
^TMP(115,1,1,0)="A12C4F12E2791D9723C3297D3C30B73C1532A1...(continues
to 256 characters)"
        ;"
^TMP(115,1,2,0)="91D9723C3297D314ADF31B85F41A12C4F12E27...(continues
to 256 characters)"
        ;"
^TMP(115,1,3,0)="3A12C4F12E271B85F4C2ED9723C3297D314ADF...(continues
to 256 characters)"
        ;"
^TMP(115,1,4,0)="85F73C1532AA12C4F12E2791D9723C3297D314...(continues
to 256 characters)"
        ;"  ^TMP(115,1,5,0)="61A85C30B73C1532AA12C4F12E2791D972"  <--
not padded with terminal zeros

        new result set result=0  ;"default to failure
        new handle set handle="TMGHANDLE"
        new abort set abort=0
        new byteIn
        new $ETRAP
        new oneLine set oneLine=""
        new curRef set curRef=globalRef

        set path=$$DEFDIR^%ZISH($get(path))
        do OPEN^%ZISH(handle,path,filename,"R")
        if POP goto B2GDone
        set $ETRAP="set abort=1,$ECODE="""" quit"
        use IO
        for  do  quit:($ZEOF)!(abort=1)!(byteIn=-1)
        . read *byteIn:2
        . if (byteIn=-1) quit
        . set oneLine=oneLine_$$HEXCHR(byteIn,2)
        . if $length(oneLine)>255 do
        . . set @curRef=oneLine
        . . set curRef=$$NEXTNODE(curRef,incSubscr)
        . . set oneLine=""
        if (oneLine'="")&(abort=0) do
        . set @curRef=oneLine
        . set oneLine=""

        if (abort'=1) set result=1 ;"SUCCESS
        do CLOSE^%ZISH(handle)


B2GDone
        quit result


NEXTNODE(curRef,incSubscr)
        ;"Purpose: to take a global reference, and increment the node
specified by incSubscr
        ;"Input:   curRef --    The reference to alter, e.g.
'^TMP(115,1,4,0)'
        ;"           incSubscr--The node to alter, e.g.
        ;"                              1-->^TMG(x,1,4,0)    x would
be incremented
        ;"                              2-->^TMG(115,x,4,0) x would be
incremented
        ;"                              3-->^TMG(115,1,x,0) x would be
incremented
        ;"                              4-->^TMG(115,1,4,x) x would be
incremented
        ;"Note: the node that incSubscr references should be numeric
(i.e. not a name)
        ;"      otherwise the alpha node will be treated as a 0
        ;"result: returns the new reference

        new i,result

        set result=$qsubscript(curRef,0)_"("
        for i=1:1:$qlength(curRef) do
        . new node
        . if i'=1 set result=result_","
        . set node=$qsubscript(curRef,i)
        . if i=incSubscr set node=node+1
        . if (node'=+node) set node=""""_node_""""
        . set result=result_node
        set result=result_")"

        quit result


HEXCHR(n,digits)
        ;"Purpose: convert n to hex characters
        ;"Input: n -- the number to convert
        ;"         digits: (optional) number of digits in output.
Leading 0's padded to
        ;"                      front of answer to set number of digits.
        ;"                      e.g. if answer is "A", then
        ;"                      2 -> mandates at least 2 digits ("0A")
        ;"                      3->3 digits ("00A")

        new lo
        new result set result=""
        new ch
        set digits=$get(digits,1)

        for  do  quit:(n=0)
        . set lo=n#16
        . if (lo<10) set ch=+lo
        . else  set ch=$char(55+lo)
        . set result=ch_result
        . set n=n\16

        for  quit:($length(result)>(digits-1))  do
        . set digits=digits-1
        . set result="0"_result

        quit result





-------------------------------------------------------
SF.Net email is Sponsored by the Better Software Conference & EXPO
September 19-22, 2005 * San Francisco, CA * Development Lifecycle Practices
Agile & Plan-Driven Development * Managing Projects & Teams * Testing & QA
Security * Process Improvement & Measurement * http://www.sqe.com/bsce5sf
_______________________________________________
Hardhats-members mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/hardhats-members

Reply via email to