'**** HEADER ************************************************************
'EXDOS06.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 setattrib() and getattrib()
'**** FUNCTION DECLARATIONS *********************************************
'library
external dosdrive() getattrib() setattrib()
external progress() progtag() spinmark()
external popuplist() vkeybox()
'core
global setup() play_attr() mainexit() std_err()
global do_add() do_sub() do_spin()
'**** VARIABLE DECLARATIONS *********************************************
'library
public ptstr psa scr dsa scc
'core
global fname attr_str
'**** CODE **************************************************************

MAIN

if setup() < 0
     mainexit(-1)
     exit
end if
play_attr()
mainexit(0)

END MAIN


function play_attr()
local x vkey
while TRUE

     x = getattrib(fname)

     attr_str = ptstr    ' always get the function result immediately

     if x < 0
          std_err("ERROR! Can't get attributes")
           exit while
     end if

     attr_str = case attr_str (NULL,"None") else attr_str
     progress(fgstandard,bgstandard,"FILE:"&fname,1)
     progtag(fgstandard,bgstandard,"Attributes:"&attr_str)
     if vkeybox(12,scc-5,"1Add 1Subtract","Esc=exit") < 0
          exit while
     end if
     screen shortrestore dsa
     vkey = ptstr   ' always get the function result immediately
     if vkey == "a"
          if len(attr_str) = 5
               std_err("Cannot add! File has all attributes")
          else
               do_add()
          end if
     elseif vkey == "s"
          if attr_str == "None"
               std_err("Cannot subtract! File has no attributes")
          else
               do_sub()
          end if
     else
          exit while
     end if
screen shortrestore psa
end while
end function 'play_attr()


function do_add()
local pkey add_attr popstr i c x
add_attr=NULL
if attr_str !! "r"
     add_attr=add_attr&"Read-only"
end if
if attr_str !! "h"
     add_attr=add_attr&"Hidden"
end if
if attr_str !! "s"
     add_attr=add_attr&"System"
end if
if attr_str !! "a"
     add_attr=add_attr&"Archive"
end if
if attr_str !! "m"
     add_attr=add_attr&"Shareable"
end if
add_attr = trim(add_attr)
x = popuplist(12,scc-25,18,add_attr,"Add ?",0,1)
if x = 0
     popstr = ptstr
     i = 1
     while TRUE
          c = case group(popstr,i)  ("Read-only","r") \
                                    ("Hidden","h") \
                                    ("System","s") \
                                    ("Archive","a") \
                                    ("Shareable","m") \
                                    else NULL
          if c = NULL
               exit while
          else
               attr_str = attr_str | c
          end if
     i=i+1
     end while
     setattrib(fname,attr_str)
end if
end function 'do_add()


function do_sub()
local pkey sub_attr popstr i c x j
sub_attr=NULL
if attr_str ! "r"
     sub_attr=sub_attr&"Read-only"
end if
if attr_str ! "h"
     sub_attr=sub_attr&"Hidden"
end if
if attr_str ! "s"
     sub_attr=sub_attr&"System"
end if
if attr_str ! "a"
     sub_attr=sub_attr&"Archive"
end if
if attr_str ! "m"
     sub_attr=sub_attr&"Shareable"
end if
sub_attr = trim(sub_attr)
x = popuplist(12,scc+15,18,sub_attr,"Subtract ?",0,1)
if x = 0
     popstr = ptstr
     i = 1
     sub_attr = NULL
     while TRUE
          c = case group(popstr,i)  ("Read-only","r") \
                                    ("Hidden","h") \
                                    ("System","s") \
                                    ("Archive","a") \
                                    ("Shareable","m") \
                                    else NULL
          if c = NULL
               exit while
          end if
          sub_attr = sub_attr|c
          i=i+1
     end while
     popstr = NULL
     if exact(sub_attr,NULL)=FALSE
          for j = 1 to len(attr_str)
               c = mid(attr_str,j,1)
               if sub_attr ! c
               else
                    popstr = popstr|c
               end if
          end for
          setattrib(fname,popstr)
     end if
end if
end function 'do_sub()


function setup()
screen clear 0 0
error off
clearerror
load "\smartii\poptools\lib\displib.psl" in-memory
progress(15,0,"Example: EXDOS06 setting up... standby",1)
do_spin()
load "\smartii\poptools\lib\doslib.psl" from-file
do_spin()
load "\smartii\poptools\lib\uintlib.psl" in-memory
do_spin()
fname = "\smartii\poptools\data\example.txt"
scr = 6
error on
screen shortrestore psa
return (case lerror (0,0) else -1)
end function 'setup()


function mainexit(m)
scr = int(scrheight/2)
screen clear 0 0
if m = 0
     progress(15,0,"Exiting Example: EXDOS06",1)
else
     progress(fgerror,bgerror,"Error setting up EXDOS06, aborting!",1)
end if
do_spin()
unload "uintlib.psl"
do_spin()
unload "doslib.psl"
screen print scrheight-4 1 15 0 "Press any key to exit"
while TRUE
     do_spin()
     if nextkey <> 0
          exit while
     end if
end while
inchar
unload "displib.psl"
end function 'mainexit()

function std_err(msg)
local kmsg col_kmsg
kmsg = "Press any key"
col_kmsg=scc-int(len(kmsg)/2)
screen shortrestore psa
beep
progtag(fgerror,bgerror,msg)
screen print scr+2 col_kmsg fginvpleasing bginvpleasing kmsg
inchar
end function 'std_err()

function do_spin()
local i
for i = 1 to 8
     spinmark(scr+1,scc,15,0)
     milli-wait 10
end for
end function 'do_spin()
