[fpc-pascal] How to create this "union" ?

Jeppe Græsdal Johansen jjohan07 at student.aau.dk
Mon Dec 26 19:42:27 CET 2011


Den 26-12-2011 15:41, ik skrev:
> Hello,
>
> I'm trying to translate the following to Pascal:
> ------------
> struct xt_option_call {
>     const char *arg, *ext_name;
>     const struct xt_option_entry *entry;
>     void *data;
>     unsigned int xflags;
>     bool invert;
>     uint8_t nvals;
>     union {
>         uint8_t u8, u8_range[2], syslog_level, protocol;
>         uint16_t u16, u16_range[2], port, port_range[2];
>         uint32_t u32, u32_range[2];
>         uint64_t u64, u64_range[2];
>         double dbl;
>         struct {
>             union nf_inet_addr haddr, hmask;
>             uint8_t hlen;
>         };
>         struct {
>             uint8_t tos_value, tos_mask;
>         };
>         struct {
>             uint32_t mark, mask;
>         };
>         uint8_t ethermac[6];
>     } val;
>     /* Wished for a world where the ones below were gone: */
>     union {
>         struct xt_entry_match **match;
>         struct xt_entry_target **target;
>     };
>     void *xt_entry;
>     void *udata;
> };
>
> ------
> h2pas does not capable of translating it, and I'm not sure that I know 
> to make the "case" statement that will be equivalent for this complex 
> union.
> How can I translate it ?
>
> Thanks,
> Ido
I think the following should do it:
uses ctypes;

{$PACKRECORDS C}

type
  xt_option_call_union = record
   case integer of
    0: (u8: cuint8);
    1: (u8_range: array[0..1] of cuint8);
    2: (sysloc_level: cuint8);
    3: (protocol: cuint8);
    4: (u16: cuint16);
    5: (u16_range: array[0..1] of cuint16);
    6: (port: cuint16);
    7: (port_range: array[0..1] of cuint16);
    8: (u32: cuint32);
    9: (u32_range: array[0..1] of cuint32);
    10: (u64: cuint64);
    11: (u64_range: array[0..1] of cuint64);
    12: (dbl: cdouble);
    13: (haddr: nf_inet_addr; hlen: cuint8);
    14: (hmask: nf_inet_addr);
    15: (tos_value, tos_mask: cuint8);
    16: (mark, mask: cuint32);
    17: (ethermac: array[0..5] of cuint8);
  end;

  xt_option_call = record
   arg,
   ext_name: pchar;
   entry: ^xt_option_entry;
   data: pointer;
   xflags: cuint;
   invert: cbool;
   nvals: cuint8;
   val: xt_option_call_union;
   case integer of
    0: (match: ^Pxt_entry_match; xt_entry, udata: pointer);
    1: (target: ^Pxt_entry_target);
  end;
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-pascal/attachments/20111226/29ecd9d1/attachment.html>


More information about the fpc-pascal mailing list