comp.lang.ada
 help / color / mirror / Atom feed
* [Q] Returning Strings From A Function
@ 1997-04-04  0:00 John McCabe
  1997-04-04  0:00 ` Joakim Olsson
                   ` (3 more replies)
  0 siblings, 4 replies; 24+ messages in thread
From: John McCabe @ 1997-04-04  0:00 UTC (permalink / raw)



Hello all.

I hope someone can help me as how to do this has completely slipped my
mind, and I'm having trouble finding what I'm looking for in the Ada
LRM (83) and Barnes.

I have a function, let's call it Text_IO.Name which returns a string.
If I have an object let's say File_Name, what would be the correct (or
best) way to declare that object in order for the statement:

File_Name := Text_IO.Name (File);

(or something similar with slices?) to work without me getting a
constraint error by trying to assign a string of n characters to an
object /=n characters long.

Should I be using subtypes here or what.

I'm sure this will appear to be a really stupid question, but at the
moment I'm being frustrated and I can't think straight!

Thanks in advance.


Best Regards
John McCabe <john@assen.demon.co.uk>




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

* Re: [Q] Returning Strings From A Function
  1997-04-04  0:00 [Q] Returning Strings From A Function John McCabe
@ 1997-04-04  0:00 ` Joakim Olsson
  1997-04-05  0:00 ` John McCabe
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 24+ messages in thread
From: Joakim Olsson @ 1997-04-04  0:00 UTC (permalink / raw)



John McCabe <john@assen.demon.co.uk> wrote in article
<33454165.1658515@news.demon.co.uk>...
> Hello all.
> 
> I hope someone can help me as how to do this has completely slipped my
> mind, and I'm having trouble finding what I'm looking for in the Ada
> LRM (83) and Barnes.
> 
> I have a function, let's call it Text_IO.Name which returns a string.
> If I have an object let's say File_Name, what would be the correct (or
> best) way to declare that object in order for the statement:
> 
> File_Name := Text_IO.Name (File);
> 
> (or something similar with slices?) to work without me getting a
> constraint error by trying to assign a string of n characters to an
> object /=n characters long.
> 
> Should I be using subtypes here or what.
> 
> I'm sure this will appear to be a really stupid question, but at the
> moment I'm being frustrated and I can't think straight!
> 
> Thanks in advance.
> 
> 
> Best Regards
> John McCabe <john@assen.demon.co.uk>


Hi John.

If you declare 'File_Name' as constant STRING it should work.
Using a constant declaration should work in Ada83 and Ada95,
but if I not remember things wrong, You don't have to have the
constant declaration in Ada95...


Example:

-- procedure or function
--
procedure Proc is
   File_Name : constant STRING := Text_IO.Name (File);
begin
.....
.....
end;

-- declarationblock inside a procedure or function
--
procedure Proc is
begin
   declare
      File_Name : constant STRING := Text_IO.Name (File);
   begin
      -- File_Name exists of course only in this block
      .....
      .....
   end;
.....
.....
end;

Se You, safe and sound hacking Ada...
/Jake




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

* Re: [Q] Returning Strings From A Function
  1997-04-05  0:00 ` johnherro
@ 1997-04-05  0:00   ` Robert Dewar
  1997-04-06  0:00     ` John McCabe
  1997-04-05  0:00   ` Mark & Zurima McKinney
  1 sibling, 1 reply; 24+ messages in thread
From: Robert Dewar @ 1997-04-05  0:00 UTC (permalink / raw)



John Herro said, talking of the question about dealing with a variable
length string value returned

<<Does anyone have other solutions?
>>

Most certainly! The normal solutoin is just to declare a local variable
and initialize it with the returned value:

   x : string := f(x);

That's straightforward. If you want to use pointers, then you can use

   type x is access all string;

   xv : x;

   xv := new String'(f(x));

These seems straightforward, so I am not quite sure I understand John
Herro's reference to "minor annoyance". In a language designed NOT to
require default heap allocation, the Ada semantics seem quite reasonable.
I also find John's suggested solutions unnecessarily complex (so perhaps
that's why he finds this so annoying :-)






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

