comp.lang.ada
 help / color / mirror / Atom feed
From: mheaney@ni.net (Matthew Heaney)
Subject: Re: How do I allocate strings of variable length at runtime?
Date: 1997/09/26
Date: 1997-09-26T00:00:00+00:00	[thread overview]
Message-ID: <mheaney-ya023680002609972244450001@news.ni.net> (raw)
In-Reply-To: 342bbda6.153101@news.rmi.de


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




  reply	other threads:[~1997-09-26  0:00 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1997-09-26  0:00 How do I allocate strings of variable length at runtime? Oliver Hilgendorf
1997-09-26  0:00 ` Matthew Heaney [this message]
1997-09-29  0:00   ` Marc Bejerano
1997-10-07  0:00     ` Robert I. Eachus
1997-09-26  0:00 ` William A Whitaker
replies disabled

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