comp.lang.ada
 help / color / mirror / Atom feed
* Using fwrite in Ada
@ 2005-08-10 17:01 Makhno
  2005-08-10 17:28 ` Georg Bauhaus
                   ` (2 more replies)
  0 siblings, 3 replies; 5+ messages in thread
From: Makhno @ 2005-08-10 17:01 UTC (permalink / raw)


Hello,
I want to read and write binary data from stdin/stdout, unfortunately Ada's
usual IO routines appear to be text only and (on Win32) convert code 10 to
codes 10 & 13.
To get round this I'm trying to use Interfaces.C_Streams, but I don't know
enough Ada to get them working.
How to I get a String into the fwrite function?
I tried

procedure MainCode is

 cptr : chars_ptr;
 Buffer : String (1..100);
 vptr : voids;

 begin

-- fill Buffer with binary data here

  cptr:=New_String(Buffer);
  vptr:=voids(cptr);

  fwrite(vptr,100,1,stdout);
 end;

But this won't compile, the compiler says that it cannot convert the
chars_ptr to voids. Coming from a C background, I don't understand why
something cannot be cast to void.

Does anybody have any idea as to how I get binary data onto stdout?

Thanks







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

* Re: Using fwrite in Ada
  2005-08-10 17:01 Using fwrite in Ada Makhno
@ 2005-08-10 17:28 ` Georg Bauhaus
  2005-08-10 18:05 ` Martin Krischik
  2005-08-11 17:01 ` tmoran
  2 siblings, 0 replies; 5+ messages in thread
From: Georg Bauhaus @ 2005-08-10 17:28 UTC (permalink / raw)


Makhno wrote:

> Does anybody have any idea as to how I get binary data onto stdout?

Use Streams and the 'Write and 'Read attributes of the type used
for binary data.

Here is a possible output part, it gets binary data onto stdout.
For the input part, use the 'read attribute where the text uses
the 'write attribute.


with Ada.Text_IO.Text_Streams;

procedure binio is

   use Ada.Text_IO;

   type Byte is mod 2**8;
   for Byte'size use 8;  -- just to make sure

   type Binary_Data is array(Natural range <>) of Byte;
     -- the type used for storing binary data internally

   stuff: Binary_Data(1..10);

   destination: Text_Streams.Stream_Access;

begin
   destination := Text_Streams.Stream (standard_output);
   Binary_Data'write(destination, stuff);
end binio;

If you just want to write integers, records (structs) etc.
to standard_output, use the 'Write attributes of the respective
types. That's what they are for. (No need to "cast" something.)



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

* Re: Using fwrite in Ada
  2005-08-10 17:01 Using fwrite in Ada Makhno
  2005-08-10 17:28 ` Georg Bauhaus
@ 2005-08-10 18:05 ` Martin Krischik
  2005-08-11 17:01 ` tmoran
  2 siblings, 0 replies; 5+ messages in thread
From: Martin Krischik @ 2005-08-10 18:05 UTC (permalink / raw)


Makhno wrote:

> Hello,
> I want to read and write binary data from stdin/stdout, unfortunately
> Ada's usual IO routines appear to be text only and (on Win32) convert code
> 10 to codes 10 & 13.

Not at all true:

http://en.wikibooks.org/wiki/Programming:Ada:InputOutput

