From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=0.2 required=5.0 tests=BAYES_00,INVALID_MSGID, REPLYTO_WITHOUT_TO_CC autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,d93b7c6dd17cbc81 X-Google-Attributes: gid103376,public From: Marin David Condic Subject: Re: Strings and reading from a file Date: 1999/05/12 Message-ID: <37399913.BD928DD1@pwfl.com>#1/1 X-Deja-AN: 477033948 Content-Transfer-Encoding: 7bit Sender: condicma@bogon.pwfl.com References: <7han2q$jkp$1@news.iinet.net.au> <7hbnnl$rca$1@nnrp1.deja.com> <7hc358$ha5$1@news.iinet.net.au> Content-Type: text/plain; charset=us-ascii Organization: Pratt & Whitney Mime-Version: 1.0 Reply-To: diespammer@pwfl.com Newsgroups: comp.lang.ada Date: 1999-05-12T00:00:00+00:00 List-Id: Cameron Hodge wrote: > > True enough. I was just hoping not to have to create another array. > You may be missing the point. Look at the packages in appendix A.4 of the ARM. Specifically, I'd recommend Ada.Strings.Unbounded in A.4.5. You have a type called Unbounded_String. You also have a function: "Length" that operates on Unbounded_String. Also functions To_Unbounded_String and To_String will be useful to you here. If you have an array of Unbounded_String, you can store whatever strings you read in and their lengths will be remembered. As you're reading in strings, you get the length as one of the returned parameters. You can "slice" the string to be just that length - thus removing anything that is garbage. The trick is to write yourself a function that returns a value of type String. Internally, it declares some sufficiently large String variable to handle a worst case, but it only returns the slice. (The space is recovered off the stack when the function returns, so it isn't wasted.) Do something like: function Next_Line return String is Str : String (1..256) ; Len : Natural ; begin Get_Line ( Item => Str, Last => Len) ; return Str (1..Len) ; end Next_Line ; You can dress up the function to make it look as pretty as you like - maybe even make it a general-purpose utility by adding a file parameter, etc. Somewhere between this and the features you have in Ada.Strings.Unbounded you can do whatever you like with flexible sized strings and you won't have to fuss with leftover garbage, unused space or anything else inconvenient. Of course, the price you pay for the convenience may be some additional execution speed or memory utilization, but unless you are working on something with a real time sensitivity, this should not be much of a concern. MDC -- Marin David Condic Real Time & Embedded Systems, Propulsion Systems Analysis United Technologies, Pratt & Whitney, Large Military Engines M/S 731-95, P.O.B. 109600, West Palm Beach, FL, 33410-9600 ***To reply, remove "bogon" from the domain name.*** Visit my web page at: http://www.flipag.net/mcondic