PDA

View Full Version : Reading file from PChar...



M109uk
24-01-2005, 11:54 PM
Hi all,

again another question,
Is it possable to read file information from a file stored in a PChar?

e.g.

type
TFile = Record
Data: PChar;
// other information
end;

TPak = class
files: Array of Tfile;
// other information
end;

procedure TPak.LoadPakFile
begin
// read pak file
For i := 0 To files count -1 Do
Begin
// read file information
stream.read(files[i].data^, files[i].size);
End;
// other information
close stream
end;


I want to load the pak file at the beginning of the program and keep it in memory, but i have no idea how i can read the file data from the pak file item!

cairnswm
25-01-2005, 04:42 AM
A PChar can act as a buffer which you could write to a memeory stream. Then you could read the file from the memory stream into whatever structure you want.

Another way is to

Type
TCharArray : Array[1..65000] of Char;

...

Data := TCharArray(PChar);

{MSX}
25-01-2005, 06:48 AM
Hi all,

again another question,
Is it possable to read file information from a file stored in a PChar?

I want to load the pak file at the beginning of the program and keep it in memory, but i have no idea how i can read the file data from the pak file item!

I think you'll better drop the pchar and use TMemoryStream instead, that is more Pascal and OOP :P
Memory streams are streams stored in ram, wh ere you can define the size. A buffer, in other word :P
You can create a memory stream and load the file there. Then you can pass the stream to all functions like "loadfromstream". Also, you can use the read methods to read the data, and you can still access to the pointer with the appropriate field (if i remember well it's MemoryStream.memory )

Bye!

TheLion
25-01-2005, 08:12 AM
Personally I prefer the PChar method over the TMemoryStream method, the main reason is that the TMemoryStream comes with a lot of overhead and in the end the TMemoryStream is using a Pointer in almost the same way you would be using the PChar anyways. The advantage of the TMemoryStream is that it comes with build in features to read things from a pointer other than (and larger than) characters, but mimicing that behaviour with a PChar or a Pointer is not hard either! ;)

However MSX is right, using TMemoryStream is more the Delphi way, I'm just old-fashioned and like to do everything on my own! ;) I'm one of those Delphi developers that only use objects where needed, and functions and where he can! ;)

So in the end... I guess it's a style thing! ;)

M109uk
25-01-2005, 05:55 PM
Thanx all for your suggestions,
i dont really want to use any more memory than needed because the main pak file will be very large, at the moment it is 40mb+ this will contain textures, models, actors, world data, and other data files.

Is there any point of keeping the data stored in memory or would it be more memory friendly to just load the data straight from the pak stream?

If i keep the PChar data how will i be able to load it in to a structure?

thanx :)

Clootie
25-01-2005, 09:49 PM
Another option is to use memory mapped files.
Anyway you can always look how pak file support was realized in Delphi remake of Quake II: http://www.sulaco.co.za/quake2/ (even in pascal) :)