Lazarus

Programming => Operating Systems => macOS / Mac OS X => Topic started by: inferno on November 26, 2019, 02:18:55 pm

Title: converting C headers to pas files
Post by: inferno 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
Title: Re: converting C headers to pas files
Post by: 440bx 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.

Title: Re: converting C headers to pas files
Post by: inferno 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
Title: Re: converting C headers to pas files
Post by: lucamar 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).
Title: Re: converting C headers to pas files
Post by: marcov 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
Title: Re: converting C headers to pas files
Post by: inferno 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
TinyPortal © 2005-2018