- cmp = 1;
- }
-
- CDEBUG6(dict_search, "Cmp '%.*s' and '%.*s' (offset=%d) => %d",
- p_child->key_len - *p_offset, p_child->key + *p_offset,
- key_len - *p_offset, key + *p_offset,
- *p_offset,cmp);
-
- end:
- *p_offset = o;
- *p_match = m;
- *p_cmp = cmp;
-}
-
-/**
- * _gras_dict_child_search:
- *
- * Search where would be inserted @key between the childs of @p_elm.
- *
- * Returns position of the child having a common prefix with this key
- * If *match==0, no child have a common prefix
- * *pos is where to add the key
- * If *match==1, A child (located at *pos) have exactly this key
- * If *match==2, A child (located at *pos) constitutes a prefix of the key
- * the recursion have to go on that guy
- * *prefix = the size of the key eaten at this level
- * If *match==3 The key is a prefix of the child at *pos
- * If *match==4, A child (loc. at *pos) share a common prefix with this key
- * *prefix = size of the prefix.
- * If searching, that's a mismatch.
- * If inserting, you have to break the child and create an
- * internal node having {child, key} as childs
- * offset is used in input and output. In input, that's the length of the key
- * handled by previous levels of recursion. In output, that the one counting
- * also this level.
- */
-static _GRAS_INLINE
-void
-_gras_dictelm_child_search(gras_dictelm_t *p_elm,
- const char *key,
- int key_len,
- int *p_pos,
- int *p_offset,
- int *p_match) {
-
- int p = 0;
- int o = *p_offset;
- int m = 0;
- int len = 0;
-
-
- CDEBUG5(dict_search, "search child [%.*s] under [%.*s] (len=%lu)",
- key_len, key,
- p_elm?p_elm->key_len:6, p_elm?p_elm->key:"(head)",
- (p_elm&&p_elm->sub)?gras_dynar_length(p_elm->sub):0);
-
-
- len = gras_dynar_length(p_elm->sub);
-
- for (p = 0; p < len; p++) {
- int cmp = 0;
-
- _dict_child_cmp(p_elm, p, key, key_len, &o, &m, &cmp);
-
- if (m)
- break;
-
- o = *p_offset;
- m = 0;
- }
-
- *p_offset = o;
- *p_pos = p;
- *p_match = m;
- CDEBUG5(dict_search, "search [%.*s] in [%.*s] => %s",
- key_len, key,
- p_elm?p_elm->key_len:6, p_elm?p_elm->key:"(head)",
- ( m == 0 ? "no child have a common prefix" :
- ( m == 1 ? "selected child have exactly this key" :
- ( m == 2 ? "selected child constitutes a prefix" :
- ( m == 3 ? "key is a prefix of selected child" :
- (m == 4 ? "selected child share a prefix" :
- "internal error")))))
- );
-}
-
-/**
- * _gras_dictelm_change_value:
- *
- * Change the value of the dictelm, making sure to free the old one, if any.
- */
-static _GRAS_INLINE
-void
-_gras_dictelm_change_value(gras_dictelm_t *p_elm,
- void *data,
- void_f_pvoid_t *free_ctn) {
-
- if (p_elm->content && p_elm->free_ctn) {
- p_elm->free_ctn(p_elm->content);
- }
-
- p_elm->free_ctn = free_ctn;
- p_elm->content = data;
-}
-
-/**
- * _gras_dictelm_set_rec:
- *
- * @head: the head of the dict
- * @key: the key to set the new data
- * @offset: offset on key.
- * @data: the data to add in the dict
- * @Returns: a gras_error
- *
- * set the @data in the structure under the @key. The @key is destroyed
- * in the process. Think to strdup it before.
- *
- * This is a helper function to gras_dict_set which locks the struct and
- * strdup the key before action.
- */
-gras_error_t
-_gras_dictelm_set_rec(gras_dictelm_t *p_head,
- char *key,
- int key_len,
- int offset,
- void *data,
- void_f_pvoid_t *free_ctn) {
- gras_error_t errcode = no_error;
- int match = 0;
- int pos = 0;
- const int old_offset = offset;
-
- CDEBUG6(dict_add, "--> Insert '%.*s' after '%.*s' (offset=%d) in tree %p",
- key_len, key,
- ((p_head && p_head->key) ? p_head->key_len : 6),
- ((p_head && p_head->key) ? p_head->key : "(head)"),
- offset, (void*)p_head);
-
- /*** The trivial cases first ***/
-
- /* there is no key (we did enough recursion), change the value of head */
- if (offset >= key_len) {
-
- CDEBUG0(dict_add, "--> Change the value of head");
-
- _gras_dictelm_change_value(p_head, data, free_ctn);
- free(key); /* Keep the key used in the tree */
-
- return errcode;
- }
-
- /*** Search where to add this child, and how ***/
- _gras_dictelm_child_search(p_head, key, key_len, &pos, &offset, &match);
-
- CDEBUG3(dict_add, "child_search => pos=%d, offset=%d, match=%d",
- pos, offset, match);
-
- switch (match) {
-
- case 0: /* no child have a common prefix */
- {
- gras_dictelm_t *p_child = NULL;
-
- TRY(_gras_dictelm_alloc(key, key_len, offset, data, free_ctn, &p_child));
- CDEBUG1(dict_add, "-> Add a child %p", (void*)p_child);
- TRY(gras_dynar_insert_at(p_head->sub, pos, &p_child));
-
- return errcode;
- }
-
- case 1: /* A child have exactly this key => change its value*/
- {
- gras_dictelm_t *p_child = NULL;
-
- gras_dynar_get(p_head->sub, pos, &p_child);
- CDEBUG1(dict_add, "-> Change the value of the child %p", (void*)p_child);
- _gras_dictelm_change_value(p_child, data, free_ctn);
-
- free(key);
-
- return errcode;
- }
-
- case 2: /* A child constitutes a prefix of the key => recurse */
- {
- gras_dictelm_t *p_child = NULL;
-
- gras_dynar_get(p_head->sub, pos, &p_child);
- CDEBUG2(dict_add,"-> Recurse on %p (offset=%d)", (void*)p_child, offset);
-
- return _gras_dictelm_set_rec(p_child, key, key_len,
- offset, data, free_ctn);
- }
-
- case 3: /* The key is a prefix of the child => child becomes child of p_new */
- {
- gras_dictelm_t *p_new = NULL;
- gras_dictelm_t *p_child = NULL;
-
- gras_dynar_get(p_head->sub, pos, &p_child);
- TRY(_gras_dictelm_alloc(key, key_len, old_offset, data, free_ctn, &p_new));
-
- CDEBUG2(dict_add, "-> The child %p become child of new dict (%p)",
- (void*)p_child, (void*)p_new);
-
- TRY(gras_dynar_push(p_new->sub, &p_child));
- p_child->offset = offset;
- TRY(gras_dynar_set(p_head->sub, pos, &p_new));
-
- return errcode;
- }
-
- case 4: /* A child share a common prefix with this key => Common ancestor */
- {
- gras_dictelm_t *p_new = NULL;
- gras_dictelm_t *p_child = NULL;
- gras_dictelm_t *p_anc = NULL;
- char *anc_key = NULL;
- int anc_key_len = offset;
-
- TRY(_gras_dictelm_alloc(key, key_len, offset, data, free_ctn, &p_new));
- gras_dynar_get(p_head->sub, pos, &p_child);
-
- anc_key = memdup(key, anc_key_len);
-
- TRY(_gras_dictelm_alloc(anc_key, anc_key_len, old_offset,
- NULL, NULL, &p_anc));
-
- CDEBUG3(dict_add, "-> Make a common ancestor %p (%.*s)",
- (void*)p_anc, anc_key_len, anc_key);
-
- if (key[offset] < p_child->key[offset]) {
- TRY(gras_dynar_push(p_anc->sub, &p_new));
- TRY(gras_dynar_push(p_anc->sub, &p_child));
- } else {
- TRY(gras_dynar_push(p_anc->sub, &p_child));
- TRY(gras_dynar_push(p_anc->sub, &p_new));
- }
-
- p_child->offset = offset;
-
- TRY(gras_dynar_set(p_head->sub, pos, &p_anc));
-
- return errcode;
- }
-
- default:
- RAISE_IMPOSSIBLE;
- }
-
-}
-
-/**
- * gras_dictelm_set_ext:
- *
- * @head: the head of the dict
- * @key: the key to set the new data
- * @data: the data to add in the dict
- * @Returns: a gras_error
- *
- * set the @data in the structure under the @key, which can be any kind
- * of data, as long as its length is provided in @key_len.
- */
-gras_error_t
-gras_dictelm_set_ext(gras_dictelm_t **pp_head,
- const char *_key,
- int key_len,
- void *data,
- void_f_pvoid_t *free_ctn) {
- gras_error_t errcode = no_error;
- gras_dictelm_t *p_head = *pp_head;
- char *key = NULL;
-
- key = memdup(_key, key_len);
- if (!key)
- RAISE_MALLOC;
-
- /* there is no head, create it */
- if (!p_head) {
- gras_dictelm_t *p_child = NULL;
-
- CDEBUG0(dict_add, "Create an head");
-
- /* The head is priviledged by being the only one with a NULL key */
- TRY(_gras_dictelm_alloc(NULL, 0, 0, NULL, NULL, &p_head));
-
- TRY(_gras_dictelm_alloc(key, key_len, 0, data, free_ctn, &p_child));
- TRY(gras_dynar_insert_at(p_head->sub, 0, &p_child));
-
- *pp_head = p_head;
-
- return errcode;
- }
-
- return _gras_dictelm_set_rec(p_head, key, key_len, 0, data, free_ctn);
-}
-
-/**
- * gras_dictelm_set:
- *
- * @head: the head of the dict
- * @key: the key to set the new data
- * @data: the data to add in the dict
- * @Returns: a gras_error
- *
- * set the @data in the structure under the @key, which is a
- * null terminated string.
- */
-gras_error_t
-gras_dictelm_set(gras_dictelm_t **pp_head,
- const char *_key,
- void *data,
- void_f_pvoid_t *free_ctn) {
-
- return gras_dictelm_set_ext(pp_head, _key, 1+strlen(_key), data, free_ctn);
-}
-
-/**
- * _gras_dict_get_rec:
- *
- * @head: the head of the dict
- * @key: the key to find data
- * @offset: offset on the key
- * @data: the data that we are looking for
- * @Returns: gras_error
- *
- * Search the given @key. mismatch_error when not found.
- */
-static
-gras_error_t
-_gras_dictelm_get_rec(gras_dictelm_t *p_head,
- const char *key,
- int key_len,
- int offset,
- void **data) {
- void *res;
-
- CDEBUG3(dict_search, "Search %.*s in %p", key_len, key, (void*)p_head);
-
- /*** The trivial case first ***/
-
- /* we did enough recursion, we're done */
- if (offset >= key_len) {
- *data = p_head->content;
-
- return no_error;
- }
-
- {
- int match = 0;
- int pos = 0;
-
- *data = NULL; /* Make it ready to answer 'not found' in one operation */
-
- /*** Search where is the good child, and how good it is ***/
- _gras_dictelm_child_search(p_head, key, key_len, &pos, &offset, &match);
-
- switch (match) {
-
- case 0: /* no child have a common prefix */
- return mismatch_error;
-
- case 1: /* A child have exactly this key => Got it */
- {
- gras_dictelm_t *p_child = NULL;
-
- gras_dynar_get(p_head->sub, pos, &p_child);
- *data = p_child->content;
-
- return no_error;
- }
-
- case 2: /* A child constitutes a prefix of the key => recurse */
- {
- gras_dictelm_t *p_child = NULL;
-
- gras_dynar_get(p_head->sub, pos, &p_child);
-
- return _gras_dictelm_get_rec(p_child, key, key_len, offset, data);
- }
-
- case 3: /* The key is a prefix of the child => not found */
- return mismatch_error;
-
- case 4: /* A child share a common prefix with this key => not found */
- return mismatch_error;
-
- default:
- RAISE_IMPOSSIBLE;
- }
- }
-}
-
-/**
- * gras_dictelm_get_ext:
- *
- * @head: the head of the dict
- * @key: the key to find data
- * @data: the data that we are looking for
- * @Returns: gras_error
- *
- * Search the given @key. mismatch_error when not found.
- */
-gras_error_t
-gras_dictelm_get_ext(gras_dictelm_t *p_head,
- const char *key,
- int key_len,
- /* OUT */void **data) {
- /* there is no head, go to hell */
- if (!p_head) {
- return mismatch_error;