/*  */

/* Example of using Utility Library functions 

    ALLOCATETAGITEMS()
    CLONETAGITEMS()
    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)

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

 clonetaglist = clonetagitems(taglist)
 call show2taglist("   Original",taglist,"   Clone",clonetaglist)

 call FREETAGITEMS(clonetaglist)
 call FREETAGITEMS(taglist)

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)  /* the way you are suppose to */
                                         /* traverse a taglist         */
            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
