Article View: comp.lang.modula2
Article #4940Re: Clever way to deal with this?
From: Tim Larson
Date: Tue, 13 Feb 1990 18:53
Date: Tue, 13 Feb 1990 18:53
117 lines
3531 bytes
3531 bytes
In article <90043.154337MARK@UCF1VM.BITNET>, MARK@UCF1VM.BITNET (Mark Woodruff)
says:
>In C, I'd say:
>
> struct
> {
> int count;
> char c;
> } buffer;
>
> for (i = 0; i <= count; i++)
> {
> putchar(&buffer.c+i);
> }
>
>In Modula-2, I said:
>
> TYPE
> BuffType =
> RECORD
> count: CARDINAL;
> text: CHAR;
> END;
>
> VAR
> buffer: BuffType;
> i: CARDINAL;
> charPtr: POINTER TO CHAR;
>
> BEGIN
> FOR i := 0 TO buffer.count DO
> charPtr := SYSTEM.ADDRESS(buffer.text)+i;
> InOut.Write(charPtr@)
> END;
> END;
>
>(note: I haven't actually tried to write this in assembly or C
>and I don't have my Modula-2 code at hand, so this is from memory)
>
>I'm not thrilled with this, but it's about the best equivalent to
>an equate or a pointer to a character I can get in Modula-2.
>
>I'm using this representation to represent buffers in an editor.
>The actual characters are fixed in length, but the length is entirely
>determined at run time.
>
Most Modula-2 systems won't like the address arithmetic, so it would be
better to write
TYPE
BuffType = RECORD
count: CARDINAL;
text: ARRAY CARDINAL OF CHAR
END;
VAR
buffer: BuffType;
i: CARDINAL;
charPtr: POINTER TO CHAR;
BEGIN
FOR i := 0 TO buffer.count DO
charPtr := SYSTEM.ADR(buffer.text[i]);
InOut.Write(charPtr:)
END
END;
This is essentially what you wrote except for the use of ADR and the use
of i as an index into buffer.text, which required a change in the type
definition. This is according to PIM 3e Modula-2. I'm not sure what the
upcoming standard will provide but I think JPI's M2 would be a little more
succinct
TYPE
bufType = RECORD
count: CARDINAL;
text: ARRAY CARDINAL OF CHAR
END;
VAR
buffer: bufType;
i: CARDINAL;
BEGIN
FOR i := 0 TO buffer.count DO
IO.WrChar (CHAR([ADR(buffer.text[i])]:))
END
END ...
Not a compelling reason to buy JPI's product, perhaps, but just a touch
more elegant since it eliminates the need for the charPtr variable and
probably speeds the compiled code up the tiniest fraction. In Modula-2,
which relies on words more than symbols, you just will never get the
bulk of this type of code down to C-size. On the other hand, it is more
difficult to do something wrong and much easier to read.
The other way to do this (if possible) is to null-terminate the string
at location buffer.count+1 and simply use WriteString.
TYPE
bufType = RECORD
count: CARDINAL;
text: ARRAY CARDINAL OF CHAR
END;
VAR
buffer: bufType;
BEGIN
IF buffer.count<MAX(CARDINAL) THEN buffer.text[buffer.count+1] := 0C END;
InOut.WriteString (buffer.text)
END ...
Hope this adds something to the discussion.
-Tim Larson
trl3@psuvm.bitnet
Technical Details
Message-ID:
<90044.135327TRL3@PSUVM.BITNET>Path:
arc1-ca.pugleaf.net!utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!cs.utexas.edu!uwm.edu!psuvax1!psuvm!trl3References:
<"90-02-12-20:09:32.76*UK4H"@DKAUNI2.BITNET> <90043.154337MARK@UCF1VM.BITNET>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!cs.utexas.edu!uwm.edu!psuvax1!psuvm!trl3 From: TRL3@psuvm.psu.edu (Tim Larson) Newsgroups: comp.lang.modula2 Subject: Re: Clever way to deal with this? Message-ID: <90044.135327TRL3@PSUVM.BITNET> Date: 13 Feb 90 18:53:27 GMT References: <"90-02-12-20:09:32.76*UK4H"@DKAUNI2.BITNET> <90043.154337MARK@UCF1VM.BITNET> Organization: Penn State University Lines: 117