Home > Posts > Converting C structs and unions to Pascal (or Object Pascal / Delphi) records

Converting C structs and unions to Pascal (or Object Pascal / Delphi) records

I’ve been recently adding support for QuickTime TimeCode tracks to the JEDI QuickTime library’s “qt_QuickTimeComponents.pas” file and I thought I should share a quick reminder on how we convert C structs (records) and unions (sets) into Pascal (Delphi / Object Pascal in this case) records:

Structs:

struct TimeCodeCounter {
long counter;
};
typedef struct TimeCodeCounter TimeCodeCounter;

type
long=integer; //in JEDI QuickTime I opted to create aliases for common C types to avoid converting them to Pascal equivalents everywhere
TimeCodeCounter = record
counter:long;
end;

Unions:

union TimeCodeRecord {
TimeCodeTime t;
TimeCodeCounter c;
};

type TimeCodeRecord = record
case integer of //C’s union
0: (t: TimeCodeTime);
1: (c: TimeCodeCounter);
end;

Do note that you should use “packed record” in Pascal if the C code uses respective #pragma command to define 0-packing gap between record fields (the default for C and Pascal compilers is to do field alignment to word [16-bit] boundaries for optimization purposes)

Advertisements
Categories: Posts Tags: , , , , ,
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: