Pokology - a community-driven site around GNU poke
_____
---' __\_______
______) Frequently Asked Questions
__)
__)
---._______)
Table of Contents
_________________
1. Language
.. 1. Is there an equivalent of _Static_assert(sizeof(struct my_struct) = 48)?
.. 2. How do I check for trailing bytes at the end of a file
1 Language
==========
1.1 Is there an equivalent of _Static_assert(sizeof(struct my_struct) = 48)?
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
If the struct type My_Struct is complete, i.e. its size can be
determined at compile-time, then you can do this:
,----
| assert (1#My_Struct == 48#B)
`----
Otherwise you can construct a struct value and use the 'size attribute
like this:
,----
| assert (My_Struct{}'size == 48#B)
`----
1.2 How do I check for trailing bytes at the end of a file
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
When you have a single mapping that (supposedly) wholly encompasses a
file, it may be useful to be able to deal with extraneous bytes at the
end: then iosize(fd) comes in handy:
,----
| var fd = open("/etc/passwd");
| type mytype = char[200];
| var relevant = mytype @ fd : 0#B;
| var trailing_len = iosize(fd) - relevant'size;
`----
One way to deal with it would be to assert that it doesn't happen:
,----
| assert(0#B == trailing_len); // or simply:
| assert(iosize(fd) == relevant'size);
`----
Or you can carve it out and parade it:
,----
| var trailing_chars = char[] @ fd : iosize(fd) - trailing_len;
| var trim = alignto(trailing_len, sizeof(short));
| var trailing_shorts = short[trailing_len - trim] @ fd : relevant'size;
| assert(relevant'size + trailing_shorts'size + trim == iosize(fd));
| assert(trim'magnitude*trim'unit == iosize(fd)/#b % 16);
`----