4 * Copyright (c) 2004 Novell, Inc. All Rights Reserved.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
25 /* Free a field value and tag */
26 static void _free_field(NMField
* field
);
28 /* Free a field value */
29 static void _free_field_value(NMField
* field
);
31 /* Make a deep copy of the field */
32 static void _copy_field(NMField
* dest
, NMField
* src
);
34 /* Make a deep copy of the field's value */
35 static void _copy_field_value(NMField
* dest
, NMField
* src
);
37 /* Create a string from a value -- for debugging */
38 static char *_value_to_string(NMField
* field
);
41 _add_blank_field(NMField
*fields
, guint32 count
)
46 fields
= g_new0(NMField
, 10);
49 if (fields
->len
< count
+ 2) {
51 fields
= g_realloc(fields
, new_len
* sizeof(NMField
));
52 fields
->len
= new_len
;
59 nm_field_add_number(NMField
* fields
, const char *tag
, guint32 size
, guint8 method
,
60 guint8 flags
, guint32 value
, guint8 type
)
65 count
= nm_count_fields(fields
);
66 fields
= _add_blank_field(fields
, count
);
68 field
= &(fields
[count
]);
69 field
->tag
= g_strdup(tag
);
71 field
->method
= method
;
76 /* Null terminate the field array */
77 field
= &((fields
)[count
+ 1]);
80 field
->ptr_value
= NULL
;
86 nm_field_add_pointer(NMField
* fields
, const char *tag
, guint32 size
, guint8 method
,
87 guint8 flags
, gpointer value
, guint8 type
)
90 NMField
*field
= NULL
;
92 count
= nm_count_fields(fields
);
93 fields
= _add_blank_field(fields
, count
);
95 field
= &(fields
[count
]);
96 field
->tag
= g_strdup(tag
);
98 field
->method
= method
;
100 field
->ptr_value
= value
;
103 /* Null terminate the field array */
104 field
= &((fields
)[count
+ 1]);
107 field
->ptr_value
= NULL
;
113 nm_count_fields(NMField
* fields
)
118 while (fields
->tag
!= NULL
) {
128 nm_free_fields(NMField
** fields
)
130 NMField
*field
= NULL
;
132 if ((fields
== NULL
) || (*fields
== NULL
))
137 while (field
->tag
!= NULL
) {
148 _free_field(NMField
* field
)
153 _free_field_value(field
);
158 _free_field_value(NMField
* field
)
163 switch (field
->type
) {
164 case NMFIELD_TYPE_BINARY
:
165 case NMFIELD_TYPE_UTF8
:
166 case NMFIELD_TYPE_DN
:
167 g_free(field
->ptr_value
);
170 case NMFIELD_TYPE_ARRAY
:
171 case NMFIELD_TYPE_MV
:
172 nm_free_fields((NMField
**)&field
->ptr_value
);
180 field
->ptr_value
= NULL
;
184 nm_locate_field(char *tag
, NMField
* fields
)
186 NMField
*ret_fields
= NULL
;
188 if ((fields
== NULL
) || (tag
== NULL
)) {
192 while (fields
->tag
!= NULL
) {
193 if (g_ascii_strcasecmp(fields
->tag
, tag
) == 0) {
204 nm_copy_field_array(NMField
* src
)
207 NMField
*dest
= NULL
;
211 count
= nm_count_fields(src
) + 1;
212 dest
= g_new0(NMField
, count
);
215 while (src
->tag
!= NULL
) {
216 _copy_field(ptr
, src
);
226 _copy_field(NMField
* dest
, NMField
* src
)
228 dest
->type
= src
->type
;
229 dest
->flags
= src
->flags
;
230 dest
->method
= src
->method
;
231 dest
->tag
= g_strdup(src
->tag
);
232 _copy_field_value(dest
, src
);
236 _copy_field_value(NMField
* dest
, NMField
* src
)
238 dest
->type
= src
->type
;
239 switch (dest
->type
) {
240 case NMFIELD_TYPE_UTF8
:
241 case NMFIELD_TYPE_DN
:
242 if (src
->size
== 0 && src
->ptr_value
!= NULL
) {
243 src
->size
= strlen((char *) src
->ptr_value
) + 1;
246 case NMFIELD_TYPE_BINARY
:
247 if (src
->size
!= 0 && src
->ptr_value
!= NULL
) {
248 dest
->ptr_value
= g_new0(char, src
->size
);
249 memcpy(dest
->ptr_value
, src
->ptr_value
, src
->size
);
253 case NMFIELD_TYPE_ARRAY
:
254 case NMFIELD_TYPE_MV
:
255 dest
->ptr_value
= nm_copy_field_array((NMField
*)src
->ptr_value
);
260 dest
->value
= src
->value
;
264 dest
->size
= src
->size
;
268 nm_remove_field(NMField
* field
)
273 if ((field
!= NULL
) && (field
->tag
!= NULL
)) {
276 /* Move fields down */
279 /* Don't overwrite the size of the array */
286 if (tmp
->tag
== NULL
)
296 nm_print_fields(NMField
* fields
)
299 NMField
*field
= fields
;
304 while (field
->tag
!= NULL
) {
305 if (field
->type
== NMFIELD_TYPE_ARRAY
|| field
->type
== NMFIELD_TYPE_MV
) {
306 printf("Subarray START: %s Method = %d\n", field
->tag
, field
->method
);
307 nm_print_fields((NMField
*) field
->ptr_value
);
308 printf("Subarray END: %s\n", field
->tag
);
310 str
= _value_to_string(field
);
311 printf("Tag=%s;Value=%s\n", field
->tag
, str
);
321 _value_to_string(NMField
* field
)
328 /* This is a single value attribute */
329 if (((field
->type
== NMFIELD_TYPE_UTF8
) ||
330 (field
->type
== NMFIELD_TYPE_DN
)) && (field
->ptr_value
!= NULL
)) {
331 value
= g_strdup((const char *) field
->ptr_value
);
332 } else if (field
->type
== NMFIELD_TYPE_BINARY
&& field
->ptr_value
!= NULL
) {
333 value
= g_new0(char, field
->size
);
334 memcpy(value
, (const char *) field
->ptr_value
, field
->size
);
335 } else if (field
->type
== NMFIELD_TYPE_BOOL
) {
337 value
= g_strdup(NM_FIELD_TRUE
);
339 value
= g_strdup(NM_FIELD_FALSE
);
342 /* assume it is a number */
343 switch (field
->type
) {
344 case NMFIELD_TYPE_BYTE
:
345 case NMFIELD_TYPE_WORD
:
346 case NMFIELD_TYPE_DWORD
:
347 value
= g_strdup_printf("%ld", (long) field
->value
);
350 case NMFIELD_TYPE_UBYTE
:
351 case NMFIELD_TYPE_UWORD
:
352 case NMFIELD_TYPE_UDWORD
:
353 value
= g_strdup_printf("%lu", (unsigned long) field
->value
);
359 value
= g_strdup("NULL");