|
NAMEstructs_type_struct —
structs types for C structures
LIBRARYPDEL Library (libpdel, -lpdel)SYNOPSIS#include <sys/types.h>
#include <stddef.h>
#include
<pdel/structs/structs.h>
#include
<pdel/structs/type/struct.h>
DESCRIPTIONTheSTRUCTS_STRUCT_TYPE () macro defines a
structs(3)
type (i.e., a struct structs_type ) for describing the
C structure struct struct_name.
The field_list parameter must point to an array of
struct structs_field structures describing the fields:
/* This structure describes one field in a structure */ struct structs_field { const char *name; /* field name */ const struct structs_type *type; /* field type */ u_int16_t size; /* field size */ u_int16_t offset; /* field offset */ }; The fields need not be listed in the array in the same order as
they are declared in the C structure. However, the array must be terminated
with #define STRUCTS_STRUCT_FIELD_END {
NULL, NULL, 0, 0 } The To define a field and give it a different
structs(3)
name than its name in the C structure, use
SEE ALSOlibpdel(3), structs(3), structs_type(3), structs_type_union(3)EXAMPLESThe program below prints out the contents (as an ASCII string) of the field specified on the command line:#include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <stdio.h> #include <stdlib.h> #include <err.h> #include <pdel/structs/structs.h> #include <pdel/structs/type/struct.h> #include <pdel/structs/type/string.h> #include <pdel/structs/type/ip4.h> #include <pdel/util/typed_mem.h> /* My structure */ struct foobar { char *name; u_int16_t index; struct in_addr ipaddr; }; /* Structs type describing a 'struct foobar' */ static const struct structs_field foobar_fields = { STRUCTS_STRUCT_FIELD(foobar, name, &structs_type_string), STRUCTS_STRUCT_FIELD(foobar, index, &structs_type_uint16), STRUCTS_STRUCT_FIELD(foobar, ipaddr, &structs_type_ip4), STRUCTS_STRUCT_FIELD_END }; static const struct structs_type foobar_type = STRUCTS_STRUCT_TYPE(foobar, &foobar_fields); int main(int argc, char **argv) { struct foobar f; const char *fieldname; char *fieldvalue; /* Initialize our structure with some contents */ if (structs_init(&foobar_type, NULL, &f) == -1) err(1, "structs_init"); f.index = 123; (void)inet_aton("12.34.56.78", &f.ipaddr); if (structs_set_string(&foobar_type, "name", "this is a string", &f, NULL, 0) == -1) err(1, "structs_set_string"); /* Get the requested field's name from the command line */ if (argc != 2) err(1, "usage: getfield <fieldname>"); fieldname = argv[1]; /* Display the requested field's value */ if ((fieldvalue = structs_get_string(&foobar_type, fieldname, &f, TYPED_MEM_TEMP)) == NULL) err(1, "%s", fieldname); printf("The value of field \"%s\" is: %s\n", fieldname, fieldvalue); /* Done, clean up */ FREE(TYPED_MEM_TEMP, fieldvalue); structs_free(&foobar_type, NULL, &f); return (0); } HISTORYThe PDEL library was developed at Packet Design, LLC.http://www.packetdesign.com/
AUTHORSArchie Cobbs ⟨archie@freebsd.org⟩
Visit the GSP FreeBSD Man Page Interface. |