SaveStructure(), LoadStructure(), CatchStructure()

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
Seymour Clufley
Addict
Addict
Posts: 1264
Joined: Wed Feb 28, 2007 9:13 am
Location: London

SaveStructure(), LoadStructure(), CatchStructure()

Post by Seymour Clufley »

Code: Select all

SaveStructure(@st,StructureType,filename$)
LoadStructure(@st,StructureType,filename$)
CatchStructure(@st,StructureType,*mem)
I realise this would be a lot of work - need to accommodate maps, lists, arrays, static arrays etc. - but it would be very useful indeed!

It would require delimiting many different things - where an array begins, where each item in the array ends etc. Perhaps some kind of file structure could be made for handling PB structures, maybe XML-based.

Of course we all write our own code for filling a structure from a file, but a universal function to do it for any structure type would cut a lot of code-writing for users. Universal functions like this are the kind of thing us users can't do! :)

If support for multidimensional arrays is going to be added in future, it may be best to wait until then.
JACK WEBB: "Coding in C is like sculpting a statue using only sandpaper. You can do it, but the result wouldn't be any better. So why bother? Just use the right tools and get the job done."
Justin
Addict
Addict
Posts: 948
Joined: Sat Apr 26, 2003 2:49 pm

Re: SaveStructure(), LoadStructure(), CatchStructure()

Post by Justin »

I use these functions to save small structures in xml files, mostly settings files. It needs some more error checking.

Code: Select all

procedure.s peekHEX(mem.i, bytes.i)
   define.s st, h
   define.i x
   define.b vs
   define.w vu
   
   for x=0 to bytes-1
      vs.b=peekb(mem+x)
      vu.w = vs & $FF
      h=hex(vu)
      if len(h)=1 : h="0" + h : endif
      st=st + h
   next
   
   procedurereturn st
endprocedure

procedure ReadHEX(hexSt.s, *mem.BYTE, memSize.i)
	define.i pos, bRead
	define.s sbyte
	
	bRead = 0
	pos=1
	sbyte = mid(hexSt, pos, 2)
	while sbyte and bRead<=memSize
		*mem\b = val("$" + sbyte)
	
		pos + 2
		*mem + sizeof(BYTE)
		sbyte = mid(hexSt, pos, 2)
		bRead + 1
	wend
endprocedure 

define.POINT pt, pt2
define.s txt

pt\x = 10
pt\y = 20

txt = peekHEX(@pt, sizeof(POINT))
debug txt

ReadHEX(txt, @pt2, sizeof(POINT))
debug pt2\x
debug pt2\y
User avatar
STARGÅTE
Addict
Addict
Posts: 2227
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

Re: SaveStructure(), LoadStructure(), CatchStructure()

Post by STARGÅTE »

SaveStructure(), LoadStructure(), CatchStructure() ar not good !

Its better to have:
WriteStructure(FileNumber, *Buffer, StructureName)
ReadStructure(FileNumber, *Buffer, StructureName)


i have same Includes for this one, but a nativ function in PB is verry good :)
PB 6.01 ― Win 10, 21H2 ― Ryzen 9 3900X, 32 GB ― NVIDIA GeForce RTX 3080 ― Vivaldi 6.0 ― www.unionbytes.de
Lizard - Script language for symbolic calculations and moreTypeface - Sprite-based font include/module
Seymour Clufley
Addict
Addict
Posts: 1264
Joined: Wed Feb 28, 2007 9:13 am
Location: London

Re: SaveStructure(), LoadStructure(), CatchStructure()

Post by Seymour Clufley »

STARGÅTE wrote:Its better to have:
WriteStructure(FileNumber, *Buffer, StructureName)
ReadStructure(FileNumber, *Buffer, StructureName)
File numbers instead of names? But how could it be safely read back in, then, if you had saved other data in the file? Save/LoadImage couldn't work like that.
JACK WEBB: "Coding in C is like sculpting a statue using only sandpaper. You can do it, but the result wouldn't be any better. So why bother? Just use the right tools and get the job done."
Post Reply