' Collection of code snippets by Arne Vajhøj 
' (from articles on eksperten.dk / vajhoej.dk written sometime between 2004 and now) 
program isq

option type = explicit

external sub test_write
external sub test_read_sequential
external sub test_read_random

print "Basic"
call test_write
call test_read_sequential
call test_read_random

end program
!
sub test_write

option type = explicit

record datarec
    string id = 4
    integer ival
end record
map (bufmap) datarec buf
external sub test_write_one(string, integer)

open "bas.isq" for output as file #1, indexed fixed, recordtype none, map bufmap, primary key buf::id
call test_write_one("CCCC", 3)
call test_write_one("BBBB", 2)
call test_write_one("AAAA", 1)
close #1

end sub
!
sub test_write_one(string id, integer ival)

option type = explicit

record datarec
    string id = 4
    integer ival
end record
map (bufmap) datarec buf

buf::id = id
buf::ival = ival
put #1

end sub
!
sub test_read_sequential

option type = explicit

record datarec
    string id = 4
    integer ival
end record
map (bufmap) datarec buf

open "bas.isq" for input as file #1, indexed fixed, recordtype none, map bufmap, primary key buf::id
handler eof_handler
end handler
when error use eof_handler
    while 1 = 1
        get #1
        print buf::id, " -> ", buf::ival
    next
end when
close #1

end sub
!
sub test_read_random

option type = explicit

record datarec
    string id = 4
    integer ival
end record
map (bufmap) datarec buf
external sub test_read_random_one(string)

open "bas.isq" for input as file #1, indexed fixed, recordtype none, map bufmap, primary key buf::id
call test_read_random_one("CCCC")
call test_read_random_one("BBBB")
call test_read_random_one("AAAA")
close #1

end sub
!
sub test_read_random_one(string id)

option type = explicit

record datarec
    string id = 4
    integer ival
end record
map (bufmap) datarec buf

get #1, key #0 eq id
print buf::id, " -> ", buf::ival

end sub
