make all variables local and clean-up fileblock.yab, fixes problems when bound.
This commit is contained in:
@@ -4,9 +4,12 @@ export sub openfile(filename$, recordlength, numberoffields)
|
|||||||
// specify the record(block) length and number of fields for the file
|
// specify the record(block) length and number of fields for the file
|
||||||
// returns the filehandle number used for other subs
|
// returns the filehandle number used for other subs
|
||||||
static numfiles
|
static numfiles
|
||||||
|
local x
|
||||||
|
|
||||||
x=open(filename$, "a")
|
x=open(filename$, "a")
|
||||||
if x=0 return 0 // error opening the file
|
if x=0 print"rror opening the file":return -1 // error opening the file
|
||||||
close x
|
close x
|
||||||
|
|
||||||
numfiles=numfiles+1
|
numfiles=numfiles+1
|
||||||
dim f$(numfiles,50,2) // f$(numfile,1-50=fieldname$
|
dim f$(numfiles,50,2) // f$(numfile,1-50=fieldname$
|
||||||
dim f(numfiles,52)
|
dim f(numfiles,52)
|
||||||
@@ -15,77 +18,94 @@ f(numfiles,51)=recordlength
|
|||||||
f(numfiles,52)=numberoffields
|
f(numfiles,52)=numberoffields
|
||||||
|
|
||||||
return numfiles
|
return numfiles
|
||||||
|
|
||||||
end sub
|
end sub
|
||||||
|
|
||||||
|
|
||||||
export sub Field( filehandle,fieldnumber, fieldname$, fieldlength)
|
export sub Field( filehandle,fieldnumber, fieldname$, fieldlength)
|
||||||
//specify the field name and length for the field numbered fieldnumber
|
//specify the field name and length for the field numbered fieldnumber
|
||||||
if fieldnumber <= f(filehandle,52) then
|
// return 1 ok, 0 bad fieldnumber
|
||||||
f$(filehandle,fieldnumber,0)=fieldname$
|
|
||||||
f(filehandle,fieldnumber)=fieldlength
|
|
||||||
f$(filehandle,fieldnumber,1)=blank$(f(filehandle,fieldnumber))
|
|
||||||
return 1
|
|
||||||
endif
|
|
||||||
return 0
|
|
||||||
end sub
|
|
||||||
|
|
||||||
|
if fieldnumber <= f(filehandle,52) then
|
||||||
|
f$(filehandle,fieldnumber,0)=fieldname$
|
||||||
|
f(filehandle,fieldnumber)=fieldlength
|
||||||
|
f$(filehandle,fieldnumber,1)=Blank$(f(filehandle,fieldnumber))
|
||||||
|
|
||||||
|
return 1
|
||||||
|
|
||||||
|
endif
|
||||||
|
|
||||||
|
return 0
|
||||||
|
|
||||||
|
end sub
|
||||||
|
|
||||||
|
|
||||||
export sub lset(filehandle,fieldname$,data$)
|
export sub lset(filehandle,fieldname$,data$)
|
||||||
//sets data$ on the left end of the field, truncates the string if it is longer than the fieldlength
|
//sets data$ on the left end of the field, truncates the string if it is longer than the fieldlength
|
||||||
//use this to prepare the record for writing
|
//use this to prepare the record for writing
|
||||||
|
|
||||||
|
local l
|
||||||
|
local x
|
||||||
|
local b$
|
||||||
|
local dt$
|
||||||
|
local a$
|
||||||
|
local lf
|
||||||
|
|
||||||
l=len(data$)
|
l=len(data$)
|
||||||
x=0
|
x=0
|
||||||
|
|
||||||
for x=1 to 50
|
for x=1 to 50
|
||||||
|
|
||||||
b$=f$(filehandle,x,0)
|
b$=f$(filehandle,x,0)
|
||||||
if (b$ = fieldname$) then
|
|
||||||
lf=f(filehandle,x)
|
|
||||||
dt$=data$
|
|
||||||
|
|
||||||
if( l > lf )dt$=left$(dt$,lf)
|
if (b$ = fieldname$) then
|
||||||
a$=blank$(lf-l)
|
|
||||||
if l<lf dt$=dt$+a$
|
|
||||||
|
|
||||||
f$(filehandle,x,1) = dt$
|
lf=f(filehandle,x)
|
||||||
|
dt$=data$
|
||||||
|
if( l > lf )dt$=left$(dt$,lf)
|
||||||
|
a$=Blank$(lf-l)
|
||||||
|
if l<lf dt$=dt$+a$
|
||||||
|
f$(filehandle,x,1) = dt$
|
||||||
|
|
||||||
end if
|
end if
|
||||||
|
|
||||||
next
|
next
|
||||||
|
|
||||||
|
|
||||||
end sub
|
end sub
|
||||||
|
|
||||||
|
|
||||||
export sub rset(filehandle,fieldname$,data$)
|
export sub rset(filehandle,fieldname$,data$)
|
||||||
//sets data$ on the right end of the field, truncates the string if it is longer than the fieldlength
|
//sets data$ on the right end of the field, truncates the string if it is longer than the fieldlength
|
||||||
//use this to prepare the record for writing
|
//use this to prepare the record for writing
|
||||||
|
local l
|
||||||
|
local x
|
||||||
|
local b$
|
||||||
|
local dt$
|
||||||
|
local a$
|
||||||
|
|
||||||
l=len(data$)
|
l=len(data$)
|
||||||
x=0
|
x=0
|
||||||
|
|
||||||
for x=1 to 50
|
for x=1 to 50
|
||||||
|
|
||||||
b$=f$(filehandle,x,0)
|
b$=f$(filehandle,x,0)
|
||||||
if (b$ = fieldname$) then
|
|
||||||
lf=f(filehandle,x)
|
|
||||||
dt$=data$
|
|
||||||
|
|
||||||
if( l > lf )dt$=left$(dt$,lf)
|
if (b$ = fieldname$) then
|
||||||
|
|
||||||
a$=blank$(lf-l)
|
lf=f(filehandle,x)
|
||||||
if l<lf dt$=a$+dt$
|
dt$=data$
|
||||||
|
if( l > lf )dt$=left$(dt$,lf)
|
||||||
|
a$=Blank$(lf-l)
|
||||||
|
if l<lf dt$=a$+dt$
|
||||||
|
f$(filehandle,x,1) = dt$
|
||||||
|
|
||||||
f$(filehandle,x,1) = dt$
|
end if
|
||||||
|
|
||||||
end if
|
|
||||||
|
|
||||||
next
|
next
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
end sub
|
end sub
|
||||||
|
|
||||||
|
|
||||||
export sub readfield$(File, fieldname$)
|
export sub readfield$(File, fieldname$)
|
||||||
//returns the data held in fieldname$
|
//returns the data held in fieldname$
|
||||||
local x
|
local x
|
||||||
@@ -98,92 +118,140 @@ next
|
|||||||
|
|
||||||
end sub
|
end sub
|
||||||
|
|
||||||
|
|
||||||
export sub write_block(recordnumber, filehandle)
|
export sub write_block(recordnumber, filehandle)
|
||||||
// Writes all the fields to the file at this record location
|
// Writes all the fields to the file at this record location
|
||||||
// Uses seek recordnumber*recordlength before the write
|
// Uses seek recordnumber*recordlength before the write
|
||||||
// Clears the field data
|
// Clears the field data
|
||||||
// Recordnumbers start at 0. ie the first record is 0, the second is 1 etc.
|
// Recordnumbers start at 0. ie the first record is 0, the second is 1 etc.
|
||||||
|
local filename$
|
||||||
|
local x
|
||||||
|
local y
|
||||||
|
local a$
|
||||||
|
|
||||||
filename$=f$(filehandle,0,0)
|
filename$=f$(filehandle,0,0)
|
||||||
x=open(filename$, "r+")
|
x=open(filename$, "r+")
|
||||||
if x=0 print "error":return 0 // error opening the file
|
if x=0 print "error":return 0 // error opening the file
|
||||||
|
|
||||||
if recordnumber = -1 then
|
if recordnumber = -1 then
|
||||||
seek x, 0, "end" // append data to the file
|
seek x, 0, "end" // append data to the file
|
||||||
else
|
else
|
||||||
seek x, ((f(filehandle,51) +1) * (recordnumber))
|
seek x, ((f(filehandle,51) +1) * (recordnumber))
|
||||||
|
|
||||||
endif
|
endif
|
||||||
|
|
||||||
printstring$=""
|
printstring$=""
|
||||||
|
|
||||||
for y=1 to f(filehandle,52)
|
for y=1 to f(filehandle,52)
|
||||||
|
|
||||||
printstring$=printstring$+f$(filehandle,y,1)
|
printstring$=printstring$+f$(filehandle,y,1)
|
||||||
|
|
||||||
next
|
next
|
||||||
|
|
||||||
l=len(printstring$)
|
l=len(printstring$)
|
||||||
|
|
||||||
for y=1 to l
|
for y=1 to l
|
||||||
|
|
||||||
a$=left$(printstring$,1)
|
a$=left$(printstring$,1)
|
||||||
printstring$=right$(printstring$,len(printstring$)-1)
|
printstring$=right$(printstring$,len(printstring$)-1)
|
||||||
poke x, a$
|
poke x, a$
|
||||||
|
|
||||||
next
|
next
|
||||||
|
|
||||||
poke x ,"\n"
|
poke x ,"\n"
|
||||||
|
|
||||||
for y=1 to f(filehandle,52)
|
for y=1 to f(filehandle,52)
|
||||||
f$(filehandle,y,1)=blank$(f(filehandle,y))
|
|
||||||
|
f$(filehandle,y,1)=Blank$(f(filehandle,y))
|
||||||
|
|
||||||
next
|
next
|
||||||
|
|
||||||
close x
|
close x
|
||||||
|
|
||||||
end sub
|
end sub
|
||||||
|
|
||||||
|
|
||||||
export sub read_block(recordnumber, filehandle)
|
export sub read_block(recordnumber, filehandle)
|
||||||
// Reads the data at this record, or the next record if recordnumber is -1
|
// Reads the data at this record, or the next record if recordnumber is -1
|
||||||
// Ffills the fields with the data
|
// Ffills the fields with the data
|
||||||
// Recordnumbers start at 0. ie the first record is 0, the second is 1 etc.
|
// Recordnumbers start at 0. ie the first record is 0, the second is 1 etc.
|
||||||
|
|
||||||
static rec
|
static rec
|
||||||
|
local filename$
|
||||||
|
local a$
|
||||||
|
local z
|
||||||
|
local y
|
||||||
|
a$=""
|
||||||
|
|
||||||
|
filename$=f$(filehandle,0,0)
|
||||||
File=open(filename$, "rb")
|
File=open(filename$, "rb")
|
||||||
if File=0 print "error":return -1 // error opening the file
|
if File=0 print "error":return -1 // error opening the file
|
||||||
|
|
||||||
if (recordnumber <> -1) then
|
if (recordnumber <> -1) then
|
||||||
rec = recordnumber
|
|
||||||
seek File, ((f(filehandle,51) +1) * (rec))
|
rec = recordnumber
|
||||||
|
seek File, ((f(filehandle,51) +1) * (rec))
|
||||||
|
|
||||||
endif
|
endif
|
||||||
a$=""
|
|
||||||
for z=1 to f(filehandle,51)
|
for z=1 to f(filehandle,51)
|
||||||
y=peek(File)
|
|
||||||
a$=a$+chr$(y)
|
y=peek(File)
|
||||||
|
a$=a$+chr$(y)
|
||||||
|
|
||||||
next
|
next
|
||||||
|
|
||||||
for y=0 to f(filehandle,52)
|
for y=0 to f(filehandle,52)
|
||||||
|
|
||||||
f$(filehandle,y,1)= left$(a$,f(filehandle,y))
|
f$(filehandle,y,1)= left$(a$,f(filehandle,y))
|
||||||
a$=right$(a$,len(a$)-f(filehandle,y))
|
a$=right$(a$,len(a$)-f(filehandle,y))
|
||||||
|
|
||||||
next
|
next
|
||||||
|
|
||||||
rec=rec+1
|
rec=rec+1
|
||||||
return rec
|
return rec
|
||||||
|
|
||||||
end sub
|
end sub
|
||||||
|
|
||||||
|
|
||||||
export sub lof(filehandle) // alias for numberofrecords()
|
export sub lof(filehandle) // alias for numberofrecords()
|
||||||
local x
|
local x
|
||||||
|
local filename$
|
||||||
|
local ret
|
||||||
filename$=f$(filehandle,0,0)
|
filename$=f$(filehandle,0,0)
|
||||||
x=open(filename$)
|
x=open(filename$)
|
||||||
seek #x, 0, "end"
|
seek #x, 0, "end"
|
||||||
ret=tell(#x)
|
ret=tell(#x)
|
||||||
if ret=0 return ret
|
if ret=0 return ret
|
||||||
|
|
||||||
ret= ret/(f(filehandle,51)+1)
|
ret= ret/(f(filehandle,51)+1)
|
||||||
ret=int(ret)
|
ret=int(ret)
|
||||||
return ret
|
return ret
|
||||||
|
|
||||||
end sub
|
end sub
|
||||||
|
|
||||||
|
|
||||||
export sub numberofrecords(filehandle) // alias for lof()
|
export sub numberofrecords(filehandle) // alias for lof()
|
||||||
|
local fh
|
||||||
|
local rtn
|
||||||
|
|
||||||
fh=filehandle
|
fh=filehandle
|
||||||
rtn=lof(fh)
|
rtn=lof(fh)
|
||||||
return rtn
|
return rtn
|
||||||
|
|
||||||
end sub
|
end sub
|
||||||
|
|
||||||
|
|
||||||
|
sub Blank$(num) // this is a local sub, not exported
|
||||||
|
|
||||||
sub blank$(num)
|
|
||||||
local s$
|
local s$
|
||||||
local x
|
local x
|
||||||
s$=""
|
s$=""
|
||||||
|
|
||||||
for x=1 to num
|
for x=1 to num
|
||||||
s$=s$+" "
|
|
||||||
|
s$=s$+" "
|
||||||
|
|
||||||
next
|
next
|
||||||
|
|
||||||
return s$
|
return s$
|
||||||
|
|
||||||
end sub
|
end sub
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user