'**** HEADER ************************************************************
'EXWRAP01.PF3 (release .02)
'Copyright (c) 1990-1991 Applied Resource Technologies, Inc.
'PO Box 64381, Dallas, Texas 75206 U.S.A. (214) 855-0449
'Description: Demonstrates wraptext()
'**** FUNCTION DECLARATIONS *********************************************
'library
external wraptext() replstr()
'core
'**** VARIABLE DECLARATIONS *********************************************
'library
public ptstr
'core
global   setup() display() nextpage() backpage() fmt_string() get_txt()
global   fname file_pos txt_string page[1] cur_page tot_len tot_pages
'fname       = the source text name
'file_pos    = the file position marker variable
'txt_string  = the string to be processed
'tot_len     = the full length of the file's data
'tot_pages   = how many pages can be expected
'cur_page    = the current page being processed
'**** CODE **************************************************************
MAIN
if setup() = 0
     get_txt()
     display()
end if
END MAIN

function display()
local x
nextpage()
while TRUE
     wraptext(3,3,19,78,fgstandard,bgstandard,txt_string,"l",1,0,0)
     screen print scrheight 1 fgpleasing bgpleasing \
          (format(upper(fname)|": section"&str(cur_page)&"of"&str(tot_pages)& \
          "    (F)orward   (B)ackward       Esc=exit","L"|str(scrwidth)))
     label getkey
     x = inchar
     if x = {f} or x = {F}
          nextpage()
     elseif x = {b} or x = {B}
          backpage()
     elseif x = {Esc}
          exit while
     else
          beep
          jump getkey
     end if
end while
unload "wraptext.psl"
unload "strlib.psl"
end function 'display()

function get_txt()
local i
redimension page[tot_pages]
page[1] = 0
for i = 2 to tot_pages
     page[i] = page[i-1]+1000
end for
end function 'get_txt()

function nextpage()
     if cur_page < tot_pages
          cur_page=cur_page+1
          fseek 1 page[cur_page]
          if cur_page = tot_pages
               fread 1 length tot_len-page[cur_page]-1 into txt_string
          else
               fread 1 length 1000 into txt_string
          end if
          fmt_string()
     else
          beep
     end if
end function 'nextpage()

function backpage()
     if cur_page > 1
          cur_page=cur_page-1
          fseek 1 page[cur_page]
          fread 1 length 1000 into txt_string
          fmt_string()
     else
          beep
     end if
end function 'backpage()

function fmt_string()
     if replstr(txt_string,chr(13)|chr(10)|chr(13)|chr(10)," >") = 0
          txt_string = ptstr
     end if
     if replstr(txt_string,chr(13)|chr(10)," ") = 0
          txt_string = ptstr
     end if
end function 'fmt_string()

function setup()
local err_code l
err_code = 0
error off
clearerror
fname = "bilrghts.txt"
load "\smartii\poptools\lib\wraptext.psl" in-memory
load "\smartii\poptools\lib\strlib.psl" in-memory
     if lerror = 0
          if file("\smartii\poptools\data\"|fname)=TRUE
               fopen "\smartii\poptools\data\"|fname as 1
               if cerror = 0
                    fread 1 into l                'do a test read
                    if cerror = 0
                         fseek 1 EOF
                         fposition 1 into tot_len
                         fseek 1 0
                         file_pos = 0
                         tot_pages = int(tot_len/1000)
                         cur_page = 0
                         if tot_pages < 1
                              tot_pages = 1
                         else
                              if mod(tot_len,tot_pages) > 0
                                   tot_pages = tot_pages + 1
                              end if
                         end if
                    else
                         err_code = -4            'empty source file
                    end if
               else
                    err_code = -3                 'could not open source file
               end if
          else
               err_code = -2                      'source file not found
          end if
     else
          err_code = -1                           ' wraptext() not found
     end if
if err_code < 0
     unload "wraptext.psl"
end if
return (err_code)
end function 'setup()


