The example below shows how to add, delete, or modify an object's attributes.
#include <stdio.h>
#include <xfn/xfn.h>
/*
This routine modifies an attribute associated
with the named object. The modify operation supported are:
FN_ATTR_OP_ADD
FN_ATTR_OP_ADD_EXCLUSIVE
FN_ATTR_OP_REMOVE
FN_ATTR_OP_ADD_VALUES
FN_ATTR_OP_REMOVE_VALUES
The function assumes the attribute values to be strings.
Examples of using the function:
The following function add an attribute of identifier "realname"
with value "James Smith" to the user object "user/jsmith".
fns_attr_modify(
"user/jsmith",
"realname",
"James Smith",
FN_ATTR_OP_ADD);
The following function removes an attribute of identifier
"location" from the printer object
"thisorgunit/service/printer/color".
fns_attr_modify(
"thisorgunit/service/printer/color",
"location",
NULL,
FN_ATTR_OP_REMOVE);
*/
static const char *attr_id_syntax = "fn_attr_syntax_ascii";
void fns_attr_modify(const char *name,
const char *attr_id,
const char *attr_value,
unsigned int operation)
{
FN_composite_name_t *name_comp;
FN_identifier_t identifier, syntax;
FN_attrvalue_t *values;
FN_attribute_t *attribute;
FN_status_t *status;
FN_ctx_t *initial_context;
name_comp = fn_composite_name_from_str((unsigned char *) name);
status = fn_status_create();
/* Obtain the initial context */
initial_context = fn_ctx_handle_from_initial(0, status);
if (!fn_status_is_success(status)) {
fprintf(stderr, "Unable to obtain intial context\n");
return;
}
/* Create the attribute to be added */
/* First, the identifier */
identifier.format = FN_ID_STRING;
identifier.length = strlen(attr_id);
identifier.contents = (void *) strdup(attr_id);
/* Second, the syntax */
syntax.format = FN_ID_STRING;
syntax.length = strlen(attr_id_syntax);
syntax.contents = (void *) strdup(attr_id_syntax);
/* Third, the attribute value */
if (attr_value) {
values = (FN_attrvalue_t *) malloc(sizeof(FN_attrvalue_t));
values->length = strlen(attr_value);
values->contents = (void *) strdup(attr_value);
} else
values = NULL;
/* Fourth, create the attribute */
attribute = fn_attribute_create(&identifier, &syntax);
/*Fifth, add the attribute value */
if (values)
fn_attribute_add(attribute, values, 0);
/* Perform the XFN operation */
fn_attr_modify(initial_context, name_comp, operation, attribute, 0,
status);
if (!fn_status_is_success(status))
fprintf(stderr, "Unable to perform attribute operation\n");
fn_ctx_destroy(initial_context);
fn_status_destroy(status);
fn_composite_name_destroy(name_comp);
fn_attibute_destroy(attribute);
free(identifier.contents);
free(syntax.contents);
if (values) {
free(values->contents);
free(values);
]
]
|