comp.lang.ada
 help / color / mirror / Atom feed
* Re: newbie problem
  1998-12-05  0:00 Graeme Wallace
                   ` (3 preceding siblings ...)
  1998-12-04  0:00 ` Gautier.DeMontmollin
@ 1998-12-04  0:00 ` David Botton
  1998-12-05  0:00 ` Matthew Heaney
  1998-12-07  0:00 ` Jeff Carter
  6 siblings, 0 replies; 31+ messages in thread
From: David Botton @ 1998-12-04  0:00 UTC (permalink / raw)


Take a look at the following URL:

http://www.cs.uofs.edu/~beidler/Ada/cgi/cgitoc-U.html#Ustrings

This is a package called UStrings that lets you use unbounded strings (what
you are looking for) in a very natural way like:

help_me_please : Unbounded_String;

Ustring.Get_Line(help_me_please);

The srouce code for the package is part of the cgi package at:

http://wuarchive.wustl.edu/languages/ada/swcomps/cgi/cgi.html

David Botton

The Ada Source Code Treasury
http://www.botton.com/ada

From: Graeme Wallace <Dizzy@interact.net.au>

>If I want to get user input for my program in the form of a  string of
>undefined length, how do I do so ?  If I set the string length to, say:
>
>help_me_please : String(1..20);
>
>I seem to be stuck with a string exactly that size.  What is the
>variable assignment which allows the actual length of the user input to
>determine the length of the string which the io system
>(ada.text_io.)gets ?
>
>G Wallace
>
>Thankyou.
>:-)
>







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

* Re: newbie problem
  1998-12-05  0:00 Graeme Wallace
@ 1998-12-04  0:00 ` Simon Bracken
  1998-12-04  0:00   ` Marin David Condic
  1998-12-04  0:00 ` Marin David Condic
                   ` (5 subsequent siblings)
  6 siblings, 1 reply; 31+ messages in thread
From: Simon Bracken @ 1998-12-04  0:00 UTC (permalink / raw)


Graeme Wallace <Dizzy@interact.net.au> wrote:
>If I want to get user input for my program in the form of a  string of
>undefined length, how do I do so ?  If I set the string length to, say:
>
>help_me_please : String(1..20);
>
>I seem to be stuck with a string exactly that size.  What is the
>variable assignment which allows the actual length of the user input to
>determine the length of the string which the io system
>(ada.text_io.)gets ?
>
>G Wallace
>
>Thankyou.
>:-)
>

I'm a newbie too (with respect to the latest edition of Ada) here follows 
what I would suggest if you were using Ada83.  I expect that one of the 
new (Ada95) string types may be more convenient but this should do the 
trick (with necessary addition of "ada.")

The following is an abstract from a noddy application: it will allow the 
user to input a string of up to 80 characters.  The result is then 
manipulated in the normal way.  {it isn't perfect so if any other pedants 
are watching I know!}


with Text_IO;
procedure Example is

  subtype TEXT_STRING is STRING (1 .. 80);
  type VARYING is
    record
      Text : TEXT_STRING;
      Last : NATURAL;
    end record;

  Input_Record : VARYING;

.. blah

begin

    ...

    Text_IO.Get_Line (Input_Record.Text, Input_Record.Last);
    case Input_Record.Text (Input_Record.Text'First) is
      when 'P' =>
        if List = null then
          List := new PAGE_ITEM;
          List.Address := Input_Record.Text (2 .. 5);
          Current_Page_Ptr := List;
        else
     ...

end Example;

Hope this helps a bit.  I must aquaint myself with the new stuff.

Cheers,

Simon.





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

* Re: newbie problem
  1998-12-05  0:00 Graeme Wallace
  1998-12-04  0:00 ` Simon Bracken
@ 1998-12-04  0:00 ` Marin David Condic
  1998-12-04  0:00 ` Mats Weber
                   ` (4 subsequent siblings)
  6 siblings, 0 replies; 31+ messages in thread
From: Marin David Condic @ 1998-12-04  0:00 UTC (permalink / raw)
  To: Graeme Wallace

Graeme Wallace wrote:
> 
> If I want to get user input for my program in the form of a  string of
> undefined length, how do I do so ?  If I set the string length to, say:
> 
> help_me_please : String(1..20);
> 
> I seem to be stuck with a string exactly that size.  What is the
> variable assignment which allows the actual length of the user input to
> determine the length of the string which the io system
> (ada.text_io.)gets ?
> 
> G Wallace
> 
> Thankyou.
> :-)

Ada.Text_IO does not have a Get or Get_Line procedure for variable
length strings. You have to declare a string of some sufficiently large
size to contain whatever you expect for input. (I usually use 256 bytes,
figuring that it is more than generous)

You should notice that the Get_Line procedure has a parameter "Last"
which indicates the index of the last character read. You can make use
of this in constructing variable length strings.

See the following package specs:

Ada.Strings    (ARM A.4.1)
Ada.Strings.Fixed    (ARM A.4.3)
Ada.Strings.Bounded    (ARM A.4.4)
Ada.Strings.Unbounded    (ARM A.4.5)

Between all of these - and a few related packages - you should find all
of the string handling features you need. The .Fixed package gives you
routines for handling the kinds of strings that Text_IO is going to give
you while .Bounded and .Unbounded will give you a choice of dynamically
sized strings & routines for handling them once you have read them in
via Text_IO.

I think a useful extension to the language would be variations on
Text_IO which used Bounded and Unbounded strings. But for now its just
Fixed.

MDC

-- 
Marin D. Condic
Real Time & Embedded Systems, Propulsion Systems Analysis
United Technologies, Pratt & Whitney, Large Military Engines
M/S 731-95, P.O.B. 109600, West Palm Beach, FL, 33410-9600
Ph: 561.796.8997         Fx: 561.796.4669

"The reasonable man adapts himself to the world; the unreasonable one
persists in trying to adapt the world to himself. Therefore all progress
depends on the unreasonable man."

        --  G.B. Shaw




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

* Re: newbie problem
  1998-12-04  0:00 ` Simon Bracken
@ 1998-12-04  0:00   ` Marin David Condic
  0 siblings, 0 replies; 31+ messages in thread
From: Marin David Condic @ 1998-12-04  0:00 UTC (permalink / raw)
  To: Simon Bracken

Simon Bracken wrote:
>
> 
> with Text_IO;
> procedure Example is
> 
>   subtype TEXT_STRING is STRING (1 .. 80);
>   type VARYING is
>     record
>       Text : TEXT_STRING;
>       Last : NATURAL;
>     end record;
> 
>   Input_Record : VARYING;
> 


You'll want to notice that you are basically duplicating the
Bounded_String type found in the package Ada.Strings.Bounded. (ARM
A.4.4) Wherever possible, IMHO, its best to use the data types &
operators supplied by the language rather than reinvent the wheel. Of
course, you do have the problem I observed in a related post in that
Text_IO will only deal directly with the standard type String.

MDC
-- 
Marin D. Condic
Real Time & Embedded Systems, Propulsion Systems Analysis
United Technologies, Pratt & Whitney, Large Military Engines
M/S 731-95, P.O.B. 109600, West Palm Beach, FL, 33410-9600
Ph: 561.796.8997         Fx: 561.796.4669

"The reasonable man adapts himself to the world; the unreasonable one
persists in trying to adapt the world to himself. Therefore all progress
depends on the unreasonable man."

        --  G.B. Shaw




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

* Re: newbie problem
  1998-12-05  0:00 Graeme Wallace
  1998-12-04  0:00 ` Simon Bracken
  1998-12-04  0:00 ` Marin David Condic
@ 1998-12-04  0:00 ` Mats Weber
  1998-12-05  0:00   ` Matthew Heaney
  1998-12-04  0:00 ` Gautier.DeMontmollin
                   ` (3 subsequent siblings)
  6 siblings, 1 reply; 31+ messages in thread
From: Mats Weber @ 1998-12-04  0:00 UTC (permalink / raw)


Graeme Wallace wrote:
> 
> If I want to get user input for my program in the form of a  string of
> undefined length, how do I do so ?  If I set the string length to, say:
> 
> help_me_please : String(1..20);
> 
> I seem to be stuck with a string exactly that size.  What is the
> variable assignment which allows the actual length of the user input to
> determine the length of the string which the io system
> (ada.text_io.)gets ?

Download GNAT and look at the package GNAT.IO_Aux. It contains two
Get_Line functions that should solve your problem:

help_me_please : constant String(1..20) := GNAT.IO_Aux.Get_Line;




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

* Re: newbie problem
  1998-12-05  0:00 Graeme Wallace
                   ` (2 preceding siblings ...)
  1998-12-04  0:00 ` Mats Weber
@ 1998-12-04  0:00 ` Gautier.DeMontmollin
  1998-12-04  0:00 ` David Botton
                   ` (2 subsequent siblings)
  6 siblings, 0 replies; 31+ messages in thread
From: Gautier.DeMontmollin @ 1998-12-04  0:00 UTC (permalink / raw)


> help_me_please : String(1..20);

s : String(1..100);
l : natural;
begin
  Get_Line(s,l);
  Put_Line("String is [" & s(1..l) & ']');
end;

-- 
Gautier




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

* newbie problem
@ 1998-12-05  0:00 Graeme Wallace
  1998-12-04  0:00 ` Simon Bracken
                   ` (6 more replies)
  0 siblings, 7 replies; 31+ messages in thread
From: Graeme Wallace @ 1998-12-05  0:00 UTC (permalink / raw)


If I want to get user input for my program in the form of a  string of
undefined length, how do I do so ?  If I set the string length to, say:

help_me_please : String(1..20);

I seem to be stuck with a string exactly that size.  What is the
variable assignment which allows the actual length of the user input to
determine the length of the string which the io system
(ada.text_io.)gets ?

G Wallace

Thankyou.
:-)





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

* Re: newbie problem
  1998-12-05  0:00 Graeme Wallace
                   ` (4 preceding siblings ...)
  1998-12-04  0:00 ` David Botton
@ 1998-12-05  0:00 ` Matthew Heaney
  1998-12-07  0:00 ` Jeff Carter
  6 siblings, 0 replies; 31+ messages in thread
From: Matthew Heaney @ 1998-12-05  0:00 UTC (permalink / raw)


Graeme Wallace <Dizzy@interact.net.au> writes:

> If I want to get user input for my program in the form of a  string of
> undefined length, how do I do so ?  If I set the string length to, say:
> 
> help_me_please : String(1..20);
> 
> I seem to be stuck with a string exactly that size.  What is the
> variable assignment which allows the actual length of the user input to
> determine the length of the string which the io system
> (ada.text_io.)gets ?

You have to allocate a buffer of some fixed size, as you have done.

What is missing from your solution is declaration of a "last" object,
whose value indicates the index value of the last valid character that
was input.

The standard idiom for doing this sort of thing is

declare
  Line : String (1 .. 80);
  Last : Natural;