Sadly the articles which describe how to attach any stream type to
stdin/stdout has not been written yet :-( .

> To get round this I'm trying to use Interfaces.C_Streams, but I don't know
> enough Ada to get them working.
> How to I get a String into the fwrite function?
> I tried
> 
> procedure MainCode is
> 
>  cptr : chars_ptr;
>  Buffer : String (1..100);

Are you sure you use the right String type?

http://en.wikibooks.org/wiki/Programming:Ada:Strings

>  vptr : voids;
> 
>  begin
> 
> -- fill Buffer with binary data here
> 
>   cptr:=New_String(Buffer);
>   vptr:=voids(cptr);
> 
>   fwrite(vptr,100,1,stdout);
>  end;
> 
> But this won't compile, the compiler says that it cannot convert the
> chars_ptr to voids. Coming from a C background, I don't understand why
> something cannot be cast to void.

Read:

http://en.wikibooks.org/wiki/Programming:Ada:Subtypes#Converting_data

and

http://en.wikibooks.org/wiki/Programming:Ada:Types:access
 
> Does anybody have any idea as to how I get binary data onto stdout?

If it is truly binary data of a fixed length of 100 bytes I would suggest
Direct I/O:

http://en.wikibooks.org/wiki/Programming:Ada:InputOutput#Direct_I.2FO

But you should really read the suggested web pages.
Martin

-- 
mailto://krischik@users.sourceforge.net
Ada programming at: http://ada.krischik.com



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

* Re: Using fwrite in Ada
  2005-08-10 17:01 Using fwrite in Ada Makhno
  2005-08-10 17:28 ` Georg Bauhaus
  2005-08-10 18:05 ` Martin Krischik
@ 2005-08-11 17:01 ` tmoran
  2005-08-11 21:59   ` Jeffrey Carter
  2 siblings, 1 reply; 5+ messages in thread
From: tmoran @ 2005-08-11 17:01 UTC (permalink / raw)


> I want to read and write binary data from stdin/stdout, unfortunately Ada's
> usual IO routines appear to be text only and (on Win32) convert code 10 to
> codes 10 & 13.
  Ada.Text_IO.Text_Streams addresses exactly that problem.

> To get round this I'm trying to use Interfaces.C_Streams, but I don't know
> How to I get a String into the fwrite function?
   Do you need to call "fwrite", or do you just need to write binary data
that can be read back by another Ada program?  In the latter case, 'write,
as has been suggested, will do the job.  The bit pattern that gets written
is implementation defined, unless you override that.  That can be handy.

>  cptr : chars_ptr;
>  Buffer : String (1..100);
>   cptr:=New_String(Buffer);
  implies that chars_ptr is an access to an unconstrained array.  So it
points to both the subscript range of the string and to the character data
in the string.  That is not the same thing as a C pointer to a byte (that
may happen to be the first byte in the string).

>   vptr:=voids(cptr);
>
>   fwrite(vptr,100,1,stdout);
> But this won't compile, the compiler says that it cannot convert the
> chars_ptr to voids. Coming from a C background, I don't understand why
> something cannot be cast to void.
   In Ada, type matters.  What would you expect to happen if you said
  cptr := chars_ptr(vptr);
How would the cptr find out the size of the string that held the byte
that vptr currently points to?

  Look further at Interfaces.  You seem to want to be able to call fwrite
with a String parameter and ignore its result:
   Tell the compiler:
   procedure fwrite(Source : String;
                   Size, Length : Natural;
                   Target : File_Ptr);
   pragma Import(fwrite, C);
then use it
   fwrite(Buffer, 1, Buffer'length, stdout);



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

* Re: Using fwrite in Ada
  2005-08-11 17:01 ` tmoran
@ 2005-08-11 21:59   ` Jeffrey Carter
  0 siblings, 0 replies; 5+ messages in thread
From: Jeffrey Carter @ 2005-08-11 21:59 UTC (permalink / raw)


tmoran@acm.org wrote:
> 
>> cptr : chars_ptr;
>> Buffer : String (1..100);
>>  cptr:=New_String(Buffer);
> 
>   implies that chars_ptr is an access to an unconstrained array.  So it
> points to both the subscript range of the string and to the character data
> in the string.  That is not the same thing as a C pointer to a byte (that
> may happen to be the first byte in the string).

No. From ARM B.3.1(22): 'The type chars_ptr is C-compatible and 
corresponds to the use of C's ``char *'' for a pointer to the first char 
in a char array terminated by nul.'

> 
>>  vptr:=voids(cptr);
>>
>>  fwrite(vptr,100,1,stdout);
>>But this won't compile, the compiler says that it cannot convert the
>>chars_ptr to voids. Coming from a C background, I don't understand why
>>something cannot be cast to void.
> 
>    In Ada, type matters.  What would you expect to happen if you said
>   cptr := chars_ptr(vptr);
> How would the cptr find out the size of the string that held the byte
> that vptr currently points to?

The problem is that Chars_Ptr is defined as

type chars_ptr is private;

Thus, it is not an access type, so there is no conversion to an access 
type defined for it.

All this is an elephant, however, since there are better ways to do 
binary output to standard output.

-- 
Jeffrey Carter
"Now go away or I shall taunt you a second time."
Monty Python and the Holy Grail
E-mail: jeffrey_r_carter-nr [commercial-at]
         raytheon [period | full stop] com



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

end of thread, other threads:[~2005-08-11 21:59 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-08-10 17:01 Using fwrite in Ada Makhno
2005-08-10 17:28 ` Georg Bauhaus
2005-08-10 18:05 ` Martin Krischik
2005-08-11 17:01 ` tmoran
2005-08-11 21:59   ` Jeffrey Carter

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