## FileConfig_ssb
## Author: Tim Swenson
## Date Created: 19 Feb 1999
## Date Edited:  12 Mar 1999

## This program will take a FileConfig configuration file
## and create a Config Block for compiling into a SuperBasic
## program.  This program is based on BasConfig by Oliver Fink
## (and modified by Norman Dunbar and Dilwyn Jones).

## BasConfig is menu driven and requires the user to 
## manualy type in the Config Block information.
## This does not allow a user to edit an existing Config Block.
## The user must re-enter the same information over again.

## FileConfig allows the user to create a Config Block configuration
## file that defines the Config Block.  FileConfig reads that 
## file and creates a Config Block.  This allows a user to essentually
## "edit" an existing Config Block.  Even though FileConfig recreates
## the Config Block, to the user, he just edits a file and runs a 
## program.

#INCLUDE poke_ssb

infile$ = CMD$

## Check to make sure only one argument
IF ( " " INSTR infile$ ) THEN
   BEEP 1000,10
   ferr$ = "Too Many Arguments"
   ferr
END IF

## Check to make sure that input file exists
IF FTEST(infile$) THEN
   BEEP 1000,10
   ferr$ = infile$&" Not Found"
   ferr
END IF

bin_file$ = DATAD$&"cfg_bin"
## Check to make sure that CFG_BIN exists
IF FTEST(bin_file$) THEN
   BEEP 1000,10
   ferr$ = "CFG_BIN Not Found"
   ferr
END IF

## Allocate space and load binary file to append data
base = ALCHP(10240)

## Load Binary File
bin_len = FLEN(\bin_file$)
LBYTES bin_file$,base
adr_list = base + bin_len

OPEN #4,infile$

## Get Software Name
INPUT #4,sw_name$
sw_name$ = sw_name$( 4 TO )
pk_$ adr_list,sw_name$

## Get Software Version
INPUT #4,sw_ver$
sw_ver$ = sw_ver$ ( 4 TO )
pk_$ adr_list,sw_ver$
adr_list = even(adr_list)

## Get Number of Config Items
INPUT #4,items$
items = items$( 4 TO )
adr_items = adr_list + 12 * items + 4

FOR loop = 1 to items
   INPUT #4,type$
   type$ = type$( 4 TO )

   IF type$ = "string" THEN cfg_string
   IF type$ = "char" THEN cfg_char
   IF type$ = "code" THEN cfg_code
   IF type$ = "byte" THEN cfg_values 8
   IF type$ = "word" THEN cfg_values 10

NEXT loop

CLOSE #4

pk_w adr_list, -1

SBYTES_O out_cfg,base,adr_items-base



DEFine PROCedure cfg_string

   ## Define String in Config Block
   pk_b adr_list, 0
   ## Set Selection Key to Null in Config Block
   pk_b adr_list, 0

   ## Get Max Length of String
   INPUT #4,max$
   max = max$(4 TO )

   s_adr = adr_items

   ## Put Max Length into Config Block
   pk_w adr_items, max+1

   ## Get Default String
   INPUT #4,default$
   default$ = default$( 4 TO )
   
   ## Put Default String into Config Block
   pk_$ adr_items, default$

   ## Pad test of string space with spaces
   FOR i = max-LEN(default$) TO max+1
      pk_b adr_items,32
   NEXT i
   adr_items = even(adr_items)
   pk_wr adr_list, s_adr
   pk_w adr_list, 0
   pk_w adr_list, 0

   it_desc = even(adr_items)
   ## GET Description String
   INPUT #4,desc$
   desc$ = desc$(4 TO )
   ## Put Description String into Config Block
   pk_$ adr_items,desc$
   pk_wr adr_list, it_desc
   s_adr = adr_items

   ## Strip Spaces? (0 or 1)
   INPUT #4,strip$
   strp = strip$(4 TO 5)
   
   ## Put into Config Block   
   pk_w adr_items,strp
   pk_wr adr_list, s_adr

END DEFine


