Added iterator global registry.

This commit is contained in:
Vreixo Formoso 2008-03-06 23:59:32 +01:00
parent d534a96c83
commit edc5ccf90a
3 changed files with 70 additions and 0 deletions

View File

@ -135,6 +135,11 @@ int iso_dir_find_children(IsoDir* dir, IsoFindCondition *cond,
data->cond = cond; data->cond = cond;
it->data = data; it->data = data;
if (iso_dir_iter_register(it) < 0) {
free(it);
return ISO_OUT_OF_MEM;
}
*iter = it; *iter = it;
return ISO_SUCCESS; return ISO_SUCCESS;
} }

View File

@ -640,6 +640,11 @@ int iso_dir_get_children(const IsoDir *dir, IsoDirIter **iter)
data->flag = 0x00; data->flag = 0x00;
it->data = data; it->data = data;
if (iso_dir_iter_register(it) < 0) {
free(it);
return ISO_OUT_OF_MEM;
}
*iter = it; *iter = it;
return ISO_SUCCESS; return ISO_SUCCESS;
} }
@ -663,6 +668,7 @@ int iso_dir_iter_has_next(IsoDirIter *iter)
void iso_dir_iter_free(IsoDirIter *iter) void iso_dir_iter_free(IsoDirIter *iter)
{ {
if (iter != NULL) { if (iter != NULL) {
iso_dir_iter_unregister(iter);
iter->class->free(iter); iter->class->free(iter);
free(iter); free(iter);
} }
@ -970,6 +976,51 @@ int iso_dir_insert(IsoDir *dir, IsoNode *node, IsoNode **pos,
return ++dir->nchildren; return ++dir->nchildren;
} }
/* iterators are stored in a linked list */
struct iter_reg_node {
IsoDirIter *iter;
struct iter_reg_node *next;
};
/* list header */
static
struct iter_reg_node *iter_reg = NULL;
/**
* Add a new iterator to the registry. The iterator register keeps track of
* all iterators being used, and are notified when directory structure
* changes.
*/
int iso_dir_iter_register(IsoDirIter *iter)
{
struct iter_reg_node *new;
new = malloc(sizeof(struct iter_reg_node));
if (new == NULL) {
return ISO_OUT_OF_MEM;
}
new->iter = iter;
new->next = iter_reg;
iter_reg = new;
return ISO_SUCCESS;
}
/**
* Unregister a directory iterator.
*/
void iso_dir_iter_unregister(IsoDirIter *iter)
{
struct iter_reg_node **pos;
pos = &iter_reg;
while (*pos != NULL && (*pos)->iter != iter) {
pos = &(*pos)->next;
}
if (*pos) {
struct iter_reg_node *tmp = (*pos)->next;
free(*pos);
*pos = tmp;
}
}
int iso_node_new_root(IsoDir **root) int iso_node_new_root(IsoDir **root)
{ {
IsoDir *dir; IsoDir *dir;

View File

@ -322,4 +322,18 @@ int iso_dir_exists(IsoDir *dir, const char *name, IsoNode ***pos);
int iso_dir_insert(IsoDir *dir, IsoNode *node, IsoNode **pos, int iso_dir_insert(IsoDir *dir, IsoNode *node, IsoNode **pos,
enum iso_replace_mode replace); enum iso_replace_mode replace);
/**
* Add a new iterator to the registry. The iterator register keeps track of
* all iterators being used, and are notified when directory structure
* changes.
*/
int iso_dir_iter_register(IsoDirIter *iter);
/**
* Unregister a directory iterator.
*/
void iso_dir_iter_unregister(IsoDirIter *iter);
#endif /*LIBISO_NODE_H_*/ #endif /*LIBISO_NODE_H_*/