'**** HEADER ************************************************************
'EXDFILE1.PF3 (release .02)
'Copyright (c) 1990-1991 Applied Resource Technologies, Inc.
'P.O. Box 64381, Dallas, Texas 75206 U.S.A. (214) 855-0449
'Description: Demonstrates getfields()
'**** FUNCTION DECLARATIONS *********************************************
'library
external getfields()
'core
'**** VARIABLE DECLARATIONS *********************************************
'library
external ptval ptary[1]
'core
'**** CODE **************************************************************

local orig_fname dest_fname orig[1] dest[1] dest_num orig_num temp fld i j
local xfer_num recs

' *****************[ complete this for similar data files ]****************

orig_fname = ?   ' original filename w/o extension
dest_fname = ?   ' identical or similar destination file w/o extension

' *************************************************************************

load "\smartii\poptools\lib\dfilelib.psl" in-memory

if getfields(dest_fname|".db",6) = 0
     dest_num = ptval                         ' # of fields in dest.db
     redimension dest[dest_num]               ' setup arrays to handle maximum
     redimension orig[dest_num]               '  number of fields
     for i = 1 to dest_num
         dest[i] = ptary[i]                   ' store the destination fields
     end for
else
     dest_num = -1
end if
if getfields(orig_fname|".db",6) = 0 and dest_num > 0
     xfer_num = 0                             ' counter for fields to transfer
     orig_num = ptval
     for i = 1 to dest_num                       ' for every match between
          for j = 1 to orig_num                  ' a destination and origin
               if dest[i] = ptary[j]             ' field name, store the
                    orig[xfer_num+1] = ptary[j]  ' field name and increment
                    xfer_num = xfer_num + 1      ' the counter
                    exit for
               end if
          end for
     end for
     file load standard-view orig_fname
     recs = records
     fopen "export.txt" as 1
     while record <= records              ' for each record
          for i = 1 to xfer_num           ' get data for all of the transfer
               fld = "["|orig[i]|"]"      ' fields (matches between orig/dest)
               temp = dbget(fld)          ' and write the data to its own line
               fwrite 1 from temp         ' in the export file
          end for
          data goto record next
     end while
     fclose 1
     file unload view orig_fname|".vws"
     file load standard-view dest_fname
     fopen "export.txt" as 1
     for i = 1 to recs                        ' reverse the export procedure
          data enter blank                    ' by entering a blank record for
          lock-record                         ' each original record, then
          for j = 1 to xfer_num               ' read the data from the export
               fld = "["|orig[j]|"]"          ' file each line/field at a time
               fread 1 into temp              ' and "stamp" the data to the
               dbput(fld,temp)                ' respective field
          end for
          write-record
     end for
     file unload view dest_fname|".vws"
     fclose 1
end if


