diff options
author | Russell Belfer <arrbee@arrbee.com> | 2012-03-07 04:14:31 +0400 |
---|---|---|
committer | Russell Belfer <arrbee@arrbee.com> | 2012-03-07 04:27:13 +0400 |
commit | ae9e29fde7e7d1c0c3e95bdabbb5c96fc71b1c71 (patch) | |
tree | 65d8215f898fc30b579b72d815e6adc78823dd6c /src/vector.c | |
parent | cb8a79617b15e347f26d21cedde0f2b8670c1876 (diff) |
Migrating diff to new error handling
Ended up migrating a bunch of upstream functions as well
including vector, attr_file, and odb in order to get this
to work right.
Diffstat (limited to 'src/vector.c')
-rw-r--r-- | src/vector.c | 63 |
1 files changed, 27 insertions, 36 deletions
diff --git a/src/vector.c b/src/vector.c index 7513ea3f0..d73c2b418 100644 --- a/src/vector.c +++ b/src/vector.c @@ -19,10 +19,9 @@ static int resize_vector(git_vector *v) v->_alloc_size = minimum_size; v->contents = git__realloc(v->contents, v->_alloc_size * sizeof(void *)); - if (v->contents == NULL) - return GIT_ENOMEM; + GITERR_CHECK_ALLOC(v->contents); - return GIT_SUCCESS; + return 0; } void git_vector_free(git_vector *v) @@ -52,30 +51,29 @@ int git_vector_init(git_vector *v, unsigned int initial_size, git_vector_cmp cmp v->sorted = 1; v->contents = git__malloc(v->_alloc_size * sizeof(void *)); - if (v->contents == NULL) - return GIT_ENOMEM; + GITERR_CHECK_ALLOC(v->contents); - return GIT_SUCCESS; + return 0; } int git_vector_insert(git_vector *v, void *element) { assert(v); - if (v->length >= v->_alloc_size) { - if (resize_vector(v) < 0) - return GIT_ENOMEM; - } + if (v->length >= v->_alloc_size && + resize_vector(v) < 0) + return -1; v->contents[v->length++] = element; v->sorted = 0; - return GIT_SUCCESS; + return 0; } -int git_vector_insert_sorted(git_vector *v, void *element, int (*on_dup)(void **old, void *new)) +int git_vector_insert_sorted( + git_vector *v, void *element, int (*on_dup)(void **old, void *new)) { - int error = GIT_SUCCESS; + int result; size_t pos; assert(v && v->_cmp); @@ -83,22 +81,18 @@ int git_vector_insert_sorted(git_vector *v, void *element, int (*on_dup)(void ** if (!v->sorted) git_vector_sort(v); - if (v->length >= v->_alloc_size) { - if (resize_vector(v) < 0) - return GIT_ENOMEM; - } - - error = git__bsearch(v->contents, v->length, element, v->_cmp, &pos); + if (v->length >= v->_alloc_size && + resize_vector(v) < 0) + return -1; - /* If we found the element and have a duplicate handler callback, - * invoke it. If it returns an error, then cancel insert, otherwise + /* If we find the element and have a duplicate handler callback, + * invoke it. If it returns non-zero, then cancel insert, otherwise * proceed with normal insert. */ - if (error == GIT_SUCCESS && on_dup != NULL) { - error = on_dup(&v->contents[pos], element); - if (error != GIT_SUCCESS) - return error; - } + if (git__bsearch(v->contents, v->length, element, v->_cmp, &pos) >= 0 && + on_dup != NULL && + (result = on_dup(&v->contents[pos], element)) < 0) + return result; /* shift elements to the right */ if (pos < v->length) { @@ -108,8 +102,7 @@ int git_vector_insert_sorted(git_vector *v, void *element, int (*on_dup)(void ** v->contents[pos] = element; v->length++; - - return GIT_SUCCESS; + return 0; } void git_vector_sort(git_vector *v) @@ -130,16 +123,14 @@ int git_vector_bsearch2(git_vector *v, git_vector_cmp key_lookup, const void *ke assert(v && key && key_lookup); /* need comparison function to sort the vector */ - if (v->_cmp == NULL) - return git__throw(GIT_ENOTFOUND, "Can't sort vector. No comparison function set"); + assert(v->_cmp != NULL); git_vector_sort(v); - if (git__bsearch(v->contents, v->length, key, key_lookup, - &pos) == GIT_SUCCESS) + if (git__bsearch(v->contents, v->length, key, key_lookup, &pos) >= 0) return (int)pos; - return git__throw(GIT_ENOTFOUND, "Can't find element"); + return GIT_ENOTFOUND; } int git_vector_search2(git_vector *v, git_vector_cmp key_lookup, const void *key) @@ -153,7 +144,7 @@ int git_vector_search2(git_vector *v, git_vector_cmp key_lookup, const void *key return i; } - return git__throw(GIT_ENOTFOUND, "Can't find element"); + return GIT_ENOTFOUND; } static int strict_comparison(const void *a, const void *b) @@ -178,13 +169,13 @@ int git_vector_remove(git_vector *v, unsigned int idx) assert(v); if (idx >= v->length || v->length == 0) - return git__throw(GIT_ENOTFOUND, "Can't remove element. Index out of bounds"); + return GIT_ENOTFOUND; for (i = idx; i < v->length - 1; ++i) v->contents[i] = v->contents[i + 1]; v->length--; - return GIT_SUCCESS; + return 0; } void git_vector_pop(git_vector *v) |