G4C  - Routines.g - general purpose routines

; This file contains routines, to be used by other programs.
; You can call them with gosub :
; GoSub Routines.g RoutineName
; Note that the routines require that you set some variables before
; calling them.

; Note also, that once you are familiar with this file, you can delete
; most of the comments in it, so that it can be loaded faster.


; ***************************** ViewFile *********************************
; This is the routine that views the file according to it's type.
; It used to be in the read.gc file.

; You will need to change the default viewers, to the ones you like to
; use, and the text reader, which in this case is the Lister.g demo
; reader, to your prefered one.

; The following variable has to be set beforehand
; rtn_var      <- Must contain the file to be viewed
;                 You MUST set this before calling the routine.
;                 Do NOT use it afterwards as it will be deleted.

; example :
; ReqFile -1 -1 300 -40 "Pick a file:" LOAD rtn_var DF0:MyDir
; GoSub  Routines.g ViewFile        ; call the routine
; **************************************************************************

xRoutine ViewFile
if $rtn_var < "  "			; no variable..
   return
endif
extract rtn_var EXT rtn_info
if $rtn_info == .info			; icons - use an other gui
   copy $rtn_var ram:t			; which will show an icon named
   ifexists file ram:t/GCXX.info	; named GCXX.info in ram:t
       delete ram:t/GCXX.info
   endif
   extract rtn_var FILE rtn_info2
   joinfile ram:t $rtn_info2 rtn_info
   rename $rtn_info ram:t/GCXX.info
   guiload guis:G4C/Rtn/iconview.g
   guiopen iconview.g
   guiscreen iconview.g front
   return
endif
docase $rtn_var
case   H= "FORM????ILBM"		; ILBM
case   H= "FORM????ANIM"		; Animation
case   H= "GIF"				; GIF
case   H= "??????JFIF"			; JPEG
       RUN 'c:ppshow $rtn_var'
       return
       break
case   H= "FORM????8SVX"		; Sound sample
       RUN 'c:playsound $rtn_var'
       return
       break
case   H= "PP"				; PowerPacked file
       RUN 'c:ppmore $rtn_var'
       return
       break
case   H= G4C				; Yes.. you guessed it.
       GuiLoad $rtn_var
       return
       break
case   H= ??-lh				; LhA
       cli 'c:Lha x $rtn_var ram:'
       return
       break
case   H= "ZOO ?.??"			; zoo
       cli 'c:Zoo <* x// $rtn_var'
       return
       break
case   H= ^Z				; pkax
       cli 'c:pkax -x $rtn_var'
       return
       break
case   H= PK				; unzip
       cli 'c:unzip $rtn_var -d ram:'
       return
       break
case   H= "@" 				; Amigaguide (probably)
       RUN 'c:amigaguide $rtn_var'
       return
       break
case   H= "%%%"				; A program ? - Use cli.gc to run it.
       setvar gcMain "$rtn_var "
       GuiLoad guis:demos/cli.gc
       GuiOpen cli.gc
       GuiScreen cli.gc front
       update  cli.gc 10 1		; set it to CLI
       setgad cli.gc 1 ON
       return
       break
endcase
; If we got this far, load & show text file
setvar     dglvfile $rtn_var
GuiLoad    guis:demos/lister.g		; no problem if already loaded
guiopen    lister.g
GuiWindow  lister.g FRONT		; bring window to front
GuiScreen  lister.g FRONT		; bring screen to front


; ***************************** VarToList *********************************
; These are two routines which do the following :

; VarToList : Takes a variable which contains a series of file names,
;             chosen through a MULTI type ASL file requester, and 
;             makes it into a list, saving it to a file which can then 
;             be used in a xListView gadget.
; VarAddList: Same as VarToList, but will append the file names to the
;             existing file.

; The both use the following 3 variables :
; rtn_var      <- Must contain the files to be listed.
;                 You MUST set this before calling the routines.
;                 Do NOT use it afterwards as it will be deleted.
; .rtn_file    <- Will be the name of resulting file (an ENV: variable)
; rtn_separator<- (Optional) The routines will normally append an
;		  ENTER "\n" character after every word. If you
;		  want something else appended, then set this
; 		  variable. Otherwise, leave it alone.
; example :
; ReqFile -1 -1 300 -40 "Pick some files:" MULTI rtn_var DF0:MyDir
; GoSub  Routines.g VarToList       ; call the routine (same for VarAddList)
; LVChange MyGui 1 ENV:.rtn_file    ; load resulting file into your listview
; **************************************************************************

xROUTINE VarToList
gosub  Routines.g VarList_rtn		; gosub routine below
setvar .rtn_file $rtn_file
delvar rtn_file

xROUTINE VarAddList
gosub  Routines.g VarList_rtn
appvar .rtn_file $rtn_file		; use appvar, instead of setvar
delvar rtn_file

; This routine does the actual work, and is used by both the above.
; At the end rtn_file contains the files, properly formated.

xROUTINE VarList_rtn
setvar rtn_file ""                     ; use a normal variable now, for speed.
cutvar rtn_var CUT WORD 1 rtn_word      ; get 1st word
while $rtn_word > " "
   ifexists variable rtn_separator	     ; append something other than \n
	appvar rtn_file $rtn_word
	appvar rtn_file $rtn_separator
	delvar rtn_separator
   else
	appvar rtn_file $rtn_word\n          ; append word + \n
   endif
   cutvar rtn_var  CUT WORD 1 rtn_word  ; get next word
   cutvar rtn_word CUT CHAR 1 ""        ; delete leading blank
endwhile
delvar rtn_var
delvar rtn_word


