Richard R wrote:
> Hello, I want to write a complex structure to a file. I am
> wondering how I should go about it. I am new to this sort of
> thing, and have found out just writing the pointer doesnt
> work. Do i need to write out each element in the structure to
> file or is there some way I can write the pointer so I can
> also load it into the structure.
In general you can't expect the value of a pointer written to a
file to have any validity as a pointer if read back. If you're
using linked or dynamic structures you need to recreate the
pointers upon reading the associated values back.
> TObjArray = array[0..1] of Pointer;
> PObjArray = ^TObjArray;
>
> TSelType = (
> stNone,
> stVerts,
> stFaces,
> stEdges
> );
>
> TIntegerArray = array[0..1] of Integer;
> PIntegerArray = ^TIntegerArray;
>
> TSelData = record
> seltype: TSelType;
> case TSelType of
> stVerts: (vxsIdxArray: PIntegerArray;
> nbrSelVxs: integer);
> stFaces: (faceIdxArray: PIntegerArray;
> nbrSelFaces: integer);
> stEdges: ( edgeArray: PIntegerArray;
> nbrSelEdges: integer );
> end;
>
> TSelDataArray = array[0..1] of TSelData;
> PSelDataArray = ^TSelDataArray;
>
> TSPEObjs = record
> nbrPEObjs: integer;
> peObjs: PObjArray;
> SelData: PSelDataArray;
> end;
> PEObjs = ^TSPEObjs;
>
Looking over all this, I'm guessing you're using the "array
[0..1]" notation to allocate arbitrary numbers of these types?
32-bit Delphi has a somewhat more elegant syntax for working with
dynamic arrays that may suit you better; they manintain the size
of the array for you.
Using these, you'd declare your types like this:
-------------------->8 cut here 8<--------------------
TSelType = (
stNone,
stVerts,
stFaces,
stEdges
);
TIntegerArray = array of Integer;
TSelData = record
seltype: TSelType;
case TSelType of
stVerts: (vxsIdxArray: TIntegerArray);
stFaces: (faceIdxArray: TIntegerArray);
stEdges: (edgeArray: TIntegerArray);
end;
TSelDataArray = array of TSelData;
TObjArray = array of Pointer;
TSPEObjs = record
nbrPEObjs: integer;
peObjs: TObjArray;
SelData: TSelDataArray;
end;
-------------------->8 cut here 8<--------------------
Now, when you start filling these arrays, you allocate space in
them using the SetLength() routine, e.g.:
var
MySelData : TSelData;
begin
MySelData.selType := stEdges;
SetLength(MySelData.edgeArray, 100);
...
You can grow or shrink the array using SetLength, previously
stored values in the elements are preserved. You index the array
using standard "[]" notation, and you don't have to disable range
checking for it to work.
Writing array values to a file is simply a matter of iterating
the array, and you have access to the Low() and High() routines
to determine the array's size:
for i:=Low(MySelData.edgeArray) to High(MySelData.edgeArray) do
begin
Write(MyFile, MySelData.edgeArray[i]);
...
> You can see how complex this is. I'm thinking I need to write
> a custom file type using writeinteger and so forth, or perhaps
> there is a quicker way? BTW I just want to write the PEObjs to
> file, which is what i'm working with. It works very well in my
> program to control user selections on geometry.
I think you're making it more complicated than it needs to be
actually.
Now, given what I've shown you, you'll still have to deal with
writing whatever the Pointers in your instances of PObjArray
point to.
How do you know that?
Stephen Posey
[EMAIL PROTECTED]
__________________________________________________
Delphi-Talk mailing list -> [email protected]
http://www.elists.org/mailman/listinfo/delphi-talk