bigfile.pbi - am Schluss ist noch ein Beispiel, das natürlich nur bei mir funktionell ist, weil es meine lokalen Dateien packt. Es soll nur die prinzipielle Benutzung zeigen.
Code: Alles auswählen
;*
;* Module BigFile
;*
;* Version: 1.1
;* Date: 2015-09-14
;*
;* Written by GPI
;*
UseLZMAPacker()
;#bigfile_create=#True
CompilerIf Not Defined(bigfile_create,#PB_Constant)
#bigfile_create=#False
CompilerEndIf
CompilerIf #bigfile_create
DeclareModule BigFileCreate
EnableExplicit
#Id=$46474942; $42494746 ;'BIGF'
#Version=$30313030;'0100'
Declare Create(file.s)
Declare Close()
Declare AddDir(path.s,BigFileDir.s=".")
Declare AddFile(file.s,BigFileDir.s=".")
EndDeclareModule
Module BigFileCreate
Macro RemoveBackslash(p):If Right(p,1)="\" : p=Left(p,Len(p)-1):EndIf:EndMacro
Macro IfSet(a,b):a=b:If a:EndMacro: ;endindent ;endindent
Macro IfNotSet(a,b):a=b:If a=#False:EndMacro: ;endindent ;endindent
Procedure _LoadData(*mem,memsize,file.s)
Define in,len
IfSet(in,ReadFile(#PB_Any,file))
Len=Lof(in)
If len>memsize
CloseFile(in)
ProcedureReturn 0
EndIf
ReadData(in,*mem,Lof(in))
CloseFile(in)
ProcedureReturn len
EndIf
ProcedureReturn 0
EndProcedure
Structure indexFile
Size.l;0=directory
PackSize.l
Start.l
EndStructure
Structure index
Map file.indexfile()
EndStructure
Global NewMap index.index()
Global BufLen
Global *Buf
Global *OutBuf
Global out
Procedure _ResizeBuf(size)
Protected *newbuf
Protected *newoutbuf
If BufLen<Size
BufLen=Size+1024*1024
IfNotSet (*newbuf,ReAllocateMemory(*buf,BufLen, #PB_Memory_NoClear))
ProcedureReturn #False
EndIf
*buf=*newbuf
IfNotSet (*newOutBuf,ReAllocateMemory(*OutBuf,BufLen, #PB_Memory_NoClear))
ProcedureReturn #False
EndIf
*OutBuf=*newoutbuf
EndIf
ProcedureReturn #True
EndProcedure
Procedure _Add(file.s,len,BigFileDir.s=".")
Protected plen
Protected ok=#False
Protected str.s
If _ResizeBuf(len)
If _LoadData(*buf,buflen,file)=len
IfSet(plen,CompressMemory(*buf,len,*OutBuf,BufLen,#PB_PackerPlugin_Lzma))
index(BigFileDir)\file(GetFilePart(file))
index()\file()\Size=len
index()\file()\Start=Loc(out)
index()\file()\PackSize=plen
WriteData(out,*OutBuf,plen)
ok=#True
EndIf
EndIf
EndIf
ProcedureReturn ok
EndProcedure
Procedure _scan(path.s,bigpath.s=".",add.s=".")
Protected dir,name.s
Protected ok=#True
Protected str.s
IfSet (dir,ExamineDirectory(#PB_Any,path+"\"+add,"*.*"))
While NextDirectoryEntry(dir) And ok
name=DirectoryEntryName(dir)
If DirectoryEntryType(dir)=#PB_DirectoryEntry_Directory
If name<>"." And name<>".."
;index(add)\file(Name)\Size=0
If add<>"."
ok=_scan(path,bigpath,add+"\"+name)
Else
ok=_scan(path,bigpath,name)
EndIf
EndIf
Else
If add="."
str=bigpath
ElseIf bigpath="."
str=add
Else
str=bigpath+"\"+add
EndIf
ok=_add(path+"\"+add+"\"+name,DirectoryEntrySize(dir),str)
EndIf
Wend
FinishDirectory(dir)
EndIf
ProcedureReturn ok
EndProcedure
Procedure Create(file.s)
Protected ok=#False
BufLen=1024*1024*10 ;10 MB
IfSet(*Buf,AllocateMemory(BufLen))
IfSet(*OutBuf,AllocateMemory(BufLen))
IfSet(out,CreateFile(#PB_Any,file))
WriteLong(out,#Id);0
WriteLong(out,#Version);4
WriteLong(out,0) ;8 startofindex
ok=#True
EndIf
EndIf
EndIf
If ok=#False
Close()
EndIf
ProcedureReturn ok
EndProcedure
Procedure Close()
Protected IndexPos
Protected len,plen
Protected *pos
Protected ok=#False
If out
IndexPos=Loc(out)
len=0
ForEach index()
If MapSize(index()\file())>0
len+2+StringByteLength(MapKey(index()),#PB_Unicode)
ForEach index()\file()
If index()\file()\size<>0
len+2+StringByteLength(MapKey(index()\file()),#PB_Unicode)
len+4;size
len+4;packsize
len+4;start
EndIf
Next
len+2
EndIf
Next
len+2
If _ResizeBuf(len)
*pos=*buf
ForEach index()
Debug "Subdir:"+MapKey(index())
If MapSize(index()\file())>0
PokeW(*pos,Len(MapKey(index()))):*pos+2
*pos+PokeS(*pos, MapKey(index()) ,-1,#PB_Unicode|#PB_String_NoZero)
ForEach index()\file()
Debug " "+MapKey(index()\file())+" "+Str(index()\file()\Size)
If index()\file()\size<>0
PokeW(*pos, Len( MapKey( index()\file() ) )):*pos+2
*pos+PokeS(*pos, MapKey( index()\file() ) ,-1,#PB_Unicode|#PB_String_NoZero)
PokeL(*pos, index()\file()\Size ):*pos+4
PokeL(*pos, index()\file()\PackSize):*pos+4
PokeL(*pos, index()\file()\Start):*pos+4
EndIf
Next
PokeW(*pos,0):*pos+2
EndIf
Next
PokeW(*pos,0):*pos+2
CompilerIf #PB_Compiler_Debugger
If *pos<>*buf+len
Debug "Error Index > "+Hex(*pos)+" "+Hex(*buf+len)+" "+Str(len)
End
EndIf
CompilerEndIf
IfSet(plen,CompressMemory(*buf,len,*OutBuf,buflen,#PB_PackerPlugin_Lzma))
WriteLong(out,len)
WriteLong(out,plen)
WriteData(out,*OutBuf,plen)
FileSeek(out,8)
WriteLong(out,IndexPos)
ok=#True
EndIf
EndIf
CloseFile(out)
out=0
EndIf
If *buf
FreeMemory(*buf)
*buf=0
EndIf
If *OutBuf
FreeMemory(*OutBuf)
*OutBuf=0
EndIf
ClearMap(index())
ProcedureReturn ok
EndProcedure
Procedure AddDir(path.s,BigFileDir.s=".")
RemoveBackslash(path)
RemoveBackslash(BigFileDir)
If Left(BigFileDir,1)="\":BigFileDir=Right(BigFileDir,Len(BigFileDir)-1):EndIf
If BigFileDir="":BigFileDir=".":EndIf
ProcedureReturn _scan(path,BigFileDir)
EndProcedure
Procedure AddFile(file.s,BigFileDir.s=".")
Protected len
RemoveBackslash(BigFileDir)
If Left(BigFileDir,1)="\":BigFileDir=Right(BigFileDir,Len(BigFileDir)-1):EndIf
If BigFileDir="":BigFileDir=".":EndIf
len=FileSize(file)
If len>0
ProcedureReturn _add(file,len,BigFileDir)
EndIf
ProcedureReturn #False
EndProcedure
EndModule
CompilerEndIf
DeclareModule BigFile
EnableExplicit
Declare Load(file.s)
Declare Catch(*mem)
Declare Clear()
CompilerIf #PB_Compiler_Debugger
Declare DebugList()
CompilerElse
Macro DebugList():EndMacro
CompilerEndIf
Declare SetPath(path.s)
Declare Examine(path.s)
Declare EntryType(*handle)
Declare FinishExamine(*handle)
Declare NextEntry(*handle)
Declare EntryIsBig(*handle)
Declare EntrySize(*handle)
Declare.s EntryName(*handle)
Declare Size(file.s)
Declare SaveFile(File.s,OutFile.s)
Declare Open(file.s)
Declare Close(*handle)
Declare.s ReadS(*handle,flag=0,length.u=-1)
Declare EOB(*handle)
Declare LOB(*handle)
Declare POS(*handle)
Declare Seek(*handle,pos,mode=#PB_Absolute)
Declare.a ReadA(*handle)
Declare.b ReadB(*handle)
Declare.c ReadC(*handle)
Declare.d ReadD(*handle)
Declare.f ReadF(*handle)
Declare.i ReadI(*handle)
Declare.l ReadL(*handle)
Declare.q ReadQ(*handle)
Declare.u ReadU(*handle)
Declare.w ReadW(*handle)
Declare ReadMem(*handle,*mem,len)
Declare Image(id,file.s)
Declare JSON(id,file.s,flag=0)
Declare Music(id,file.s)
Declare Sound(id,file.s,flag=0)
Declare Sprite(id,file.s,flag=0)
Declare OpenPref(file.s)
Declare ClosePref(*handle)
Declare ExaminePrefGroups(*handle)
Declare ExaminePrefKeys(*handle)
Declare NextPrefGroup(*handle)
Declare NextPrefKey(*handle)
Declare PrefGroup(*handle,name.s)
Declare.s PrefGroupName(*handle)
Declare.s PrefKeyName(*handle)
Declare.s PrefKeyValue(*handle)
Declare.d ReadPrefD(*handle,key.s,DefaultValue.d)
Declare.f ReadPrefF(*handle,key.s,DefaultValue.f)
Declare.i ReadPrefI(*handle,key.s,DefaultValue.i)
Declare.l ReadPrefL(*handle,key.s,DefaultValue.l)
Declare.q ReadPrefQ(*handle,key.s,DefaultValue.q)
Declare.s ReadPrefS(*handle,key.s,DefaultValue.s)
EndDeclareModule
Module BigFile
Macro IfSet(a,b):a=b:If a:EndMacro: ;endindent ;endindent
Macro IfNotSet(a,b):a=b:If a=#False:EndMacro: ;endindent ;endindent
Macro RemoveBackslash(p):If Right(p,1)="\" : p=Left(p,Len(p)-1):EndIf:EndMacro
Macro IndexFile:index()\file():EndMacro
Macro SetIndex(a,b):index(a)\file(b):EndMacro
Macro FindIndex(a,b): Bool(FindMapElement(index(),a) And FindMapElement(index()\file(),b)) : EndMacro
#Id=$46474942; $42494746 ;'BIGF'
#Version=$30313030;'0100'
Global RealPath.s
CompilerIf #PB_Compiler_Thread
Global MutexBigId=CreateMutex()
Macro LockBigId:LockMutex(MutexBigId):EndMacro
Macro UnLockBigId:UnlockMutex(MutexBigId):EndMacro
Global MutexIndex=CreateMutex()
Macro LockIndex:LockMutex(MutexIndex):EndMacro
Macro UnLockIndex:UnlockMutex(MutexIndex):EndMacro
CompilerElse
Macro LockBigId:EndMacro
Macro UnLockBigId:EndMacro
Macro LockIndex:EndMacro
Macro UnLockIndex:EndMacro
CompilerEndIf
#BigId_Mem=-2
#BigId_File=-1
Global NewList BigId.s()
Procedure AddBigId(file.s)
Protected ret
LockBigId
LastElement(BigId())
AddElement(BigId())
BigId()=file
ret=ListIndex(BigId())
UnLockBigId
ProcedureReturn ret
EndProcedure
Procedure.s GetBigId(id)
Protected ret.s
LockBigId
SelectElement(BigId(),id)
ret=BigId()
UnLockBigId
ProcedureReturn ret
EndProcedure
Structure iFile
Size.l;0=file
PackSize.l
Start.i
BigId.l
EndStructure
Structure Index
Map file.iFile()
EndStructure
Global NewMap index.index()
Procedure _scanIndex(*outbuf,BigId,*start=0)
Protected *pos
Protected file.s
Protected slen
Protected len
Protected dir.s
Protected str.s,addx.s,i
LockIndex
*pos=*outbuf
Repeat
slen=PeekW(*pos):*pos+2
If slen=0
Break
EndIf
dir=PeekS(*pos,slen,#PB_Unicode):*pos+slen+slen
addx=".":i=0
Repeat
i+1
str=StringField(dir,i,"\")
If str
index(addx)\file(str)\Size=0
If addx=".":addx=str:Else:addx+"\"+str:EndIf
Else
Break
EndIf
ForEver
Repeat
slen=PeekW(*pos):*pos+2
If slen=0
Break
EndIf
file=PeekS(*pos,slen,#PB_Unicode):*pos+slen+slen
SetIndex(dir,file)
IndexFile\BigId=BigId
len=PeekL(*pos):*pos+4
IndexFile\Size=len
If len
IndexFile\PackSize=PeekL(*pos):*pos+4
IndexFile\Start=PeekL(*pos)+*start:*pos+4
EndIf
ForEver
ForEver
UnLockIndex
EndProcedure
Procedure Catch(*mem)
Protected StartIndex
Protected len,plen
Protected *outbuf
Protected ok=#False
If PeekL(*mem)<>#id Or PeekL(*mem+4)<>#version
ProcedureReturn #False
EndIf
StartIndex=PeekL(*mem+8)
len=PeekL(*mem+StartIndex)
plen=PeekL(*mem+StartIndex+4)
IfSet(*outbuf,AllocateMemory(len))
If UncompressMemory(*mem+StartIndex+8,plen,*outbuf,len,#PB_PackerPlugin_Lzma)=len
_scanIndex(*outbuf,#BigId_Mem,*mem)
ok=#True
EndIf
FreeMemory(*outbuf)
EndIf
ProcedureReturn ok
EndProcedure
Procedure Load(file.s)
Protected in
Protected StartIndex
Protected len,plen
Protected *buf,*outbuf
Protected ok=#False
Protected BigId
BigId=AddBigId(file)
IfSet(in,ReadFile(#PB_Any,file,#PB_File_SharedRead))
If ReadLong(in)<>#id
CloseFile(in)
ProcedureReturn #False
EndIf
If ReadLong(in)<>#Version
CloseFile(in)
ProcedureReturn #False
EndIf
StartIndex=ReadLong(in)
FileSeek(in,StartIndex)
len=ReadLong(in)
plen=ReadLong(in)
IfSet(*buf,AllocateMemory(len+plen))
*outbuf=*buf+plen
ReadData(in,*buf,plen)
If UncompressMemory(*buf,plen,*outbuf,len,#PB_PackerPlugin_Lzma)=len
_scanIndex(*outbuf,BigId)
ok=#True
EndIf
FreeMemory(*buf)
EndIf
CloseFile(in)
EndIf
ProcedureReturn ok
EndProcedure
Procedure Clear()
LockIndex
LockBigId
ClearMap(index())
ClearList(BigId())
UnLockIndex
UnLockBigId
EndProcedure
CompilerIf #PB_Compiler_Debugger
Procedure DebugList()
LockIndex
ForEach index()
Debug "Dir:"+MapKey(index())
ForEach index()\file()
If IndexFile\Size=0
Debug "["+MapKey(IndexFile)+"]"
Else
Debug MapKey(IndexFile)+" "+IndexFile\size+" "+IndexFile\packsize+" s:"+IndexFile\start+" id:"+IndexFile\BigId
EndIf
Next
Debug ""
Next
UnLockIndex
EndProcedure
CompilerEndIf
Procedure SetPath(path.s)
RemoveBackslash(path)
If path="":path="." : EndIf
RealPath=path
EndProcedure
Procedure _ScanDir(path.s,*handle.index)
Protected dir
Protected name.s
Protected size
IfSet (dir,ExamineDirectory(#PB_Any,RealPath+"\"+path,"*.*"))
While NextDirectoryEntry(dir)
name=DirectoryEntryName(dir)
If DirectoryEntryType(dir)=#PB_DirectoryEntry_Directory
If name<>"." And name<>".."
*handle\file(name)\Size=0
EndIf
Else
size=DirectoryEntrySize(dir)
If size>0
*handle\file(name)\Size=size
*handle\file()\BigId=#BigId_File
EndIf
EndIf
Wend
FinishDirectory(dir)
EndIf
EndProcedure
Procedure _ScanBig(path.s,*handle.index)
LockIndex
If FindMapElement(index(),path)
ForEach index()\file()
*handle\file(MapKey(IndexFile))
*handle\file()\BigId=IndexFile\BigId
*handle\file()\PackSize=IndexFile\PackSize
*handle\file()\Size=IndexFile\Size
*handle\file()\Start=IndexFile\Start
Next
EndIf
UnLockIndex
EndProcedure
Procedure Examine(path.s)
Protected *handle.index
RemoveBackslash(path)
If path="":path="." : EndIf
IfSet(*handle,AllocateStructure(Index))
CompilerIf #PB_Compiler_Debugger
_ScanBig(path,*handle) ;debugger = dir > big
_ScanDir(path,*handle)
CompilerElse
_ScanDir(path,*handle) ;execute = big < dir
_ScanBig(path,*handle)
CompilerEndIf
ResetMap(*handle\file())
EndIf
ProcedureReturn *handle
EndProcedure
Procedure EntryType(*handle.index)
If *handle\file()\Size=0
ProcedureReturn #PB_DirectoryEntry_Directory
Else
ProcedureReturn #PB_DirectoryEntry_File
EndIf
EndProcedure
Procedure FinishExamine(*handle)
FreeStructure(*handle)
EndProcedure
Procedure NextEntry(*handle.index)
ProcedureReturn NextMapElement(*handle\file())
EndProcedure
Procedure EntryIsBig(*handle.index)
ProcedureReturn Bool(*handle\file()\BigId<>#BigId_File)
EndProcedure
Procedure EntrySize(*handle.index)
ProcedureReturn *handle\file()\Size
EndProcedure
Procedure.s EntryName(*handle.index)
ProcedureReturn MapKey(*handle\file())
EndProcedure
Procedure Size(file.s)
Protected path.s
Protected ret
Protected realfile.s
path=GetPathPart(file)
RemoveBackslash(path)
If Path="" : path="." : EndIf
If Left(file,1)="\"
realfile=RealPath+file
Else
realfile=RealPath+"\"+file
EndIf
LockIndex
CompilerIf #PB_Compiler_Debugger
ret=FileSize(realfile); dir > big
If ret=-1 Or ret=0
If FindIndex(path,file)
ret=IndexFile\Size
If ret=0
ret=-2
EndIf
Else
ret=-1
EndIf
EndIf
CompilerElse
If FindIndex(path,file)
ret=IndexFile\Size
If ret=0
ret=-2
EndIf
Else
ret=FileSize(realfile)
EndIf
CompilerEndIf
UnLockIndex
ProcedureReturn ret
EndProcedure
Procedure _LoadBig(file.s,offset=0,addlen=0)
Protected path.s
Protected *mem,*mempack
Protected big.s
Protected ok=#False
Protected in
path=GetPathPart(file)
RemoveBackslash(path)
If Path="" : path="." : EndIf
LockIndex
If FindIndex(path,GetFilePart(file))
IfSet(*mem,AllocateMemory(IndexFile\Size+offset+addlen))
If IndexFile\BigId=#BigId_Mem
*mempack=IndexFile\Start
ok=#True
Else
IfSet(*mempack,AllocateMemory(IndexFile\PackSize))
big=GetBigId(IndexFile\BigId)
IfSet(in,ReadFile(#PB_Any,big,#PB_File_SharedRead|#PB_File_NoBuffering))
FileSeek(in,IndexFile\Start)
If ReadData(in,*mempack,IndexFile\PackSize)=IndexFile\PackSize
ok=#True
EndIf
CloseFile(in)
EndIf
EndIf
EndIf
If ok
If UncompressMemory(*mempack,IndexFile\PackSize,*mem+offset,IndexFile\Size,#PB_PackerPlugin_Lzma)<>IndexFile\Size
ok=#False
EndIf
EndIf
If IndexFile\BigId<>#BigId_Mem And *mempack
FreeMemory(*mempack)
EndIf
If ok=#False
FreeMemory(*mem)
*mem=0
EndIf
EndIf
EndIf
UnLockIndex
ProcedureReturn *mem
EndProcedure
Procedure _LoadFile(file.s,offset=0,addlen=0)
Protected *mem
Protected ok=#False
Protected in
If Left(file,1)="\"
file=RealPath+file
Else
file=RealPath+"\"+file
EndIf
IfSet (in, ReadFile(#PB_Any,file,#PB_File_SharedRead|#PB_File_NoBuffering))
IfSet(*mem,AllocateMemory(Lof(in)+offset+addlen))
If ReadData(in,*mem+offset,Lof(in))=Lof(in)
ok=#True
EndIf
EndIf
CloseFile(in)
EndIf
If ok
ProcedureReturn *mem
ElseIf *mem
FreeMemory(*mem)
EndIf
ProcedureReturn 0
EndProcedure
Procedure _LoadMem(file.s,offset=0,addlen=0)
Protected *mem
CompilerIf #PB_Compiler_Debugger
IfNotSet(*mem,_LoadFile(file,offset,addlen))
*mem=_LoadBig(file,offset,addlen)
EndIf
CompilerElse
IfNotSet(*mem,_LoadBig(file,offset,addlen))
*mem=_LoadFile(File,offset,addlen)
EndIf
CompilerEndIf
ProcedureReturn *mem
EndProcedure
Procedure SaveFile(File.s,OutFile.s)
Protected *mem
Protected out
Protected size
Protected ok=#False
IfSet(*mem,_LoadMem(file))
size=size(file)
IfSet(out,CreateFile(#PB_Any,OutFile))
If WriteData(out,*mem,size)=size
ok=#True
EndIf
CloseFile(out)
EndIf
FreeMemory(*mem)
EndIf
ProcedureReturn OK
EndProcedure
Structure filehandle
pos.l
size.l
forceflag.l
Dat.b
EndStructure
Procedure Open(file.s)
Protected *mem.filehandle
IfSet(*mem,_LoadMem(file,OffsetOf(filehandle\Dat),10))
*mem\size=Size(file)
CompilerIf #PB_Compiler_Unicode
*mem\forceflag=#PB_UTF8
CompilerElse
*mem\forceflag=#PB_Ascii
CompilerEndIf
EndIf
ProcedureReturn *mem
EndProcedure
Procedure Close(*handle)
FreeMemory(*handle)
EndProcedure
Procedure.s ReadS(*handle.filehandle,allflag=0,length.u=-1)
Protected *posu.unicode
Protected *posa.ascii
Protected *start
Protected ret.s="Error"
Protected savea.a,saveu.u
Protected readed.u
Protected flag=allflag & (#PB_Unicode|#PB_UTF8|#PB_Ascii)
Protected IgnoreFlag=allflag & #PB_File_IgnoreEOL
If *handle\pos>=*handle\size
ProcedureReturn ""
EndIf
;bom check
*posa=*handle+OffsetOf(filehandle\Dat)+*handle\pos
If *posa\a=$ef
*posa+1
If *posa\a=$bb
*posa+1
If *posa\a=$bf
flag=#PB_UTF8
*handle\forceflag=flag
*handle\pos+3
EndIf
EndIf
ElseIf *posa\a=$FF
*posa+1
If *posa\a=$FE
flag=#PB_Unicode
*handle\forceflag=flag
*handle\pos+2
EndIf
EndIf
If flag=0
flag=*handle\forceflag
EndIf
Select flag
Case #PB_Ascii,#PB_UTF8
*posa=*handle+OffsetOf(filehandle\Dat)+*handle\pos
*start=*posa
Repeat
If (Ignoreflag=0 And (*posa\a=13 Or *posa\a=10)) Or *posa\a=0 Or *handle\pos>=*handle\size Or readed>=length
savea=*posa\a
*posa\a=0
ret=PeekS(*start,-1,flag)
*posa\a=savea
If Ignoreflag=0
If *posa\a=0
*posa+1
*handle\pos+1
Else
If *posa\a=13
*posa+1
*handle\pos+1
EndIf
If *posa\a=10
*posa+1
*handle\pos+1
EndIf
EndIf
EndIf
Break
EndIf
If flag=#PB_UTF8
;start of a multi-byte?
; 87654321 87654321
If (*posa\a & %11000000)=%11000000
Repeat
*posa+1
*handle\pos+1
; 87654321 87654321
Until (*posa\a & %11000000)<>%10000000
readed+1
Else
*posa+1
*handle\pos+1
readed+1
EndIf
Else
*posa+1
*handle\pos+1
readed+1
EndIf
ForEver
Case #PB_Unicode
*posu=*handle+OffsetOf(filehandle\Dat)+*handle\pos
*start=*posu
Repeat
If (ignoreFlag=0 And (*posu\u=13 Or *posu\u=10)) Or *posu\u=0 Or *handle\pos>=*handle\size Or readed>=length
saveu=*posu\u
*posu\u=0
ret=PeekS(*start,-1,flag)
*posu\u=saveu
If Ignoreflag=0
If *posu\u=0
*posu+2
*handle\pos+2
Else
If *posu\u=13
*posu+2
*handle\pos+2
EndIf
If *posu\u=10
*posu+2
*handle\pos+2
EndIf
EndIf
EndIf
Break
EndIf
*posu+2
*handle\pos+2
readed+1
ForEver
EndSelect
ProcedureReturn ret
EndProcedure
Procedure EOB(*handle.filehandle)
ProcedureReturn Bool(*handle\pos>=*handle\size)
EndProcedure
Procedure LOB(*handle.filehandle)
ProcedureReturn *handle\size
EndProcedure
Procedure POS(*handle.filehandle)
ProcedureReturn *handle\pos
EndProcedure
Procedure Seek(*handle.filehandle,pos,mode=#PB_Absolute)
If mode=#PB_Absolute
*handle\pos=pos
Else
*handle\pos+pos
EndIf
If *handle\pos<0
*handle\pos=0
ElseIf *handle\pos>*handle\size
*handle\pos=*handle\size
EndIf
EndProcedure
Macro Readx(i,in)
Procedure.i Read#i(*handle.filehandle)
Protected *var.in=*handle+OffsetOf(filehandle\dat)+*handle\pos
*handle\pos+SizeOf(in)
ProcedureReturn *var\i
EndProcedure
EndMacro
readx(a,ascii)
readx(b,byte)
readx(c,character)
readx(d,double)
readx(f,float)
readx(i,integer)
readx(l,long)
readx(q,quad)
readx(u,unicode)
readx(w,word)
Procedure ReadMem(*handle.filehandle,*mem,len)
Protected max
max=*handle\size-*handle\pos
If len>=max : len=max : EndIf
CopyMemory(*handle+OffsetOf(filehandle\Dat)+*handle\pos,*mem,len)
*handle\pos+len
ProcedureReturn len
EndProcedure
Procedure Image(id,file.s)
Protected *mem
Protected ret
IfSet (*mem,_LoadMem(file))
ret=CatchImage(id,*mem,size(file))
FreeMemory(*mem)
EndIf
ProcedureReturn ret
EndProcedure
Procedure JSON(id,file.s,flag=0)
Protected *mem
Protected ret
IfSet( *mem,_LoadMem(file))
If flag=0
ret=CatchJSON(id,*mem,size(file))
Else
ret=CatchJSON(id,*mem,size(file),flag)
EndIf
FreeMemory(*mem)
EndIf
ProcedureReturn ret
EndProcedure
Procedure Music(id,file.s)
Protected *mem
Protected ret
IfSet(*mem,_LoadMem(file))
ret=CatchMusic(id,*mem,size(file))
FreeMemory(*mem)
EndIf
ProcedureReturn ret
EndProcedure
Procedure Sound(id,file.s,flag=0)
Protected *mem
Protected ret
IfSet (*mem,_LoadMem(file))
If flag=0
ret=CatchSound(id,*mem,size(file))
Else
ret=CatchSound(id,*mem,size(file),flag)
EndIf
FreeMemory(*mem)
EndIf
ProcedureReturn ret
EndProcedure
Procedure Sprite(id,file.s,flag=0)
Protected *mem
Protected ret
IfSet (*mem,_LoadMem(file))
If flag=0
ret=CatchSprite(id,*mem)
Else
ret=CatchSprite(id,*mem,flag)
EndIf
FreeMemory(*mem)
EndIf
ProcedureReturn ret
EndProcedure
Structure pref_key
Map key.string()
EndStructure
Structure pref
valid.i
Map group.pref_key()
EndStructure
Procedure OpenPref(file.s)
Protected *handle.pref
Protected in
Protected ok=#False
Protected str.s
Protected group.s,key.s,value.s
Protected a
IfSet (*handle,AllocateStructure(pref))
IfSet(in,Open(file))
While Not eob(in)
str=Trim(reads(in))
Select Left(str,1)
Case ";";nothing
Case "[";group
group=Mid(str,2,Len(str)-2)
Default
a=FindString(str,"=")
If a
key=Trim(Left(str,a-1))
value=Trim(Right(str,Len(str)-a))
*handle\group(group)\key(key)\s=value
EndIf
EndSelect
Wend
ok=#True
close(in)
EndIf
EndIf
If ok=#False
If *handle
FreeStructure(*handle)
EndIf
*handle=0
Else
If FindMapElement(*handle\group(),"")
ResetMap(*handle\group()\key())
*handle\valid=#True
Else
*handle\valid=#False
EndIf
EndIf
ProcedureReturn *handle
EndProcedure
Procedure ClosePref(*handle.pref)
FreeStructure(*handle)
EndProcedure
Procedure ExaminePrefGroups(*handle.pref)
ResetMap(*handle\group())
*handle\valid=#False
ProcedureReturn #True
EndProcedure
Procedure ExaminePrefKeys(*handle.pref)
If *handle\valid
ResetMap(*handle\group()\key())
ProcedureReturn #True
Else
ProcedureReturn #False
EndIf
EndProcedure
Procedure NextPrefGroup(*handle.pref)
If NextMapElement(*handle\group())
*handle\valid=#True
Else
*handle\valid=#False
EndIf
ProcedureReturn *handle\valid
EndProcedure
Procedure NextPrefKey(*handle.pref)
If *handle\valid
ProcedureReturn NextMapElement(*handle\group()\key())
Else
ProcedureReturn #False
EndIf
EndProcedure
Procedure PrefGroup(*handle.pref,name.s)
If FindMapElement(*handle\group(),name)
ResetMap(*handle\group()\key())
*handle\valid=#True
Else
ResetMap(*handle\group())
*handle\valid=#False
EndIf
ProcedureReturn *handle\valid
EndProcedure
Procedure.s PrefGroupName(*handle.pref)
If *handle\valid
ProcedureReturn MapKey(*handle\group())
EndIf
ProcedureReturn ""
EndProcedure
Procedure.s PrefKeyName(*handle.pref)
If *handle\valid
ProcedureReturn MapKey(*handle\group()\key())
EndIf
ProcedureReturn ""
EndProcedure
Procedure.s PrefKeyValue(*handle.pref)
If *handle\valid
ProcedureReturn *handle\group()\key()\s
EndIf
ProcedureReturn""
EndProcedure
Macro ReadPrefX(d)
Procedure.d ReadPref#d(*handle.pref,key.s,DefaultValue.d)
If *handle\valid And FindMapElement(*handle\group()\key(),key)
ProcedureReturn Val#d(*handle\group()\key()\s)
EndIf
ProcedureReturn DefaultValue
EndProcedure
EndMacro
ReadPrefX(d)
ReadPrefX(f)
Macro ReadPrefY(q)
Procedure.q ReadPref#q(*handle.pref,key.s,DefaultValue.q)
If *handle\valid And FindMapElement(*handle\group()\key(),key)
ProcedureReturn Val(*handle\group()\key()\s)
EndIf
ProcedureReturn DefaultValue
EndProcedure
EndMacro
ReadPrefY(i)
ReadPrefY(l)
ReadPrefY(q)
Procedure.s ReadPrefS(*handle.pref,key.s,DefaultValue.s)
If *handle\valid And FindMapElement(*handle\group()\key(),key)
ProcedureReturn *handle\group()\key()\s
EndIf
ProcedureReturn DefaultValue
EndProcedure
EndModule
CompilerIf #PB_Compiler_IsMainFile
CompilerIf #bigfile_create
If BigFileCreate::Create("E:\purebasic\Temp\Bigdata.dat")
Debug BigFileCreate::AddDir("E:\purebasic\Temp\Bigdata")
Debug BigFileCreate::Close()
EndIf
If BigFileCreate::Create("E:\purebasic\Temp\Bigdata2.dat")
Debug BigFileCreate::AddDir("E:\purebasic\Temp\Bigdata2")
Debug BigFileCreate::AddFile("E:\purebasic\Temp\Data\PureBasic.prefs","hidden\very\very\hidden")
Debug BigFileCreate::close()
EndIf
CompilerEndIf
;using
OpenConsole()
PrintN("Load bigdata:")
PrintN(Str(BigFile::Load("E:\purebasic\Temp\Bigdata.dat")))
;PrintN(Str(BigFile::Load("E:\purebasic\Temp\Bigdata2.dat")))
PrintN(Str(BigFile::Catch(?internbig)))
bigfile::DebugList()
BigFile::SetPath("E:\PureBasic\Temp\Data\")
BigFile::DebugList()
Debug BigFile::SaveFile("german.html","E:\purebasic\Temp\German.html")
Debug BigFile::SaveFile("Data\1.wav", "e:\purebasic\temp\1.wav")
PrintN("Examine \")
Define handle
handle=BigFile::Examine("\")
If handle
While BigFile::NextEntry(handle)
If bigfile::EntryType(handle)=#PB_DirectoryEntry_Directory
PrintN("["+BigFile::EntryName(handle)+"]")
Else
PrintN(BigFile::EntryName(handle)+" "+bigfile::EntrySize(handle)+" "+bigfile::EntryIsBig(handle))
EndIf
Wend
bigfile::FinishExamine(handle)
EndIf
PrintN("-----")
PrintN("Examine Img")
handle=BigFile::Examine("Img")
If handle
While BigFile::NextEntry(handle)
If bigfile::EntryType(handle)=#PB_DirectoryEntry_Directory
PrintN("["+BigFile::EntryName(handle)+"]")
Else
PrintN(BigFile::EntryName(handle)+" "+bigfile::EntrySize(handle)+" "+bigfile::EntryIsBig(handle))
EndIf
Wend
bigfile::FinishExamine(handle)
EndIf
PrintN("File: controlmap.txt "+BigFile::Size("controlmap.txt"))
Define in
Define str.s
Define i=0,a
in=BigFile::open("German.html")
If in
str=bigfile::reads(in,#PB_File_IgnoreEOL,10)
PrintN( " limit:"+str)
While Not BigFile::eob(in)
str=BigFile::ReadS(in)
PrintN(" Line:"+str)
Debug " Line:"+str
i+1:If i=5
Break
EndIf
Wend
BigFile::close(in)
EndIf
Define in
in=BigFile::OpenPref("pref.pref")
Debug in
If in
Debug "PREF"
bigFile::ExaminePrefGroups(in)
While bigfile::NextPrefGroup(in)
PrintN("["+ bigfile::PrefGroupName(in) +"]")
Debug "["+ bigfile::PrefGroupName(in) +"]"
BigFile::ExaminePrefKeys(in)
While bigfile::NextPrefKey(in)
PrintN(" "+BigFile::PrefKeyName(in))
Debug" "+BigFile::PrefKeyName(in)
Wend
Wend
bigfile::PrefGroup(in,"common")
PrintN(BigFile::ReadPrefS(in,"aber","defstr"))
bigfile::PrefGroup(in,"")
PrintN(StrD(BigFile::ReadPrefd(in,"double",123),2))
PrintN(StrF(BigFile::ReadPreff(in,"float",123),2))
BigFile::ClosePref(in)
EndIf
UsePNGImageDecoder()
pic=BigFile::Image( #PB_Any, "data\freestyle.bmp")
If OpenWindow(0,0,0,200,200,"test",#PB_Window_SystemMenu)
ImageGadget(0,0,0,200,200,ImageID(pic))
While WaitWindowEvent()<>#PB_Event_CloseWindow
Wend
CloseWindow(0)
EndIf
bigfile::clear()
DataSection
internbig:
IncludeBinary "E:\purebasic\Temp\Bigdata2.dat"
EndDataSection
Input()
CompilerEndIf