#MKfiles 2

;--------------------
; Register definitons
;--------------------

#reg a flag		; a floag showing if a decimal file exists
			; if 0, then file "REGISTER" doesn't exist

;--------------------
; Main Menu description
;--------------------

#MENU
	#CSTRING 1 "Decimal Zone"		; title
	#HLINE
	#ACTION "Save registers..." savereg	; will run savereg
	#?ACTION "...Nothing to load..." loadreg flag "Load registers..."
						; conditional line according to flag
	#HLINE
	#ACTION "Exit !" Progend		; exit program
#ENDMENU

;--------------------
; My initializations
;--------------------

1 SET \DiskEnable			; enable disk operations
DO
	#NUM mydir #FINDFILEID		; does my directory "BACKUP" exist?
WHILE(x<0)
	#NUM mydir #COPYFILENAME	; if no, copy file name to UBB
	2 #CREATEFILE			; and create it
LOOP
#LOADFILE				; when "BACKUP" exists, open dir!


GOSUB DecimalExist			; set the flag according to the presence of "REGISTERS" file

;--------------------
; The main loop here
;--------------------

#MENUMAINLOOP 7

;--------------------
; My subprograms
;--------------------

savereg:
	Gosub Display				; display "Working..."
	#NUM mydecimal #FINDFILEID		; does the file "REGISTERS" exist?
	IF(X<0)THEN
		#NUM mydecimal #COPYFILENAME	; if not, then copy name to UBB
		4 #CREATEFILE			; and create it (registers are also saved!)
		1 STO Flag			; flag updated
	ELSE
		#OVERWRITEFILE			; if file exist, just overwrite it
	ENDIF
	RTN

loadreg:
	GOSUB DecimalExist			; does the file "REGISTERS" exist?
	IF(X<>0)THEN
		Gosub Display			; if YES, display "Working..."
		RCL flag 1 -			; flag-1 is the line in the directory
		#LOADFILE			; load the file!
		1 STO Flag			; restore flag as it has been overwritten
	ENDIF					; if file doesn't exist, nothing to do
	RTN

Progend:
	0 SET \DiskEnable			; disable disk operations
	STOP					; program terminated

DecimalExist:
	#NUM mydecimal #FINDFILEID		; find my file "REGISTER" in current directory
	1 + STO flag				; if non existent, returns -1, so +1= 0
	RTN

Display:
	2 EXEC \Cls				; clear screen
	#NUM work EXEC \PrintStringP		; display text
	GRPH					; update screen
	RTN

mydir:		#FILENAMEID "BACKUP" 2		; directory name with ID=2 for dir
mydecimal:	#FILENAMEID "REGISTERS" 4	; file name with ID=4 for decimal
work:		#TEXT0 "Working..."

#ADDFINDFILE


