[comp.os.vms] Quoted string parameters in VAX Pascal?

elkins@elbereth.rutgers.edu (George Elkins) (04/30/88)

Is there a way in VAX Pascal to define a procedure that will take
a variable length quoted string as a parameter?  What I would like
to do is imitate the variable length quoted string parameter behavior
of the WRITE and WRITELN procedures, although this is not possible
for user-written procedures in STANDARD Pascal.  I want the quoted
string parameter to be within the argument list itself, as in the
WRITELN procedure.  I am trying to avoid assigning the string to an
arbitrarily long fixed-length packed array and passing the Pascal
identifier for the array and the integer length of the string.

Example of what I want to do:

     foo('This is a string of any length');  {User-written procedure foo}

Examples of what I am avoiding:

     foo(PackedArrayName, StringLength);  {Packed array and string length}
or
     foo(String.Body, String.Length);  {Record with packed array and integer}

Is this possible or conceivable in VAX Pascal?  (I'll admit that I haven't
consulted the documentation.  I don't have easy access to it.)

George Elkins

cfchiesa@bsu-cs.UUCP (Christopher Chiesa) (04/30/88)

In article <Apr.29.15.09.31.1988.23840@elbereth.rutgers.edu>, elkins@elbereth.rutgers.edu (George Elkins) writes:
> 
> Is there a way in VAX Pascal to define a procedure that will take
> a variable length quoted string as a parameter?  What I would like
> to do is imitate the variable length quoted string parameter behavior
> of the WRITE and WRITELN procedures, although this is not possible
> for user-written procedures in STANDARD Pascal.  I want the quoted
> string parameter to be within the argument list itself, as in the
> WRITELN procedure.  I am trying to avoid assigning the string to an
> arbitrarily long fixed-length packed array and passing the Pascal
> identifier for the array and the integer length of the string.
> 
> Example of what I want to do:
> 
>      foo('This is a string of any length');  {User-written procedure foo}
> 
> Examples of what I am avoiding:
> 
>      foo(PackedArrayName, StringLength);  {Packed array and string length}
> or
>      foo(String.Body, String.Length);  {Record with packed array and integer}
> 
> Is this possible or conceivable in VAX Pascal?  (I'll admit that I haven't
> consulted the documentation.  I don't have easy access to it.)
> 
> George Elkins

When in doubt, use a RECORD.  One hitch with that is that you usually can't
pass a RECORD data type as a parameter, but you can get around THAT by pass-
ing a POINTER to the record you're after.

For variable-length strings, you'll still have to work within SOME maximum-
length limit, but in general you'd do something like this:

TYPE
      vstring: ^vrec;

      vrec: RECORD
           body   : PACKED ARRAY [1..maxstring] OF char;
           length : INTEGER;
        END;

PROCEDURE FakeWriteln (VAR s: vstring);
  BEGIN
   (* Assorted manipulations of the string having body s^.body, of which 
      the first s^.length characters are valid. *)
   .
   .
   .
  END;

Now, it just so happens that the folks at DEC anticipated this, and built in
a datatype you may be aware of but haven't mentioned:

       VARYING [...] OF char;

This creates an "implicit record" type, containing the fields "body" and 
"length" pretty much as I declared 'em above for (semi-) standard Pascal.

In Vax Pascal, anyway, "write" and "read" and "writeln" and so on, will oper-
ate quite happily on these variable-length strings.

You can pass them to system routines, also, using either the "%desc" or 
"%stdesc" passing-mechanism specifiers (I forget which is for VARYING strings
and which is for PACKED strings; try 'em and use whatever works... :-)  )

Hope this is of use to you; feel free to e-mail for clarification.

Chris Chiesa

-- 
UUCP: <backbones>!{iuvax,pur-ee,uunet}!bsu-cs!cfchiesa 
cfchiesa@bsu-cs.UUCP                                           

schwartz@gondor.cs.psu.edu (Scott Schwartz) (05/01/88)

In article <2812@bsu-cs.UUCP> cfchiesa@bsu-cs.UUCP (Christopher Chiesa) writes:
>In article <Apr.29.15.09.31.1988.23840@elbereth.rutgers.edu>, elkins@elbereth.rutgers.edu (George Elkins) writes:
>> 
>> Is there a way in VAX Pascal to define a procedure that will take
>> a variable length quoted string as a parameter?

>Now, it just so happens that the folks at DEC anticipated this, and built in
>a datatype you may be aware of but haven't mentioned:
>
>       VARYING [...] OF char;
>
>This creates an "implicit record" type, containing the fields "body" and 
>"length" pretty much as I declared 'em above for (semi-) standard Pascal.


Interesting.  Did DEC happen to implement conformant arrays, as 
required by the ISO level 1 standard?  

(For those who don't know, conformant arrays are a mechanism to 
pass arbitrarily sized arrays to a procedure.  The declaration looks like:

procedure foo( bar: array[low..high: integer] of thingy );

Inside foo, the variables low and high give the bounds of the 
actual parameter passed to foo.)


-- Scott Schwartz     schwartz@gondor.cs.psu.edu    schwartz@psuvaxg.bitnet

sommar@enea.se (Erland Sommarskog) (05/01/88)

George Elkins (elkins@elbereth.rutgers.edu) writes:
>Is there a way in VAX Pascal to define a procedure that will take
>a variable length quoted string as a parameter?  
>...
>     foo ('This is a string of any length');  {User-written procedure foo}

Normally I should have used mail, but Christopher Chiesa messed things
up in his article, so I take it officially if someone else got confused.

The answer to your question is: Yes. Simple do as below:
   PROCEDURE Foo(Str : PACKED ARRAY(. Low..High : integer .) OF char);
or if you prefer the varying strings Chiesa told you of:
   PROCEDURE Foo(Str : VARYING(. Len .) OF char);
This is known as a conformant array. Low, High and Len are available
as constants (actually I don't know whether you can modify them)
in Foo. If your procedure takes more than one string parameter,
be sure to use different names for the bounds (like Low1, Low2 etc),
since your parameters are not likely to be of the same length.

>Is this possible or conceivable in VAX Pascal?  (I'll admit that I haven't
>consulted the documentation.  I don't have easy access to it.)

Have you tried HELP PASCAL? I don't know exactly under which subtopic
you should look to learn about conformant arrays and varying strings,
but the PASCAL entry under HELP does give a condensed version of the 
manual, so the info you need should be there.
-- 
Erland Sommarskog        "No protection on a motor-bike, then
ENEA Data, Stockholm      Sooner or later, that normal traffic's gonna get you"
sommar@enea.UUCP          -- Peter Hammill    

sanford@tramp.Colorado.EDU (SANFORD STEPHEN LEE) (05/03/88)

In his provocative and exciting article,
   elkins@elbereth.rutgers.edu (George Elkins) writes:
>
>Is there a way in VAX Pascal to define a procedure that will take
>a variable length quoted string as a parameter?  What I would like
>to do is imitate the variable length quoted string parameter behavior
>of the WRITE and WRITELN procedures, although this is not possible
>for user-written procedures in STANDARD Pascal.  I want the quoted
>string parameter to be within the argument list itself, as in the
>WRITELN procedure.  I am trying to avoid assigning the string to an
>arbitrarily long fixed-length packed array and passing the Pascal
>identifier for the array and the integer length of the string.
>
>Example of what I want to do:
>
>     foo('This is a string of any length');  {User-written procedure foo}
>  . . . 
>
>George Elkins

One possibility is to define your "string" as follows:
----------------------------------------------------------
     type stringptr: ^newstring;
          newstring: record
                      ch: char;
                      next: stringptr
                     end;
     var x: newstring;
----------------------------------------------------------
Then, work with the string using procedures.  For example:
----------------------------------------------------------
     function length (x: newstring): integer;
      begin
       if (x = nil)
       then length := 0
       else length := 1 + length (newstring^.next)
      end;

     procedure readin (var x: newstring);
      var temp: newstring;
            ch: char;
      begin
        x := nil; 
        read (ch);
        if (not (eoln)) then
         begin
           new (x); x^.ch := ch; x^.next := nil; temp := x;
           read (ch);
           while (not (eoln)) do
            begin
              new (temp^.next);
              temp := temp^.next;
              temp^.ch := ch; temp^.next := nil;
              read (ch)
            end;
         end;
      end;   (* readin *)
---------------------------------------------------------------------
{These procedures could probably be improved upon; the checks for (eoln) might
  be improperly placed.  Give me more time, I'll give you correct code. }

Obviously, the data structure isn't a static "string", but a linked list
of characters.  The procedures are a bit more complicated, but it gives you
the "variability" you desired; strings can be as short or as long as you 
want, and "foo (x)" will work with them regardless.
     Good luck.  If you want, e-mail me a request for more (correct) code;
I'm just a poor student, who will do ANYTHING to avoid studying for finals.
                                  **************************************** 
      ___                        * If you set your mind to it, you can do *
     /__ teve                    *   do anything.  Except ski through a   *
    ___/anford Jr.               *           revolving door.              *
                                  ***************** - some optimist ******

sqkeith@csvax.liv.ac.uk (05/04/88)

In article <2812@bsu-cs.UUCP>, cfchiesa@bsu-cs.UUCP (Christopher Chiesa) writes:
> In article <Apr.29.15.09.31.1988.23840@elbereth.rutgers.edu>, elkins@elbereth.rutgers.edu (George Elkins) writes:
>> 
>> Is there a way in VAX Pascal to define a procedure that will take
>> a variable length quoted string as a parameter?  What I would like
>> to do is imitate the variable length quoted string parameter behavior
>> of the WRITE and WRITELN procedures, although this is not possible
>> for user-written procedures in STANDARD Pascal.

> TYPE
>       vstring: ^vrec;
> 
>       vrec: RECORD
>            body   : PACKED ARRAY [1..maxstring] OF char;
>            length : INTEGER;
>         END;
> 
>        VARYING [...] OF char;
> 
> This creates an "implicit record" type, containing the fields "body" and 
> "length" pretty much as I declared 'em above for (semi-) standard Pascal.
> 

VAX Pascal extends the notion of conformant array schemas to that of conformant
variant schemas. For example:

procedure my_writeln ( s : varying [x] of char );
  var i : integer;
  begin
    for i := 1 to x do operate_upon(s[i]);
  end;
.
.
my_writeln('This is a test that will work with any length string');
.
.

Hope this helps..
Keith

+-----------------------------------------------------------------------------+
| Keith Halewood, Janet:    sqkeith@csvax.liv.ac.uk                           |
|                 UUCP:     ...!mcvax!ukc!mupsy!liv-cs!sqkeith                |
|                 Internet: sqkeith%csvax.liv.ac.uk@cunyvm.cuny.edu           |
+-----------------------------------------------------------------------------+