added Item$ = "__SmIC__="+FileName$ to COLUMNBOX ADD, add a filemode: r+ ( read/write )
This commit is contained in:
62
yab-IDE/Programs/Examples/BlockReadWriteDemo.yab
Normal file
62
yab-IDE/Programs/Examples/BlockReadWriteDemo.yab
Normal file
@@ -0,0 +1,62 @@
|
||||
import fileblock
|
||||
|
||||
x=openfile("/boot/home/test.file", 150,3)
|
||||
if x < 1 error "cannot open file"
|
||||
|
||||
|
||||
ok=Field(x,1,"name",50)
|
||||
ok=ok+Field(x,2,"address",50)
|
||||
ok=ok+Field(x,3,"phonenumber",50)
|
||||
if (!ok = 3 ) error "bad field"
|
||||
// werite some blocks
|
||||
for z=1 to 20
|
||||
lset(x,"name", "Some dude")
|
||||
lset(x,"address", "Some address")
|
||||
rset(x,"phonenumber","123-456-7890")
|
||||
y=write_block(-1, x) // -1 appends the data to the file.
|
||||
next
|
||||
// over-write a block with new data
|
||||
lset(x,"name", "Some other dude")
|
||||
lset(x,"address", "Some kewl address")
|
||||
rset(x,"phonenumber","098-765-4321")
|
||||
write_block(6, x)
|
||||
|
||||
// read back the over-written block
|
||||
read_block(6, x)
|
||||
|
||||
// get the data from the read operation
|
||||
name$=readfield$(x, "name")
|
||||
address$=readfield$(x, "address")
|
||||
phone$=readfield$(x, "phonenumber")
|
||||
// show the data
|
||||
print name$
|
||||
print address$
|
||||
print trim$(phone$)
|
||||
|
||||
// over write the block again
|
||||
lset(x,"name", "")
|
||||
lset(x,"address", "")
|
||||
rset(x,"phonenumber","")
|
||||
write_block(6, x)
|
||||
|
||||
//read back the over-written block
|
||||
|
||||
recno = read_block(6, x) // returns the read block that will next be read if called with -1 recordnumber
|
||||
name$=readfield$(x, "name")
|
||||
address$=readfield$(x, "address")
|
||||
phone$=readfield$(x, "phonenumber")
|
||||
print name$
|
||||
print address$
|
||||
print phone$
|
||||
print recno
|
||||
|
||||
//read the next block
|
||||
|
||||
recno = read_block(-1, x)
|
||||
name$=readfield$(x, "name")
|
||||
address$=readfield$(x, "address")
|
||||
phone$=readfield$(x, "phonenumber")
|
||||
print name$
|
||||
print address$
|
||||
print phone$
|
||||
print recno
|
||||
@@ -1,9 +1,12 @@
|
||||
#!yab
|
||||
|
||||
|
||||
import Filebox
|
||||
|
||||
|
||||
dir$ = "/boot/home/"
|
||||
|
||||
window open 100,100 to 500,500, "Demo","Demo"
|
||||
window open 100,100 to 500,500, "Demo",dir$
|
||||
Filebox(10,10,350,390,"Filebox", 1, "Demo")
|
||||
FileboxDirectory("Filebox", dir$, false)
|
||||
button 360,10,390,30, "DirUp", "..", "Demo"
|
||||
@@ -13,6 +16,7 @@ inloop = true
|
||||
while(inloop)
|
||||
n = split(message$, msg$(), "|")
|
||||
for i=1 to n
|
||||
print msg$(i)
|
||||
// Quit
|
||||
if(msg$(i) = "Demo:_QuitRequested") inloop = false
|
||||
|
||||
@@ -23,7 +27,9 @@ while(inloop)
|
||||
t = t - 1
|
||||
wend
|
||||
dir$ = left$(dir$,t)
|
||||
WINDOW SET "Demo", "Title", dir$
|
||||
FileboxDirectory("Filebox", dir$, false)
|
||||
|
||||
endif
|
||||
|
||||
// An item is invoked
|
||||
@@ -31,11 +37,12 @@ while(inloop)
|
||||
position = val(right$(msg$(i),len(msg$(i))-16))
|
||||
|
||||
// if the item is a directory, then update the filebox else print the filename
|
||||
if(FileboxIsDirectory("Filebox", position)) then
|
||||
if(FileboxIsDirectory("Filebox", position,dir$)) then
|
||||
dir$ = dir$ + FileboxName$("Filebox", position) + "/"
|
||||
WINDOW SET "Demo", "Title", dir$
|
||||
FileboxDirectory("Filebox", dir$, false)
|
||||
else
|
||||
print FileboxName$("Filebox", position)
|
||||
print dir$+FileboxName$("Filebox", position)
|
||||
endif
|
||||
endif
|
||||
next i
|
||||
|
||||
53
yab-IDE/Programs/Examples/Filebox.yab → yab-IDE/Programs/libs/Filebox.yab
Executable file → Normal file
53
yab-IDE/Programs/Examples/Filebox.yab → yab-IDE/Programs/libs/Filebox.yab
Executable file → Normal file
@@ -26,10 +26,12 @@ export sub FileboxName$(ID$, position)
|
||||
end sub
|
||||
|
||||
// return true, if the row position is a directory
|
||||
export sub FileboxIsDirectory(ID$, position)
|
||||
export sub FileboxIsDirectory(ID$, position,dir$)
|
||||
local t$
|
||||
t$ = columnbox get$ ID$, 1, position
|
||||
if(t$ = "__Mime__=application/x-vnd.Be-directory") return true
|
||||
t$ = columnbox get$ ID$, 2, position
|
||||
print "t$ "+t$
|
||||
//if(t$ = "__Path__="+path$+Name$"__Mime__=application/x-vnd.Be-directory") return true
|
||||
if(system("test -d \""+ dir$+t$+"\"") = 0) system("addattr -t mime BEOS:TYPE application/x-vnd.Be-directory \""+dir$+t$+"\"") : return true
|
||||
return false
|
||||
end sub
|
||||
|
||||
@@ -50,48 +52,65 @@ export sub FileboxDirectory(ID$, dir$, showDot)
|
||||
|
||||
columnbox clear ID$
|
||||
if(showDot) then
|
||||
t$ = system$("ls --format=single-column --color=none -a "+dir$+" |sort -f")
|
||||
t$ = system$("ls --format=single-column --color=none -aF \""+dir$+"\" |sort -f")
|
||||
else
|
||||
t$ = system$("ls --format=single-column --color=none "+dir$+" |sort -f")
|
||||
t$ = system$("ls --format=single-column --color=none -F \""+dir$+"\" |sort -f")
|
||||
endif
|
||||
|
||||
dim splitdir$(1)
|
||||
|
||||
n = split(t$, splitdir$(), "\n")
|
||||
for i=1 to n-1
|
||||
if(system("test -d \""+ dir$+splitdir$(i)+"\"") = 0) then
|
||||
|
||||
//print dir$+splitdir$(i)
|
||||
|
||||
|
||||
//print dir$+splitdir$(i)
|
||||
if (right$(splitdir$(i),1)="/") then
|
||||
|
||||
//if(system("test -d \""+ dir$+splitdir$(i)+"\"") = 0) then
|
||||
// comment the if clause out if you want to have the direcotries "." and ".." listed
|
||||
if(splitdir$(i)<>"." and splitdir$(i)<>"..") then
|
||||
arraysizeDir = arraysizeDir + 1
|
||||
dim directories$(arraysizeDir)
|
||||
directories$(arraysizeDir-1) = splitdir$(i)
|
||||
endif
|
||||
// if(splitdir$(i)<>"." and splitdir$(i)<>"..") then
|
||||
arraysizeDir = arraysizeDir + 1
|
||||
dim directories$(arraysizeDir)
|
||||
splitdir$(i) = left$(splitdir$(i),len( splitdir$(i))-1)
|
||||
directories$(arraysizeDir-1) = splitdir$(i)
|
||||
//endif
|
||||
// handle files
|
||||
|
||||
else
|
||||
|
||||
|
||||
arraysizeFiles = arraysizeFiles + 1
|
||||
dim files$(arraysizeFiles)
|
||||
files$(arraysizeFiles-1) = splitdir$(i)
|
||||
endif
|
||||
next i
|
||||
for i=0 to arraysizeDir-1
|
||||
FileboxAdd(ID$, directories$(i), true)
|
||||
FileboxAdd(ID$, directories$(i), true,dir$)
|
||||
next i
|
||||
for i=0 to arraysizeFiles-1
|
||||
FileboxAdd(ID$, files$(i), false)
|
||||
FileboxAdd(ID$, files$(i), false,dir$)
|
||||
next i
|
||||
return
|
||||
end sub
|
||||
|
||||
sub FileboxAdd(ID$, Name$, IsFolder)
|
||||
sub FileboxAdd(ID$, Name$, IsFolder, path$)
|
||||
local maxpos
|
||||
|
||||
maxpos = (columnbox count ID$) + 1
|
||||
|
||||
a$=""
|
||||
|
||||
if(IsFolder) then
|
||||
columnbox add ID$, 1, maxpos, 18, "__Mime__=application/x-vnd.Be-directory"
|
||||
columnbox add ID$, 1, maxpos, 18, "__SmIC__="+path$+Name$
|
||||
else
|
||||
columnbox add ID$, 1, maxpos, 18, "__Mime__=application/octet-stream"
|
||||
columnbox add ID$, 1, maxpos, 18, "__SmIC__="+path$+Name$
|
||||
|
||||
//else
|
||||
//columnbox add ID$, 1, maxpos, 18, "__Mime__=application/octet-stream"
|
||||
endif
|
||||
columnbox add ID$, 2, maxpos, 20, Name$
|
||||
columnbox select ID$, 1
|
||||
columnbox select ID$, 0
|
||||
return
|
||||
end sub
|
||||
168
yab-IDE/Programs/libs/fileblock.yab
Normal file
168
yab-IDE/Programs/libs/fileblock.yab
Normal file
@@ -0,0 +1,168 @@
|
||||
|
||||
|
||||
export sub openfile(filename$, recordlength, numberoffields)
|
||||
// specify the record(block) length and number of fields for the file
|
||||
// returns the filehandle number used for other subs
|
||||
static numfiles
|
||||
x=open(filename$, "a")
|
||||
if x=0 return 0 // error opening the file
|
||||
close x
|
||||
numfiles=numfiles+1
|
||||
dim f$(numfiles,50,2) // f$(numfile,1-50=fieldname$
|
||||
dim f(numfiles,52)
|
||||
f$(numfiles,0,0)=filename$
|
||||
f(numfiles,51)=recordlength
|
||||
f(numfiles,52)=numberoffields
|
||||
|
||||
return numfiles
|
||||
end sub
|
||||
|
||||
|
||||
export sub Field( filehandle,fieldnumber, fieldname$, fieldlength)
|
||||
//specify the field name and length for the field numbered fieldnumber
|
||||
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$)
|
||||
//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
|
||||
|
||||
l=len(data$)
|
||||
x=0
|
||||
for x=1 to 50
|
||||
|
||||
b$=f$(filehandle,x,0)
|
||||
if (b$ = fieldname$) then
|
||||
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
|
||||
|
||||
next
|
||||
|
||||
|
||||
end sub
|
||||
|
||||
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
|
||||
//use this to prepare the record for writing
|
||||
|
||||
l=len(data$)
|
||||
x=0
|
||||
for x=1 to 50
|
||||
|
||||
b$=f$(filehandle,x,0)
|
||||
if (b$ = fieldname$) then
|
||||
lf=f(filehandle,x)
|
||||
dt$=data$
|
||||
|
||||
if( l > lf )dt$=left$(dt$,lf)
|
||||
|
||||
a$=blank$(lf-l)
|
||||
if l<lf dt$=a$+dt$
|
||||
|
||||
f$(filehandle,x,1) = dt$
|
||||
|
||||
end if
|
||||
|
||||
next
|
||||
|
||||
|
||||
|
||||
|
||||
end sub
|
||||
|
||||
export sub readfield$(File, fieldname$)
|
||||
//returns the data held in fieldname$
|
||||
local x
|
||||
for x=1 to 50
|
||||
if f$(File,x,0)=fieldname$ then
|
||||
|
||||
return f$(File,x,1)
|
||||
end if
|
||||
next
|
||||
|
||||
end sub
|
||||
|
||||
export sub write_block(recordnumber, filehandle)
|
||||
// Writes all the fields to the file at this record location
|
||||
// Uses seek recordnumber*recordlength before the write
|
||||
// Clears the field data
|
||||
// Recordnumbers start at 0. ie the first record is 0, the second is 1 etc.
|
||||
|
||||
filename$=f$(filehandle,0,0)
|
||||
x=open(filename$, "r+")
|
||||
if x=0 print "error":return 0 // error opening the file
|
||||
if recordnumber = -1 then
|
||||
seek x, 0, "end" // append data to the file
|
||||
else
|
||||
seek x, ((f(filehandle,51) +1) * (recordnumber))
|
||||
endif
|
||||
printstring$=""
|
||||
for y=1 to f(filehandle,52)
|
||||
printstring$=printstring$+f$(filehandle,y,1)
|
||||
next
|
||||
l=len(printstring$)
|
||||
for y=1 to l
|
||||
a$=left$(printstring$,1)
|
||||
printstring$=right$(printstring$,len(printstring$)-1)
|
||||
poke x, a$
|
||||
next
|
||||
poke x ,"\n"
|
||||
for y=1 to f(filehandle,52)
|
||||
f$(filehandle,y,1)=blank$(f(filehandle,y))
|
||||
next
|
||||
close x
|
||||
end sub
|
||||
|
||||
export sub read_block(recordnumber, filehandle)
|
||||
// Reads the data at this record, or the next record if recordnumber is -1
|
||||
// Ffills the fields with the data
|
||||
// Recordnumbers start at 0. ie the first record is 0, the second is 1 etc.
|
||||
static rec
|
||||
File=open(filename$, "rb")
|
||||
if File=0 print "error":return -1 // error opening the file
|
||||
if (recordnumber <> -1) then
|
||||
rec = recordnumber
|
||||
seek File, ((f(filehandle,51) +1) * (rec))
|
||||
endif
|
||||
a$=""
|
||||
for z=1 to f(filehandle,51)
|
||||
y=peek(File)
|
||||
a$=a$+chr$(y)
|
||||
next
|
||||
|
||||
for y=0 to f(filehandle,52)
|
||||
f$(filehandle,y,1)= left$(a$,f(filehandle,y))
|
||||
a$=right$(a$,len(a$)-f(filehandle,y))
|
||||
next
|
||||
rec=rec+1
|
||||
return rec
|
||||
end sub
|
||||
|
||||
|
||||
sub blank$(num)
|
||||
local s$
|
||||
local x
|
||||
s$=""
|
||||
for x=1 to num
|
||||
s$=s$+" "
|
||||
next
|
||||
return s$
|
||||
end sub
|
||||
|
||||
|
||||
Reference in New Issue
Block a user