GSP
Quick Navigator

Search Site

Unix VPS
A - Starter
B - Basic
C - Preferred
D - Commercial
MPS - Dedicated
Previous VPSs
* Sign Up! *

Support
Contact Us
Online Help
Handbooks
Domain Status
Man Pages

FAQ
Virtual Servers
Pricing
Billing
Technical

Network
Facilities
Connectivity
Topology Map

Miscellaneous
Server Agreement
Year 2038
Credits
 

USA Flag

 

 

Man Pages
STRUCTS_TYPE_STRUCT(3) FreeBSD Library Functions Manual STRUCTS_TYPE_STRUCT(3)

structs_type_struct
structs types for C structures

PDEL Library (libpdel, -lpdel)

#include <sys/types.h>
#include <stddef.h>
#include <pdel/structs/structs.h>
#include <pdel/structs/type/struct.h>

STRUCTS_STRUCT_TYPE(struct_name, field_list);

STRUCTS_STRUCT_FIELD(struct_name, field_name, field_type);

STRUCTS_STRUCT_FIELD2(struct_name, field_name, display_name, field_type);

The STRUCTS_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 STRUCTS_STRUCT_FIELD_END, which is defined as follows:

#define STRUCTS_STRUCT_FIELD_END { NULL, NULL, 0, 0 }

The STRUCTS_STRUCT_FIELD() macro should be used to define an entry in the field array: field_name is the name of the field and field_type is a pointer to the structs(3) type describing the field.

To define a field and give it a different structs(3) name than its name in the C structure, use STRUCTS_STRUCT_FIELD2() with the desired display_name in double quotes.

libpdel(3), structs(3), structs_type(3), structs_type_union(3)

The 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);
}

The PDEL library was developed at Packet Design, LLC. http://www.packetdesign.com/

Archie Cobbs ⟨archie@freebsd.org⟩
April 22, 2002 FreeBSD 13.1-RELEASE

Search for    or go to Top of page |  Section 3 |  Main Index

Powered by GSP Visit the GSP FreeBSD Man Page Interface.
Output converted with ManDoc.