begin
  Ada.Text_IO.Get_Line (Line, Last);

  ... Line (Line'First .. Last) ...

end;


The value of Last tells you how much of the buffer is actually in use.

Be careful not to name the Last object "Length."  That would be
misleading, and potentially error-prone, because "last index value" and
"length of input string" are two different concepts, and often have
different values.  They only have the same value here, because the first
index value of the string happens to be 1.




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

* Re: newbie problem
  1998-12-04  0:00 ` Mats Weber
@ 1998-12-05  0:00   ` Matthew Heaney
  0 siblings, 0 replies; 31+ messages in thread
From: Matthew Heaney @ 1998-12-05  0:00 UTC (permalink / raw)


Mats Weber <Mats.Weber@elca-matrix.ch> writes:

> Download GNAT and look at the package GNAT.IO_Aux. It contains two
> Get_Line functions that should solve your problem:
> 
> help_me_please : constant String(1..20) := GNAT.IO_Aux.Get_Line;

I really don't think you meant to write this.  I think you meant:

  Help_Me_Please : constant String := GNAT.IO_Aux.Get_Line;

Unless the input string is exactly 20 characters, you'll get
Constraint_Error every time you try to get a line.







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

* Re: newbie problem
  1998-12-05  0:00 Graeme Wallace
                   ` (5 preceding siblings ...)
  1998-12-05  0:00 ` Matthew Heaney
@ 1998-12-07  0:00 ` Jeff Carter
  6 siblings, 0 replies; 31+ messages in thread
From: Jeff Carter @ 1998-12-07  0:00 UTC (permalink / raw)


Graeme Wallace wrote:
> 
> If I want to get user input for my program in the form of a  string of
> undefined length, how do I do so ?  If I set the string length to, say:
> 
> help_me_please : String(1..20);
> 
> I seem to be stuck with a string exactly that size.  What is the
> variable assignment which allows the actual length of the user input to
> determine the length of the string which the io system
> (ada.text_io.)gets ?
> 

The way to do this is to use a function that returns a String:

function Get_Line
   (File : Ada.Text_Io.File_Type := Ada.Text_Io.Current_Input)
return String;

Help_Me_Please : String := Get_Line;

This will generally serve. You can obtain more flexibility at the price
of more complexity by converting the result of the function to a
variable-length string (Ada.Strings.Bounded or Ada.Strings.Unbounded).
-- 
Jeff Carter
E-mail: carter commercial-at innocon [period | full stop] com
"Your mother was a hamster and your father smelt of elderberries."
Monty Python & the Holy Grail




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

* newbie problem
@ 2009-05-13 14:16 Olivier Scalbert
  2009-05-13 14:54 ` Martin
                   ` (4 more replies)
  0 siblings, 5 replies; 31+ messages in thread
From: Olivier Scalbert @ 2009-05-13 14:16 UTC (permalink / raw)


Hello,

I am doing a little package and I have problems just for writing the 
specification ...

In short, this package should offer services to create sequential files 
that store stereo sound samples.

Here is the code:

with Sequential_Io; -- Problem 2  not nice should be hidden but how ?

package adasound is

     type Amplitude_T is new Float;
     type Frequency_T is new Float;
     type Time_T      is new Float;

     type Stereo_Amplitude_T is record
         Left : Amplitude_T;
         Right: Amplitude_T;
     end record;

     type Als_File_T is private;

     function  Als_Create(File_Name: String) return Als_File_T;
     procedure Als_Write(Als_File: Als_File_T; Stereo_Amplitude: 
Stereo_Amplitude_T);
     procedure Als_Close(Als_File: Als_File_T);

private
     package Stereo_Amplitude_Io is new Sequential_Io (Stereo_Amplitude_T);

     type Als_File_T is record
         File_Type: Stereo_Amplitude_Io.File_Type; -- Problem 1 !!
         -- perhaps more stuff later
     end record;

end adasound;

Problem 1: it does not compile !
adasound.ads:23:10: completion of nonlimited type cannot be limited
adasound.ads:23:10: component "File_Type" of type "Als_File_T" has 
limited type

Problem 2: how to avoid the first "with Sequential_Io;" ?

Thanks to help me !

Olivier.



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

* Re: newbie problem
  2009-05-13 14:16 newbie problem Olivier Scalbert
@ 2009-05-13 14:54 ` Martin
  2009-05-13 15:20   ` Olivier Scalbert
  2009-05-13 15:14 ` Adam Beneschan
                   ` (3 subsequent siblings)
  4 siblings, 1 reply; 31+ messages in thread
From: Martin @ 2009-05-13 14:54 UTC (permalink / raw)


On May 13, 3:16 pm, Olivier Scalbert <olivier.scalb...@algosyn.com>
wrote:
> Problem 1: it does not compile !
> adasound.ads:23:10: completion of nonlimited type cannot be limited
> adasound.ads:23:10: component "File_Type" of type "Als_File_T" has
> limited type

Store an 'access' to the limited type.

> Problem 2: how to avoid the first "with Sequential_Io;" ?

private with Ada.Sequential_IO;  -- I always prefer the
                                 -- Ada. prefix just to be clear
                                 -- it's a standard unit to any
                                 -- 'casual' reader

Cheers
-- Martin



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

* Re: newbie problem
  2009-05-13 14:16 newbie problem Olivier Scalbert
  2009-05-13 14:54 ` Martin
@ 2009-05-13 15:14 ` Adam Beneschan
  2009-05-13 15:54   ` Olivier Scalbert
  2009-05-13 15:44 ` Ludovic Brenta
                   ` (2 subsequent siblings)
  4 siblings, 1 reply; 31+ messages in thread
From: Adam Beneschan @ 2009-05-13 15:14 UTC (permalink / raw)


On May 13, 7:16 am, Olivier Scalbert <olivier.scalb...@algosyn.com>
wrote:
> Hello,
>
> I am doing a little package and I have problems just for writing the
> specification ...
>
> In short, this package should offer services to create sequential files
> that store stereo sound samples.
>
> Here is the code:
>
> with Sequential_Io; -- Problem 2  not nice should be hidden but how ?
>
> package adasound is
>
>      type Amplitude_T is new Float;
>      type Frequency_T is new Float;
>      type Time_T      is new Float;
>
>      type Stereo_Amplitude_T is record
>          Left : Amplitude_T;
>          Right: Amplitude_T;
>      end record;
>
>      type Als_File_T is private;
>
>      function  Als_Create(File_Name: String) return Als_File_T;
>      procedure Als_Write(Als_File: Als_File_T; Stereo_Amplitude:
> Stereo_Amplitude_T);
>      procedure Als_Close(Als_File: Als_File_T);
>
> private
>      package Stereo_Amplitude_Io is new Sequential_Io (Stereo_Amplitude_T);
>
>      type Als_File_T is record
>          File_Type: Stereo_Amplitude_Io.File_Type; -- Problem 1 !!
>          -- perhaps more stuff later
>      end record;
>
> end adasound;
>
> Problem 1: it does not compile !
> adasound.ads:23:10: completion of nonlimited type cannot be limited
> adasound.ads:23:10: component "File_Type" of type "Als_File_T" has
> limited type

You could make it an access to a File_Type, as Martin mentioned.  But
you may also want to make *your* type limited.  Making it limited
would prevent the program from copying objects of that type, and that
could be exactly what you want.

A record that represents information about a file that's currently
being used can be a natural candidate for a "limited" type.  Suppose,
hypothetically, that you're writing your own file I/O package, and not
using the standard Ada libraries.  You'll probably have some sort of
record somewhere to store information about the current file position,
perhaps some cached data from the file, etc.:

   type File_Info is record
      Curr_Position : Long_Integer;
      Buffer        : Byte_Array(1..Buf_Size);
      ...
   end record;

Most likely, this would be in the private part of your package.
Suppose you declare this without using "limited":

   type File_Info is private;

Now some other part of the program that WITH's your package can say

   F1 : File_Info;
   F2 : File_Info;

and then, after F1 has been created and some I/O done using it:

   F2 := F1;

This could screw up your whole file package's operations.  Suppose the
program later does some operations on F1, and it modifies the
Curr_Position and fills some data in the Buffer.  This will change the
information in F1, but not the information in F2.  If the program then
calls an I/O procedure using F2, the package will go bonkers, because
F2 doesn't have the correct file position or the correct buffered
data, and it's very likely that something bad will happen, such as
data being flushed to the wrong position of the file and turning your
file into junk.

A solution here is to prevent assignment from happening, by declaring
the type File_Info as "limited private".  This would make the above
assignment statement illegal, as well as other Ada constructs that
could result in two copies of the record being created.

I'm not saying that this is definitely what you want.  But you should
consider it.  If you do, you'd change the first declaration to

    type Als_File_T is limited private;

You'll also have to look into Ada 2005's "extended return" statement
in order to write the body of Als_Create.

Anyway, that's one thing I think you should consider.  If that isn't
what you want---i.e. if Als_File_T is supposed to represent a
"reference" or "handle" to a file rather than the file itself---or if
this is all too confusing, then access types will work.  It's a design
decision.

                               -- Adam




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

* Re: newbie problem
  2009-05-13 14:54 ` Martin
@ 2009-05-13 15:20   ` Olivier Scalbert
  0 siblings, 0 replies; 31+ messages in thread
From: Olivier Scalbert @ 2009-05-13 15:20 UTC (permalink / raw)


Martin wrote:
>> Problem 1: it does not compile !
>> adasound.ads:23:10: completion of nonlimited type cannot be limited
>> adasound.ads:23:10: component "File_Type" of type "Als_File_T" has
>> limited type
> 
> Store an 'access' to the limited type.
> 
>> Problem 2: how to avoid the first "with Sequential_Io;" ?
> 
> private with Ada.Sequential_IO;  -- I always prefer the
>                                  -- Ada. prefix just to be clear
>                                  -- it's a standard unit to any
>                                  -- 'casual' reader
> 
> Cheers
> -- Martin

Thanks Martin,

I have modified the specification and it compiles now:

private with Ada.Sequential_Io;

package adasound is

     type Amplitude_T is new Float;
     type Frequency_T is new Float;
     type Time_T      is new Float;

     type Stereo_Amplitude_T is record
         Left : Amplitude_T;
         Right: Amplitude_T;
     end record;

     type Als_File_T is private;

     function  Als_Create(File_Name: String) return Als_File_T;
     procedure Als_Write(Als_File: Als_File_T; Stereo_Amplitude: 
Stereo_Amplitude_T);
     procedure Als_Close(Als_File: Als_File_T);

private
     package Stereo_Amplitude_Io is new Ada.Sequential_Io 
(Stereo_Amplitude_T);

     type Als_File_T is record
         File_Type: access Stereo_Amplitude_Io.File_Type;
         -- perhaps more stuff later
     end record;

end adasound;


How can implement the Als_Create function ?
Here is my code, but it does not compile !

package body adasound is

function Als_Create(File_Name: String) return Als_File_T is
     Result : Als_File_T;
     File: Stereo_Amplitude_Io.File_Type; -- Error !!!
begin
     Stereo_Amplitude_Io.Create(File, Stereo_Amplitude_Io.Out_File, 
File_Name);
     Result.File_Type := File; -- Line 10 Error !!!
     return Result;
end Als_Create;
...

gcc-4.3 -c adasound.adb
adasound.adb:10:22: left hand of assignment must not be limited type


Olivier.







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

* Re: newbie problem
  2009-05-13 14:16 newbie problem Olivier Scalbert
  2009-05-13 14:54 ` Martin
  2009-05-13 15:14 ` Adam Beneschan
@ 2009-05-13 15:44 ` Ludovic Brenta
  2009-05-13 16:03   ` Olivier Scalbert
  2009-05-13 18:00 ` Olivier Scalbert
  2009-05-14  2:59 ` anon
  4 siblings, 1 reply; 31+ messages in thread
From: Ludovic Brenta @ 2009-05-13 15:44 UTC (permalink / raw)


Olivier Scalbert wrote on comp.lang.ada:
> Problem 1: it does not compile !
> adasound.ads:23:10: completion of nonlimited type cannot be limited
> adasound.ads:23:10: component "File_Type" of type "Als_File_T" has
> limited type

File_Type is of a limited type; any type containing it must therefore
also be limited.  Martin and Adam explained that very well.  My
preferred solution would be to make Als_File_T limited, too, since it
also represents a file.

> Problem 2: how to avoid the first "with Sequential_Io;" ?

Solution 1, as explaines by Martin: private with Ada.Sequential_IO;
this assumes that the presence of the with clause is only a minor
problem.  I would tend to agree that this problem is minor.

Solution 2: If you absolutely want to hide the with clause, you can
move it to the body of your package by making Als_File_T completely
opaque, like so:

package Adasound is
   type Als_File_T is private; -- or limited private
private
   type Internal_T;
   type Als_File_T is access Internal_T;
end Adasound;

with Ada.Sequential_IO;
package body Adasound is
   package Stereo_Amplitude_IO is new Sequential_Io
(Stereo_Amplitude_T);
   type Internal_T is limited record
      File_Type : Stereo_Amplitude_IO.File_Type;
   end record;
end Adasound;

The drawback of making Als_File_T opaque is that not even child
packages of Adasound can see the internals of the type.  This may be a
problem if you want to write unit tests (which are often child
packages or child procedures of the unit under test).

--
Ludovic Brenta.



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

* Re: newbie problem
  2009-05-13 15:14 ` Adam Beneschan
@ 2009-05-13 15:54   ` Olivier Scalbert
  0 siblings, 0 replies; 31+ messages in thread
From: Olivier Scalbert @ 2009-05-13 15:54 UTC (permalink / raw)


Adam Beneschan wrote:
> You could make it an access to a File_Type, as Martin mentioned.  But
> you may also want to make *your* type limited.  Making it limited
> would prevent the program from copying objects of that type, and that
> could be exactly what you want.
> 
> A record that represents information about a file that's currently
> being used can be a natural candidate for a "limited" type.  Suppose,
> hypothetically, that you're writing your own file I/O package, and not
> using the standard Ada libraries.  You'll probably have some sort of
> record somewhere to store information about the current file position,
> perhaps some cached data from the file, etc.:
> 
>    type File_Info is record
>       Curr_Position : Long_Integer;
>       Buffer        : Byte_Array(1..Buf_Size);
>       ...
>    end record;
> 
> Most likely, this would be in the private part of your package.
> Suppose you declare this without using "limited":
> 
>    type File_Info is private;
> 
> Now some other part of the program that WITH's your package can say
> 
>    F1 : File_Info;
>    F2 : File_Info;
> 
> and then, after F1 has been created and some I/O done using it:
> 
>    F2 := F1;
> 
> This could screw up your whole file package's operations.  Suppose the
> program later does some operations on F1, and it modifies the
> Curr_Position and fills some data in the Buffer.  This will change the
> information in F1, but not the information in F2.  If the program then
> calls an I/O procedure using F2, the package will go bonkers, because
> F2 doesn't have the correct file position or the correct buffered
> data, and it's very likely that something bad will happen, such as
> data being flushed to the wrong position of the file and turning your
> file into junk.
> 
> A solution here is to prevent assignment from happening, by declaring
> the type File_Info as "limited private".  This would make the above
> assignment statement illegal, as well as other Ada constructs that
> could result in two copies of the record being created.
> 
> I'm not saying that this is definitely what you want.  But you should
> consider it.  If you do, you'd change the first declaration to
> 
>     type Als_File_T is limited private;
> 
> You'll also have to look into Ada 2005's "extended return" statement
> in order to write the body of Als_Create.
> 
> Anyway, that's one thing I think you should consider.  If that isn't
> what you want---i.e. if Als_File_T is supposed to represent a
> "reference" or "handle" to a file rather than the file itself---or if
> this is all too confusing, then access types will work.  It's a design
> decision.
> 
>                                -- Adam
> 

Thanks Adam for the very clear explanation !

Olivier.





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

* Re: newbie problem
  2009-05-13 15:44 ` Ludovic Brenta
@ 2009-05-13 16:03   ` Olivier Scalbert
  0 siblings, 0 replies; 31+ messages in thread
From: Olivier Scalbert @ 2009-05-13 16:03 UTC (permalink / raw)


> The drawback of making Als_File_T opaque is that not even child
> packages of Adasound can see the internals of the type.  This may be a
> problem if you want to write unit tests (which are often child
> packages or child procedures of the unit under test).
> 
> --
> Ludovic Brenta.

Thanks Ludovic.

Sometimes I tend to do over-encapsulation !

Do you know where I can find advices on unit testing Ada projects ?

Thanks,

Olivier.



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

* Re: newbie problem
  2009-05-13 14:16 newbie problem Olivier Scalbert
                   ` (2 preceding siblings ...)
  2009-05-13 15:44 ` Ludovic Brenta
@ 2009-05-13 18:00 ` Olivier Scalbert
  2009-05-13 18:51   ` Martin
  2009-05-14  2:59 ` anon
  4 siblings, 1 reply; 31+ messages in thread
From: Olivier Scalbert @ 2009-05-13 18:00 UTC (permalink / raw)


Finally I have done some changes. Als_Create is now a procedure.

Here is the code:

--------------------------------------
-- adasound.ads
--------------------------------------
private with Ada.Sequential_Io;

package adasound is

     type Amplitude_T is new Float;
     type Frequency_T is new Float;
     type Time_T      is new Float;

     type Stereo_Amplitude_T is record
         Left : Amplitude_T;
         Right: Amplitude_T;
     end record;

     type Als_File_T is limited private;

     procedure Als_Create(Als_File: out Als_File_T; File_Name: String);
     procedure Als_Write(Als_File: Als_File_T; Stereo_Amplitude: 
Stereo_Amplitude_T);
     procedure Als_Close(Als_File: out Als_File_T);

private
     package Stereo_Amplitude_Io is new Ada.Sequential_Io 
(Stereo_Amplitude_T);

     type Als_File_T is record
         File_Type: Stereo_Amplitude_Io.File_Type;
         -- perhaps more stuff later
     end record;

end adasound;

--------------------------------------
-- adasound.adb
--------------------------------------
package body adasound is

procedure Als_Create(Als_File: out Als_File_T; File_Name: String) is
     File: Stereo_Amplitude_Io.File_Type;
begin
     Stereo_Amplitude_Io.Create(Als_File.File_Type, 
Stereo_Amplitude_Io.Out_File, File_Name);
end Als_Create;

procedure Als_Write(Als_File: Als_File_T; Stereo_Amplitude: 
Stereo_Amplitude_T) is
begin
     Stereo_Amplitude_Io.Write(Als_File.File_Type, Stereo_Amplitude);
end Als_Write;

procedure Als_Close(Als_File: out Als_File_T) is
begin
     Stereo_Amplitude_Io.Close(Als_File.File_Type);
end Als_Close;

end adasound;

--------------------------------------
-- driver.adb
--------------------------------------
with adasound;
use adasound;

procedure driver is
     Als_File : Als_File_T;
     Stereo : Stereo_Amplitude_T;
     Sample_Rate : constant Integer := 44_100;
     Nb_Seconds  : constant Integer :=  3_600;
begin
     Stereo.Left  := 0.0;
     Stereo.Right := 0.0;
     Als_Create(Als_File, "test.als");
     for i in 1..Sample_Rate * Nb_Seconds loop
         Als_Write(Als_File, Stereo);
     end loop;
     Als_Close(Als_File);
end driver;

I do not know if the design is better or not but at least it works.

I am also very pleased with the performance. Of course, it is nearly 
only IO but ...

time ./driver
real    0m15.648s
user    0m10.305s
sys     0m3.364s

ls -l test.als
-rw-r--r-- 1 ols ols 1270080000 2009-05-13 18:41 test.als


Olivier.



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

* Re: newbie problem
  2009-05-13 18:00 ` Olivier Scalbert
@ 2009-05-13 18:51   ` Martin
  2009-05-13 19:45     ` sjw
  2009-05-13 19:48     ` Olivier Scalbert
  0 siblings, 2 replies; 31+ messages in thread
From: Martin @ 2009-05-13 18:51 UTC (permalink / raw)


On May 13, 7:00 pm, Olivier Scalbert <olivier.scalb...@algosyn.com>
wrote:
> Finally I have done some changes. Als_Create is now a procedure.
>
> Here is the code:
>
> --------------------------------------
> -- adasound.ads
> --------------------------------------
> private with Ada.Sequential_Io;
>
> package adasound is
>
>      type Amplitude_T is new Float;
>      type Frequency_T is new Float;
>      type Time_T      is new Float;
>
>      type Stereo_Amplitude_T is record
>          Left : Amplitude_T;
>          Right: Amplitude_T;
>      end record;
>
>      type Als_File_T is limited private;
>
>      procedure Als_Create(Als_File: out Als_File_T; File_Name: String);
>      procedure Als_Write(Als_File: Als_File_T; Stereo_Amplitude:
> Stereo_Amplitude_T);
>      procedure Als_Close(Als_File: out Als_File_T);
>
> private
>      package Stereo_Amplitude_Io is new Ada.Sequential_Io
> (Stereo_Amplitude_T);
>
>      type Als_File_T is record
>          File_Type: Stereo_Amplitude_Io.File_Type;
>          -- perhaps more stuff later
>      end record;
>
> end adasound;
>
> --------------------------------------
> -- adasound.adb
> --------------------------------------
> package body adasound is
>
> procedure Als_Create(Als_File: out Als_File_T; File_Name: String) is
>      File: Stereo_Amplitude_Io.File_Type;
> begin
>      Stereo_Amplitude_Io.Create(Als_File.File_Type,
> Stereo_Amplitude_Io.Out_File, File_Name);
> end Als_Create;
>
> procedure Als_Write(Als_File: Als_File_T; Stereo_Amplitude:
> Stereo_Amplitude_T) is
> begin
>      Stereo_Amplitude_Io.Write(Als_File.File_Type, Stereo_Amplitude);
> end Als_Write;
>
> procedure Als_Close(Als_File: out Als_File_T) is
> begin
>      Stereo_Amplitude_Io.Close(Als_File.File_Type);
> end Als_Close;
>
> end adasound;
>
> --------------------------------------
> -- driver.adb
> --------------------------------------
> with adasound;
> use adasound;
>
> procedure driver is
>      Als_File : Als_File_T;
>      Stereo : Stereo_Amplitude_T;
>      Sample_Rate : constant Integer := 44_100;
>      Nb_Seconds  : constant Integer :=  3_600;
> begin
>      Stereo.Left  := 0.0;
>      Stereo.Right := 0.0;
>      Als_Create(Als_File, "test.als");
>      for i in 1..Sample_Rate * Nb_Seconds loop
>          Als_Write(Als_File, Stereo);
>      end loop;
>      Als_Close(Als_File);
> end driver;
>
> I do not know if the design is better or not but at least it works.
>
> I am also very pleased with the performance. Of course, it is nearly
> only IO but ...
>
> time ./driver
> real    0m15.648s
> user    0m10.305s
> sys     0m3.364s
>
> ls -l test.als
> -rw-r--r-- 1 ols ols 1270080000 2009-05-13 18:41 test.als
>
> Olivier.

Nice one.

Stylistically, why isn't the package called 'ALS' and then you could
remove the tautological 'Als_' everywhere? Perhaps a root package of
'Audio' and a child package of 'Audio.ALS' - that way you could expand
to support other audio file formats 'Audio.mp3', 'Audio.Wav', etc.

Again a style thing, but '_T' looks rather ugly and isn't common usage
- see the RM, esp the Ada.Container.* hierarchy for naming ideas.

Cheers
-- Martin



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

* Re: newbie problem
  2009-05-13 18:51   ` Martin
@ 2009-05-13 19:45     ` sjw
  2009-05-13 19:48     ` Olivier Scalbert
  1 sibling, 0 replies; 31+ messages in thread
From: sjw @ 2009-05-13 19:45 UTC (permalink / raw)


On May 13, 7:51 pm, Martin <martin.do...@btopenworld.com> wrote:

> Stylistically, why isn't the package called 'ALS' and then you could
> remove the tautological 'Als_' everywhere? Perhaps a root package of
> 'Audio' and a child package of 'Audio.ALS' - that way you could expand
> to support other audio file formats 'Audio.mp3', 'Audio.Wav', etc.

I would be quite happy with adasound (well, reasonably happy -- I can
tell it's written in Ada, or is there a special kind of sound called
adasound that's different from other kinds?) but one thing I wouldn't
have done is prefix things with als_. Look at the way the Ada.*_IO
packages work -- File_Type (eeew), Create, Write. Close.

   F : Adasound.File_Type;
begin
   Adasound.Create (F, "test.als");




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

* Re: newbie problem
  2009-05-13 18:51   ` Martin
  2009-05-13 19:45     ` sjw
@ 2009-05-13 19:48     ` Olivier Scalbert
  2009-05-14 19:41       ` sjw
  2009-05-14 22:39       ` Jeffrey R. Carter
  1 sibling, 2 replies; 31+ messages in thread
From: Olivier Scalbert @ 2009-05-13 19:48 UTC (permalink / raw)


Martin wrote:
> 
> Nice one.
> 
> Stylistically, why isn't the package called 'ALS' and then you could
> remove the tautological 'Als_' everywhere? Perhaps a root package of
> 'Audio' and a child package of 'Audio.ALS' - that way you could expand
> to support other audio file formats 'Audio.mp3', 'Audio.Wav', etc.
> 
> Again a style thing, but '_T' looks rather ugly and isn't common usage
> - see the RM, esp the Ada.Container.* hierarchy for naming ideas.
> 
> Cheers
> -- Martin

Completely agree !

But I do not know how to organize the files hierarchy ... Should I 
create child directories for child packages ?

Also for the _T, I have not a lot of ideas. All day long I am using case 
sensitive languages. So I can have for ex:
File file ...; Which is not possible in Ada
File aFile ..; Which gives A_File: File; which is not very nice ...
File outputFife ...; Which gives: Output_File: File; Mmmn this one is 
nice !!
But I will read the RM !

Olivier.




Olivier.



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

* Re: newbie problem
  2009-05-13 14:16 newbie problem Olivier Scalbert
                   ` (3 preceding siblings ...)
  2009-05-13 18:00 ` Olivier Scalbert
@ 2009-05-14  2:59 ` anon
  4 siblings, 0 replies; 31+ messages in thread
From: anon @ 2009-05-14  2:59 UTC (permalink / raw)


In order to create a hidden structures, the easies is to insert the IO into the 
body, but you may have to create your own "File_Type" in the spec file.  But 
another version is to use extra files and packages to hide the IO. There are a 
few others ways but the more you try to hide a package or the operations of 
that package the more complex the Ada packages and structures can become.  

--
-- sound.ads  -- Type used in Sound.IO
--
package Sound is

  pragma Pure ( Sound ) ;

  type Amplitude_Type is new Float ;
  type Frequency_Type is new Float ;
  type Time_Type      is new Float ;

  type Stereo_Amplitude_Type is record
                               Left : Amplitude_Type ;
                               Right: Amplitude_Type ;
                             end record ;
end Sound ;
--
-- Sound-io.ads
--
with Ada.Sequential_IO ;

package Sound.IO is new Ada.Sequential_IO ( Stereo_Amplitude_Type ) ;

--------------------------------------
-- adasound.ads
--------------------------------------
with Sound ;
with Sound.IO ;

package AdaSound is

     procedure Als_Create ( Als_File  : in out Sound.IO.File_Type ; 
                            File_Name : String ) ;

     procedure Als_Write ( Als_File : in out Sound.IO.File_Type ; 
                           Value    : Sound.Stereo_Amplitude_Type ) ;

     procedure Als_Close  ( Als_File : in out Sound.IO.File_Type ) ;

end AdaSound ;
--------------------------------------
-- adasound.adb
--------------------------------------
with Sound ;
with Sound.IO ;

package body adasound is

  procedure Als_Create ( Als_File : in out Sound.IO.File_Type ; 
                         File_Name: String     ) is
      File : Sound.IO.File_Type ;
    begin
      Sound.IO.Create ( Als_File, Sound.IO.Out_File, File_Name ) ;
    end Als_Create ;

  procedure Als_Write ( Als_File : in out Sound.IO.File_Type ; 
                        Value    : Sound.Stereo_Amplitude_Type ) is
    begin
      Sound.IO.Write ( Als_File, Value ) ;
    end Als_Write ;

  procedure Als_Close ( Als_File: in out Sound.IO.File_Type ) is
    begin
      Sound.IO.Close ( Als_File ) ;
    end Als_Close ;
end adasound ;
--------------------------------------
-- driver.adb
--------------------------------------

with Sound ;
with Sound.IO ;

with AdaSound ;

procedure driver is
     Sample_Rate : constant Integer := 44_100 ;
     Nb_Seconds  : constant Integer :=  3_600 ;

     Als_File    : Sound.IO.File_Type ;
     Stereo      : Sound.Stereo_Amplitude_Type ;

begin
  Stereo.Left  := 0.0 ;
  Stereo.Right := 0.0 ;
  Als_Create ( Als_File, "test.als" ) ;
  for i in 1..Sample_Rate * Nb_Seconds loop
    Als_Write( Als_File, Stereo ) ;
  end loop ;
  Als_Close( Als_File ) ;
end driver;


In <4a0ad646$0$2854$ba620e4c@news.skynet.be>, Olivier Scalbert <olivier.scalbert@algosyn.com> writes:
>Hello,
>
>I am doing a little package and I have problems just for writing the 
>specification ...
>
>In short, this package should offer services to create sequential files 
>that store stereo sound samples.
>
>Here is the code:
>
>with Sequential_Io; -- Problem 2  not nice should be hidden but how ?
>
>package adasound is
>
>     type Amplitude_T is new Float;
>     type Frequency_T is new Float;
>     type Time_T      is new Float;
>
>     type Stereo_Amplitude_T is record
>         Left : Amplitude_T;
>         Right: Amplitude_T;
>     end record;
>
>     type Als_File_T is private;
>
>     function  Als_Create(File_Name: String) return Als_File_T;
>     procedure Als_Write(Als_File: Als_File_T; Stereo_Amplitude: 
>Stereo_Amplitude_T);
>     procedure Als_Close(Als_File: Als_File_T);
>
>private
>     package Stereo_Amplitude_Io is new Sequential_Io (Stereo_Amplitude_T);
>
>     type Als_File_T is record
>         File_Type: Stereo_Amplitude_Io.File_Type; -- Problem 1 !!
>         -- perhaps more stuff later
>     end record;
>
>end adasound;
>
>Problem 1: it does not compile !
>adasound.ads:23:10: completion of nonlimited type cannot be limited
>adasound.ads:23:10: component "File_Type" of type "Als_File_T" has 
>limited type
>
>Problem 2: how to avoid the first "with Sequential_Io;" ?
>
>Thanks to help me !
>
>Olivier.




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

* Re: newbie problem
  2009-05-13 19:48     ` Olivier Scalbert
@ 2009-05-14 19:41       ` sjw
  2009-05-15  5:02         ` Olivier Scalbert
                           ` (2 more replies)
  2009-05-14 22:39       ` Jeffrey R. Carter
  1 sibling, 3 replies; 31+ messages in thread
From: sjw @ 2009-05-14 19:41 UTC (permalink / raw)


On May 13, 8:48 pm, Olivier Scalbert <olivier.scalb...@algosyn.com>
wrote:

> But I do not know how to organize the files hierarchy ... Should I
> create child directories for child packages ?

No, not necessary, not in the first instance.

Compilers (well, GNAT) will be quite happy with

  audio.ads
  audio-als.ads
  audio-als.adb
  audio-mp3.ads
  audio-mp3.adb

in the same directory.

What you won't get away with is needing more than one version of the
same file in the same directory: you might need

  audio-aac.ads
  macos/audio-aac.adb
  windows/audio-aac.adb

at which point you need to control which variant the compiler gets to
see, depending on the environment. But I think that's a topic for a
different discussion!

> Also for the _T, I have not a lot of ideas. All day long I am using case
> sensitive languages. So I can have for ex:
> File file ...; Which is not possible in Ada
> File aFile ..; Which gives A_File: File; which is not very nice ...
> File outputFife ...; Which gives: Output_File: File; Mmmn this one is
> nice !!

We proposed a (joke) coding standard that the length of a name should
be log-base-2 the length of its scope in lines. So I might be quite
happy with Output : File_Type; for example. Depends whether you have
anything else that might be confused with it.



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

* Re: newbie problem
  2009-05-13 19:48     ` Olivier Scalbert
  2009-05-14 19:41       ` sjw
@ 2009-05-14 22:39       ` Jeffrey R. Carter
  1 sibling, 0 replies; 31+ messages in thread
From: Jeffrey R. Carter @ 2009-05-14 22:39 UTC (permalink / raw)


Olivier Scalbert wrote:
> 
> Also for the _T, I have not a lot of ideas. 

You might want to look at

http://groups.google.com/group/comp.lang.ada/msg/d878b7b859401621

-- 
Jeff Carter
"Your mother was a hamster and your father smelt of elderberries."
Monty Python & the Holy Grail
06



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

* Re: newbie problem
  2009-05-14 19:41       ` sjw
@ 2009-05-15  5:02         ` Olivier Scalbert
  2009-05-15  8:05         ` Jean-Pierre Rosen
  2009-05-18 10:23         ` Olivier Scalbert
  2 siblings, 0 replies; 31+ messages in thread
From: Olivier Scalbert @ 2009-05-15  5:02 UTC (permalink / raw)


sjw wrote:
> 
> We proposed a (joke) coding standard that the length of a name should
> be log-base-2 the length of its scope in lines. So I might be quite
> happy with Output : File_Type; for example. Depends whether you have
> anything else that might be confused with it.

I like the log-base-2 !
;-)

Olivier.



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

* Re: newbie problem
  2009-05-14 19:41       ` sjw
  2009-05-15  5:02         ` Olivier Scalbert
@ 2009-05-15  8:05         ` Jean-Pierre Rosen
  2009-05-18 10:23         ` Olivier Scalbert
  2 siblings, 0 replies; 31+ messages in thread
From: Jean-Pierre Rosen @ 2009-05-15  8:05 UTC (permalink / raw)


sjw a �crit :
> We proposed a (joke) coding standard that the length of a name should
> be log-base-2 the length of its scope in lines. 
Great idea for a new rule in AdaControl. Feel free to implement it, I'll
be happy to integrate it in the next version. :-)