* Re: [Q] Returning Strings From A Function
  1997-04-04  0:00 [Q] Returning Strings From A Function John McCabe
  1997-04-04  0:00 ` Joakim Olsson
@ 1997-04-05  0:00 ` John McCabe
  1997-04-05  0:00   ` Robert A Duff
  1997-04-05  0:00   ` Robert Dewar
  1997-04-05  0:00 ` johnherro
  1997-04-06  0:00 ` [Q] Returning Strings From A Function John McCabe
  3 siblings, 2 replies; 24+ messages in thread
From: John McCabe @ 1997-04-05  0:00 UTC (permalink / raw)



Hello all,

To everyone who responded thank you. You have put my mind straight.

I was aware that the method everyone described i.e.

declare
   File_Name : constant string := Text_IO.Name (File);
being
   :
   :
end;

would be a solution, but I felt sure there was another way to do it so
that I could declare File_Name as an object of a string type e.g.:

type File_Name_Type is new String (1..12);

then use:-

procedure ...

   File_Name : File_Name_Type;

begin
   :
   File_Name := Text_IO.Name (File);
   :
end...

or something similar. But unfortunately this is obviously not
possible.

Thanks again anyway.


Best Regards
John McCabe <john@assen.demon.co.uk>




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

* Re: [Q] Returning Strings From A Function
  1997-04-05  0:00 ` John McCabe
@ 1997-04-05  0:00   ` Robert A Duff
  1997-04-05  0:00   ` Robert Dewar
  1 sibling, 0 replies; 24+ messages in thread
From: Robert A Duff @ 1997-04-05  0:00 UTC (permalink / raw)



In article <33468b81.762963@news.demon.co.uk>,
John McCabe <john@assen.demon.co.uk> wrote:
>   File_Name : constant string := Text_IO.Name (File);

I don't remember if you were specifically asking about Ada 83, but Ada
95 also allows:

    File_Name : String := Text_IO.Name (File);

which allows you to change the characters of File_Name, but not its
length.

>type File_Name_Type is new String (1..12);
>
>then use:-
>
>procedure ...
>
>   File_Name : File_Name_Type;
>
>begin
>   :
>   File_Name := Text_IO.Name (File);
>   :
>end...
>
>or something similar. But unfortunately this is obviously not
>possible.

Did anyone suggest this:

    type String_Ptr is access String;

    procedure ...

       File_Name : String_Ptr;

    begin
       :
       File_Name := new String'(Text_IO.Name (File));
       :
    end...

?  Then you can change the length of the string by throwing away the old
one, and creating a new one on the heap.  In Ada 83, you have to worry
about storage management.  In Ada 95, you can encapsulate the storage
management using a controlled type -- and that's essentially what the
Strings.Unbounded package does.

- Bob




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

* Re: [Q] Returning Strings From A Function
  1997-04-05  0:00 ` John McCabe
  1997-04-05  0:00   ` Robert A Duff
@ 1997-04-05  0:00   ` Robert Dewar
  1997-04-06  0:00     ` John McCabe
  1 sibling, 1 reply; 24+ messages in thread
From: Robert Dewar @ 1997-04-05  0:00 UTC (permalink / raw)



John McCabe said

<<would be a solution, but I felt sure there was another way to do it so
that I could declare File_Name as an object of a string type e.g.:

type File_Name_Type is new String (1..12);

then use:-

procedure ...

   File_Name : File_Name_Type;

begin
   :
   File_Name := Text_IO.Name (File);
   :
end...

or something similar. But unfortunately this is obviously not possible>>


John, it is still very difficult to understand what you are asking for.
Your code above makes perfect sense if you want File_Name to be 12 characters
long, and of course the assignment will check for 12 characters.

That is apparently NOT what you want, so it is not clear what "this" is
that is "obviously impossible". You need to say what you have in mind.

Do you want to pad the name with blanks to make the length 12? In that
case you could write

   File_name :=
     Ada.Strings.Fixed.Head (Text_IO.Name (File), File_Name'Length));

which seems pretty clear.

If you what you wanted was a string whose maximum length was 12, but to
which smaller strings could be assigned, then Ada.Strings.Bounded is just
the ticket.

It is simply not at all clear to me from the above code what you have in
mind. When you write some perfectly legal, perfectly sensible Ada code,
and say "What I wanted was to do that, but it is impossible, you are
obviously trying to say that you expected some other semantics from the
code than the semantics defined in Ada. That's fine, anyone is allowed
to say such things, and it is often interesting to know what features 
people want, but it is inmpossible to *guess* from this perfectly 
reasonable Ada code what you wanted it to do that was different from
what it actually does in Ada.

So why not try again saying exactly what you have in mind in a clear form.
Don
't try to write Ada, since apparently you think you can't do what you
want to in Ada. Instead describe informally what it is that you think
you cannot do that you would like to do.





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

* Re: [Q] Returning Strings From A Function
  1997-04-05  0:00 ` johnherro
  1997-04-05  0:00   ` Robert Dewar
@ 1997-04-05  0:00   ` Mark & Zurima McKinney
  1997-04-07  0:00     ` Jon S Anthony
                       ` (3 more replies)
  1 sibling, 4 replies; 24+ messages in thread
From: Mark & Zurima McKinney @ 1997-04-05  0:00 UTC (permalink / raw)



johnherro@aol.com wrote:
> 
> procedure Set(Target: out String;
>               Target_Len: out Natural;
>               Source: in String) is
> begin
>    Target_Len := Source'Length;
>    Target(1 .. Source'Length) := Source;


maybe Target(Target'First .. Target'First + Source'Length -1);


> end Set;
> 


Mark McKinney




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

* Re: [Q] Returning Strings From A Function
  1997-04-04  0:00 [Q] Returning Strings From A Function John McCabe
  1997-04-04  0:00 ` Joakim Olsson
  1997-04-05  0:00 ` John McCabe
@ 1997-04-05  0:00 ` johnherro
  1997-04-05  0:00   ` Robert Dewar
  1997-04-05  0:00   ` Mark & Zurima McKinney
  1997-04-06  0:00 ` [Q] Returning Strings From A Function John McCabe
  3 siblings, 2 replies; 24+ messages in thread
From: johnherro @ 1997-04-05  0:00 UTC (permalink / raw)



(John McCabe) writes:
> [In Ada 83] I have a function ... which returns a string.
> If I have an object, let's say File_Name, what would be
> the correct (or best) way to declare that object in order
> for the statement:
>
> File_Name := Text_IO.Name (File);
>
> ... to work without me getting a constraint error by trying
> to assign a string of n characters to an object /=n 
> characters long?

This has always been a minor annoyance with Ada 83.  Of course it's not a problem with Ada 95's unbounded strings.

Obviously you can't use direct assignment if the strings have different lengths.  One solution is to use the package Text_Handler recommended in Sec. 7.6 of the Ada 83 LRM.  The LRM supplies only the
package specification, but writing the body is only about an hour's work.  (Some Ada 83 compilers, such as Open Ada, come with  the Text_Handler package ready to use.)

A similar, but simpler, solution is simply to declare *two* variables, File_Name and File_Name_Len.  You can write a simple procedure to assign to them together:

procedure Set(Target: out String;
              Target_Len: out Natural;
              Source: in String) is
begin
   Target_Len := Source'Length;
   Target(1 .. Source'Length) := Source;
end Set;

In the second executable line above, you can't replace Source'Length with Target_Len (in Ada 83) unless you make Target_Len an "in out" parameter.  Of course, the call to the procedure would be

Set(File_Name, File_Name_Len, Text_IO.Name(File));

This call isn't particularly easy to read, so named parameter association is definitely an option to consider here.

Does anyone have other solutions?

- John Herro
Software Innovations Technology
http://members.aol.com/AdaTutor
ftp://members.aol.com/AdaTutor






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

* Re: [Q] Returning Strings From A Function
  1997-04-05  0:00   ` Robert Dewar
@ 1997-04-06  0:00     ` John McCabe
  1997-04-06  0:00       ` Robert Dewar
  0 siblings, 1 reply; 24+ messages in thread
From: John McCabe @ 1997-04-06  0:00 UTC (permalink / raw)



dewar@merv.cs.nyu.edu (Robert Dewar) wrote:

>John Herro said, talking of the question about dealing with a variable
>length string value returned
>
><<Does anyone have other solutions?
>>>
>
>Most certainly! The normal solutoin is just to declare a local variable
>and initialize it with the returned value:
>
>   x : string := f(x);
>
>That's straightforward. If you want to use pointers, then you can use
>
>   type x is access all string;
                     ^^^ In Ada83? I don't think so!

<..snip..>

Best Regards
John McCabe <john@assen.demon.co.uk>




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

* Re: [Q] Returning Strings From A Function
  1997-04-05  0:00   ` Robert Dewar
@ 1997-04-06  0:00     ` John McCabe
  1997-04-06  0:00       ` Robert Dewar
  1997-04-06  0:00       ` Matthew Heaney
  0 siblings, 2 replies; 24+ messages in thread
From: John McCabe @ 1997-04-06  0:00 UTC (permalink / raw)



dewar@merv.cs.nyu.edu (Robert Dewar) wrote:

>John McCabe said
<..snip..>

>John, it is still very difficult to understand what you are asking for.
>Your code above makes perfect sense if you want File_Name to be 12 characters
>long, and of course the assignment will check for 12 characters.

Exactly - but I want the assignment to check for <=12 characters.

>That is apparently NOT what you want, so it is not clear what "this" is
>that is "obviously impossible". You need to say what you have in mind.
>
>Do you want to pad the name with blanks to make the length 12? In that
>case you could write
>
>   File_name :=
>     Ada.Strings.Fixed.Head (Text_IO.Name (File), File_Name'Length));
>
>which seems pretty clear.

And not in Ada83, and not what I want.

>If you what you wanted was a string whose maximum length was 12, but to
>which smaller strings could be assigned, then Ada.Strings.Bounded is just
>the ticket.

EXACTLY - but it's not in Ada83.

>It is simply not at all clear to me from the above code what you have in
>mind. When you write some perfectly legal, perfectly sensible Ada code,
>and say "What I wanted was to do that, but it is impossible, you are
>obviously trying to say that you expected some other semantics from the
>code than the semantics defined in Ada. That's fine, anyone is allowed
>to say such things, and it is often interesting to know what features 
>people want, but it is inmpossible to *guess* from this perfectly 
>reasonable Ada code what you wanted it to do that was different from
>what it actually does in Ada.

I don't really know what you're waffling on about here since
all I originally asked for really was an example of how to use
functions that return strings in Ada83 (if you check my original
article, it mentions LRM83, not ARM95, hence if you think about it, it
should be obvious I'm using Ada83 despite it not being explicitly
stated).

>So why not try again saying exactly what you have in mind in a clear form.

See above.

>Don't try to write Ada, since apparently you think you can't do what you
>want to in Ada.

Yes, but in all your postings I have noticed you interpret Ada to mean
Ada95, and in that language I believe I can do what I'm trying to do.
Ada83, however does not let me, but I can get by without doing that.
As someone mentioned, string handling in Ada83 has its little
annoyances, and its obvious there has been major revisions placed in
the Ada95 definition.

>Instead describe informally what it is that you think you cannot do that
>you would like to do.



Best Regards
John McCabe <john@assen.demon.co.uk>




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

* Re: [Q] Returning Strings From A Function
  1997-04-04  0:00 [Q] Returning Strings From A Function John McCabe
                   ` (2 preceding siblings ...)
  1997-04-05  0:00 ` johnherro
@ 1997-04-06  0:00 ` John McCabe
  3 siblings, 0 replies; 24+ messages in thread
From: John McCabe @ 1997-04-06  0:00 UTC (permalink / raw)



Hello,

I now have the calrification and information I need. Thanks for
everyone's help, but let's give this thread a rest. It's trivial and
getting out of hand. I know I certainly won't be contributing to it
again.

Thanks again.


Best Regards
John McCabe <john@assen.demon.co.uk>




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

* Re: [Q] Returning Strings From A Function
  1997-04-06  0:00     ` John McCabe
@ 1997-04-06  0:00       ` Robert Dewar
  0 siblings, 0 replies; 24+ messages in thread
From: Robert Dewar @ 1997-04-06  0:00 UTC (permalink / raw)



John said

<<>   type x is access all string;
                     ^^^ In Ada83? I don't think so!>>

First of all John, you have been using the plain Ada word, to refer to
Ada 83, please don't it is confusing! We have agreed in this newsgroup
to always say Ada 83, and not plain Ada when talking about things where
it makes a difference.

Second, this is a trivial response. My solution works fine in Ada 83,
just take out the "all".





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

* Re: [Q] Returning Strings From A Function
  1997-04-06  0:00     ` John McCabe
@ 1997-04-06  0:00       ` Robert Dewar
  1997-04-06  0:00         ` Nick Roberts
  1997-04-06  0:00       ` Matthew Heaney
  1 sibling, 1 reply; 24+ messages in thread
From: Robert Dewar @ 1997-04-06  0:00 UTC (permalink / raw)



John says

<<Yes, but in all your postings I have noticed you interpret Ada to mean
Ada95, and in that language I believe I can do what I'm trying to do.
Ada83, however does not let me, but I can get by without doing that.
As someone mentioned, string handling in Ada83 has its little
annoyances, and its obvious there has been major revisions placed in
the Ada95 definition.>>


That's a bit confused. There are no real language features added to
Ada 95, just some standard libraries. In Ada 83 you could

(a) code these yourself
(b) use the ADAR components which mimic the Ada 95 facilities in Ada 83

But you can certainly do what you want. Yes, it is more convenient in
Ada 95 to have those right at hand, but a real life Ada program ends up
reusing all sorts of useful Ada library components that are not in the
Ada 95 RM.

No one sits around saying "gosh in Ada 83, it is impossible to write a dot
product, there is no built in feature in the language -- they just program
a loop!" Well we have the same situation here, you can do what you want
perfectly well in Ada 83, you just have to do some programming.

If you find that programming too burdensome -- then all the more reason
to switch to Ada 95 one of these days :-)





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

* Re: [Q] Returning Strings From A Function
  1997-04-06  0:00       ` Robert Dewar
@ 1997-04-06  0:00         ` Nick Roberts
  1997-04-07  0:00           ` Robert A Duff
  0 siblings, 1 reply; 24+ messages in thread
From: Nick Roberts @ 1997-04-06  0:00 UTC (permalink / raw)





John, did you see my previous post on this subject? In case not, have a
quick butcher's at the following.


declare
   subtype Filename_Index is Integer range 1..12;
   type Filename_String is array (Filename_Index range <>) of Character;
   ...
   FS: Filename_String := ""; -- unconstrained type, so must be initialised
   SS: String := "";
   ...
begin
   ...
   FS := Text_IO.Name(F); -- constraint error raised if longer than 12
   ...
   SS := "Name is: "+Filename_String(FS); -- note the need to convert
   ...
end;


Hopefully this gives you the flavour of it.

Nick.





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

* Re: [Q] Returning Strings From A Function
  1997-04-06  0:00     ` John McCabe
  1997-04-06  0:00       ` Robert Dewar
@ 1997-04-06  0:00       ` Matthew Heaney
  1 sibling, 0 replies; 24+ messages in thread
From: Matthew Heaney @ 1997-04-06  0:00 UTC (permalink / raw)



In article <33478738.2129057@news.demon.co.uk>, john@assen.demon.co.uk
(John McCabe) wrote:

>
>>John, it is still very difficult to understand what you are asking for.
>>Your code above makes perfect sense if you want File_Name to be 12 characters
>>long, and of course the assignment will check for 12 characters.
>
>Exactly - but I want the assignment to check for <=12 characters.

In Ada 83, you can just assign the function return value to a (constant)
object, and then do whatever you want with it (including assigning it to a
fixed-length string).  For example,

subtype Name_String is String (1 .. 12);

procedure Get_Name (Name : out Name_String) is

   The_Name : constant String := Text_IO.Name (The_File);

   Pad_Length : constant Natural := Name'Length - The_Name'Length;

   Pad : constant String (1 .. Pad_Length) := (others => ' ');

begin

   Name := The_Name & Pad;

end;

A hipper technique is to use a string buffer, as follows:

subtype Name_Length_Range is Natural range 0 .. 12;

type Name_Buffer (Length : Name_Length_Range := 0) is
   record
      Name : String (1 .. Length);
   end record;

procedure Get_Name (Name : out Name_Buffer) is

   The_Name : constant String := Text_IO.Name (The_File);

begin

   Name := (The_Name'Length, The_Name);

end;

Hope that helps,

Matt

--------------------------------------------------------------------
Matthew Heaney
Software Development Consultant
<mailto:matthew_heaney@acm.org>
(818) 985-1271




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

* Re: [Q] Returning Strings From A Function
  1997-04-07  0:00     ` johnherro
@ 1997-04-07  0:00       ` Robert Dewar
  0 siblings, 0 replies; 24+ messages in thread
From: Robert Dewar @ 1997-04-07  0:00 UTC (permalink / raw)



John Herro said

<<Absolutely!  I've often warned my students not to assume that the index of a str
ing starts at 1, and even have that warning in my Ada Tutor program.  Then in my
 haste I made that error myself in the
above posting!
>.

Of course it is quite reasonable in a given environment to have the convention
that aoll functions returning strings always return a low bound of 1, and then
to count on this (the string packages in Annex A work this way).





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

* Re: [Q] Returning Strings From A Function
  1997-04-05  0:00   ` Mark & Zurima McKinney
@ 1997-04-07  0:00     ` Jon S Anthony
  1997-04-07  0:00       ` johnherro
  1997-04-07  0:00     ` johnherro
                       ` (2 subsequent siblings)
  3 siblings, 1 reply; 24+ messages in thread
From: Jon S Anthony @ 1997-04-07  0:00 UTC (permalink / raw)



In article <19970407112901.HAA07881@ladder01.news.aol.com> johnherro@aol.com writes:

> I wrote:
> >>    Target(1 .. Source'Length) := Source;
> 
> Mark & Zurima McKinney <mckmark@us.net> wrote:
> > maybe Target(Target'First .. Target'First + Source'Length -1);
> 
> Absolutely!  I've often warned my students not to assume that the
> index of a string starts at 1, and even have that warning in my Ada
> Tutor program.  Then in my haste I made that error myself in the
> above posting!

IMO, the simplest way of getting at the (Target'First .. Target'First
+ Source'Length -1 business is to write:

    Target(Source'Range) := Source;

Of course this may not be what you are after since the slice
represented here may not be the one you really want...

/Jon

-- 
Jon Anthony
Organon Motives, Inc.
Belmont, MA 02178
617.484.3383
jsa@organon.com





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

* Re: [Q] Returning Strings From A Function
  1997-04-07  0:00     ` Jon S Anthony
@ 1997-04-07  0:00       ` johnherro
  0 siblings, 0 replies; 24+ messages in thread
From: johnherro @ 1997-04-07  0:00 UTC (permalink / raw)



Mark & Zurima McKinney <mckmark@us.net> wrote:
> Target(Target'First .. Target'First + Source'Length -1);

jsa@alexandria (Jon S Anthony) wrote:
> Target(Source'Range) := Source;

But Source'Range assumes that the index range of Source is appropriate to Target.  Target'First .. Target'First + Source'Length -1 assumes only that the Target string is long enough.

- John Herro
Software Innovations Technology
http://members.aol.com/AdaTutor
ftp://members.aol.com/AdaTutor






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

* Re: [Q] Returning Strings From A Function
  1997-04-08  0:00             ` Nick Roberts
@ 1997-04-07  0:00               ` Matthew Heaney
  0 siblings, 0 replies; 24+ messages in thread
From: Matthew Heaney @ 1997-04-07  0:00 UTC (permalink / raw)



In article <01bc43b8$a4c441a0$09f682c1@xhv46.dial.pipex.com>, "Nick
Roberts" <Nick.Roberts@dial.pipex.com> wrote:


>Argh! The embarrassment! Sudden brainstorm, Bob (my excuse being that I am
>using a lot of different languages at the moment (pretty feeble, eh?)).
>Anyway, what I _should_ have written was this...
>
>
>declare
>   F: Text_IO.File_Type;
>   ...
>   type Filename_String is array (1..12 range <>) of Character;
>   type Filename_Ref is access Filename_String;
>   ...
>   S: Filename_Ref;
>   ...
>begin
>   ...
>   S := new Filename_String'(Text_IO.Name(F)); -- constraint error raised
>if name longer than 12 chars
>   ...
>   Text_IO.Put("Name of file is: "+String(S.all)); -- note the need to
>dereference and convert
>   ...
>end;

Nick: 

This is an equally confusing solution.  No heap is required:

declare
   The_Name : String (1 .. 12);
begin
   The_Name := Text_IO.Name (F);
   -- This assignment will raise Constraint_Error if the length of the 
   -- return value is not _exactly_ 12.  (It could be lesser or greater.)

   Text_IO.Put ("Name is " & The_Name);
exception
   when Constraint_Error => ...;
end;

Two issues:

1.  Do not use heap when the stack will do.

2. Don't create new string types, ie use subtypes of Standard.String unless
you have a compelling need not to.  In this case, you wanted a string of
length 12, so just use Standard.String:

The_Name : String (1 .. 12);  -- an "anonymous" subtype

subtype Name_String is String (1 .. 12);
The_Name : Name_String;  -- a named subtype

KISS is the operative word.

Matt

--------------------------------------------------------------------
Matthew Heaney
Software Development Consultant
<mailto:matthew_heaney@acm.org>
(818) 985-1271




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

* Re: [Q] Returning Strings From A Function
  1997-04-05  0:00   ` Mark & Zurima McKinney
  1997-04-07  0:00     ` Jon S Anthony
@ 1997-04-07  0:00     ` johnherro
  1997-04-07  0:00       ` Robert Dewar
  1997-04-08  0:00     ` Jeff Carter
  1997-04-09  0:00     ` Looking for an Ada SCIENTIFIC UNITS checking package Ron House
  3 siblings, 1 reply; 24+ messages in thread
From: johnherro @ 1997-04-07  0:00 UTC (permalink / raw)



I wrote:
>>    Target(1 .. Source'Length) := Source;

Mark & Zurima McKinney <mckmark@us.net> wrote:
> maybe Target(Target'First .. Target'First + Source'Length -1);

Absolutely!  I've often warned my students not to assume that the index of a string starts at 1, and even have that warning in my Ada Tutor program.  Then in my haste I made that error myself in the
above posting!

- John Herro
Software Innovations Technology
http://members.aol.com/AdaTutor
ftp://members.aol.com/AdaTutor





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

* Re: [Q] Returning Strings From A Function
  1997-04-06  0:00         ` Nick Roberts
@ 1997-04-07  0:00           ` Robert A Duff
  1997-04-08  0:00             ` Nick Roberts
  0 siblings, 1 reply; 24+ messages in thread
From: Robert A Duff @ 1997-04-07  0:00 UTC (permalink / raw)



In article <01bc42af$e32e3ea0$90f482c1@xhv46.dial.pipex.com>,
Nick Roberts <Nick.Roberts@dial.pipex.com> wrote:
>   FS: Filename_String := ""; -- unconstrained type, so must be initialised

FS is forever constrained to length zero.

>   FS := Text_IO.Name(F); -- constraint error raised if longer than 12

No, this will raise C_E, unless the name is of length zero.

Ada's built-in arrays cannot change size.

- Bob




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

* Re: [Q] Returning Strings From A Function
  1997-04-05  0:00   ` Mark & Zurima McKinney
  1997-04-07  0:00     ` Jon S Anthony
  1997-04-07  0:00     ` johnherro
@ 1997-04-08  0:00     ` Jeff Carter
  1997-04-09  0:00     ` Looking for an Ada SCIENTIFIC UNITS checking package Ron House
  3 siblings, 0 replies; 24+ messages in thread
From: Jeff Carter @ 1997-04-08  0:00 UTC (permalink / raw)



Guess I ought to put my 2 (cents|pence|centimes|etc :) in.

The poster requested information on how to handle the return value from
a function that returns type String in Ada 83. Given

function F return String;

the simplest is

I. Use it to initialize a constant

S : constant String := F;

This is fine if he only needs to store the value. If he also needs to
modify the value, things get a little more complicated. The simple
answer is

II. Do I. and copy the value to a variable

Cs : constant String := F;
S : String (Cs'range) := Cs;

This is often acceptable. Another way is

III. Obtain a variable-length string handling package, that provides for
conversions to and from String

with VSH;
...
S : VSH.V_String := VSH.Convert (F);

This is sometimes overkill. Intermediate complexity is

IV. Write a small function to store a length and value

type V_String is record
   Length : Natural := 0;
   Value  : String (1 .. Max);
end record;

function Convert (S : String) return V_String is
   Result : V_String;
begin -- Convert
   Result.Length := S'Length;
   Result.Value (1 .. S'Length) := S;

   return Result;
end Convert;
...
S : V_String := Convert (F);

Convert raises Constraint_Error if S won't fit in a V_String. It is also
possible for Convert to silently truncate.

A common objection to II. is that it "wastes space." Since the strings
found in real applications are usually short, this is not a real
concern. The poster has since indicated that he's interested in strings
<= 12 characters long, so I. or II. should work for him.

A common objection to III. is that it's overkill and links in a bunch of
excess stuff the program doesn't need. This is also often not a real
concern. There are also objections based on NIH or the cost of writing
or buying such a package. A good variable-length string package is an
essential part of the standard library for an Ada-83 project. Any
compentent software engineer can develop one in a few days. PragmAda
Software Engineering will sell you one for $25.

IV. attempts to overcome the objections to II. and III. This may
complicate the software with no true benefit.

In Ada (95), one can write

S : String := F;

and variable-length string packages are part of the language-defined
standard library.
-- 
Jeff Carter  ( carter @ innocon . com )

"Now go away, or I shall taunt you a second time." Monty Python & the
Holy Grail




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

* Re: [Q] Returning Strings From A Function
  1997-04-07  0:00           ` Robert A Duff
@ 1997-04-08  0:00             ` Nick Roberts
  1997-04-07  0:00               ` Matthew Heaney
  0 siblings, 1 reply; 24+ messages in thread
From: Nick Roberts @ 1997-04-08  0:00 UTC (permalink / raw)




Robert A Duff <bobduff@world.std.com> wrote in article
<E89sJq.62y@world.std.com>...
> In article <01bc42af$e32e3ea0$90f482c1@xhv46.dial.pipex.com>,
> Nick Roberts <Nick.Roberts@dial.pipex.com> wrote:
> >   FS: Filename_String := ""; -- unconstrained type, so must be
initialised
> 
> FS is forever constrained to length zero.
> 
> >   FS := Text_IO.Name(F); -- constraint error raised if longer than 12
> 
> No, this will raise C_E, unless the name is of length zero.
> 
> Ada's built-in arrays cannot change size.


Argh! The embarrassment! Sudden brainstorm, Bob (my excuse being that I am
using a lot of different languages at the moment (pretty feeble, eh?)).
Anyway, what I _should_ have written was this...


declare
   F: Text_IO.File_Type;
   ...
   type Filename_String is array (1..12 range <>) of Character;
   type Filename_Ref is access Filename_String;
   ...
   S: Filename_Ref;
   ...
begin
   ...
   S := new Filename_String'(Text_IO.Name(F)); -- constraint error raised
if name longer than 12 chars
   ...
   Text_IO.Put("Name of file is: "+String(S.all)); -- note the need to
dereference and convert
   ...
end;


John: I hope I did not end up confusing you. Many apologies. The above will
actually work, and will hopefully be a relatively simple solution to your
problem.

Nick.






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

* Looking for an Ada SCIENTIFIC UNITS checking package
  1997-04-05  0:00   ` Mark & Zurima McKinney
                       ` (2 preceding siblings ...)
  1997-04-08  0:00     ` Jeff Carter
@ 1997-04-09  0:00     ` Ron House
  3 siblings, 0 replies; 24+ messages in thread
From: Ron House @ 1997-04-09  0:00 UTC (permalink / raw)



I posted this question a few days ago, but it doesn't
seem to have made it to the newsgroup. Apologies if
anyone gets the query twice.

I am looking for a package (library, whatever) that
enables me to check scientific units - prevent
addition of length to temperature, etc. I know there
is one, but I've lost my reference to it.

Thanks for any help anyone can give me.

-- 
Ron House

house@usq.edu.au




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

end of thread, other threads:[~1997-04-09  0:00 UTC | newest]

Thread overview: 24+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1997-04-04  0:00 [Q] Returning Strings From A Function John McCabe
1997-04-04  0:00 ` Joakim Olsson
1997-04-05  0:00 ` John McCabe
1997-04-05  0:00   ` Robert A Duff
1997-04-05  0:00   ` Robert Dewar
1997-04-06  0:00     ` John McCabe
1997-04-06  0:00       ` Robert Dewar
1997-04-06  0:00         ` Nick Roberts
1997-04-07  0:00           ` Robert A Duff
1997-04-08  0:00             ` Nick Roberts
1997-04-07  0:00               ` Matthew Heaney
1997-04-06  0:00       ` Matthew Heaney
1997-04-05  0:00 ` johnherro
1997-04-05  0:00   ` Robert Dewar
1997-04-06  0:00     ` John McCabe
1997-04-06  0:00       ` Robert Dewar
1997-04-05  0:00   ` Mark & Zurima McKinney
1997-04-07  0:00     ` Jon S Anthony
1997-04-07  0:00       ` johnherro
1997-04-07  0:00     ` johnherro
1997-04-07  0:00       ` Robert Dewar
1997-04-08  0:00     ` Jeff Carter
1997-04-09  0:00     ` Looking for an Ada SCIENTIFIC UNITS checking package Ron House
1997-04-06  0:00 ` [Q] Returning Strings From A Function John McCabe

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