1 /*****************************************************************************/
2 /* LibreDWG - free implementation of the DWG file format */
3 /* */
4 /* Copyright (C) 2018-2020 Free Software Foundation, Inc. */
5 /* */
6 /* This library is free software, licensed under the terms of the GNU */
7 /* General Public License as published by the Free Software Foundation, */
8 /* either version 3 of the License, or (at your option) any later version. */
9 /* You should have received a copy of the GNU General Public License */
10 /* along with this program. If not, see <http://www.gnu.org/licenses/>. */
11 /*****************************************************************************/
12
13 /*
14 * dynapi.h: dynamic access to all object and field names and types
15 * written by Reini Urban
16 */
17
18 #ifndef DYNAPI_H
19 #define DYNAPI_H
20
21 #include "config.h"
22 #include <stdbool.h>
23 #include "common.h"
24 #include "dwg.h"
25
26 #ifdef HAVE_STDDEF_H
27 # include <stddef.h>
28 #else /* cygwin */
29 # ifndef offsetof
30 # define offsetof(type, member) ((size_t) & (((type *)0)->member))
31 # endif
32 #endif
33 #define OFF(st, f) offsetof (st, f)
34
35 // avoid double linkage on windows with unit-testing
36 #if defined(DYNAPI_TEST_C)
37 # undef EXPORT
38 # define EXPORT
39 #endif
40
41 #ifndef _DWG_API_H_
42 /* Public API, duplicate of dwg_api.h */
43 typedef struct dwg_field_name_type_offset
44 {
45 const char *const name; /* field name */
46 const char *const type; /* e.g "RS" for BITCODE_RS */
47 const unsigned short size; /* e.g. 2 for RS, 4 for BL */
48 const unsigned short offset;
49 const unsigned short
50 is_indirect : 1; // for pointers, references, like 3BD, CMC, H, TV
51 const unsigned short is_malloc : 1; // for strings and dynamic arrays only,
52 // H*, TV, 3BD*, unknown size
53 const unsigned short is_string : 1; // for null-terminated strings, use
54 // strcpy/wcscpy. not memcpy
55 const short dxf;
56 } Dwg_DYNAPI_field;
57
58 EXPORT bool is_dwg_entity (const char *name) __nonnull ((1));
59 EXPORT bool is_dwg_object (const char *name) __nonnull ((1));
60 EXPORT bool dwg_dynapi_header_value (const Dwg_Data *restrict dwg,
61 const char *restrict fieldname,
62 void *restrict out,
63 Dwg_DYNAPI_field *restrict fp)
64 __nonnull ((1, 2, 3));
65 EXPORT bool
66 dwg_dynapi_entity_value (void *restrict entity, const char *restrict dxfname,
67 const char *restrict fieldname, void *restrict out,
68 Dwg_DYNAPI_field *restrict fp)
69 __nonnull ((1, 2, 3, 4));
70 EXPORT bool
71 dwg_dynapi_common_value (void *restrict _obj, const char *restrict fieldname,
72 void *restrict out, Dwg_DYNAPI_field *restrict fp)
73 __nonnull ((1, 2, 3));
74 // r_2007+ creates a fresh UTF-8 copy, <r2007 returns the field value
75 EXPORT bool dwg_dynapi_header_utf8text (const Dwg_Data *restrict dwg,
76 const char *restrict fieldname,
77 char **restrict out, int *isnew,
78 Dwg_DYNAPI_field *restrict fp)
79 __nonnull ((1, 2, 3));
80 EXPORT bool dwg_dynapi_entity_utf8text (void *restrict _obj,
81 const char *restrict name,
82 const char *restrict fieldname,
83 char **restrict out, int *isnew,
84 Dwg_DYNAPI_field *restrict fp)
85 __nonnull ((1, 2, 3, 4));
86 EXPORT bool dwg_dynapi_common_utf8text (void *restrict _obj,
87 const char *restrict fieldname,
88 char **restrict out, int *isnew,
89 Dwg_DYNAPI_field *restrict fp)
90 __nonnull ((1, 2, 3));
91
92 EXPORT bool dwg_dynapi_header_set_value (Dwg_Data *restrict dwg,
93 const char *restrict fieldname,
94 const void *restrict value,
95 const bool is_utf8)
96 __nonnull ((1, 2, 3));
97 EXPORT bool dwg_dynapi_entity_set_value (void *restrict entity,
98 const char *restrict dxfname,
99 const char *restrict fieldname,
100 const void *restrict value,
101 const bool is_utf8)
102 __nonnull ((1, 2, 3, 4));
103 EXPORT bool dwg_dynapi_common_set_value (void *restrict entity,
104 const char *restrict fieldname,
105 const void *restrict value,
106 const bool is_utf8)
107 __nonnull ((1, 2, 3));
108
109 EXPORT bool dwg_dynapi_subclass_value (const void *restrict ptr,
110 const char *restrict subclass,
111 const char *restrict fieldname,
112 void *restrict out,
113 Dwg_DYNAPI_field *restrict fp)
114 __nonnull ((1, 2, 3, 4));
115 EXPORT bool dwg_dynapi_field_get_value (const void *restrict ptr,
116 const Dwg_DYNAPI_field *restrict field,
117 void *restrict out)
118 __nonnull ((1, 2, 3));
119
120 EXPORT bool dwg_dynapi_field_set_value (
121 const Dwg_Data *restrict dwg, /* only needed if unicode strings */
122 void *restrict ptr, const Dwg_DYNAPI_field *restrict field,
123 const void *restrict value, const bool is_utf8) __nonnull ((2, 3, 4));
124
125 EXPORT char *dwg_dynapi_handle_name (const Dwg_Data *restrict dwg,
126 Dwg_Object_Ref *restrict hdl,
127 int *alloced) __nonnull ((1, 2, 3));
128
129 EXPORT const Dwg_DYNAPI_field *
130 dwg_dynapi_header_field (const char *restrict fieldname) __nonnull ((1));
131
132 EXPORT const Dwg_DYNAPI_field *
133 dwg_dynapi_entity_field (const char *restrict name,
134 const char *restrict fieldname) __nonnull ((1, 2));
135
136 EXPORT const Dwg_DYNAPI_field *
137 dwg_dynapi_subclass_field (const char *restrict name,
138 const char *restrict fieldname) __nonnull ((1, 2));
139
140 EXPORT const Dwg_DYNAPI_field *
141 dwg_dynapi_common_entity_field (const char *restrict fieldname)
142 __nonnull ((1));
143
144 EXPORT const Dwg_DYNAPI_field *
145 dwg_dynapi_common_object_field (const char *restrict fieldname)
146 __nonnull ((1));
147
148 /* The array of all fields, NULL terminated. Also for all objects. */
149 EXPORT const Dwg_DYNAPI_field *
150 dwg_dynapi_entity_fields (const char *restrict name) __nonnull ((1));
151
152 EXPORT const Dwg_DYNAPI_field *dwg_dynapi_common_entity_fields (void);
153
154 EXPORT const Dwg_DYNAPI_field *dwg_dynapi_common_object_fields (void);
155
156 /* Find the fields for this subclass. See dwg.h */
157 EXPORT const Dwg_DYNAPI_field *
158 dwg_dynapi_subclass_fields (const char *restrict name) __nonnull ((1));
159
160 /* Search fields by dxf. Returns the first found field,
161 Sets unique to 1 if it's the only result */
162 EXPORT const Dwg_DYNAPI_field *
163 dwg_dynapi_field_dxf (const Dwg_DYNAPI_field *restrict fields, const int dxf,
164 int *restrict unique);
165
166 // The struct size of the object or entity
167 EXPORT int dwg_dynapi_entity_size (const char *restrict name) __nonnull ((1));
168
169 // The struct size of the subclass
170 EXPORT int dwg_dynapi_subclass_size (const char *restrict name)
171 __nonnull ((1));
172
173 int _fields_size_sum (const Dwg_DYNAPI_field *restrict fields);
174
175 // The size of the entity or subclass name.
176 EXPORT int dwg_dynapi_fields_size (const char *restrict name) __nonnull ((1));
177
178 // Converts from the fields type, like "Dwg_MLINESTYLE_line*" to the
179 // subclass name, like "MLINESTYLE_line".
180 ATTRIBUTE_MALLOC char *dwg_dynapi_subclass_name (const char *restrict type);
181
182 #endif
183
184 /* Searches in dwg_name_subclasses[].
185 FIXME: Not yet ready. Need to expand all defines in the spec.
186 Not yet public in 0.11.1 */
187 EXPORT bool dwg_has_subclass (const char *restrict classname,
188 const char *restrict subclass)
189 __nonnull ((1, 2));
190
191 EXPORT bool dwg_dynapi_is_angle (const char *restrict name,
192 const char *restrict fieldname)
193 __nonnull ((1, 2));
194
195 EXPORT bool dwg_dynapi_is_float (const Dwg_DYNAPI_field *f) __nonnull ((1));
196
197 #endif