Page 2 of 2

Posted: Mon Aug 23, 2004 11:23 am
by Froggerprogger
Hi once again!

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' ?

Re: CopyStructure()

Posted: Thu Aug 26, 2004 9:24 pm
by ROUMANET
Karbon wrote:I didn't realize until just now that there is no way to copy a structure (with it's data)..

It'd be nice to have a CopyStructure() and some way to compare whole structures (and the data within).
I would the same thing + WriteStructureFile() and ReadStructureFile() like in Rapidq Basic

Golfy

Posted: Thu Aug 26, 2004 9:51 pm
by Shannara
Hmm, but remember, the more strings you have in your structures the faster your memory willl leak :( Hopefully fred has this fixed in the next version of PB along with the network memory leak.

Posted: Mon Aug 30, 2004 12:36 am
by newbie
I didn't read carefully all your posts, but in a program I have done, I was storing strings into structures like this :

Code: Select all

Structure mystruct
    var1.b[20]
    var2.b[50]
    var3.l
EndStructure

struct.mystruct

string.s = "mystring here"
PokeS(@struct\var1[0], string)
string = "another text"
PokeS(@struct\var2[0], string)
struct\var3 = 99

Debug "struct before copymemory"
Debug "struct\var1[0] = " + PeekS(@struct\var1[0])
Debug "struct\var2[0] = " + PeekS(@struct\var2[0])
Debug "struct\var3 = " + Str(struct\var3)
Debug " "

anotherstruct.mystruct

CopyMemory(@struct, @anotherstruct, SizeOf(mystruct))

string = "toto"
PokeS(@struct\var1[0], string)
PokeS(@struct\var2[0], string)
struct\var3 = 32

Debug "struct modified purposefully after copymemory :"
Debug "struct\var1[0] = " + PeekS(@struct\var1[0])
Debug "struct\var2[0] = " + PeekS(@struct\var2[0])
Debug "struct\var3 = "    + Str(struct\var3)
Debug " "

Debug "anotherstruct after copymemory (not altered): "
Debug "anotherstruct\var1[0] = " + PeekS(@anotherstruct\var1[0])
Debug "anotherstruct\var2[0] = " + PeekS(@anotherstruct\var2[0])
Debug "anotherstruct\var3 = "    + Str(anotherstruct\var3)

Actually this seems to work.

Posted: Mon Aug 30, 2004 9:44 am
by Froggerprogger
@newbie
Yes, but than you are limited to a fixed string-size.
And you always do have to use PokeS (and PeekS to use the string-functions, because there are no Stringpointers e.g. *bla.s = @struct\var1[0]).

Posted: Mon Aug 30, 2004 11:19 am
by newbie
I am not limited, I just size my structure with the max size of the string (e.g 256 for a file name).
The pb is that you waste few bytes (those unused) and that a dedicated CopyStructure would allow us to declare string instead of bytes, but as I said,
it works, take it as a workaround ;)