Annotation of embedaddon/libpdel/structs/type/structs_type_struct.3, revision 1.1.1.1
1.1 misho 1: .\" Copyright (c) 2001-2002 Packet Design, LLC.
2: .\" All rights reserved.
3: .\"
4: .\" Subject to the following obligations and disclaimer of warranty,
5: .\" use and redistribution of this software, in source or object code
6: .\" forms, with or without modifications are expressly permitted by
7: .\" Packet Design; provided, however, that:
8: .\"
9: .\" (i) Any and all reproductions of the source or object code
10: .\" must include the copyright notice above and the following
11: .\" disclaimer of warranties; and
12: .\" (ii) No rights are granted, in any manner or form, to use
13: .\" Packet Design trademarks, including the mark "PACKET DESIGN"
14: .\" on advertising, endorsements, or otherwise except as such
15: .\" appears in the above copyright notice or in the software.
16: .\"
17: .\" THIS SOFTWARE IS BEING PROVIDED BY PACKET DESIGN "AS IS", AND
18: .\" TO THE MAXIMUM EXTENT PERMITTED BY LAW, PACKET DESIGN MAKES NO
19: .\" REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING
20: .\" THIS SOFTWARE, INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED
21: .\" WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE,
22: .\" OR NON-INFRINGEMENT. PACKET DESIGN DOES NOT WARRANT, GUARANTEE,
23: .\" OR MAKE ANY REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS
24: .\" OF THE USE OF THIS SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY,
25: .\" RELIABILITY OR OTHERWISE. IN NO EVENT SHALL PACKET DESIGN BE
26: .\" LIABLE FOR ANY DAMAGES RESULTING FROM OR ARISING OUT OF ANY USE
27: .\" OF THIS SOFTWARE, INCLUDING WITHOUT LIMITATION, ANY DIRECT,
28: .\" INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, PUNITIVE, OR CONSEQUENTIAL
29: .\" DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, LOSS OF
30: .\" USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY THEORY OF
31: .\" LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32: .\" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
33: .\" THE USE OF THIS SOFTWARE, EVEN IF PACKET DESIGN IS ADVISED OF
34: .\" THE POSSIBILITY OF SUCH DAMAGE.
35: .\"
36: .\" Author: Archie Cobbs <archie@freebsd.org>
37: .\"
38: .\" $Id: structs_type_struct.3,v 1.8 2004/12/16 16:49:57 archie Exp $
39: .\"
40: .Dd April 22, 2002
41: .Dt STRUCTS_TYPE_STRUCT 3
42: .Os
43: .Sh NAME
44: .Nm structs_type_struct
45: .Nd structs types for C structures
46: .Sh LIBRARY
47: PDEL Library (libpdel, \-lpdel)
48: .Sh SYNOPSIS
49: .In sys/types.h
50: .In stddef.h
51: .In pdel/structs/structs.h
52: .In pdel/structs/type/struct.h
53: .Fn STRUCTS_STRUCT_TYPE struct_name field_list
54: .Fn STRUCTS_STRUCT_FIELD struct_name field_name field_type
55: .Fn STRUCTS_STRUCT_FIELD2 struct_name field_name display_name field_type
56: .Sh DESCRIPTION
57: The
58: .Fn STRUCTS_STRUCT_TYPE
59: macro defines a
60: .Xr structs 3
61: type (i.e., a
62: .Dv "struct structs_type" )
63: for describing the C structure
64: .Li struct
65: .Fa struct_name .
66: The
67: .Fa field_list
68: parameter must point to an array of
69: .Li "struct structs_field"
70: structures describing the fields:
71: .Pp
72: .Bd -literal -compact -offset 3n
73: /* This structure describes one field in a structure */
74: struct structs_field {
75: const char *name; /* field name */
76: const struct structs_type *type; /* field type */
77: u_int16_t size; /* field size */
78: u_int16_t offset; /* field offset */
79: };
80: .Ed
81: .Pp
82: The fields need not be listed in the array in the same order as they
83: are declared in the C structure.
84: However, the array must be terminated with
85: .Dv STRUCTS_STRUCT_FIELD_END ,
86: which is defined as follows:
87: .Pp
88: .Dl #define STRUCTS_STRUCT_FIELD_END { NULL, NULL, 0, 0 }
89: .Pp
90: The
91: .Fn STRUCTS_STRUCT_FIELD
92: macro should be used to define an entry in the field array:
93: .Fa field_name
94: is the name of the field and
95: .Fa field_type
96: is a pointer to the
97: .Xr structs 3
98: type describing the field.
99: .Pp
100: To define a field and give it a different
101: .Xr structs 3
102: name than its name in the C structure, use
103: .Fn STRUCTS_STRUCT_FIELD2
104: with the desired
105: .Fa display_name
106: in double quotes.
107: .Sh SEE ALSO
108: .Xr libpdel 3 ,
109: .Xr structs 3 ,
110: .Xr structs_type 3 ,
111: .Xr structs_type_union 3
112: .Sh EXAMPLES
113: The program below prints out the contents (as an ASCII string)
114: of the field specified on the command line:
115: .Pp
116: .Bd -literal -compact -offset 3n
117: #include <sys/types.h>
118: #include <sys/socket.h>
119: #include <netinet/in.h>
120: #include <arpa/inet.h>
121:
122: #include <stdio.h>
123: #include <stdlib.h>
124: #include <err.h>
125:
126: #include <pdel/structs/structs.h>
127: #include <pdel/structs/type/struct.h>
128: #include <pdel/structs/type/string.h>
129: #include <pdel/structs/type/ip4.h>
130: #include <pdel/util/typed_mem.h>
131:
132: /* My structure */
133: struct foobar {
134: char *name;
135: u_int16_t index;
136: struct in_addr ipaddr;
137: };
138:
139: /* Structs type describing a 'struct foobar' */
140: static const struct structs_field foobar_fields = {
141: STRUCTS_STRUCT_FIELD(foobar, name, &structs_type_string),
142: STRUCTS_STRUCT_FIELD(foobar, index, &structs_type_uint16),
143: STRUCTS_STRUCT_FIELD(foobar, ipaddr, &structs_type_ip4),
144: STRUCTS_STRUCT_FIELD_END
145: };
146: static const struct structs_type foobar_type =
147: STRUCTS_STRUCT_TYPE(foobar, &foobar_fields);
148:
149: int
150: main(int argc, char **argv)
151: {
152: struct foobar f;
153: const char *fieldname;
154: char *fieldvalue;
155:
156: /* Initialize our structure with some contents */
157: if (structs_init(&foobar_type, NULL, &f) == -1)
158: err(1, "structs_init");
159: f.index = 123;
160: (void)inet_aton("12.34.56.78", &f.ipaddr);
161: if (structs_set_string(&foobar_type, "name",
162: "this is a string", &f, NULL, 0) == -1)
163: err(1, "structs_set_string");
164:
165: /* Get the requested field's name from the command line */
166: if (argc != 2)
167: err(1, "usage: getfield <fieldname>");
168: fieldname = argv[1];
169:
170: /* Display the requested field's value */
171: if ((fieldvalue = structs_get_string(&foobar_type,
172: fieldname, &f, TYPED_MEM_TEMP)) == NULL)
173: err(1, "%s", fieldname);
174: printf("The value of field \\"%s\\" is: %s\\n",
175: fieldname, fieldvalue);
176:
177: /* Done, clean up */
178: FREE(TYPED_MEM_TEMP, fieldvalue);
179: structs_free(&foobar_type, NULL, &f);
180: return (0);
181: }
182: .Ed
183: .Sh HISTORY
184: The PDEL library was developed at Packet Design, LLC.
185: .Dv "http://www.packetdesign.com/"
186: .Sh AUTHORS
187: .An Archie Cobbs Aq archie@freebsd.org
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>