🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: comp.lang.pascal.borland
7 messages
7 total messages Started by Xy Tue, 30 May 2000 00:00
CASE in records
#184
Author: Xy
Date: Tue, 30 May 2000 00:00
33 lines
1647 bytes
Hey all

Could someone please explain me these piece of code:

  tPolygonRec = Record
  ...
  ...
  Case Integer of  { Cases for the various polygon types }
      1 : (AB1, AB2, AB3 : UVCoord; Intensity : Byte);
      2 : (AB4, AB5, AB6 : UVCoord; I1, I2, I3 : Byte);
      3 : (AB7, AB8, AB9 : UVCoord);
      4 : (UV1, UV2, UV3 : UVCoord);
      5 : (AC1, AC2, AC3 : UVCoord; I_tensity : Byte);
      6 : (AC4, AC5, AC6 : UVCoord; I4, I5, I6 : Byte);
      7 : (AC7, AC8, AC9 : UVCoord; PT1, PT2, PT3 : UVCoord);
  End;

-or-

Type Register = Record
    Case Byte of
      0  : (w1, w2  : Word);
      1  : (b1, b2, b3, b4 : Byte);
  End;

(I copied these pieces of code from somebody elses sources)

What i don't understand is the 'CASE' statements in these records. Can
someone please explain to me how i use 'CASE' in records and why this is
useful??  Thnx in advance!

Grtnx
Xy
Re: CASE in records
#185
Author: byatesiii
Date: Tue, 30 May 2000 00:00
53 lines
2510 bytes
In article <3933FF8D.FFBE7575@vgrieken.demon.nl>,
  Xy <xy@vgrieken.demon.nl> wrote:
> Hey all
>
> Could someone please explain me these piece of code:
>
>   tPolygonRec = Record
>   ...
>   ...
>   Case Integer of  { Cases for the various polygon types }
>       1 : (AB1, AB2, AB3 : UVCoord; Intensity : Byte);
>       2 : (AB4, AB5, AB6 : UVCoord; I1, I2, I3 : Byte);
>       3 : (AB7, AB8, AB9 : UVCoord);
>       4 : (UV1, UV2, UV3 : UVCoord);
>       5 : (AC1, AC2, AC3 : UVCoord; I_tensity : Byte);
>       6 : (AC4, AC5, AC6 : UVCoord; I4, I5, I6 : Byte);
>       7 : (AC7, AC8, AC9 : UVCoord; PT1, PT2, PT3 : UVCoord);
>   End;
>
> -or-
>
> Type Register = Record
>     Case Byte of
>       0  : (w1, w2  : Word);
>       1  : (b1, b2, b3, b4 : Byte);
>   End;
>
> (I copied these pieces of code from somebody elses sources)
>
> What i don't understand is the 'CASE' statements in these records. Can
> someone please explain to me how i use 'CASE' in records and why this
is
> useful??  Thnx in advance!
>
> Grtnx
> Xy
>
>
There are bound to be better replies, as I'm not an expert.
These are also referred to as "Variant records".
It allows you to store different data-types or data-sizes within the
same record.
Your 2nd example allows a person to store either two words or four
bytes (same size) within the record, and Pascal chooses which one by
the type of the data you are storing there (byte or word).
In some implementations I think the size of each variant had to be the
same, or the larger ones came first. Your first example doesn't appear
to follow this restriction. But then again, I'm a UCSD Pascal
programmer... the only one left, I imagine :)


Sent via Deja.com http://www.deja.com/
Before you buy.
Re: CASE in records
#186
Author: The Scarlet Manuka
Date: Wed, 31 May 2000 00:00
28 lines
2354 bytes
byatesiii@my-deja.com wrote:
[variant records]
> Your 2nd example allows a person to store either two words or four
> bytes (same size) within the record, and Pascal chooses which one by
> the type of the data you are storing there (byte or word).
> In some implementations I think the size of each variant had to be the
> same, or the larger ones came first. Your first example doesn't appear
> to follow this restriction. But then again, I'm a UCSD Pascal
> programmer... the only one left, I imagine :)

Variant records can have variants of different sizes; enough space is
allocated to store the largest. The type used is determined when the data is
read or written; this makes it easy to convert data between compatible 
types.  For instance, TP has a variant record type for the registers; so you
can store data in the 8-bit registers and read it from the 16-bit registers,
or the other way around. Many other applications are possible. In
particular, it makes it possible to pass different but related types to a
single procedure or function, as in your polygon example. Each form needs
its own parameters and storage, but a single routine can handle them by
using the variant record.

-- 
______________________________________________________________________
     The Scarlet Manuka,      |        Nitpickers' Party motto:
  Pratchett Quoter At Large,  |  "He who guards his lips guards his
 First Prophet of Bonni, is:  |  soul, but he who speaks rashly will
   sacha@maths.uwa.edu.au     |    come to ruin." -- Proverbs 13:3
______________________________|_______________________________________
Re: CASE in records
#179
Author: The Scarlet Manuka
Date: Tue, 13 Jun 2000 00:00
26 lines
1883 bytes
Marc Griffin <cannonfod@earthling.net> wrote:
> Simply put:

> Type
>   tString = Record
>     Case Boolean Of
>       True: (AsStr :String);
>       False: (AsByte :Byte; AsChar :Array[0..253] Of Char);
>   End;

> In your program, you can either reference "tString.AsStr" as a string type,
> or reference it as "AsByte" and "AsChar" to get the length (AsByte) and it's
> contents (AsChar).  This means you can use the PChar routines on
> tString.AsChar (as long as you remember to update AsByte with the string
> length). You can empty the string by "tString.AsStr := '';" or

You'll also need to make sure that you write a terminating #0 byte each time
you modify the string, if you want to use it with PChar routines.

-- 
______________________________________________________________________
     The Scarlet Manuka,      |        Nitpickers' Party motto:
  Pratchett Quoter At Large,  |  "He who guards his lips guards his
 First Prophet of Bonni, is:  |  soul, but he who speaks rashly will
   sacha@maths.uwa.edu.au     |    come to ruin." -- Proverbs 13:3
______________________________|_______________________________________
Re: CASE in records
#180
Author: Marco van de Voort
Date: Tue, 13 Jun 2000 00:00
20 lines
1570 bytes
>>   tString = Record
>>     Case Boolean Of
>>       True: (AsStr :String);
>>       False: (AsByte :Byte; AsChar :Array[0..253] Of Char);
>>   End;
>
>> In your program, you can either reference "tString.AsStr" as a string type,
>> or reference it as "AsByte" and "AsChar" to get the length (AsByte) and it's
>> contents (AsChar).  This means you can use the PChar routines on
>> tString.AsChar (as long as you remember to update AsByte with the string
>> length). You can empty the string by "tString.AsStr := '';" or
>
>You'll also need to make sure that you write a terminating #0 byte each time
>you modify the string, if you want to use it with PChar routines.

Now we are on this topic, does somebody have a unit for modula2 styled 
array of char ?

This is zero terminated, except when the array is full (then length is
sizeof/high(x)), which allows the last char to be used for text.
Re: CASE in records
#181
Author: Marc Griffin
Date: Tue, 13 Jun 2000 00:00
32 lines
2002 bytes
I can write the code for you quite quickly, but why would you want this
array style?

Marco van de Voort <marcov@toad.stack.nl> wrote in message
news:slrn8kbock.1i2m.marcov@toad.stack.nl...
> >>   tString = Record
> >>     Case Boolean Of
> >>       True: (AsStr :String);
> >>       False: (AsByte :Byte; AsChar :Array[0..253] Of Char);
> >>   End;
> >
> >> In your program, you can either reference "tString.AsStr" as a string
type,
> >> or reference it as "AsByte" and "AsChar" to get the length (AsByte) and
it's
> >> contents (AsChar).  This means you can use the PChar routines on
> >> tString.AsChar (as long as you remember to update AsByte with the
string
> >> length). You can empty the string by "tString.AsStr := '';" or
> >
> >You'll also need to make sure that you write a terminating #0 byte each
time
> >you modify the string, if you want to use it with PChar routines.
>
> Now we are on this topic, does somebody have a unit for modula2 styled
> array of char ?
>
> This is zero terminated, except when the array is full (then length is
> sizeof/high(x)), which allows the last char to be used for text.
>
>
>
Re: CASE in records
#182
Author: Marco van de Voort
Date: Tue, 13 Jun 2000 00:00
14 lines
1336 bytes
>I can write the code for you quite quickly, but why would you want this
>array style?

I can do it myself too, I just wondered if it already existed, and save some
work. Also, it has to be done/converted to Free Pascal, so BP internal asm
is useless.

I still have datafiles with records that contain strings in this format,
have ported the apps that generate them to Free Pascal, but want an
import and export function in the old format.

Pchar is possible, but the strings unit is quite limited, shortstring is too
short. Ansistring would be best, but I want a small TP compilable app too
:-)
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads