comp.lang.ada
 help / color / mirror / Atom feed
From: johnherro@aol.com
Subject: Re: [Q] Returning Strings From A Function
Date: 1997/04/05
Date: 1997-04-05T00:00:00+00:00	[thread overview]
Message-ID: <19970405130401.IAA25121@ladder01.news.aol.com> (raw)
In-Reply-To: 33454165.1658515@news.demon.co.uk


(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






  parent reply	other threads:[~1997-04-05  0:00 UTC|newest]

Thread overview: 24+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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       ` Matthew Heaney
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-05  0:00 ` johnherro [this message]
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
replies disabled

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