[fpc-pascal] How to translate this C header struct to Pascal

Jonas Maebe jonas.maebe at elis.ugent.be
Fri Oct 23 22:29:08 CEST 2009


Graeme Geldenhuys wrote on Fri, 23 Oct 2009:

>     uint32_t    SearchStart:31;    // file offset to full text search table
>     uint32_t    searchlen:1;          // if high bit set, size of
> search record size is 16-bit
>
> If not, I guess I can continue with what I already do -  read this as
> a 32bit unsigned and then when I need to work with that data, split
> the 32bit value into two separate variables as shown below.
>
> var
>   SearchTableOffset: longint;
>   SearchTableRecordLengthIs16Bit: boolean;
> begin
>   SearchTableOffset := _pHeader^.SearchStart and $7fffffff;
>   SearchTableRecordLengthIs16Bit := _pHeader^.SearchStart and $80000000 > 0;

This code will at best only work on little endian systems. Most/all  
big endian systems also count the bits in bitpacked structs in the  
opposite order. The exact layout of a bitpacked C struct can however  
only be known by reading the ABI for that platform (it can even differ  
between different OSes on the same hardware architectures).

The safest way is to look things up in the ABIs you want to support,  
add code similar to the above for that specific ABI, and add a  
compiler directive to abort the compilation if someone tries to  
compile for an ABI you have not checked.

> Is there any better way of doing this?  Does Object Pascal record
> structure support bit size definitions?

Not in a C/ABI compatible way at this time.


Jonas

----------------------------------------------------------------
This message was sent using IMP, the Internet Messaging Program.




More information about the fpc-pascal mailing list