comp.lang.ada
 help / color / mirror / Atom feed
From: psinntp!psinntp!jupiter.fuentez.com!hildjj@uunet.uu.net  (Joe Hildebrand)
Subject: Re: How to assign STRINGs?
Date: 26 Aug 93 14:09:09 GMT	[thread overview]
Message-ID: <HILDJJ.93Aug26100909@jupiter.fuentez.com> (raw)

>> On 25 Aug 93 21:42:26 GMT, alex@cs.umd.edu (Alex Blakemore) said:
In article <25gmc2$3cf@neomimsy.cs.umd.edu> alex@cs.umd.edu (Alex Blakemore) wr
ites:


   Alex> In article <1993Aug25.121754.20413@hellgate.utah.edu>
   Alex> matwood%peruvian.cs.utah.edu@cs.utah.edu (Mark Atwood)
   Alex> writes:
   >> how DO i assign the results of a function returning an
   >> unconstrained string to a constrained string?

   Alex> in Ada83, you have two good choices (and one sort of kludgy
   Alex> one)

   Alex> a. initialize a constant string with the function result
   Alex> cmdline : constant string := dos.get_parms;

   Alex> b. pass the function result as an in parameter to another
   Alex> subprogram text_io.put_line (dos.get_parms);

   Alex> or [worst of all]

   Alex> c. get the bounds first, declare the variable, call the
   Alex> function

   Alex> declare cmdline : string (dos.first_parm_number
   Alex> .. dos.last_parm_number); begin cmdline := dos.get_parms;
   Alex> end;

   Alex> -- this is brittle, esp if you ever add concurrency

We created a simple function that combines b. and c.


NOTE: I don't claim this is pretty, but it seems to work.  It needs
some cleaning up, since this is one one the first functions we wrote
when we were learning Ada.  In particular, Out_Str should only be an
out parameter.  PAR_STR_Length returns the number of "useful"
characters in a string by subtracting the number of trailing spaces
and ascii.nul's.


procedure PAR_STR_copy( Out_str   : in out string;
                        In_Str    : in     string;
                        Pad_Char  : in     character := ' ' ) is 

     In_str_len	 : integer := 0;
     Out_str_len : integer := 0;
     Low_Char    : integer := 0;
     Num_Chars   : integer := 0;

begin

--	Get the length of the string to be copied

   In_Str_Len := PAR_STR_length( In_Str );

--	Obtain the size of the target string

   Out_Str_Len := Out_Str'last - Out_Str'first + 1;

--	Find the first character position in the In_Str to be copied.
--	This is in case the In_Str is passed in as a string slice.

   Low_Char := In_Str'first;

--	Only copy In_Str_Len number of characters

   Num_Chars := In_Str_Len;

--	Make sure we only copy the number of characters the target
--	string can hold.

   if ( Num_Chars > Out_Str_Len ) then
      Num_Chars := Out_Str_Len;
   end if;

--	Copy the string.

   Out_Str( Out_Str'first..(Out_Str'first+Num_Chars-1)) := 
                             In_Str(Low_Char..(Low_Char+Num_Chars-1));

--	Pad out the remaining cells with the pad character.

Pad_LOOP:
   for i in (Out_Str'first+Num_Chars)..Out_Str'last loop
     Out_Str(i) := Pad_Char;
   end loop Pad_LOOP;

end PAR_STR_copy;


--

----------
Joe Hildebrand                          Fuentez Systems Concepts
hildjj@fuentez.com                      11781 Lee-Jackson Hwy, Suite 700
Software Engineer                       Fairfax, VA 22033
                                        Phone: (703)273-1447   
                                        Fax:   (703)273-2972

Standard disclaimers apply

             reply	other threads:[~1993-08-26 14:09 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1993-08-26 14:09 Joe Hildebrand [this message]
  -- strict thread matches above, loose matches on Subject: below --
1993-08-26 16:54 How to assign STRINGs? cis.ohio-state.edu!magnus.acs.ohio-state.edu!usenet.ins.cwru.edu!howland.
1993-08-26 18:17 cis.ohio-state.edu!pacific.mps.ohio-state.edu!math.ohio-state.edu!howland
1993-08-27 15:35 cis.ohio-state.edu!math.ohio-state.edu!howland.reston.ans.net!noc.near.ne
1993-08-27 19:00 Tucker Taft
1993-08-27 20:23 Robert I. Eachus
1993-09-03 18:18 dog.ee.lbl.gov!overload.lbl.gov!agate!doc.ic.ac.uk!uknet!mcsun!ub4b!cfmu!
1993-09-07 21:56 Robert I. Eachus
replies disabled

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