DBin_
Posted: Mon May 29, 2006 1:53 am
Here is my opensource version of VBin_ , its called DBin_ !!!
Its quickly put together and is a bit of a hack. It was mainly written because there is no VBin_ for V4 and to allow something I wished VBin_ could do: Allow you to append your data file to an exe and load the files directly (rather than have to copy it off the executable first).
The commands are the same as VBin_'s, just change the V for a D. The AddFile has an optional compression setting. Encryption is missing, I didn't use it - but it would be good if someone else adds it.
The routines beginning with iDBin_ are the internal routines, making it into a library should be easy using TailBite, probabily just make the non-iDBin routines into ProcedureDLLs.
Its opensource, but it would be a good idea if it gets added to either Droopy's forthcoming v4 lib or PBOSL - I don't mind which.
Hope you all like it.
-Anthony
Code: Select all
Global vDBin_Size,vDBin_Compressed,vDBin_CRC
Procedure iDBin_Header(file,string$,size,compressed,crc)
len=Len(string$)
If len
WriteString(file,string$)
EndIf
WriteByte(file,len) ; filename len
WriteLong(file,size) ; no size
WriteLong(file,compressed) ; no pack
WriteLong(file,crc) ; crc of data
WriteLong(file,$dbdbdbdb) ; id string
EndProcedure
Procedure iDBin_CheckHeader(file,pos)
result=0
If pos>17
FileSeek(file,pos-4)
If ReadLong(file)=$dbdbdbdb
FileSeek(file,pos-17)
len=ReadByte(file)&$ff
size=ReadLong(file)
compressed=ReadLong(file)
stringpos=pos-17-len
If compressed
result=stringpos-compressed
Else
result=stringpos-size
EndIf
FileSeek(file,pos-17)
EndIf
EndIf
ProcedureReturn result
EndProcedure
Procedure DBin_Create(string$)
file=CreateFile(#PB_Any,string$)
If file
iDBin_Header(file,"",0,0,0)
CloseFile(file)
EndIf
ProcedureReturn file
EndProcedure
Procedure DBin_AddFile(dest$,string$,comp=-1)
result=#False
endoffile=FileSize(dest$)
If endoffile>0
string$=LCase(ReplaceString(string$,"/","\"))
size=FileSize(string$)
If size>0
source=AllocateMemory(size)
If source
dest=AllocateMemory(size+8)
If dest
file=ReadFile(#PB_Any,string$)
If file
If ReadData(file,source,size)=size
If comp>=0
compressed=PackMemory(source,dest,size,comp)
Else
compressed=PackMemory(source,dest,size)
EndIf
file2=OpenFile(#PB_Any,dest$)
If file2
FileSeek(file2,endoffile)
If compressed
WriteData(file2,dest,compressed)
Else
WriteData(file2,source,size)
EndIf
iDBin_Header(file2,string$,size,compressed,CRC32Fingerprint(source,size))
CloseFile(file2)
result=#True
EndIf
EndIf
CloseFile(file)
EndIf
FreeMemory(dest)
EndIf
FreeMemory(source)
EndIf
EndIf
EndIf
ProcedureReturn result
EndProcedure
Procedure DBin_TotalFiles(string$)
result=0
endoffile=FileSize(string$)
If endoffile>16
file=ReadFile(#PB_Any,string$)
If file
Repeat
endoffile=iDBin_CheckHeader(file,endoffile)
If endoffile
result+1
EndIf
Until Not endoffile
CloseFile(file)
EndIf
EndIf
ProcedureReturn result
EndProcedure
Procedure.s DBin_Dir(string$,pos)
atfile=0
result$=""
endoffile=FileSize(string$)
If endoffile>16
file=ReadFile(#PB_Any,string$)
If file
Repeat
endoffile2=endoffile
endoffile=iDBin_CheckHeader(file,endoffile)
If endoffile
atfile+1
If atfile>=pos
len=ReadByte(file)&$ff
If len
result$=Space(len)
FileSeek(file,endoffile2-17-len)
ReadData(file,@result$,len)
ReadByte(file) ; skip string len
vDBin_Size=ReadLong(file)
vDBin_Compressed=ReadLong(file)
vDBin_CRC=ReadLong(file)
EndIf
Break
EndIf
EndIf
Until Not endoffile
CloseFile(file)
EndIf
EndIf
ProcedureReturn result$
EndProcedure
Procedure DBin_FindFile(string$,file$)
file$=LCase(ReplaceString(file$,"/","\"))
result=#False
endoffile=FileSize(string$)
If endoffile>16
file=ReadFile(#PB_Any,string$)
If file
Repeat
endoffile2=endoffile
endoffile=iDBin_CheckHeader(file,endoffile)
If endoffile
len=ReadByte(file)&$ff
If len
compare$=Space(len)
FileSeek(file,endoffile2-17-len)
ReadData(file,@compare$,len)
If file$=compare$
ReadByte(file) ; skip string len
vDBin_Size=ReadLong(file)
vDBin_Compressed=ReadLong(file)
vDBin_CRC=ReadLong(file)
result=endoffile
Break
EndIf
EndIf
EndIf
Until Not endoffile
CloseFile(file)
EndIf
EndIf
ProcedureReturn result
EndProcedure
Procedure DBin_OriginalSize(string$,file$)
If DBin_FindFile(string$,file$)
ProcedureReturn vDBin_Size
Else
ProcedureReturn 0
EndIf
EndProcedure
Procedure DBin_CompressedSize(string$,file$)
If DBin_FindFile(string$,file$)
ProcedureReturn vDBin_Compressed
Else
ProcedureReturn 0
EndIf
EndProcedure
Procedure DBin_SaveFile(string$,file$,save$)
result=#False
pos=DBin_FindFile(string$,file$)
If pos
dest=AllocateMemory(vDBin_Size)
If dest
file=ReadFile(#PB_Any,string$)
If file
FileSeek(file,pos)
If vDBin_Compressed
source=AllocateMemory(vDBin_Compressed)
If source
If ReadData(file,source,vDBin_Compressed)=vDBin_Compressed
UnpackMemory(source,dest)
FreeMemory(source)
result=#True
EndIf
EndIf
Else
If ReadData(file,dest,vDBin_Size)=vDBin_Size
result=#True
EndIf
EndIf
CloseFile(file)
If result
result=#False
file=CreateFile(#PB_Any,save$)
If file
WriteData(file,dest,vDBin_Size)
CloseFile(file)
result=#True
EndIf
EndIf
EndIf
FreeMemory(dest)
EndIf
EndIf
ProcedureReturn result
EndProcedure
Procedure DBin_(string$,file$)
result=#False
pos=DBin_FindFile(string$,file$)
If pos
dest=AllocateMemory(vDBin_Size)
If dest
file=ReadFile(#PB_Any,string$)
If file
FileSeek(file,pos)
If vDBin_Compressed
source=AllocateMemory(vDBin_Compressed)
If source
If ReadData(file,source,vDBin_Compressed)=vDBin_Compressed
UnpackMemory(source,dest)
FreeMemory(source)
result=dest
EndIf
EndIf
Else
If ReadData(file,dest,vDBin_Size)=vDBin_Size
result=dest
EndIf
EndIf
CloseFile(file)
EndIf
If result=#False
FreeMemory(dest)
EndIf
EndIf
EndIf
ProcedureReturn result
EndProcedure
Procedure DBin_Free(mem)
ProcedureReturn FreeMemory(mem)
EndProcedure
The commands are the same as VBin_'s, just change the V for a D. The AddFile has an optional compression setting. Encryption is missing, I didn't use it - but it would be good if someone else adds it.
The routines beginning with iDBin_ are the internal routines, making it into a library should be easy using TailBite, probabily just make the non-iDBin routines into ProcedureDLLs.
Its opensource, but it would be a good idea if it gets added to either Droopy's forthcoming v4 lib or PBOSL - I don't mind which.

Hope you all like it.
-Anthony