Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2011-12

How to create this "union" ?

8 messages · 4 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

ik

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

Reply by email

ik
to ik

Better redefining my problem:

I have a first union. But on records we do not end case with "end". and beneath the union, I have to place another union, or another fields that is not part of the union.
How can I do that ?

Thanks,
Ido

ik wrote 46 lines
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

Reply by email

Jonas Maebe
to ik
ik wrote
I have a first union. But on records we do not end case with "end". and beneath the union, I have to place another union, or another fields that is not part of the union. How can I do that ?

You have to create an extra nested record that contains that union.

Jonas

Reply by email

Jeppe Græsdal Johansen
to ik: I'm trying to translate the following to Pascal: ------------ struct xt_option_call {…
ik wrote 46 lines
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;

Reply by email

Jonas Maebe
to Jeppe Græsdal Johansen
Jeppe Græsdal Johansen wrote 7 lines
> union { > struct xt_entry_match **match; > struct xt_entry_target **target; > }; > void *xt_entry; > void *udata; > }

...

case integer of 0: (match: ^Pxt_entry_match; xt_entry, udata: pointer); 1: (target: ^Pxt_entry_target); end;

While that does happen to work here, it's not a good way to translate such records because it's easy to make mistakes in case not all parts of the union are of the same size.

Jonas

Reply by email

Jeppe Græsdal Johansen
to Jonas Maebe
Jonas Maebe wrote 20 lines
On 26 Dec 2011, at 19:42, Jeppe Græsdal Johansen wrote: >> union { >> struct xt_entry_match **match; >> struct xt_entry_target **target; >> }; >> void *xt_entry; >> void *udata; >> } ... > case integer of > 0: (match: ^Pxt_entry_match; xt_entry, udata: pointer); > 1: (target: ^Pxt_entry_target); > end; While that does happen to work here, it's not a good way to translate such records because it's easy to make mistakes in case not all parts of the union are of the same size. Jonas_______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/mailman/listinfo/fpc-pascal

True. I thought about it for some time, but realized that it is okay in this case because they are pointers. It's probably the same reason the comment is there in the C struct

Reply by email

dmitry boyarintsev
to Jeppe Græsdal Johansen

Chelper also has hard-timer converting the structure: It failed on "union nf_inet_addr haddr, hmask;" so I had to comment it out. Some structures are missing completely.

type
  TAuxType0 = packed record
  case Integer of
    0:(u8 : uint8_t;u8_range : array [0..1] of uint8_t;syslog_level :
uint8_t;protocol : uint8_t;);
    1:(u16 : uint16_t;u16_range : array [0..1] of uint16_t;port :
uint16_t;port_range : array [0..1] of uint16_t;);
    2:(u32 : uint32_t;u32_range : array [0..1] of uint32_t;);
    3:(u64 : uint64_t;u64_range : array [0..1] of uint64_t;);
    4:(dbl : double;);
    5:();
    //union nf_inet_addr haddr, hmask;
    6:();
    7:();
    8:(ethermac : array [0..5] of uint8_t;);
  end;
  xt_option_call = packed record
    arg : Pchar_;
    ext_name : Pchar_;
    entry : Pxt_option_entry;
    data : Pointer;
    xflags : unsigned int;
    invert : bool;
    nvals : uint8_t;
    val : TAuxType0;
    (* Wished for a world where the ones below were gone:  *)
    xt_entry : Pointer;
    udata : Pointer;
  end;

Reply by email

ik
to dmitry boyarintsev

Thank you all for your help

Ido

On Mon, Dec 26, 2011 at 20:54, dmitry boyarintsev <

Show quoted text 40 lines
wrote: Chelper also has hard-timer converting the structure: It failed on "union nf_inet_addr haddr, hmask;" so I had to comment it out. Some structures are missing completely. type TAuxType0 = packed record case Integer of 0:(u8 : uint8_t;u8_range : array [0..1] of uint8_t;syslog_level : uint8_t;protocol : uint8_t;); 1:(u16 : uint16_t;u16_range : array [0..1] of uint16_t;port : uint16_t;port_range : array [0..1] of uint16_t;); 2:(u32 : uint32_t;u32_range : array [0..1] of uint32_t;); 3:(u64 : uint64_t;u64_range : array [0..1] of uint64_t;); 4:(dbl : double;); 5:(); //union nf_inet_addr haddr, hmask; 6:(); 7:(); 8:(ethermac : array [0..5] of uint8_t;); end; xt_option_call = packed record arg : Pchar_; ext_name : Pchar_; entry : Pxt_option_entry; data : Pointer; xflags : unsigned int; invert : bool; nvals : uint8_t; val : TAuxType0; (* Wished for a world where the ones below were gone: *) xt_entry : Pointer; udata : Pointer; end; _______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/mailman/listinfo/fpc-pascal

Reply by email