comp.lang.ada
 help / color / mirror / Atom feed
* Concatenating files
@ 2007-09-19 21:33 mhamel_98
  2007-09-20  4:28 ` Jeffrey R. Carter
                   ` (2 more replies)
  0 siblings, 3 replies; 8+ messages in thread
From: mhamel_98 @ 2007-09-19 21:33 UTC (permalink / raw)


Hello c.l.a.  I have a storage system for a program of mine that uses
two files for each object - a text header file via text_io and a data
file via sequential_io.  I am wondering if there is an elegant way to
put the contents of these two files into one.  The textual header bit
is fixed size/length.  Any ideas?  ObjectAda 95 on NT.




^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Concatenating files
  2007-09-19 21:33 Concatenating files mhamel_98
@ 2007-09-20  4:28 ` Jeffrey R. Carter
  2007-09-20 11:41 ` Jacob Sparre Andersen
  2007-09-20 13:11 ` gautier_niouzes
  2 siblings, 0 replies; 8+ messages in thread
From: Jeffrey R. Carter @ 2007-09-20  4:28 UTC (permalink / raw)


mhamel_98@yahoo.com wrote:
> Hello c.l.a.  I have a storage system for a program of mine that uses
> two files for each object - a text header file via text_io and a data
> file via sequential_io.  I am wondering if there is an elegant way to
> put the contents of these two files into one.  The textual header bit
> is fixed size/length.  Any ideas?  ObjectAda 95 on NT.

You could use streams to first write the String(s) of the header, then 
the data. Or you could create a composite object with the fixed-length 
String(s) of the header and the data all in one, and write an object of 
that type with Sequential_IO.

-- 
Jeff Carter
"Hello! Smelly English K...niggets."
Monty Python & the Holy Grail
08



^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Concatenating files
  2007-09-19 21:33 Concatenating files mhamel_98
  2007-09-20  4:28 ` Jeffrey R. Carter
@ 2007-09-20 11:41 ` Jacob Sparre Andersen
  2007-09-20 13:11 ` gautier_niouzes
  2 siblings, 0 replies; 8+ messages in thread
From: Jacob Sparre Andersen @ 2007-09-20 11:41 UTC (permalink / raw)


mhamel_98@yahoo.com writes:

> Hello c.l.a.  I have a storage system for a program of mine that
> uses two files for each object - a text header file via text_io and
> a data file via sequential_io.  I am wondering if there is an
> elegant way to put the contents of these two files into one.  The
> textual header bit is fixed size/length.  Any ideas?  ObjectAda 95
> on NT.

If ObjectAda 95 on NT has something like POSIX.Memory_Mapping, then
you could do it like this:

   Data_Length : constant Natural
     := (File_Size (File) - Fixed_Header_Length) * 8 / Element_Type'Size;

   type File_Content is
      record
         Header : String (1 .. Fixed_Header_Length);
         Data   : array (1 .. Data_Length) of Element_Type;
      end record;
   
   [...]

   Address := Map_Memory (Length     => File_Size (File),
                          Protection => Allow_Read + Allow_Write,
                          Mapping    => Map_Shared,
                          File       => File,
                          Offset     => 0);

   [...]

   Content : File_Content;
   for Content'Address use Address;

   --  Here you can access Content.Header and Content.Data just like
   --  any other object in Ada.

The tricky part in this is when you want to append to the file.  For
now I will leave that as an exercise for the reader.

Greetings,

Jacob (a big fan of POSIX.Memory_Mapping ;-)
-- 
"Banning open source would have immediate, broad, and
 strongly negative impacts on the ability of many sensitive
 and security-focused DOD groups to protect themselves
 against cyberattacks"                        -- Mitre Corp.




^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Concatenating files
  2007-09-19 21:33 Concatenating files mhamel_98
  2007-09-20  4:28 ` Jeffrey R. Carter
  2007-09-20 11:41 ` Jacob Sparre Andersen
@ 2007-09-20 13:11 ` gautier_niouzes
  2007-09-22 17:38   ` mhamel_98
  2 siblings, 1 reply; 8+ messages in thread
From: gautier_niouzes @ 2007-09-20 13:11 UTC (permalink / raw)


On 19 Sep., 23:33, mhamel...@yahoo.com wrote:
> Hello c.l.a.  I have a storage system for a program of mine that uses
> two files for each object - a text header file via text_io and a data
> file via sequential_io.  I am wondering if there is an elegant way to
> put the contents of these two files into one.  The textual header bit
> is fixed size/length.  Any ideas?  ObjectAda 95 on NT.

Do you also need to keep both files separately ?
If no, you can use the Append_File mode when writing the binary data
immediately at the end of the first one.
If yes, I would prefer a straightforward way which doesn't use much
memory even if the files are big: use Sequential_IO to read Size(T)
bytes
of the text file T and write into mixed file M, then read Size(D)
bytes
of the data file D and write them into M.
It's portable, endian-proof, etc. ...
______________________________________________________________
Gautier         -- http://www.mysunrise.ch/users/gdm/index.htm
Ada programming -- http://www.mysunrise.ch/users/gdm/gsoft.htm

NB: For a direct answer, e-mail address on the Web site!