-- 
---------------------------------------------------------
           J-P. Rosen (rosen@adalog.fr)
Visit Adalog's web site at http://www.adalog.fr



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

* Re: newbie problem
  2009-05-14 19:41       ` sjw
  2009-05-15  5:02         ` Olivier Scalbert
  2009-05-15  8:05         ` Jean-Pierre Rosen
@ 2009-05-18 10:23         ` Olivier Scalbert
  2009-05-18 10:48           ` Martin
  2009-05-18 10:52           ` Ludovic Brenta
  2 siblings, 2 replies; 31+ messages in thread
From: Olivier Scalbert @ 2009-05-18 10:23 UTC (permalink / raw)


sjw wrote:
> We proposed a (joke) coding standard that the length of a name should
> be log-base-2 the length of its scope in lines. So I might be quite
> happy with Output : File_Type; for example. Depends whether you have
> anything else that might be confused with it.

Hi,

I have reorganized the code a little bit.

------------------------------------------
-- audio.ads
------------------------------------------
package audio is

     type Amplitude is new Float;
     type Frequency is new Float;
     type Time      is new Float;

     type Stereo_Amplitude is record
         Left : Amplitude;
         Right: Amplitude;
     end record;

end audio;

------------------------------------------
-- audio-als.ads
------------------------------------------
private with Ada.Sequential_Io;

package audio.als is

type File is limited private;

procedure Create (F: out File; File_Name: String);
procedure Write  (F: in  File; Sample: Stereo_Amplitude);
procedure Close  (F: out File);
procedure Get_Min_Max (File_Name: String; Min: out Amplitude; Max: out 
Amplitude);

private

package Stereo_Amplitude.Io is new Ada.Sequential_Io (Stereo_Amplitude);

type File is record
     File_Type: Stereo_Amplitude_Io.File_Type;
     -- perhaps more stuff later
end record;

end audio.als;

------------------------------------------
-- audio-als.adb
------------------------------------------
...

When I do: gnatmake audio-als.adb, I've got:

gnatmake audio-als.adb
gcc-4.3 -c audio-als.adb
audio-als.ads:14:09: child unit allowed only at library level

line 14 is : package Stereo_Amplitude.Io is new Ada.Sequential_Io 
(Stereo_Amplitude);

I must admit that I have no idea on how to solve it ...



Olivier

















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

* Re: newbie problem
  2009-05-18 10:23         ` Olivier Scalbert
@ 2009-05-18 10:48           ` Martin
  2009-05-18 10:54             ` Olivier Scalbert
  2009-05-18 10:52           ` Ludovic Brenta
  1 sibling, 1 reply; 31+ messages in thread
From: Martin @ 2009-05-18 10:48 UTC (permalink / raw)


On May 18, 11:23 am, Olivier Scalbert <olivier.scalb...@algosyn.com>
wrote:
> sjw wrote:
> > We proposed a (joke) coding standard that the length of a name should
> > be log-base-2 the length of its scope in lines. So I might be quite
> > happy with Output : File_Type; for example. Depends whether you have
> > anything else that might be confused with it.
>
> Hi,
>
> I have reorganized the code a little bit.
>
> ------------------------------------------
> -- audio.ads
> ------------------------------------------
> package audio is
>
>      type Amplitude is new Float;
>      type Frequency is new Float;
>      type Time      is new Float;
>
>      type Stereo_Amplitude is record
>          Left : Amplitude;
>          Right: Amplitude;
>      end record;
>
> end audio;
>
> ------------------------------------------
> -- audio-als.ads
> ------------------------------------------
> private with Ada.Sequential_Io;
>
> package audio.als is
>
> type File is limited private;
>
> procedure Create (F: out File; File_Name: String);
> procedure Write  (F: in  File; Sample: Stereo_Amplitude);
> procedure Close  (F: out File);
> procedure Get_Min_Max (File_Name: String; Min: out Amplitude; Max: out
> Amplitude);
>
> private
>
> package Stereo_Amplitude.Io is new Ada.Sequential_Io (Stereo_Amplitude);
>
> type File is record
>      File_Type: Stereo_Amplitude_Io.File_Type;
>      -- perhaps more stuff later
> end record;
>
> end audio.als;
>
> ------------------------------------------
> -- audio-als.adb
> ------------------------------------------
> ...
>
> When I do: gnatmake audio-als.adb, I've got:
>
> gnatmake audio-als.adb
> gcc-4.3 -c audio-als.adb
> audio-als.ads:14:09: child unit allowed only at library level
>
> line 14 is : package Stereo_Amplitude.Io is new Ada.Sequential_Io
> (Stereo_Amplitude);
>
> I must admit that I have no idea on how to solve it ...
>
> Olivier