DEFine PROCedure cfg_code
   ## Define CODE
   pk_b adr_list,4
   ## Define Selection Key
   pk_b adr_list,0
   ## Get Default Code value
   INPUT #4,default$
   default = default$( 4 TO )

   c_adr = adr_items
   pk_b adr_items,default
   adr_items = even(adr_items)
   pk_wr adr_list,c_adr
   pk_w adr_list,0
   pk_w adr_list,0

   it_desc = even(adr_items)
   ## GET Description String
   INPUT #4,desc$
   desc$ = desc$(4 TO )
   ## Put Description String into Config Block
   pk_$ adr_items,desc$
   pk_wr adr_list, it_desc

   adr_items = even(adr_items)
   c_adr = adr_items

   ## get number of codes
   INPUT #4,num_sel$
   num_sel = num_sel$( 4 TO )

   FOR x = 1 to num_sel
      ## Get Code
      INPUT #4,ccode$
      ccode = ccode$( 4 TO )
      pk_b adr_items,ccode
      ## selection keystroke
      pk_b adr_items,0
      ## Get String for Code
      INPUT #4,code_string$
      code_string$ = code_string$( 4 TO )
      pk_$ adr_items,code_string$
      adr_items = even(adr_items)
   NEXT x

      pk_w adr_items,-1
      pk_wr adr_list,c_adr

END DEFine cfg_code


DEFine PROCedure cfg_char
   ## Define CHAR
   pk_b adr_list,2
   ## Selection Key
   pk_b adr_list,0  
   c_adr = adr_items

   ## Get Default Character   
   INPUT #4,default$
   default$ = default$(4)
   pk_b adr_items,CODE(default$)
   pk_wr adr_list,c_adr
   adr_items=even(adr_items)
   pk_w adr_list,0
   pk_w adr_list,0


   it_desc = even(adr_items)
   ## GET Description String
   INPUT #4,desc$
   desc$ = desc$(4 TO )
   ## Put Description String into Config Block
   pk_$ adr_items,desc$
   pk_wr adr_list, it_desc
   s_adr = adr_items

   ## Get Special Attributes
   INPUT #4,attr$
   attr$ = attr$(4 TO 9)
   byte = 0
   byte = 1 - attr$(1)
   byte = byte + (2 * attr$(2))
   byte = byte + (4 * attr$(3))
   byte = byte + (8 * attr$(4))
   byte = byte + (16 * attr$(5))
   byte = byte + (64 * attr$(6))
   c_adr = adr_items
   pk_w adr_items,byte
   pk_wr adr_list,c_adr
END DEFine


DEFine PROCedure cfg_values ( length )

   ## Define Type (byte,word,long)
   pk_b adr_list,length
   ## Define Selection Key
   pk_b adr_list,0

   ## Get Inital Value
   INPUT #4,initial$
   initial = initial$(4 TO )
   b_adr = adr_items
   value_poke length, adr_items, initial
   pk_wr adr_list,b_adr
   pk_w adr_list,0
   pk_w adr_list,0

   it_desc = even(adr_items)
   ## GET Description String
   INPUT #4,desc$
   desc$ = desc$(4 TO )
   ## Put Description String into Config Block
   pk_$ adr_items,desc$
   pk_wr adr_list, it_desc
   s_adr = adr_items

   ## Get Minimum Value
   INPUT #4,min$
   min = min$(4 TO )
   b_adr = adr_items
   value_poke length, adr_items, min
   pk_wr adr_list,b_adr

   ## Get Maximun Value
   INPUT #4,max$
   max = max$(4 TO )
   value_poke length, adr_items, max

END DEFine

DEFine PROCedure ferr
   OPEN #5,scr_20x60a100x150
   PAPER #5,0: INK #5,7: BORDER #5,4: CLS #5
   PRINT #5,ferr$
   PRINT #5,"Hit Any Key"
   temp$ = INKEY$(#5,-1)
   CLOSE #5
   CLOSE #4
   STOP
END DEFine ferr
  




## OK for Selection Key to be zero?
## May want to add some error correction.
