[fpc-pascal] Problem with c header conversion

Jonas Maebe jonas.maebe at elis.ugent.be
Sun Jan 4 11:42:22 CET 2009


On 03 Jan 2009, at 12:54, gabor wrote:

> I'm trying to convert c header code from "Windows Mobile API 5" to  
> pascal and I'm getting wrong size of converted record:
>
> original c header:
>
> typedef struct _DDPIXELFORMAT
> {
>    DWORD	dwSize;
>    DWORD	dwFlags;
>    DWORD	dwFourCC;
>    union
>    {
> 	DWORD	dwRGBBitCount;
> 	DWORD	dwYUVBitCount;
> 	DWORD	dwAlphaBitDepth;
>    };
>    union
>    {
> 	DWORD	dwRBitMask;
> 	DWORD	dwYBitMask;
>    };
[snip]
> and my pascal conversion:

"union" in C means that the fields inside this union{..} block all  
start at the same address, but whatever comes after the union{} block  
follows all of those fields. Your translation does exactly the  
inverse: it makes all union blocks start at the same address, but puts  
the fields inside the union blocks after each other.

I don't think you can create sequential unnamed variant parts in  
Pascal records like the above C struct though, but you can get the  
same layout and usage by doing this:

PDDPixelFormat = ^TDDPixelFormat;
  _DDPIXELFORMAT = record
    dwSize: DWORD;
    dwFlags: DWORD;
    dwFourCC: DWORD;
    case Integer of
      0: (
          dwRGBBitCount : DWORD;
         );
      1: (
          dwYUVBitCount : DWORD;
         );
      2: (
          dwAlphaBitDepth : DWORD;
          case Integer of
           0: (
               dwRBitMask : DWORD;
              );
           1: (
               dwYBitMask : DWORD;
               case Integer of
                 0: (
                     dwGBitMask : DWORD;
                    );
                 1: (
                     dwUBitMask : DWORD;
                     case Integer of
                       0: (
                           dwBBitMask : DWORD;
                          );
                       1: (
                           dwVBitMask : DWORD;
                           case Integer of
                             0: (
                                 dwRGBAlphaBitMask: DWORD;
                                );
                          );
                    );
              );
          );
    end;

In this case it's easy because in each union every field has the same  
size. If that weren't the case, you would have to put the nested  
variant parts after the largest field of each variant. Or you could  
put the variant parts in separate record types and then declare fields  
of these types (which is cleaner, but then when accessing the fields  
you will have to add the record field names).


Jonas



More information about the fpc-pascal mailing list