diff options
Diffstat (limited to 'src/util.c')
-rw-r--r-- | src/util.c | 108 |
1 files changed, 78 insertions, 30 deletions
diff --git a/src/util.c b/src/util.c index 1ca9d850c..ce770203a 100644 --- a/src/util.c +++ b/src/util.c @@ -1,5 +1,5 @@ /* - * Copyright (C) 2009-2011 the libgit2 contributors + * Copyright (C) 2009-2012 the libgit2 contributors * * This file is part of libgit2, distributed under the GNU GPL v2 with * a Linking Exception. For full terms see the included COPYING file. @@ -31,19 +31,33 @@ void git_strarray_free(git_strarray *array) git__free(array->strings); } -int git__fnmatch(const char *pattern, const char *name, int flags) +int git_strarray_copy(git_strarray *tgt, const git_strarray *src) { - int ret; - - ret = p_fnmatch(pattern, name, flags); - switch (ret) { - case 0: - return GIT_SUCCESS; - case FNM_NOMATCH: - return GIT_ENOMATCH; - default: - return git__throw(GIT_EOSERR, "Error trying to match path"); + size_t i; + + assert(tgt && src); + + memset(tgt, 0, sizeof(*tgt)); + + if (!src->count) + return 0; + + tgt->strings = git__calloc(src->count, sizeof(char *)); + GITERR_CHECK_ALLOC(tgt->strings); + + for (i = 0; i < src->count; ++i) { + tgt->strings[tgt->count] = git__strdup(src->strings[i]); + + if (!tgt->strings[tgt->count]) { + git_strarray_free(tgt); + memset(tgt, 0, sizeof(*tgt)); + return -1; + } + + tgt->count++; } + + return 0; } int git__strtol64(int64_t *result, const char *nptr, const char **endptr, int base) @@ -61,7 +75,7 @@ int git__strtol64(int64_t *result, const char *nptr, const char **endptr, int ba /* * White space */ - while (isspace(*p)) + while (git__isspace(*p)) p++; /* @@ -111,34 +125,40 @@ int git__strtol64(int64_t *result, const char *nptr, const char **endptr, int ba } Return: - if (ndig == 0) - return git__throw(GIT_ENOTNUM, "Failed to convert string to long. Not a number"); + if (ndig == 0) { + giterr_set(GITERR_INVALID, "Failed to convert string to long. Not a number"); + return -1; + } if (endptr) *endptr = p; - if (ovfl) - return git__throw(GIT_EOVERFLOW, "Failed to convert string to long. Overflow error"); + if (ovfl) { + giterr_set(GITERR_INVALID, "Failed to convert string to long. Overflow error"); + return -1; + } *result = neg ? -n : n; - return GIT_SUCCESS; + return 0; } int git__strtol32(int32_t *result, const char *nptr, const char **endptr, int base) { - int error = GIT_SUCCESS; + int error; int32_t tmp_int; int64_t tmp_long; - if ((error = git__strtol64(&tmp_long, nptr, endptr, base)) < GIT_SUCCESS) + if ((error = git__strtol64(&tmp_long, nptr, endptr, base)) < 0) return error; tmp_int = tmp_long & 0xFFFFFFFF; - if (tmp_int != tmp_long) - return git__throw(GIT_EOVERFLOW, "Failed to convert. '%s' is too large", nptr); + if (tmp_int != tmp_long) { + giterr_set(GITERR_INVALID, "Failed to convert. '%s' is too large", nptr); + return -1; + } *result = tmp_int; - + return error; } @@ -355,23 +375,27 @@ int git__bsearch( int (*compare)(const void *, const void *), size_t *position) { - int lim, cmp; + unsigned int lim; + int cmp = -1; void **part, **base = array; - for (lim = array_len; lim != 0; lim >>= 1) { + for (lim = (unsigned int)array_len; lim != 0; lim >>= 1) { part = base + (lim >> 1); cmp = (*compare)(key, *part); if (cmp == 0) { - *position = (part - array); - return GIT_SUCCESS; - } else if (cmp > 0) { /* key > p; take right partition */ + base = part; + break; + } + if (cmp > 0) { /* key > p; take right partition */ base = part + 1; lim--; } /* else take left partition */ } - *position = (base - array); - return GIT_ENOTFOUND; + if (position) + *position = (base - array); + + return (cmp == 0) ? 0 : -1; } /** @@ -387,3 +411,27 @@ int git__strcmp_cb(const void *a, const void *b) return strcmp(stra, strb); } + +int git__parse_bool(int *out, const char *value) +{ + /* A missing value means true */ + if (value == NULL) { + *out = 1; + return 0; + } + + if (!strcasecmp(value, "true") || + !strcasecmp(value, "yes") || + !strcasecmp(value, "on")) { + *out = 1; + return 0; + } + if (!strcasecmp(value, "false") || + !strcasecmp(value, "no") || + !strcasecmp(value, "off")) { + *out = 0; + return 0; + } + + return -1; +} |