! ISMBG2.BAS --- demonstrate bizarre bug in ISAM function 1
! This problem occurs on AMOS 2.2C(452)-9 using ISAM.LIT
! This program compiles with /M.  Use in conjunction with ISMBG2.CMD
! Bob Fowler, Applied Systems Research, 1994-Oct-19

! THE PROBLEM:
! AlphaBASIC ISAM calls using X format variables generally work ok.
! The ISAM routines in SYSSTD,ISMSYM,ISMSBR,ERRSBR operate this way.
! If a string variable is passed, however, the calls will often fail.
! In particular, "ISAM #chan,1,string" (find key) may return an ERF
! error code 33 (key not found) when in fact the key actually exists.

! The program below demonstrates circumstances in which this occurs.
! The size of a displayed string effects the fate of the ISAM call,
! even though the display has absolutely nothing to do with the call.
! If a string of 6 characters is displayed, no problem occurs.
! If a string of 7 characters is displayed, the problem occurs, but
! a subsequent call with 6 characters displayed will cure the problem.
! If a string of 8 characters is displayed, the problem occurs, and
! will not go away until the program is exited and rerun.

! To demonstrate problem:
!   (1) execute ISMBG2.CMD first
!   (2) RUN ISMBG2.RUN to add new key and exit
!   (3) RUN ISMBG2.RUN again to demonstrate problem

       MAP1 KeyX,X,15
       MAP1 KeyS,S,15          ! increasing to 16 or 64 does not help
       MAP1 KeyS1,@KeyS
               MAP2 KeyS2,X,15
       MAP1 Option,S,1
       MAP1 I,F,6
       MAP1 Length,F,6
       MAP1 RELKEY,F,6
       MAP1 Buffer,S,100

       OPEN #1000, "ISMBG2", INDEXED, 256, RELKEY

GetOpt:
       Option = "E"
       INPUT "Enter E(nd) A(dd) S(tring) X(unformatted): ", Option
       IF Option = "E" THEN END
       IF Option = "A" THEN GOTO Add
       IF Option = "S" THEN GOTO Try
       IF Option = "X" THEN GOTO Try
       GOTO GetOpt

Add:
       KeyS = "ABCDE" : Buffer = KeyS
       KeyX = KeyS                     ! make sure that add works
       ISAM #1000, 1, KeyX
       IF ERF(1000) = 0 THEN PRINT "Record exists" : GOTO AddEnd
       IF ERF(1000) # 33 THEN PRINT "ERF"; ERF(1000) : END
       PRINT "ADDING KEY"
       ISAM #1000, 5, KeyX
       WRITEL #1000, Buffer
       ISAM #1000, 3, KeyX
AddEnd:
       UNLOKR #1000
! adding record causes problem to occur permanently
       GOTO GetOpt

Try:
       INPUT "Length of dummy string  (6,7,8): ", Length
! printing 6/7/8 x's causes no/temporary/permanent problem - bizarre!!!
       IF Length = 6 THEN PRINT "xxxxxx"
       IF Length = 7 THEN PRINT "xxxxxxx"
       IF Length = 8 THEN PRINT "xxxxxxxx"
NxtTry:
!       KeyS = ""                       ! does NOT fix problem
       KeyS = "ABCDE"                  ! key exists in file
       KeyX = KeyS                     ! doesn't cause problem
       IF Option = "S" THEN ISAM #1000, 1, KeyS        ! gets
       IF Option = "X" THEN ISAM #1000, 1, KeyX        ! always ok
! printing following causes problem to occur permanently
!       FOR I = 1 TO 15 : PRINT ASC(KeyS2[I,I]); : NEXT I : PRINT
! the problem: following prints a phoney error 33 (record not found)
       PRINT "Error"; ERF(1000)
       UNLOKR #1000
       GOTO GetOpt