Change

package Stereo_Amplitude.Io

to

package Stereo_Amplitude_Io

Cheers
-- Martin



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

* Re: newbie problem
  2009-05-18 10:23         ` Olivier Scalbert
  2009-05-18 10:48           ` Martin
@ 2009-05-18 10:52           ` Ludovic Brenta
  2009-05-18 11:09             ` Olivier Scalbert
  1 sibling, 1 reply; 31+ messages in thread
From: Ludovic Brenta @ 2009-05-18 10:52 UTC (permalink / raw)


On May 18, 12:23 pm, Olivier Scalbert <olivier.scalb...@algosyn.com>
wrote:
> sjw wrote:
> > We proposed a (joke) coding standard that the length of a name should
> > be log-base-2 the length of its scope in lines. So I might be quite
> > happy with Output : File_Type; for example. Depends whether you have
> > anything else that might be confused with it.
>
> Hi,
>
> I have reorganized the code a little bit.
>
> ------------------------------------------
> -- audio.ads
> ------------------------------------------
> package audio is
>
>      type Amplitude is new Float;
>      type Frequency is new Float;
>      type Time      is new Float;
>
>      type Stereo_Amplitude is record
>          Left : Amplitude;
>          Right: Amplitude;
>      end record;
>
> end audio;
>
> ------------------------------------------
> -- audio-als.ads
> ------------------------------------------
> private with Ada.Sequential_Io;
>
> package audio.als is
>
> type File is limited private;
>
> procedure Create (F: out File; File_Name: String);
> procedure Write  (F: in  File; Sample: Stereo_Amplitude);
> procedure Close  (F: out File);
> procedure Get_Min_Max (File_Name: String; Min: out Amplitude; Max: out
> Amplitude);
>
> private
>
> package Stereo_Amplitude.Io is new Ada.Sequential_Io (Stereo_Amplitude);
>
> type File is record
>      File_Type: Stereo_Amplitude_Io.File_Type;
>      -- perhaps more stuff later
> end record;
>
> end audio.als;
>
> ------------------------------------------
> -- audio-als.adb
> ------------------------------------------
> ...
>
> When I do: gnatmake audio-als.adb, I've got:
>
> gnatmake audio-als.adb
> gcc-4.3 -c audio-als.adb
> audio-als.ads:14:09: child unit allowed only at library level
>
> line 14 is : package Stereo_Amplitude.Io is new Ada.Sequential_Io
> (Stereo_Amplitude);
>
> I must admit that I have no idea on how to solve it ...

Rename it Stereo_Amplitude_IO. (You don't yet have a package named
Stereo_Amplitude, BTW). If you insist on making a package named
Stereo_Amplitude.IO then you must put it at library level, i.e. not
inside another package, like this:

-- stereo_amplitude.ads
package Stereo_Amplitude is
end Stereo_Amplitude;

-- stereo_amplitude-io.ads
with Ada.Sequential_IO
with Audio;
package Stereo_Amplitude.IO is new Ada.Sequential_IO
(Audio.Stereo_Amplitude);

but I fail to see the benefit of this.

--
Ludovic Brenta.



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

* Re: newbie problem
  2009-05-18 10:48           ` Martin
@ 2009-05-18 10:54             ` Olivier Scalbert
  0 siblings, 0 replies; 31+ messages in thread
From: Olivier Scalbert @ 2009-05-18 10:54 UTC (permalink / raw)


Martin wrote:

> 
> Change
> 
> package Stereo_Amplitude.Io
> 
> to
> 
> package Stereo_Amplitude_Io
> 
> Cheers
> -- Martin

Ooops, I should stop for today !
Thanks Martin !



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

* Re: newbie problem
  2009-05-18 10:52           ` Ludovic Brenta
@ 2009-05-18 11:09             ` Olivier Scalbert
  0 siblings, 0 replies; 31+ messages in thread
From: Olivier Scalbert @ 2009-05-18 11:09 UTC (permalink / raw)


Ludovic Brenta wrote:

> Rename it Stereo_Amplitude_IO. (You don't yet have a package named
> Stereo_Amplitude, BTW). If you insist on making a package named
> Stereo_Amplitude.IO then you must put it at library level, i.e. not
> inside another package, like this:
> 
> -- stereo_amplitude.ads
> package Stereo_Amplitude is
> end Stereo_Amplitude;
> 
> -- stereo_amplitude-io.ads
> with Ada.Sequential_IO
> with Audio;
> package Stereo_Amplitude.IO is new Ada.Sequential_IO
> (Audio.Stereo_Amplitude);
> 
> but I fail to see the benefit of this.
> 
> --
> Ludovic Brenta.

Thanks Ludovic.

In fact in "package Stereo_Amplitude.Io" I have not noticed that a "_" 
has been transformed into a "." !
Cosmic particle ? Beaujolais effect ? I do not know ...
;-)

Olivier



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

end of thread, other threads:[~2009-05-18 11:09 UTC | newest]

Thread overview: 31+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2009-05-13 14:16 newbie problem Olivier Scalbert
2009-05-13 14:54 ` Martin
2009-05-13 15:20   ` Olivier Scalbert
2009-05-13 15:14 ` Adam Beneschan
2009-05-13 15:54   ` Olivier Scalbert
2009-05-13 15:44 ` Ludovic Brenta
2009-05-13 16:03   ` Olivier Scalbert
2009-05-13 18:00 ` Olivier Scalbert
2009-05-13 18:51   ` Martin
2009-05-13 19:45     ` sjw
2009-05-13 19:48     ` Olivier Scalbert
2009-05-14 19:41       ` sjw
2009-05-15  5:02         ` Olivier Scalbert
2009-05-15  8:05         ` Jean-Pierre Rosen
2009-05-18 10:23         ` Olivier Scalbert
2009-05-18 10:48           ` Martin
2009-05-18 10:54             ` Olivier Scalbert
2009-05-18 10:52           ` Ludovic Brenta
2009-05-18 11:09             ` Olivier Scalbert
2009-05-14 22:39       ` Jeffrey R. Carter
2009-05-14  2:59 ` anon
  -- strict thread matches above, loose matches on Subject: below --
1998-12-05  0:00 Graeme Wallace
1998-12-04  0:00 ` Simon Bracken
1998-12-04  0:00   ` Marin David Condic
1998-12-04  0:00 ` Marin David Condic
1998-12-04  0:00 ` Mats Weber
1998-12-05  0:00   ` Matthew Heaney
1998-12-04  0:00 ` Gautier.DeMontmollin
1998-12-04  0:00 ` David Botton
1998-12-05  0:00 ` Matthew Heaney
1998-12-07  0:00 ` Jeff Carter

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