int use_count;
};
-hnode_t *xref_alloc(void *c __attribute__((unused)))
+hnode_t *xref_alloc(void *c UNUSED)
{
return malloc(sizeof *xref_alloc(NULL));
}
-void xref_free(hnode_t *n, void *c __attribute__((unused)))
+void xref_free(hnode_t *n, void *c UNUSED)
{
struct xref_node *xr = (struct xref_node *)hnode_get(n);
free((void*)hnode_getkey(n));
Gedcom_ctxt object)
{
struct xref_node *xr = NULL;
- const char *key = strdup(xrefstr);
+ char *key = strdup(xrefstr);
if (key) {
xr = make_xref_node();
xr->xref.type = xref_type;
if (xr->xref.string)
free(xr->xref.string);
xr->xref.string = strdup(xrefstr);
- if (! xr->xref.string) MEMORY_ERROR;
- hash_alloc_insert(xrefs, key, xr);
+ if (! xr->xref.string) {
+ MEMORY_ERROR;
+ free(key);
+ delete_xref_node(xr);
+ xr = NULL;
+ }
+ else {
+ hash_alloc_insert(xrefs, key, xr);
+ }
}
else
MEMORY_ERROR;
xr = add_xref(xref_type, raw_value, NULL);
}
- set_xref_fields(xr, ctxt, xref_type);
- return &(xr->xref);
+ if (xr) {
+ set_xref_fields(xr, ctxt, xref_type);
+ return &(xr->xref);
+ }
+ else
+ return NULL;
}
/* Functions for retrieving, modifying and deleting cross-references */
+int is_valid_pointer(const char *key)
+{
+ return (strlen(key) <= 22 &&
+ gedcom_check_token(key, STATE_NORMAL, POINTER) == 0);
+}
+
+/** Retrieve an xref_value by its key.
+
+ \param key The given cross-reference key
+
+ \return The object referenced by the key, or \c NULL if the given key
+ isn't a valid cross-reference key (see detailed description of
+ \ref parsed_xref) or isn't used.
+*/
struct xref_value* gedcom_get_by_xref(const char *key)
{
- if (gedcom_check_token(key, STATE_NORMAL, POINTER) != 0) {
+ if (!is_valid_pointer(key)) {
gedcom_error(_("String '%s' is not a valid cross-reference key"), key);
return NULL;
}
}
}
+/** Add an xref_value of the given type, with the given key, to the given
+ object, with a use count equal to 0.
+
+ \param type The type of the referenced object
+ \param xrefstr The key for the object
+ \param object The object to be referenced
+
+ \return The new xref_value if success, or \c NULL in one of the following
+ cases:
+ - the key isn't a valid cross-reference key (see detailed description of
+ \ref parsed_xref)
+ - there is already an xref_value with the same key
+ - there was a memory allocation error
+*/
struct xref_value* gedcom_add_xref(Xref_type type, const char* xrefstr,
Gedcom_ctxt object)
{
struct xref_node *xr = NULL;
- if (gedcom_check_token(xrefstr, STATE_NORMAL, POINTER) != 0) {
+ if (!is_valid_pointer(xrefstr)) {
gedcom_error(_("String '%s' is not a valid cross-reference key"), xrefstr);
}
else {
gedcom_error(_("Cross-reference %s already exists"), xrefstr);
}
else {
- xr = add_xref(type, xrefstr, object);
- set_xref_fields(xr, XREF_DEFINED, type);
+ xr = add_xref(type, xrefstr, object);
+ if (xr)
+ set_xref_fields(xr, XREF_DEFINED, type);
}
}
if (xr)
return NULL;
}
+/** Declare the xref_value corresponding to the given key as being used as the
+ given type. The use of this function is not mandatory, but it can aid in
+ spotting places in the code where xref_value objects are deleted while
+ they are still referenced.
+
+ \param type The type of the referenced object
+ \param xrefstr The key for the object
+
+ \return The xref_value object if success, and its use count is incremented.
+ Returns NULL in one of the following cases:
+ - the key isn't a valid cross-reference key (see detailed description of
+ \ref parsed_xref)
+ - there is no xref_value with the given key
+ - the xref_value was previously added as another type than the type
+ provided here
+ */
struct xref_value* gedcom_link_xref(Xref_type type, const char* xrefstr)
{
struct xref_node *xr = NULL;
- if (gedcom_check_token(xrefstr, STATE_NORMAL, POINTER) != 0) {
+ if (!is_valid_pointer(xrefstr)) {
gedcom_error(_("String '%s' is not a valid cross-reference key"), xrefstr);
}
else {
return NULL;
}
+/** Declare the xref_value corresponding to the given key no longer used.
+ The use of this function is not mandatory, but it can aid in
+ spotting places in the code where xref_value objects are deleted while
+ they are still referenced.
+
+ \param type The type of the referenced object
+ \param xrefstr The key for the object
+
+ \return The xref_value object if success, and its use count is decremented.
+ Returns NULL in one of the following cases:
+ - the key isn't a valid cross-reference key (see detailed description of
+ \ref parsed_xref)
+ - there is no xref_value with the given key
+ - the xref_value was previously added as another type than the type
+ provided here
+ */
struct xref_value* gedcom_unlink_xref(Xref_type type, const char* xrefstr)
{
struct xref_node *xr = NULL;
- if (gedcom_check_token(xrefstr, STATE_NORMAL, POINTER) != 0) {
+ if (!is_valid_pointer(xrefstr)) {
gedcom_error(_("String '%s' is not a valid cross-reference key"), xrefstr);
}
else {
return NULL;
}
+/** Delete the xref_value corresponding to the given key.
+
+ \param xrefstr The key for the object
+
+ \return 0 if success; 1 in one of the following cases:
+ - the key isn't a valid cross-reference key (see detailed description of
+ \ref parsed_xref)
+ - there is no xref_value with the given key
+ - the xref_value is still in use, i.e. its use count is not 0 (see
+ gedcom_link_xref() and gedcom_unlink_xref())
+ */
int gedcom_delete_xref(const char* xrefstr)
{
struct xref_node *xr = NULL;
int result = 1;
- if (gedcom_check_token(xrefstr, STATE_NORMAL, POINTER) != 0) {
+ if (!is_valid_pointer(xrefstr)) {
gedcom_error(_("String '%s' is not a valid cross-reference key"), xrefstr);
}
else {