The examples below show techniques to list and work with attributes of an object.
The example below shows how to list the attributes of an object.
#include <stdio.h>
#include <xfn/xfn.h>
/*
This routine prints all the attributes associated
with the named object to the standard output.
Examples of using the function:
fns_attr_list("user/jsmith");
fns_attr_list("thisorgunit/service/printer/color");
*/
void fns_attr_list(const char *name)
{
FN_composite_name_t *name_comp;
const FN_identifier_t *identifier;
FN_attribute_t *attribute;
const FN_attrvalue_t *values;
char *id, *val;
FN_multigetlist_t *attrset;
void *ip;
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;
}
/* Obtain all the attributes */
attrset = fn_attr_multi_get(initial_context, name_comp, 0, 0,
status);
if (!fn_status_is_success(status)) {
fprintf(stderr, "Unable to obtain attributes\n");
return;
}
/* List all attributes */
while (attribute = fn_multigetlist_next(attrset, status)) {
identifier = fn_attribute_identifier(attribute);
switch(identifier->format) {
case FN_ID_STRING:
id = (char *) malloc(identifier->length + 1);
memcpy(id, identifier->contents, identifier->length);
id[identifier->length] = '\0';
printf("Attribute Identifier: %s", id);
free(id);
break;
default:
printf("Attribute of non-string format\n\n");
continue;
}
for (values = fn_attribute_first(attribute, &ip);
values != NULL;
values = fn_attribute_next(attribute, &ip)) {
val = (char *) malloc(values->length + 1);
memcpy(val, values->contents, values->length);
val[values->length] = '\0';
printf("Value: %s", val);
free(val);
}
fn_attribute_destroy(attribute);
printf("\n");
}
fn_multigetlist_destroy(attrset);
fn_ctx_destroy(initial_context);
fn_status_destroy(status);
fn_composite_name_destroy(name_comp);
}
|
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);
]
]
|