/*  */

/* Example of using Utility Library functions 

    ALLOCATETAGITEMS()
    FINDTAGITEM()
    GETTAGDATA()
    TAGINARRAY()
    FREETAGITEMS() 
    NEXTTAGITEM()

*/

 x = addlib("apig.library",0,-30,0)

 call set_apig_globals()

 taglist = ALLOCATETAGITEMS(20)    /* allocate twenty slots */

 call SETTAGSLOT(taglist,0,'80080001'x,'n',100)
 call SETTAGSLOT(taglist,1,'80080002'x,'n',200)
 call SETTAGSLOT(taglist,2,'80080003'x,'n',300)
 call SETTAGSLOT(taglist,3,'80080004'x,'n',400)
 call SETTAGSLOT(taglist,4,'80080005'x,'n',500)
 call SETTAGSLOT(taglist,5,'80080006'x,'n',600)
 call SETTAGSLOT(taglist,6,'80080007'x,'n',700)
 call SETTAGSLOT(taglist,7,'80080008'x,'n',800)
 call SETTAGSLOT(taglist,8,'80080009'x,'n',900)
 call SETTAGSLOT(taglist,9,'80080010'x,'n',1000)
 call SETTAGSLOT(taglist,10,TAG_DONE,'n',0)


 tagarray = ALLOCMEM(80,MEMF_CLEAR)   /* twenty array slots */

 call SETVALUE(tagarray,0,4,'p','80080101'x)  /* must specify values as */
                                              /* hex-strings (pointers) */
 call SETVALUE(tagarray,4,4,'p','80080102'x)
 call SETVALUE(tagarray,8,4,'p','80080103'x)
 call SETVALUE(tagarray,12,4,'p','80080004'x) /* the only one in taglist */
 call SETVALUE(tagarray,16,4,'p','80080105'x)
 call SETVALUE(tagarray,20,4,'p','80080106'x)
 call SETVALUE(tagarray,24,4,'p','80080107'x)
 call SETVALUE(tagarray,28,4,'p','80080108'x)
 call SETVALUE(tagarray,32,4,'p','80080109'x)
 call SETVALUE(tagarray,36,4,'p','80080110'x)
 call SETVALUE(tagarray,40,4,'p',TAG_DONE)

 call show2taglist("   Original",taglist,"",null())


 /* FINDTAGITEM */
 tagfound = FINDTAGITEM('80080004'x,taglist)
 if tagfound ~= null() then 
    do
        tagval  = GETVALUE(tagfound,0,4,'p')
        tagdata = GETVALUE(tagfound,4,4,'n')
        t1 = '(' || c2x(tagval) || ':' || tagdata || ')'
        say "Tag was found = " t1 
    end
  else
        say "Tag NOT found"

say ""
say ""
 /* GETTAGDATA */
 tagdata = GETTAGDATA('80080004'x,-1,taglist)
 if tagdata ~= -1 then 
        say "TagData was found = " tagdata
  else
        say "TagData NOT found"


 tagdata = GETTAGDATA('80080ff4'x,-5,taglist)
 if tagdata ~= -5 then 
        say "TagData was found = " tagdata
  else
        say "TagData NOT found = " tagdata


 tagdata = GETTAGDATA('80080ff4'x,15,taglist)
 if tagdata ~= 15 then 
        say "TagData was found = " tagdata
  else
        say "TagData NOT found = " tagdata


 tagdata = GETTAGDATA('80080006'x,-1,taglist)
 if tagdata ~= -1 then 
        say "TagData was found = " tagdata
  else
        say "TagData NOT found"

say ""
say ""
 /* TAGINARRAY */
  tag = TAGINARRAY('80080006'x,tagarray)
  if tag = 1 then
     say "TAG: '80080006'x in TagArray"
  else
     say "TAG: '80080006'x NOT in TagArray"

  tag = TAGINARRAY('80080004'x,tagarray)
  if tag = 1 then
     say "TAG: '80080004'x in TagArray"
  else
     say "TAG: '80080004'x NOT in TagArray"

  tag = TAGINARRAY('80080106'x,tagarray)
  if tag = 1 then
     say "TAG: '80080106'x in TagArray"
  else
     say "TAG: '80080106'x NOT in TagArray"


 call FREETAGITEMS(taglist)
 call FREEMEM(tagarray,80)

exit

/* -------------------------------------- */
show2taglist: procedure expose MEMF_CLEAR
/* -------------------------------------- */

parse arg L1heading,list1,L2heading,list2

   tstate1 = ALLOCMEM(4,MEMF_CLEAR)   /* needed, since NEXTTAGITEM wants   */
                                      /* it as doubly-indirected reference */

   tstate2 = ALLOCMEM(4,MEMF_CLEAR)  

   if list1 ~= null() then 
      call export(tstate1,list1,4)     /* copy list1 pointer into tstate1 */

   if list2 ~= null() then 
      call export(tstate2,list2,4)     /* copy list2 pointer into tstate2 */

   say copies(' ',8) L1heading copies(' ',25-length(L1heading)) L2heading

   do forever

      t1 = copies(' ',20)
      if tag1 ~= null() then 
         do
            tag1 = NEXTTAGITEM(tstate1)
            if tag1 ~= null() then
               do
                  tagval  = GETVALUE(tag1,0,4,'p')
                  tagdata = GETVALUE(tag1,4,4,'n')
                  t1 = '(' || c2x(tagval) || ':' || tagdata || ')'
               end
         end
      
      t2 = ''
      if tag2 ~= null() then 
         do
            tag2 = NEXTTAGITEM(tstate2)
            if tag2 ~= null() then
               do
                  tagval  = GETVALUE(tag2,0,4,'p')
                  tagdata = GETVALUE(tag2,4,4,'n')
                  t2 = copies(' ',25-length(t1))
                  t2 = t2 || '(' || c2x(tagval) || ':' || tagdata || ')'
               end
         end
      
      if (tag1 = null()) & (tag2 = null()) then leave

      say copies(' ',8) t1 t2
      
   end

   say ""
   call FREEMEM(tstate1,4)
   call FREEMEM(tstate2,4)

return 1
