Forget about my above solution, it's too complicated and not various enough.
Here's a way from 'NicTheQuick' from the German board:
Code: Select all
Structure Strukt
Byte.b
Word.w
Long1.l
Long2.l
String1.s
Float.f
String2.s
EndStructure
Global Struc_Strukt.s
Struc_Strukt = "bwllsfs" ; This structure's types in a string
Structure AllTypes
StructureUnion
b.b
w.w
l.l
f.f
s.s
EndStructureUnion
EndStructure
Procedure CopyStructure(*Var1.AllTypes, *Var2.AllTypes, *Struc.BYTE)
Protected Length.l
While *Struc\b
Select *Struc\b
Case 'b' : *Var2\b = *Var1\b : *Var1 + 1 : *Var2 + 1
Case 'w' : *Var2\w = *Var1\w : *Var1 + 2 : *Var2 + 2
Case 'l' : *Var2\l = *Var2\l : *Var1 + 4 : *Var2 + 4
Case 'f' : *Var2\f = *Var1\f : *Var1 + 4 : *Var2 + 4
Case 's' : *Var2\s = *Var1\s : *Var1 + 4 : *Var2 + 4
EndSelect
*Struc + 1
Wend
EndProcedure
Similar to this a PB-built-in CopyStructure could work:
CopyStructure(*struct1.myStructX, flags, [*struct2.myStructY])
When flags are 0 then only the memory is copied similar to CopyMemory.
With flag "#PB_CopyStrings" set, PB looks at compile-time into the structure-definition of the structure (needn't be the same for both - if the optional aim is given just ignore it's structure) and duplicates all strings (even those of contained structures - which is no problem also).
If no optional aim is given return pointer to a new copy of *struct1.
Of course this wouldn't work for WinApi-Structures containing stringpointers declared as '.l', but who cares about that as long as it works for all strings declared as '.s' ?