Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
121 lines (101 sloc) 3.42 KB
'Here's the first example type. It's got 4 fields - an int, a float, a string, and a pointer to an object of another type
Type mytype1
Field anint%,afloat#,astring$
Field anotherobject:mytype2
Method save(f:TStream)
'Call this method to save the object to a given filestream, which must be writeable
'Write the int, float and string to the file, they're easy to do
WriteInt f,anint
WriteFloat f,afloat
WriteLine f,astring 'watch out, it's got to be writeline so the string's terminated and it can be loaded back in properly
'For the pointer, we first need an int that shows whether it's null or not (you can't write 'Null' to a file!)
If anotherobject
WriteInt f,1
'As the pointer isn't null, call the object's save method
WriteInt f,0
End Method
Function load:mytype1(f:TStream)
'Call this function (in the form m:mytype1=mytype1.load() because the object doesn't
'exist yet) To load an Object from a filestream f, which must be readable.
m:mytype1=New mytype1 'Create a new blank object to fill in
'Load the easy bits in
hasanotherobject=Readint(f) 'This int will = 1 if there's the other object to load
If hasanotherobject
m.anotherobject=mytype2.load(f) 'Load in the other object
Return m
End Function
End Type
'This is the other demo type, just used to show how to save and load pointers to other objects
Type mytype2
Field anotherint%
Method save(f:TStream)
WriteInt f,anotherint
End Method
Function load:mytype2(f:TStream)
m:mytype2=New mytype2
Return m
End Function
End Type
'This function saves a list of mytype1 objects to the given filestream, which must be writeable
Function savelistofmytype1(list:TList,f:TStream)
For m:mytype1=EachIn list
End Function
'This function loads a list of mytype1 objects from the given filestream, which must be readable
Function loadlistofmytype1:TList(f:TStream)
list:TList=New TList
While Not Eof(f)
list.addlast m
Return list
End Function
'This function prints out the values of all the objects in the list,
'so you can compare the saved and loaded data to check they're the same.
Function printmytype1list(list:TList)
For m:mytype1=EachIn list
Print m.anint
Print m.afloat
Print m.astring
If m.anotherobject
Print "another object: "+String(m.anotherobject.anotherint)
End Function
'demo: Create a list, fill it with 10 objects, half of them with the anotherobject pointer
' non-null, save it, forget about it, then load it in again.
mylist:TList=New TList
For c=1 To 10
m:mytype1=New mytype1
If c Mod 2 = 0
m.anotherobject=New mytype2
mylist.addlast m
'Print created data
Print "///SAVING///"
f:TStream=OpenFile("save.txt",0,1) 'open save file, writeable
savelistofmytype1(mylist,f) 'save list of objects
CloseFile f 'close file
mylist=Null 'get rid of list
FlushMem 'make sure it's gone :)
Print "///LOADING///"
f:TStream=OpenFile("save.txt",1,0) 'open save file again, readable
mylist:TList=loadlistofmytype1(f) 'load list in again
CloseFile f
'Print loaded data