Article View: comp.lang.modula2
Article #4933Re: Clever way to deal with this?
From: Mark Woodruff
Date: Mon, 12 Feb 1990 20:43
Date: Mon, 12 Feb 1990 20:43
108 lines
3648 bytes
3648 bytes
In article <"90-02-12-20:09:32.76*UK4H"@DKAUNI2.BITNET>,
"JAE (Juergen A. Erhard)" <UK4H@DKAUNI2.BITNET> says:
>mark wrote:
>> I have a data structure consisting of a cardinal value followed by a
>> variable number of characters. The cardinal indicates how many
>> characters follow.
>>
>> How would you express this in Modula-2?
>How DO you express that in, say, C. Or some other language (not a 4GL,
>OC)
Let's assume I want to print the characters in my buffer and that
the number of characters run from one to MAX(CARDINAL):
In 370 Assembly, I'd say:
LH 2,COUNT
LA 3,TEXT
WRTERM (3),(R2)
*
BUFFER DSECT ,
COUNT DS H
TEXT DS C
In C, I'd say:
struct
{
int count;
char c;
} buffer;
for (i = 0; i <= count; i++)
{
putchar(&buffer.c+i);
}
In Smalltalk, I'd just create a class with count as an instance
variable with byteIndexedInstanceVariables for the characters.
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.
>What I like to know is: Are you porting a program from another language
>to M2, or did you devise such a struct out of thin air?
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.
This arrangement is also the same as variable blocked records on
standard labeled tapes.
>Anyway you want it, M2 is not the ideal language for such a struct.
>Some way (a kludge, to be honest) would be:
>
> type mytype=record
> numOfChars:CARDINAL;
> chars:POINTER TO ARRAY <0..Max> OF CHAR;
> end;
>
>Two notes:
> 1. The idea behind the whole thing is to Storage.ALLOCATE() (or some
> such) as much memory as needed to hold all chars. This would mean
> you have to do all ALLOCATE'ING/DEALLOCATE'ING by 'hand'.
> 2. Set square brackets for < and > (I'm sittin' in front of a grubby
> old IBM 3278 (sucks!), and it doesn't give me no squares...)
> Also set Max and mytype to your choice...
By the way, you can get brackets on almost any 3270 terminal by
setting the right input and output translations. Send me mail
if you want more info.
>
>Juergen A. Erhard
mark
-------
Bitnet: MARK@UCF1VM.BITNET
Internet: mark@mamab.FIDONET.ORG
UUCP: uunet!tarpit!libcmp!mamab!mark
Fidonet: mark at 1:363/9.0
Technical Details
Message-ID:
<90043.154337MARK@UCF1VM.BITNET>Path:
arc1-ca.pugleaf.net!utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!uwm.edu!rpi!zaphod.mps.ohio-state.edu!swrinde!ucsd!ucbvax!UCF1VM.BITNET!MARKReferences:
<"90-02-12-20:09:32.76*UK4H"@DKAUNI2.BITNET>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!uwm.edu!rpi!zaphod.mps.ohio-state.edu!swrinde!ucsd!ucbvax!UCF1VM.BITNET!MARK From: MARK@UCF1VM.BITNET (Mark Woodruff) Newsgroups: comp.lang.modula2 Subject: Re: Clever way to deal with this? Message-ID: <90043.154337MARK@UCF1VM.BITNET> Date: 12 Feb 90 20:43:37 GMT References: <"90-02-12-20:09:32.76*UK4H"@DKAUNI2.BITNET> Sender: daemon@ucbvax.BERKELEY.EDU Reply-To: Modula2 List <INFO-M2%UCF1VM.BITNET@PSUVM.PSU.EDU> Organization: University Of Central Florida Lines: 108