* Copyright (c) 2004 Novell, Inc. All Rights Reserved. * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; version 2 of the License. * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA /* Free a field value and tag */ static void _free_field(NMField * field); static void _free_field_value(NMField * field); /* Make a deep copy of the field */ static void _copy_field(NMField * dest, NMField * src); /* Make a deep copy of the field's value */ static void _copy_field_value(NMField * dest, NMField * src); /* Create a string from a value -- for debugging */ static char *_value_to_string(NMField * field); _add_blank_field(NMField *fields, guint32 count) fields = g_new0(NMField, 10); if (fields->len < count + 2) { fields = g_realloc(fields, new_len * sizeof(NMField)); nm_field_add_number(NMField * fields, const char *tag, guint32 size, guint8 method, guint8 flags, guint32 value, guint8 type) count = nm_count_fields(fields); fields = _add_blank_field(fields, count); field = &(fields[count]); field->tag = g_strdup(tag); /* Null terminate the field array */ field = &((fields)[count + 1]); nm_field_add_pointer(NMField * fields, const char *tag, guint32 size, guint8 method, guint8 flags, gpointer value, guint8 type) count = nm_count_fields(fields); fields = _add_blank_field(fields, count); field = &(fields[count]); field->tag = g_strdup(tag); field->ptr_value = value; /* Null terminate the field array */ field = &((fields)[count + 1]); nm_count_fields(NMField * fields) while (fields->tag != NULL) { nm_free_fields(NMField ** fields) if ((fields == NULL) || (*fields == NULL)) while (field->tag != NULL) { _free_field(NMField * field) _free_field_value(field); _free_field_value(NMField * field) case NMFIELD_TYPE_BINARY: g_free(field->ptr_value); nm_free_fields((NMField **)&field->ptr_value); nm_locate_field(char *tag, NMField * fields) NMField *ret_fields = NULL; if ((fields == NULL) || (tag == NULL)) { while (fields->tag != NULL) { if (g_ascii_strcasecmp(fields->tag, tag) == 0) { nm_copy_field_array(NMField * src) count = nm_count_fields(src) + 1; dest = g_new0(NMField, count); while (src->tag != NULL) { _copy_field(NMField * dest, NMField * src) dest->flags = src->flags; dest->method = src->method; dest->tag = g_strdup(src->tag); _copy_field_value(dest, src); _copy_field_value(NMField * dest, NMField * src) if (src->size == 0 && src->ptr_value != NULL) { src->size = strlen((char *) src->ptr_value) + 1; case NMFIELD_TYPE_BINARY: if (src->size != 0 && src->ptr_value != NULL) { dest->ptr_value = g_new0(char, src->size); memcpy(dest->ptr_value, src->ptr_value, src->size); dest->ptr_value = nm_copy_field_array((NMField *)src->ptr_value); dest->value = src->value; nm_remove_field(NMField * field) if ((field != NULL) && (field->tag != NULL)) { /* Don't overwrite the size of the array */ nm_print_fields(NMField * fields) while (field->tag != NULL) { if (field->type == NMFIELD_TYPE_ARRAY || field->type == NMFIELD_TYPE_MV) { printf("Subarray START: %s Method = %d\n", field->tag, field->method); nm_print_fields((NMField *) field->ptr_value); printf("Subarray END: %s\n", field->tag); str = _value_to_string(field); printf("Tag=%s;Value=%s\n", field->tag, str); _value_to_string(NMField * field) /* This is a single value attribute */ if (((field->type == NMFIELD_TYPE_UTF8) || (field->type == NMFIELD_TYPE_DN)) && (field->ptr_value != NULL)) { value = g_strdup((const char *) field->ptr_value); } else if (field->type == NMFIELD_TYPE_BINARY && field->ptr_value != NULL) { value = g_new0(char, field->size); memcpy(value, (const char *) field->ptr_value, field->size); } else if (field->type == NMFIELD_TYPE_BOOL) { value = g_strdup(NM_FIELD_TRUE); value = g_strdup(NM_FIELD_FALSE); /* assume it is a number */ value = g_new0(char, 20); value = g_strdup_printf("%ld", (long) field->value); case NMFIELD_TYPE_UDWORD: value = g_strdup_printf("%lu", (unsigned long) field->value); value = g_strdup("NULL");