Take care about duplicate node names in iso_node_set_name().

This commit is contained in:
Vreixo Formoso 2007-12-06 02:22:11 +01:00
parent 06db9a3c73
commit d4deb4866f
2 changed files with 31 additions and 8 deletions

View File

@ -191,11 +191,15 @@ void iso_node_ref(IsoNode *node);
void iso_node_unref(IsoNode *node); void iso_node_unref(IsoNode *node);
/** /**
* Set the name of a node. * Set the name of a node. Note that if the node is already added to a dir
* this can fail if dir already contains a node with the new name.
* *
* @param name The name in UTF-8 encoding * @param name
* The name in UTF-8 encoding
* @return
* 1 on success, < 0 on error
*/ */
void iso_node_set_name(IsoNode *node, const char *name); int iso_node_set_name(IsoNode *node, const char *name);
/** /**
* Get the name of a node (in UTF-8). * Get the name of a node (in UTF-8).
@ -289,10 +293,12 @@ int iso_dir_add_node(IsoDir *dir, IsoNode *child, int replace);
* doesn't have a child with the given name. * doesn't have a child with the given name.
* The node will be owned by the dir and shouldn't be unref(). Just call * The node will be owned by the dir and shouldn't be unref(). Just call
* iso_node_ref() to get your own reference to the node. * iso_node_ref() to get your own reference to the node.
* Note that you can pass NULL is the only thing you want to do is check
* if a node with such name already exists on dir.
* @return * @return
* 1 node found, 0 child has no such node, < 0 error * 1 node found, 0 child has no such node, < 0 error
* Possible errors: * Possible errors:
* ISO_NULL_POINTER, if dir, node or name are NULL * ISO_NULL_POINTER, if dir or name are NULL
*/ */
int iso_dir_get_node(IsoDir *dir, const char *name, IsoNode **node); int iso_dir_get_node(IsoDir *dir, const char *name, IsoNode **node);

View File

@ -68,10 +68,17 @@ void iso_node_unref(IsoNode *node)
* *
* @param name The name in UTF-8 encoding * @param name The name in UTF-8 encoding
*/ */
void iso_node_set_name(IsoNode *node, const char *name) int iso_node_set_name(IsoNode *node, const char *name)
{ {
if (node->parent != NULL) {
/* check if parent already has a node with same name */
if (iso_dir_get_node(node->parent, name, NULL) == 1) {
return ISO_NODE_NAME_NOT_UNIQUE;
}
}
free(node->name); free(node->name);
node->name = strdup(name); node->name = strdup(name);
return node->name != NULL ? ISO_SUCCESS : ISO_MEM_ERROR;
} }
/** /**
@ -221,13 +228,19 @@ int iso_dir_add_node(IsoDir *dir, IsoNode *child, int replace)
* @param node * @param node
* Location for a pointer to the node, it will filled with NULL if the dir * Location for a pointer to the node, it will filled with NULL if the dir
* doesn't have a child with the given name. * doesn't have a child with the given name.
* The node will be owned by the dir and shouldn't be unref(). Just call
* iso_node_ref() to get your own reference to the node.
* Note that you can pass NULL is the only thing you want to do is check
* if a node with such name already exists on dir.
* @return * @return
* 1 node found, 0 child has no such node, < 0 error * 1 node found, 0 child has no such node, < 0 error
* Possible errors:
* ISO_NULL_POINTER, if dir or name are NULL
*/ */
int iso_dir_get_node(IsoDir *dir, const char *name, IsoNode **node) int iso_dir_get_node(IsoDir *dir, const char *name, IsoNode **node)
{ {
IsoNode *pos; IsoNode *pos;
if (dir == NULL || name == NULL || node == NULL) { if (dir == NULL || name == NULL) {
return ISO_NULL_POINTER; return ISO_NULL_POINTER;
} }
@ -237,11 +250,15 @@ int iso_dir_get_node(IsoDir *dir, const char *name, IsoNode **node)
} }
if (pos == NULL || strcmp(pos->name, name)) { if (pos == NULL || strcmp(pos->name, name)) {
*node = NULL; if (node) {
*node = NULL;
}
return 0; /* node not found */ return 0; /* node not found */
} }
*node = pos; if (node) {
*node = pos;
}
return 1; return 1;
} }