Board index » delphi » Passing strings to and from procedure/functions

Passing strings to and from procedure/functions

Hi, I'm trying to write a replacement for the readln command in tp(dos) with
better operation. It works in function form when the headers are...
function getstring(x,y,length: integer): string;
and will work with fixed-length strings, chars and open strings. However when
I tried to write it as a procedure for ease of use it only allows me to pass
open strings or whatever sort of string I've declared in its parameters like
such
procedure getstring(var st: string; x,y,length); only allows me to pass open
strings. I would eventually like to get it to work exactly like readln; in
that in can accept any sort of string or char. Is this possible?
Thanks

--
Help preserve Usenets precious resources
  --Recycle old jokes NOW --

 

Re:Passing strings to and from procedure/functions


This is a multi-part message in MIME format.

--------------6C88F147C96
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit

Quote
> Hi, I'm trying to write a replacement for the readln command in tp(dos) with
> better operation. It works in function form when the headers are...
> function getstring(x,y,length: integer): string;
> and will work with fixed-length strings, chars and open strings. However when
> I tried to write it as a procedure for ease of use it only allows me to pass
> open strings or whatever sort of string I've declared in its parameters like
> such
> procedure getstring(var st: string; x,y,length); only allows me to pass open
> strings. I would eventually like to get it to work exactly like readln; in
> that in can accept any sort of string or char. Is this possible?

   Try this routine (which you'll have to modify some):

--------------6C88F147C96
Content-Type: text/plain; charset=us-ascii; name="Myread"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline; filename="Myread"

procedure MYREAD (var ST : string; Q : Byte);  { internal string }
var C1,C2 : char;
begin
  ST := '';                           { initialize string length }
  repeat
    C1 := ReadKey;
    case C1 of
      #27  : begin                                      { escape }
             end;
      #00  : begin
               C2 := ReadKey;                  { fetch scan code }
               case C2 of
                 #59  : ;                                   { F1 }
                 #60  : ;                                   { F2 }
                 #61  : ;                                   { F3 }
                 #62  : ;                                   { F4 }
                 #63  : ;                                   { F5 }
                 #71  : ;                                 { Home }
                 #79  : ;                                  { End }
                 #72  : ;                             { Up Arrow }
                 #80  : ;                           { Down Arrow }
                 #108 : ;                               { Alt-F5 }
                 else   begin                       { all others }
                        end
               end  { case }
             end;
      #13  : ;                                             { C/R }
      #08  : begin                                   { Backspace }
               if Length(ST) > 0 then
                 begin
                   Dec (ST[0]);                { truncate string }
                   Write (#08#32#08);
                 end;
             end;
      else   begin                            { normal character }
               ST := ST+C1;    { concatenate character to string }
               Write (C1);
             end
    end;  { case }
  until (C1 = #13) or (Length(ST) >= Q)
end;  { MYREAD }

--------------6C88F147C96--

Re:Passing strings to and from procedure/functions


Quote
Maelstrom (maelst...@deathsdoor.com.removethis) wrote:
> Hi, I'm trying to write a replacement for the readln command in tp(dos) with
> better operation. It works in function form when the headers are...
> function getstring(x,y,length: integer): string;
> and will work with fixed-length strings, chars and open strings. However when
> I tried to write it as a procedure for ease of use it only allows me to pass
> open strings or whatever sort of string I've declared in its parameters like
> such
> procedure getstring(var st: string; x,y,length); only allows me to pass open
> strings. I would eventually like to get it to work exactly like readln; in
> that in can accept any sort of string or char. Is this possible?

No, it's not possible to declare a procedure like readln, see the recent
thread about "Variable number of parameters".

But you can use the existing readln by writing a TFDD (Text file device
driver) - look for information about it (one example of a TFDD - though for
a different purpose - can be found at #CrLf at my URL below).

You don't say what you mean by "better operation". I imagine something like
enhanced editing facilities - in this case a TFDD would be the way to go.
(It's even easier to implement than a new readln procedure, if that was
possible, - once you've grasped the concept of a TFDD.)

--
Frank Heckenbach, Erlangen, Germany
heck...@mi.uni-erlangen.de
Turbo Pascal:   http://www.mi.uni-erlangen.de/~heckenb/programs.htm
Internet links: http://www.mi.uni-erlangen.de/~heckenb/links.htm

Re:Passing strings to and from procedure/functions


In article <5hds89$...@rznews.rrze.uni-erlangen.de>, heck...@mi.uni-erlangen.de (Frank Heckenbach) writes:

Quote
> Maelstrom (maelst...@deathsdoor.com.removethis) wrote:
>> Hi, I'm trying to write a replacement for the readln command in tp(dos) with
>> better operation. It works in function form when the headers are...
>> function getstring(x,y,length: integer): string;
>> and will work with fixed-length strings, chars and open strings. However when
>> I tried to write it as a procedure for ease of use it only allows me to pass
>> open strings or whatever sort of string I've declared in its parameters like
>> such
>> procedure getstring(var st: string; x,y,length); only allows me to pass open
>> strings. I would eventually like to get it to work exactly like readln; in
>> that in can accept any sort of string or char. Is this possible?

> No, it's not possible to declare a procedure like readln, see the recent
> thread about "Variable number of parameters".

> But you can use the existing readln by writing a TFDD (Text file device
> driver) - look for information about it (one example of a TFDD - though for
> a different purpose - can be found at #CrLf at my URL below).

I agree that text-file device drivers are the way to go.  I've used them
many times and found them to be easy to implement and extremely flexible.
For example, you can change a console application instantly to work over the
serial port just by including ComDrvr in your uses clause and adding an
AssignCom(input,...) and AssignCom(output,...) calls.  Very slick!

However, if you just want to be allowed to pass a var string of variable
length to your getstring(...) procedure (not having variable parameter types),
just relax the string type checking with $v- or openstring.

Ex:
{$v-}
procedure getstring(var s:string...);
{$v+}
begin
...  { in this code, make sure you do not overrun the length of the
       string buffer that was passed in -- very difficult, since you
       have no way of knowing the length of the string!
     }
end;

In practice, you should only allow openstrings (or $v- strings) to be passed
as var parameters if the routine is guaranteed not to increase the length
of the string.  Acceptable uses would to ToUpperCase, Truncate, etc.
NOT Append, ReadInto, etc.

good luck!

Paul

Other Threads