^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Concatenating files
  2007-09-20 13:11 ` gautier_niouzes
@ 2007-09-22 17:38   ` mhamel_98
  2007-09-22 20:07     ` Jeffrey R. Carter
  0 siblings, 1 reply; 8+ messages in thread
From: mhamel_98 @ 2007-09-22 17:38 UTC (permalink / raw)


Thanks for the replies!  How about this, instantiating Sequential_Io
with a variant record?  The header data can be put into a record
easily enough, so I would write the first sequential record as a
header type with the following as the original data records.  The
problem is, some of the header data will not be known until all the
data records have been written.  Is there a way to edit, in place, the
first record of a sequential file?  Thanks much again c.l.a!

On Sep 20, 6:11 am, gautier_niou...@hotmail.com wrote:
> On 19 Sep., 23:33, mhamel...@yahoo.com wrote:
>
> > Hello c.l.a.  I have a storage system for a program of mine that uses
> > two files for each object - a text header file via text_io and a data
> > file via sequential_io.  I am wondering if there is an elegant way to
> > put the contents of these two files into one.  The textual header bit
> > is fixed size/length.  Any ideas?  ObjectAda 95 on NT.
>
> Do you also need to keep both files separately ?
> If no, you can use the Append_File mode when writing the binary data
> immediately at the end of the first one.
> If yes, I would prefer a straightforward way which doesn't use much
> memory even if the files are big: use Sequential_IO to read Size(T)
> bytes
> of the text file T and write into mixed file M, then read Size(D)
> bytes
> of the data file D and write them into M.
> It's portable, endian-proof, etc. ...
> ______________________________________________________________
> Gautier         --http://www.mysunrise.ch/users/gdm/index.htm
> Ada programming --http://www.mysunrise.ch/users/gdm/gsoft.htm
>
> NB: For a direct answer, e-mail address on the Web site!





^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Concatenating files
  2007-09-22 17:38   ` mhamel_98
@ 2007-09-22 20:07     ` Jeffrey R. Carter
  2007-09-22 22:51       ` mhamel_98
  0 siblings, 1 reply; 8+ messages in thread
From: Jeffrey R. Carter @ 2007-09-22 20:07 UTC (permalink / raw)


mhamel_98@yahoo.com wrote:
> Thanks for the replies!  How about this, instantiating Sequential_Io
> with a variant record?  The header data can be put into a record
> easily enough, so I would write the first sequential record as a
> header type with the following as the original data records.  The
> problem is, some of the header data will not be known until all the
> data records have been written.  Is there a way to edit, in place, the
> first record of a sequential file?  Thanks much again c.l.a!

Sounds as if Ada.Direct_IO would be better.

-- 
Jeff Carter
"Ada has made you lazy and careless. You can write programs in C that
are just as safe by the simple application of super-human diligence."
E. Robert Tisdale
72



^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Concatenating files
  2007-09-22 20:07     ` Jeffrey R. Carter
@ 2007-09-22 22:51       ` mhamel_98
  2007-09-23  1:45         ` Jeffrey R. Carter
  0 siblings, 1 reply; 8+ messages in thread
From: mhamel_98 @ 2007-09-22 22:51 UTC (permalink / raw)



Hi Jeff, I don't have any resources available in front of me, but I
always thought Direct_Io was essentially the same as Sequential_Io
save for the additional ability of retrieving, or reading, the "nth"
record without having to start from the beginning of the file.  Does
Direct_Io allow editing, or writing, of said "nth" record without
disrupting records written before or after?  Or, does Direct_Io allow
writing to the "nth" place? in which case I would start writing my
data starting at 2, for instance, then when complete, write the first,
or header record.

On Sep 22, 1:07 pm, "Jeffrey R. Carter"
<spam.jrcarter....@acm.nospam.org> wrote:
> mhamel...@yahoo.com wrote:
> > Thanks for the replies!  How about this, instantiating Sequential_Io
> > with a variant record?  The header data can be put into a record
> > easily enough, so I would write the first sequential record as a
> > header type with the following as the original data records.  The
> > problem is, some of the header data will not be known until all the
> > data records have been written.  Is there a way to edit, in place, the
> > first record of a sequential file?  Thanks much again c.l.a!
>
> Sounds as if Ada.Direct_IO would be better.
>
> --
> Jeff Carter
> "Ada has made you lazy and careless. You can write programs in C that
> are just as safe by the simple application of super-human diligence."
> E. Robert Tisdale
> 72





^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Concatenating files
  2007-09-22 22:51       ` mhamel_98
@ 2007-09-23  1:45         ` Jeffrey R. Carter
  0 siblings, 0 replies; 8+ messages in thread
From: Jeffrey R. Carter @ 2007-09-23  1:45 UTC (permalink / raw)


mhamel_98@yahoo.com wrote:
> Hi Jeff, I don't have any resources available in front of me, but I
> always thought Direct_Io was essentially the same as Sequential_Io
> save for the additional ability of retrieving, or reading, the "nth"
> record without having to start from the beginning of the file.  Does
> Direct_Io allow editing, or writing, of said "nth" record without
> disrupting records written before or after?  Or, does Direct_Io allow
> writing to the "nth" place? in which case I would start writing my
> data starting at 2, for instance, then when complete, write the first,
> or header record.

Ada.Direct_IO is specified in ARM A.8.4:

http://www.adaic.org/standards/05rm/html/RM-A-8-4.html

It includes the operations

procedure Read (File : in     File_Type;
                 Item :    out Element_Type;
                 From : in     Positive_Count);
procedure Write(File : in File_Type;
                 Item : in  Element_Type;
                 To   : in Positive_Count);

You may read and write from any position in the file.

-- 
Jeff Carter
"Ada has made you lazy and careless. You can write programs in C that
are just as safe by the simple application of super-human diligence."
E. Robert Tisdale
72



^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2007-09-23  1:45 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-09-19 21:33 Concatenating files mhamel_98
2007-09-20  4:28 ` Jeffrey R. Carter
2007-09-20 11:41 ` Jacob Sparre Andersen
2007-09-20 13:11 ` gautier_niouzes
2007-09-22 17:38   ` mhamel_98
2007-09-22 20:07     ` Jeffrey R. Carter
2007-09-22 22:51       ` mhamel_98
2007-09-23  1:45         ` Jeffrey R. Carter

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox