News:

PlayBASIC2DLL V0.99 Revision I Commercial Edition released! - Convert PlayBASIC programs to super fast Machine Code. 

Main Menu

Load/Save Saving String Data Via Banks

Started by kevin, June 04, 2007, 02:00:50 AM

Previous topic - Next topic

kevin

    This example loads/write string data into a stack like cache (for the purposes of the demo)  and includes load and Save array function.  The function spool data into an bank for batch read/writing.    

   Could easily be expanded to support encryption/decryption of  load/saved bank.   But that's left as an exercise for the programmer.


PlayBASIC Code: [Select]
;  Filename of the the temp file will be written (and later deleted)
Filename$=Currentdir$()+"TestSave.txt"


; Declare the stack array so we have something to
Dim Stack$(0)


; Push a bunch of strings to the the STACK$() array
AddStr("Hello World")
AddStr("Some Numbers")
for lp=0 to 10
AddStr(STR$(rnd(1000)))
next
AddStr("Yeah Yeah Yeah")
AddStr("This is another string")
AddStr("Could this be the end of the strings ?")



; Display the contents of the Stack$() array
Print "Stack$() Contents"
ShowArray( Stack$())


; Save this String held in the String Array
SaveStringArray(Stack$(),Filename$)



; Declare Another String Array
Dim MyArray$(0)


; Load the previously Saved data back into this second array
LoadStringArray(MyArray$(),Filename$)


; Show the Newly loaded arrays contents
Print "MyArray$() Contents"
ShowArray( MyArray$())


; ditch the temp file that this demo creates
if fileexist(filename$) then deletefile filename$


Sync
waitkey



; *=--------------------------------------------------------------------=*
; >> Save String Array <<
; *=--------------------------------------------------------------------=*
;
; This function dumps the contents string array into a bank then writes
; them to a file
;
; *=--------------------------------------------------------------------=*


Function SaveStringArray(me$(),Filename$)

; Calc total size
Items=getarrayelements(me$(),1)
For lp=0 to Items
Size=Len(Me$(lp))+1
TotalSize=TotalSize+Size
Next


; Add Header Size to TotalSize
TotalSize=TotalSize+16

; Dump to Memory
Bank=NewBank(Totalsize)
Address=GetBankPtr(Bank)

; Write the Header
Pokeint Address,items ; Number of items
Address=Address+4

; Dump Them as a sseries of null terms strings
For lp=0 to Items
Size=Len(me$(lp))+1
PokeString Address,Me$(lp),0
Address=Address+Size
next

; Save bank
f=getfreefile()
if f
p=GetBankPtr(Bank)
WriteFile Filename$, f
writememory f,p,p+TotalSize
Closefile f
endif

; Clean up
Deletebank Bank

EndFunction




; *=--------------------------------------------------------------------=*
; >> Load String Array <<
; *=--------------------------------------------------------------------=*
;
; This function reads data file and decodes it to a string array
;
; *=--------------------------------------------------------------------=*



Function LoadStringArray(me$(),Filename$)

Size=Filesize(Filename$)
if Size>0
Bank=NewBank(size+4)
Address=GetBankPtr(Bank)

f=getfreefile()
ReadFile Filename$,f
ReadMemory f,Address,Size
Closefile f

; Grab Strings
StringCount=PeekInt(Address)
Address=Address+4

Dim me$(StringCount)
For lp=0 to StringCount
me$(lp)=PeekString(Address,0)
Address=Address+Len(me$(lp))+1
next

deletebank bank
endif

EndFunction




Login required to view complete source code




Related Examples

     Load TextFiles To String Array
     Dictionary Searching


 Help Files:

     File Commands In PlayBASIC
     Memory Management / Bank Commands In PlayBASIC