From: jerry@jvdsys.stuyts.nl
Subject: Re: Dynamic Array Sizing
Date: 1999/06/19
Date: 1999-06-19T00:00:00+00:00 [thread overview]
Message-ID: <FDKnBy.BF@stuyts.nl> (raw)
In-Reply-To: 376B1811.666F042@hotmail.com
Nick Wilson <snow_moose@hotmail.com> wrote:
: I have a file containing an variable amount of data that I want to read
: into an array. My problem is that if I use a dynamic array, eg
: type Array_Store is array (integer range <>) of boolean;
: I have to declare an actual array sometime before reading the values
: into the array, but don't know what size to make the array. I can do two
: passes of the file, one to see the size, then declare the array in a
: block and do another pass to read the values into it.
If you are using GNAT, you could do something like:
-- * notes: - assumes 1-bit booleans
-- - using streams is slower
with GNAT.OS_Lib; use GNAT.OS_Lib;
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Sequential_IO;
with Ada.Unchecked_Deallocation;
procedure Stuff is
Data_Size_Error : exception; -- no data in file
File_Name_Error : exception; -- file cannot be opened
type Data_Storage_Type is array (Positive range <>) of Boolean;
-- an array to store the data in
type Data_Access is access all Data_Storage_Type;
-- a pointer to the data
procedure Free is new
Ada.Unchecked_Deallocation (Data_Storage_Type, Data_Access);
-- deallocate a data storage array
package Boolean_File is new Ada.Sequential_IO (Boolean);
use Boolean_File;
-- package for using files of Booleans
Data : Data_Access;
Num_Elements : Positive;
File_Name : constant String := "test.dat";
-----------------------------------
-- Get number of data elements --
-----------------------------------
function Data_Elements (Path : String) return Positive is
Fd : File_Descriptor;
Size : Long_Integer;
C_Name : String := Path & ASCII.NUL;
begin
Fd := Open_Read (C_Name'Address, Binary);
if Fd = Invalid_FD then
raise File_Name_Error;
end if;
Size := File_Length (Fd);
Close (Fd);
if Size < 1 then
raise Data_Size_Error;
end if;
return Positive (Size);
end Data_Elements;
------------------------------------------------------------
-- Create data storage array and return a pointer to it --
------------------------------------------------------------
function Create_Data_Storage (Elements : Positive) return Data_Access is
Storage : Data_Access;
begin
if Elements < 1 then
raise Data_Size_Error;
end if;
Storage := new Data_Storage_Type (1 .. Elements);
return Storage;
end Create_Data_Storage;
---------------------------------------
-- Read data in data storage array --
---------------------------------------
procedure Load_Data (Path : in String; Data : in Data_Access) is
File : Boolean_File.File_Type;
begin
Open (File, In_File, Path);
for I in 1 .. Data.all'Length loop
Read (File, Data(I));
end loop;
Close (File);
end Load_Data;
begin
Num_Elements := Data_Elements (File_Name);
Data := Create_Data_Storage (Num_Elements);
Load_Data (File_Name, Data);
Put_Line ("Loaded" & Data.all'Length'Img & " Booleans from " & File_Name);
Free (Data);
end Stuff;
--
-- Jerry van Dijk | Leiden, Holland
-- Team Ada | jdijk@acm.org
-- see http://stad.dsl.nl/~jvandyk
next prev parent reply other threads:[~1999-06-19 0:00 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
1999-06-19 0:00 Dynamic Array Sizing Nick Wilson
1999-06-19 0:00 ` Matthew Heaney
1999-06-20 0:00 ` Nick Wilson
1999-06-20 0:00 ` Matthew Heaney
1999-06-19 0:00 ` Tom Moran
1999-06-19 0:00 ` jerry [this message]
1999-06-19 0:00 ` jerry
1999-06-19 0:00 ` Matthew Heaney
1999-06-19 0:00 ` jerry
1999-06-20 0:00 ` Matthew Heaney
1999-06-21 0:00 ` jerry
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox