const char* GetNodeUserField_bn ( const node_bn*  node,   const char*  name,   int*  length,   int  kind )

Returns the user-defined data associated with node on a named-field basis (i.e., attribute-value). For more information, see SetNodeUserField_bn.

For name pass the name of the field to be read, which was passed to SetNodeUserField_bn when the data was set.

Pass 0 for kind. It is only for future expansion.

This function will return a pointer to the data, and set *length to the length that was passed into SetNodeUserField_bn. If you try to retrieve a field that was never set, this will return a null terminated empty string (""), and will set *length to -1.

Netica always places two null bytes after the end of the data (without altering length of course), which is of no consequence if the data is arbitrary bytes, but may be helpful if it is an ascii or Unicode string, and you want to safely retrieve it solely by pointer, ignoring length. The return type is a pointer to char, which is also of no consequence if arbitrary bytes were passed to SetNodeUserField_bn, but is handy if strings were passed, which is recommended when feasible.

If you need the result to persist, make a copy of the data returned, since its contents may become invalid after further calls to Netica API. Do not try to directly modify or free the string returned.

Some helpful functions to read user fields which are integers, real numbers and strings are: GetNodeUserInt, GetNodeUserNumber and GetNodeUserString, which are provided in NeticaEx.c, and in the examples below. See SetNodeUserField_bn for the matching functions that do the setting.

Version:

Versions 2.00 and later have this function.

See also:

SetNodeUserField_bn    Sets them
GetNodeNthUserField_bn    Retrieve field by index. Iterate over fields
GetNodeUserData_bn    For user-managed data, which is not saved to file
GetNetUserField_bn    User field data attached to the whole net

Example:

// To get a user field which is an ascii string
//
const char* GetNodeUserString (node_bn* node, const char* fieldname){
    return GetNodeUserField_bn (node, fieldname, NULL, 0);
}
Example 2:
The following function is available in NeticaEx.c:
// To get a user field which is an integer // #include <stdlib.h> long GetNodeUserInt (node_bn* node, const char* fieldname){ int length; const char* str = GetNodeUserField_bn (node, fieldname, length, 0); if (length == -1) NewError (env, 0, ERROR_ERR, "GetNodeUserInt: There is no user field named '%s' in node '%s'", fieldname, GetNodeName_bn (node)); else { char* end; long num = strtol (str, end, 10); if (*end != 0) NewError (env, 0, ERROR_ERR, "GetNodeUserInt: Field named '%s' of node '%s' was not storing an integer", fieldname, GetNodeName_bn (node)); else return num; } return 0; }
Example 3:
The following function is available in NeticaEx.c:
// To get a user field which is a real number // #include <stdlib.h> double GetNodeUserNumber (node_bn* node, const char* fieldname){ int length; const char* str = GetNodeUserField_bn (node, fieldname, length, 0); if (length == -1) NewError (env, 0, ERROR_ERR, "GetNodeUserInt: There is no user field named '%s' in node '%s'", fieldname, GetNodeName_bn (node)); else { char* end; double num = strtod (str, end); if (*end != 0) NewError (env, 0, ERROR_ERR, "GetNodeUserInt: Field named '%s' of node '%s' was not storing a number", fieldname, GetNodeName_bn (node)); else return num; } return 0; }