comp.lang.ada
 help / color / mirror / Atom feed
* How do I allocate strings of variable length at runtime?
@ 1997-09-26  0:00 Oliver Hilgendorf
  1997-09-26  0:00 ` William A Whitaker
  1997-09-26  0:00 ` Matthew Heaney
  0 siblings, 2 replies; 5+ messages in thread
From: Oliver Hilgendorf @ 1997-09-26  0:00 UTC (permalink / raw)



I want to open several textfiles using text_io.open. The length of
each filename is only know at runtime. However if I define a string of
the maximum filename-length to hold the filename, all trailing blanks
are also passed to text_io.open and this results in name_error because
a file with trailing blanks does not exist.
So I have to pass a string that is defined with exactly the same
length as the actual filename.
How can I define a string object with variable length at runtime?

Oliver




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

* Re: How do I allocate strings of variable length at runtime?
  1997-09-26  0:00 How do I allocate strings of variable length at runtime? Oliver Hilgendorf
@ 1997-09-26  0:00 ` William A Whitaker
  1997-09-26  0:00 ` Matthew Heaney
  1 sibling, 0 replies; 5+ messages in thread
From: William A Whitaker @ 1997-09-26  0:00 UTC (permalink / raw)
  To: Oliver Hilgendorf


Oliver Hilgendorf wrote:
> 
> I want to open several textfiles using text_io.open. The length of
> each filename is only know at runtime. However if I define a string of
> the maximum filename-length to hold the filename, all trailing blanks
> are also passed to text_io.open and this results in name_error because
> a file with trailing blanks does not exist.
> So I have to pass a string that is defined with exactly the same
> length as the actual filename.
> How can I define a string object with variable length at runtime?
> 
> Oliver


You can use the function Trim in Ada.Strings.Fixed.

(You probably want to extract this function along with what other string
handling functionality you use and put them in your own string handling
package rather than with the full package every time.)

However you obtain your Sloppy_String pass it something like this:

Open(Input, In_File, Trim(Sloppy_String, Both);

Unfortunately you have to put in the additional parameter, Both means
clean up the blanks at both ends (be safe).  You would think this
function would be defaulted to that, but it is not, although there are
several overloads and several defaults.

Whitaker




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

* Re: How do I allocate strings of variable length at runtime?
  1997-09-26  0:00 How do I allocate strings of variable length at runtime? Oliver Hilgendorf
  1997-09-26  0:00 ` William A Whitaker
@ 1997-09-26  0:00 ` Matthew Heaney
  1997-09-29  0:00   ` Marc Bejerano
  1 sibling, 1 reply; 5+ messages in thread
From: Matthew Heaney @ 1997-09-26  0:00 UTC (permalink / raw)



In article <342bbda6.153101@news.rmi.de>, hilgend@rmi.de (Oliver
Hilgendorf) wrote:

>I want to open several textfiles using text_io.open. The length of
>each filename is only know at runtime. However if I define a string of
>the maximum filename-length to hold the filename, all trailing blanks
>are also passed to text_io.open and this results in name_error because
>a file with trailing blanks does not exist.
>So I have to pass a string that is defined with exactly the same
>length as the actual filename.
>How can I define a string object with variable length at runtime?

You need to specify more information: What is the source of filenames?  Do
you have to store the filenames prior to opening the files?

What you need to do is use a bounded string or an unbounded string.  These
data structures keep track of the logical length of the filename, even if
the physical length of the data structure is larger.

Your basic problem is that you're using an array as a data structure
directly, when arrays (and similarly linked lists) should only be used to
implement more abstract data structures (such as a bounded buffer).

A rule of thumb for determining whether you need a better abstraction is,
Is all of the array used to store the data, or only part?  Is there another
piece of data used to keep track of the last index position of the "good"
data, after which is unused garbage?  If so, then you want to bundle them
together as part of another, higher-layer-of-abstraction data structure. 
In fact, for strings, that data structure has already been written for you:
Ada.Strings.Bounded and Ada.Strings.Unbounded.

Email me if you have any more questions.

Matt

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




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

* Re: How do I allocate strings of variable length at runtime?
  1997-09-26  0:00 ` Matthew Heaney
@ 1997-09-29  0:00   ` Marc Bejerano
  1997-10-07  0:00     ` Robert I. Eachus
  0 siblings, 1 reply; 5+ messages in thread
From: Marc Bejerano @ 1997-09-29  0:00 UTC (permalink / raw)



Just use Ada.Strings.Unbounded and when you need to call Text_IO.Open simply
pass it the To_String version of your variable. For example:

with Ada.Strings.Unbounded; use Ada.Strings.Unbounded;
with Ada.Text_IO; use Ada.Text_IO;

...
fileName: Unbounded_String;
inputFile: File_Type;

fileName := To_Unbounded_String ("whatever.data");
Open (inputFile, In_File, To_String (fileName);

This will solve your dilemma regarding the blanks at he end of the string.
Alternatively, you could use the Ada.Strings.Maps package and physically
Trim(...) the string prior to passing it to the Open call.

-Marc

P.S. Remember, keep it simple.

Matthew Heaney wrote:

> In article <342bbda6.153101@news.rmi.de>, hilgend@rmi.de (Oliver
> Hilgendorf) wrote:
>
> >I want to open several textfiles using text_io.open. The length of
> >each filename is only know at runtime. However if I define a string of
> >the maximum filename-length to hold the filename, all trailing blanks
> >are also passed to text_io.open and this results in name_error because
> >a file with trailing blanks does not exist.
> >So I have to pass a string that is defined with exactly the same
> >length as the actual filename.
> >How can I define a string object with variable length at runtime?
>
> You need to specify more information: What is the source of filenames?  Do
> you have to store the filenames prior to opening the files?
>

<snip>






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

* Re: How do I allocate strings of variable length at runtime?
  1997-09-29  0:00   ` Marc Bejerano
@ 1997-10-07  0:00     ` Robert I. Eachus
  0 siblings, 0 replies; 5+ messages in thread
From: Robert I. Eachus @ 1997-10-07  0:00 UTC (permalink / raw)



In article <342FDBDA.67D8427E@linkabit.titan.com> Marc Bejerano <t_mjb@linkabit.titan.com> writes:

  > Just use Ada.Strings.Unbounded and when you need to call
  > Text_IO.Open simply pass it the To_String version of your
  > variable.
 
  Correct, very correct! ;-)

  > This will solve your dilemma regarding the blanks at he end of the string.

  Note that this solves the dilemma by eliminating it completely.
Unbounded strings are an abstract type which have the property of a
current length, but no concept of padding.  Exactly what you want.
The actual implementation may or may not be complex, but the author
doesn't even have to think about that.


--

					Robert I. Eachus

with Standard_Disclaimer;
use  Standard_Disclaimer;
function Message (Text: in Clever_Ideas) return Better_Ideas is...




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

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

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1997-09-26  0:00 How do I allocate strings of variable length at runtime? Oliver Hilgendorf
1997-09-26  0:00 ` William A Whitaker
1997-09-26  0:00 ` Matthew Heaney
1997-09-29  0:00   ` Marc Bejerano
1997-10-07  0:00     ` Robert I. Eachus

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