Read Structure from Data Section

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
c4s
Addict
Addict
Posts: 1981
Joined: Thu Nov 01, 2007 5:37 pm
Location: Germany

Read Structure from Data Section

Post by c4s »

If we want to read a structure from a data section, we currently have to do something like this:

Code: Select all

Structure MyStruc
	String.s
	Integer.i
	Float.f
EndStructure

DataSection
	MyData:
	Data.i 2
	Data.s "Text 1" : Data.i 12 : Data.f 3.4
	Data.s "Text 2" : Data.i 56 : Data.f 7.8
EndDataSection


Define i, iMax, NewList Xs.MyStruc()

Restore MyData
Read.i iMax
For i = 1 To iMax
	AddElement(Xs())
	Read.s Xs()\String
	Read.i Xs()\Integer
	Read.f Xs()\Float
Next
This can obviously get very tiresome for larger structures. So how about allowing the following more intuitive way:

Code: Select all

Structure MyStruc
	String.s
	Integer.i
	Float.f
EndStructure

DataSection
	MyData:
	Data.i 2
	Data.MyStruc "Text 1", 12, 3.4  ; If absolutely necessary the default way of storing would be ok too (see first example)
	Data.MyStruc "Text 2", 56, 7.8
EndDataSection


Define i, iMax, NewList Xs.MyStruc()

Restore MyData
Read.i iMax
For i = 1 To iMax
	AddElement(Xs())
	Read.MyStruc Xs()  ; Here is the magic!
Next
...I figured that it probably wouldn't work for "complex structures" (those including lists). So just allow it for structures of basic types. I'd say it would still be a great improvement.
If any of you native English speakers have any suggestions for the above text, please let me know (via PM). Thanks!
User avatar
Tenaja
Addict
Addict
Posts: 1959
Joined: Tue Nov 09, 2010 10:15 pm

Re: Read Structure from Data Section

Post by Tenaja »

+1, it would be handy to have this natively.


In the meantime, I have written a macro so the actual pseudo-datasection is cleaner:

Code: Select all

DataStruct("TextHere",		64)
The macro just creates two DataSections, but unfortunately, you still have to create a loop to read them.
User avatar
Tenaja
Addict
Addict
Posts: 1959
Joined: Tue Nov 09, 2010 10:15 pm

Re: Read Structure from Data Section

Post by Tenaja »

Oh, yeah...already requested, with complete workarounds:
http://purebasic.fr/english/viewtopic.p ... 66#p411422
(Using the last example eliminates the requirement for the loop.)
c4s
Addict
Addict
Posts: 1981
Joined: Thu Nov 01, 2007 5:37 pm
Location: Germany

Re: Read Structure from Data Section

Post by c4s »

Thanks for pointing me to that thread. It's interesting to see that KJ67 had the exact same idea. And you're right, bosker's workaround is quite clever. However, I think we all agree that a native solution would be even better... ;)
If any of you native English speakers have any suggestions for the above text, please let me know (via PM). Thanks!
Maitre_Kanter
User
User
Posts: 84
Joined: Mon Sep 06, 2010 3:05 pm

Re: Read Structure from Data Section

Post by Maitre_Kanter »

Use the following code @"string" instead of "string".

Code: Select all

Structure MyStruc
  String.s
  Integer.i
  Float.f
EndStructure

DataSection
  MyDataSize:
  Data.i 2
  MyDataStart:
  Data.i @"Text 1" : Data.i 12 : Data.f 3.4
  Data.i @"Text 2" : Data.i 56 : Data.f 7.8
EndDataSection

Restore MyDataSize
Read.i iMax
For i = 0 To iMax - 1
  AddElement(Xs2())
  CopyStructure( ?MyDataStart + SizeOf(MyStruc) * i , @Xs2() , MyStruc )
  Debug Xs2()\String
  Debug Xs2()\Integer
  Debug Xs2()\Float 
Next
Post Reply