Recent

Author Topic: converting C headers to pas files  (Read 441 times)

inferno

  • New Member
  • *
  • Posts: 19
converting C headers to pas files
« on: November 26, 2019, 02:18:55 pm »
Hi all,
I'm trying to convert C headers with the structures as follow:

Code: C  [Select]
  1. typedef struct {
  2. #define KAUTH_GUID_SIZE 16      /* 128-bit identifier */
  3.         unsigned char g_guid[KAUTH_GUID_SIZE];
  4. } guid_t;
  5.  
  6. struct _acl_entry {
  7.         u_int32_t       ae_magic;
  8. #define _ACL_ENTRY_MAGIC        0xac1ac101
  9.         u_int32_t       ae_tag;
  10.         guid_t          ae_applicable;
  11.         u_int32_t       ae_flags;
  12.         u_int32_t       ae_perms;
  13. };
  14.  
  15. struct _acl {
  16.         u_int32_t       a_magic;
  17. #define _ACL_ACL_MAGIC          0xac1ac102
  18.         unsigned        a_entries;
  19.         int             a_last_get;
  20.         u_int32_t       a_flags;
  21.         struct _acl_entry a_ace[ACL_MAX_ENTRIES];
  22. };

The pascal versions below. I don't know what to do with const values and also not sure about types conversion...

Code: Pascal  [Select]
  1. guid_t = packed record
  2.     KAUTH_GUID_SIZE: Integer; //const = 16
  3.     g_guid: array[0..(KAUTH_GUID_SIZE)-1] of byte;
  4. end;
  5.  
  6. _acl_entry = packed record
  7.     ae_magic: dword;
  8.     _ACL_ENTRY_MAGIC: Cardinal; //const = $ac1ac101
  9.     ae_tag: dword;
  10.     ae_applicable: guid_t;
  11.     ae_flags: dword;
  12.     ae_perms: dword;
  13.  end;
  14.  
  15.  _acl = packed record
  16.     a_magic: dword;
  17.     _ACL_ENTRY_MAGIC: Cardinal; //const = $ac1ac102
  18.     a_entries: Cardinal;
  19.     a_last_get: Integer;
  20.     a_flags: dword;
  21.     a_ace: array[0..(ACL_MAX_ENTRIES)-1] of _acl_entry;
  22.  end;

Any suggestions?

Best regards,
Inferno

440bx

  • Hero Member
  • *****
  • Posts: 1422
Re: converting C headers to pas files
« Reply #1 on: November 26, 2019, 02:38:42 pm »
I don't know what to do with const values and also not sure about types conversion...

All you need to do is convert the #define(s) to Pascal const(s), for those you showed:
Code: C  [Select]
  1. #define KAUTH_GUID_SIZE 16      /* 128-bit identifier */
  2. #define _ACL_ENTRY_MAGIC        0xac1ac101
  3. #define _ACL_ACL_MAGIC          0xac1ac102
  4.  
the equivalent Pascal version is:
Code: Pascal  [Select]
  1. const
  2.   KAUTH_GUID_SIZE   = 16;
  3.   _ACL_ENTRY_MAGIC  = $ac1ac101;
  4.   _ACL_ACL_MAGIC    = $ac1ac102;
  5.  
That's all there is to it.

Also, get rid of the declarations you currently have for those #define(s).  The way you have them now makes them fields of the record and they are _not_ fields of those records, they are simply constant declarations.

HTH.

« Last Edit: November 26, 2019, 02:42:57 pm by 440bx »
using FPC v3.0.4 and Lazarus 1.8.2 on Windows 7 64bit.

inferno

  • New Member
  • *
  • Posts: 19
Re: converting C headers to pas files
« Reply #2 on: November 26, 2019, 05:45:21 pm »
Thank you 440bx! How about type conversions - are they correct (u_int32_t -> dword, unsigned -> cardinal, int -> integer)?

Best regards,
Inferno

lucamar

  • Hero Member
  • *****
  • Posts: 2407
Re: converting C headers to pas files
« Reply #3 on: November 26, 2019, 06:38:42 pm »
How about type conversions - are they correct (u_int32_t -> dword, unsigned -> cardinal, int -> integer)?

They are correct but note that there are aliases in the system unit which were added, IIRC, precisely to ease this kind of conversions: Int8, UInt8, Int16, UInt16, Int32, UInt32, Int64, UInt64, an its corresponding pointers: PInt8, PUInt8, PInt16, etc.

They are described in the help for that unit (in the RTL help).
« Last Edit: November 26, 2019, 06:41:02 pm by lucamar »
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus 2.0.4/2.0.6  - FPC 3.0.4 on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

marcov

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 7931
Re: converting C headers to pas files
« Reply #4 on: November 26, 2019, 08:02:29 pm »
Somewhat older is unit ctypes, it defines many C types for use in headers, prefixed with a "C"

https://www.freepascal.org/docs-html/current/rtl/ctypes/index-3.html

inferno

  • New Member
  • *
  • Posts: 19
Re: converting C headers to pas files
« Reply #5 on: December 02, 2019, 03:26:55 pm »
Thanks lucamar and marcov! I got it working. On macOS there is the useful unit MacTypes - maybe it will be useful for somebody.

Best regards,
Inferno