Commit a70b3c73 by Vicent Martí

Merge pull request #436 from libgit2/config-int-types

config: Proper type declarations for 64 bit ints
parents 358a15fd 107e30e9
...@@ -166,7 +166,7 @@ GIT_EXTERN(void) git_config_free(git_config *cfg); ...@@ -166,7 +166,7 @@ GIT_EXTERN(void) git_config_free(git_config *cfg);
* @param out pointer to the variable where the value should be stored * @param out pointer to the variable where the value should be stored
* @return GIT_SUCCESS or an error code * @return GIT_SUCCESS or an error code
*/ */
GIT_EXTERN(int) git_config_get_int(git_config *cfg, const char *name, int *out); GIT_EXTERN(int) git_config_get_int32(git_config *cfg, const char *name, int32_t *out);
/** /**
* Get the value of a long integer config variable. * Get the value of a long integer config variable.
...@@ -176,7 +176,7 @@ GIT_EXTERN(int) git_config_get_int(git_config *cfg, const char *name, int *out); ...@@ -176,7 +176,7 @@ GIT_EXTERN(int) git_config_get_int(git_config *cfg, const char *name, int *out);
* @param out pointer to the variable where the value should be stored * @param out pointer to the variable where the value should be stored
* @return GIT_SUCCESS or an error code * @return GIT_SUCCESS or an error code
*/ */
GIT_EXTERN(int) git_config_get_long(git_config *cfg, const char *name, long long *out); GIT_EXTERN(int) git_config_get_int64(git_config *cfg, const char *name, int64_t *out);
/** /**
* Get the value of a boolean config variable. * Get the value of a boolean config variable.
...@@ -212,7 +212,7 @@ GIT_EXTERN(int) git_config_get_string(git_config *cfg, const char *name, const c ...@@ -212,7 +212,7 @@ GIT_EXTERN(int) git_config_get_string(git_config *cfg, const char *name, const c
* @param value Integer value for the variable * @param value Integer value for the variable
* @return GIT_SUCCESS or an error code * @return GIT_SUCCESS or an error code
*/ */
GIT_EXTERN(int) git_config_set_int(git_config *cfg, const char *name, int value); GIT_EXTERN(int) git_config_set_int32(git_config *cfg, const char *name, int32_t value);
/** /**
* Set the value of a long integer config variable. * Set the value of a long integer config variable.
...@@ -222,7 +222,7 @@ GIT_EXTERN(int) git_config_set_int(git_config *cfg, const char *name, int value) ...@@ -222,7 +222,7 @@ GIT_EXTERN(int) git_config_set_int(git_config *cfg, const char *name, int value)
* @param value Long integer value for the variable * @param value Long integer value for the variable
* @return GIT_SUCCESS or an error code * @return GIT_SUCCESS or an error code
*/ */
GIT_EXTERN(int) git_config_set_long(git_config *cfg, const char *name, long long value); GIT_EXTERN(int) git_config_set_int64(git_config *cfg, const char *name, int64_t value);
/** /**
* Set the value of a boolean config variable. * Set the value of a boolean config variable.
......
...@@ -164,16 +164,16 @@ int git_config_delete(git_config *cfg, const char *name) ...@@ -164,16 +164,16 @@ int git_config_delete(git_config *cfg, const char *name)
* Setters * Setters
**************/ **************/
int git_config_set_long(git_config *cfg, const char *name, long long value) int git_config_set_int64(git_config *cfg, const char *name, int64_t value)
{ {
char str_value[32]; /* All numbers should fit in here */ char str_value[32]; /* All numbers should fit in here */
p_snprintf(str_value, sizeof(str_value), "%" PRId64, value); p_snprintf(str_value, sizeof(str_value), "%" PRId64, value);
return git_config_set_string(cfg, name, str_value); return git_config_set_string(cfg, name, str_value);
} }
int git_config_set_int(git_config *cfg, const char *name, int value) int git_config_set_int32(git_config *cfg, const char *name, int32_t value)
{ {
return git_config_set_long(cfg, name, (long long)value); return git_config_set_int64(cfg, name, (int64_t)value);
} }
int git_config_set_bool(git_config *cfg, const char *name, int value) int git_config_set_bool(git_config *cfg, const char *name, int value)
...@@ -199,11 +199,11 @@ int git_config_set_string(git_config *cfg, const char *name, const char *value) ...@@ -199,11 +199,11 @@ int git_config_set_string(git_config *cfg, const char *name, const char *value)
* Getters * Getters
***********/ ***********/
int git_config_get_long(git_config *cfg, const char *name, long long *out) int git_config_get_int64(git_config *cfg, const char *name, int64_t *out)
{ {
const char *value, *num_end; const char *value, *num_end;
int ret; int ret;
long long num; int64_t num;
ret = git_config_get_string(cfg, name, &value); ret = git_config_get_string(cfg, name, &value);
if (ret < GIT_SUCCESS) if (ret < GIT_SUCCESS)
...@@ -214,35 +214,45 @@ int git_config_get_long(git_config *cfg, const char *name, long long *out) ...@@ -214,35 +214,45 @@ int git_config_get_long(git_config *cfg, const char *name, long long *out)
return git__rethrow(ret, "Failed to convert value for '%s'", name); return git__rethrow(ret, "Failed to convert value for '%s'", name);
switch (*num_end) { switch (*num_end) {
case '\0': case 'g':
break; case 'G':
case 'k':
case 'K':
num *= 1024; num *= 1024;
break; /* fallthrough */
case 'm': case 'm':
case 'M': case 'M':
num *= 1024 * 1024; num *= 1024;
break; /* fallthrough */
case 'g':
case 'G':
num *= 1024 * 1024 * 1024;
break;
default:
return git__throw(GIT_EINVALIDTYPE, "Failed to get value for '%s'. Value is of invalid type", name);
}
*out = num; case 'k':
case 'K':
num *= 1024;
return GIT_SUCCESS; /* check that that there are no more characters after the
* given modifier suffix */
if (num_end[1] != '\0')
return git__throw(GIT_EINVALIDTYPE,
"Failed to get value for '%s'. Invalid type suffix", name);
/* fallthrough */
case '\0':
*out = num;
return GIT_SUCCESS;
default:
return git__throw(GIT_EINVALIDTYPE,
"Failed to get value for '%s'. Value is of invalid type", name);
}
} }
int git_config_get_int(git_config *cfg, const char *name, int *out) int git_config_get_int32(git_config *cfg, const char *name, int32_t *out)
{ {
long long tmp_long; int64_t tmp_long;
int tmp_int, ret; int32_t tmp_int;
int ret;
ret = git_config_get_long(cfg, name, &tmp_long); ret = git_config_get_int64(cfg, name, &tmp_long);
if (ret < GIT_SUCCESS) if (ret < GIT_SUCCESS)
return git__rethrow(ret, "Failed to convert value for '%s'", name); return git__rethrow(ret, "Failed to convert value for '%s'", name);
...@@ -284,7 +294,7 @@ int git_config_get_bool(git_config *cfg, const char *name, int *out) ...@@ -284,7 +294,7 @@ int git_config_get_bool(git_config *cfg, const char *name, int *out)
} }
/* Try to parse it as an integer */ /* Try to parse it as an integer */
error = git_config_get_int(cfg, name, out); error = git_config_get_int32(cfg, name, out);
if (error == GIT_SUCCESS) if (error == GIT_SUCCESS)
*out = !!(*out); *out = !!(*out);
......
...@@ -46,10 +46,10 @@ int git__fnmatch(const char *pattern, const char *name, int flags) ...@@ -46,10 +46,10 @@ int git__fnmatch(const char *pattern, const char *name, int flags)
} }
} }
int git__strtol64(long long *result, const char *nptr, const char **endptr, int base) int git__strtol64(int64_t *result, const char *nptr, const char **endptr, int base)
{ {
const char *p; const char *p;
long long n, nn; int64_t n, nn;
int c, ovfl, v, neg, ndig; int c, ovfl, v, neg, ndig;
p = nptr; p = nptr;
...@@ -124,10 +124,11 @@ Return: ...@@ -124,10 +124,11 @@ Return:
return GIT_SUCCESS; return GIT_SUCCESS;
} }
int git__strtol32(int *result, const char *nptr, const char **endptr, int base) int git__strtol32(int32_t *result, const char *nptr, const char **endptr, int base)
{ {
int tmp_int, error = GIT_SUCCESS; int error = GIT_SUCCESS;
long long tmp_long; 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)) < GIT_SUCCESS)
return error; return error;
......
...@@ -75,8 +75,8 @@ GIT_INLINE(void *) git__realloc(void *ptr, size_t size) ...@@ -75,8 +75,8 @@ GIT_INLINE(void *) git__realloc(void *ptr, size_t size)
extern int git__prefixcmp(const char *str, const char *prefix); extern int git__prefixcmp(const char *str, const char *prefix);
extern int git__suffixcmp(const char *str, const char *suffix); extern int git__suffixcmp(const char *str, const char *suffix);
extern int git__strtol32(int *n, const char *buff, const char **end_buf, int base); extern int git__strtol32(int32_t *n, const char *buff, const char **end_buf, int base);
extern int git__strtol64(long long *n, const char *buff, const char **end_buf, int base); extern int git__strtol64(int64_t *n, const char *buff, const char **end_buf, int base);
extern void git__hexdump(const char *buffer, size_t n); extern void git__hexdump(const char *buffer, size_t n);
extern uint32_t git__hash(const void *key, int len, uint32_t seed); extern uint32_t git__hash(const void *key, int len, uint32_t seed);
......
...@@ -556,7 +556,7 @@ BEGIN_TEST(rmdir1, "make sure non-empty dir cannot be deleted recusively") ...@@ -556,7 +556,7 @@ BEGIN_TEST(rmdir1, "make sure non-empty dir cannot be deleted recusively")
END_TEST END_TEST
BEGIN_TEST(strtol0, "parsing out 32 integers from a string") BEGIN_TEST(strtol0, "parsing out 32 integers from a string")
int i; int32_t i;
must_pass(git__strtol32(&i, "123", NULL, 10)); must_pass(git__strtol32(&i, "123", NULL, 10));
must_be_true(i == 123); must_be_true(i == 123);
...@@ -575,7 +575,7 @@ BEGIN_TEST(strtol0, "parsing out 32 integers from a string") ...@@ -575,7 +575,7 @@ BEGIN_TEST(strtol0, "parsing out 32 integers from a string")
END_TEST END_TEST
BEGIN_TEST(strtol1, "parsing out 64 integers from a string") BEGIN_TEST(strtol1, "parsing out 64 integers from a string")
long long i; int64_t i;
must_pass(git__strtol64(&i, "123", NULL, 10)); must_pass(git__strtol64(&i, "123", NULL, 10));
must_be_true(i == 123); must_be_true(i == 123);
......
...@@ -37,10 +37,10 @@ ...@@ -37,10 +37,10 @@
*/ */
BEGIN_TEST(config0, "read a simple configuration") BEGIN_TEST(config0, "read a simple configuration")
git_config *cfg; git_config *cfg;
int i; int32_t i;
must_pass(git_config_open_ondisk(&cfg, CONFIG_BASE "/config0")); must_pass(git_config_open_ondisk(&cfg, CONFIG_BASE "/config0"));
must_pass(git_config_get_int(cfg, "core.repositoryformatversion", &i)); must_pass(git_config_get_int32(cfg, "core.repositoryformatversion", &i));
must_be_true(i == 0); must_be_true(i == 0);
must_pass(git_config_get_bool(cfg, "core.filemode", &i)); must_pass(git_config_get_bool(cfg, "core.filemode", &i));
must_be_true(i == 1); must_be_true(i == 1);
...@@ -134,29 +134,29 @@ END_TEST ...@@ -134,29 +134,29 @@ END_TEST
BEGIN_TEST(config5, "test number suffixes") BEGIN_TEST(config5, "test number suffixes")
git_config *cfg; git_config *cfg;
long long i; int64_t i;
must_pass(git_config_open_ondisk(&cfg, CONFIG_BASE "/config5")); must_pass(git_config_open_ondisk(&cfg, CONFIG_BASE "/config5"));
must_pass(git_config_get_long(cfg, "number.simple", &i)); must_pass(git_config_get_int64(cfg, "number.simple", &i));
must_be_true(i == 1); must_be_true(i == 1);
must_pass(git_config_get_long(cfg, "number.k", &i)); must_pass(git_config_get_int64(cfg, "number.k", &i));
must_be_true(i == 1 * 1024); must_be_true(i == 1 * 1024);
must_pass(git_config_get_long(cfg, "number.kk", &i)); must_pass(git_config_get_int64(cfg, "number.kk", &i));
must_be_true(i == 1 * 1024); must_be_true(i == 1 * 1024);
must_pass(git_config_get_long(cfg, "number.m", &i)); must_pass(git_config_get_int64(cfg, "number.m", &i));
must_be_true(i == 1 * 1024 * 1024); must_be_true(i == 1 * 1024 * 1024);
must_pass(git_config_get_long(cfg, "number.mm", &i)); must_pass(git_config_get_int64(cfg, "number.mm", &i));
must_be_true(i == 1 * 1024 * 1024); must_be_true(i == 1 * 1024 * 1024);
must_pass(git_config_get_long(cfg, "number.g", &i)); must_pass(git_config_get_int64(cfg, "number.g", &i));
must_be_true(i == 1 * 1024 * 1024 * 1024); must_be_true(i == 1 * 1024 * 1024 * 1024);
must_pass(git_config_get_long(cfg, "number.gg", &i)); must_pass(git_config_get_int64(cfg, "number.gg", &i));
must_be_true(i == 1 * 1024 * 1024 * 1024); must_be_true(i == 1 * 1024 * 1024 * 1024);
git_config_free(cfg); git_config_free(cfg);
...@@ -195,37 +195,37 @@ END_TEST ...@@ -195,37 +195,37 @@ END_TEST
BEGIN_TEST(config9, "replace a value") BEGIN_TEST(config9, "replace a value")
git_config *cfg; git_config *cfg;
int i; int i;
long long l, expected = +9223372036854775803; int64_t l, expected = +9223372036854775803;
/* By freeing the config, we make sure we flush the values */ /* By freeing the config, we make sure we flush the values */
must_pass(git_config_open_ondisk(&cfg, CONFIG_BASE "/config9")); must_pass(git_config_open_ondisk(&cfg, CONFIG_BASE "/config9"));
must_pass(git_config_set_int(cfg, "core.dummy", 5)); must_pass(git_config_set_int32(cfg, "core.dummy", 5));
git_config_free(cfg); git_config_free(cfg);
must_pass(git_config_open_ondisk(&cfg, CONFIG_BASE "/config9")); must_pass(git_config_open_ondisk(&cfg, CONFIG_BASE "/config9"));
must_pass(git_config_get_int(cfg, "core.dummy", &i)); must_pass(git_config_get_int32(cfg, "core.dummy", &i));
must_be_true(i == 5); must_be_true(i == 5);
git_config_free(cfg); git_config_free(cfg);
must_pass(git_config_open_ondisk(&cfg, CONFIG_BASE "/config9")); must_pass(git_config_open_ondisk(&cfg, CONFIG_BASE "/config9"));
must_pass(git_config_set_int(cfg, "core.dummy", 1)); must_pass(git_config_set_int32(cfg, "core.dummy", 1));
git_config_free(cfg); git_config_free(cfg);
must_pass(git_config_open_ondisk(&cfg, CONFIG_BASE "/config9")); must_pass(git_config_open_ondisk(&cfg, CONFIG_BASE "/config9"));
must_pass(git_config_set_long(cfg, "core.verylong", expected)); must_pass(git_config_set_int64(cfg, "core.verylong", expected));
git_config_free(cfg); git_config_free(cfg);
must_pass(git_config_open_ondisk(&cfg, CONFIG_BASE "/config9")); must_pass(git_config_open_ondisk(&cfg, CONFIG_BASE "/config9"));
must_pass(git_config_get_long(cfg, "core.verylong", &l)); must_pass(git_config_get_int64(cfg, "core.verylong", &l));
must_be_true(l == expected); must_be_true(l == expected);
git_config_free(cfg); git_config_free(cfg);
must_pass(git_config_open_ondisk(&cfg, CONFIG_BASE "/config9")); must_pass(git_config_open_ondisk(&cfg, CONFIG_BASE "/config9"));
must_fail(git_config_get_int(cfg, "core.verylong", &i)); must_fail(git_config_get_int32(cfg, "core.verylong", &i));
git_config_free(cfg); git_config_free(cfg);
must_pass(git_config_open_ondisk(&cfg, CONFIG_BASE "/config9")); must_pass(git_config_open_ondisk(&cfg, CONFIG_BASE "/config9"));
must_pass(git_config_set_long(cfg, "core.verylong", 1)); must_pass(git_config_set_int64(cfg, "core.verylong", 1));
git_config_free(cfg); git_config_free(cfg);
END_TEST END_TEST
...@@ -233,11 +233,11 @@ END_TEST ...@@ -233,11 +233,11 @@ END_TEST
BEGIN_TEST(config10, "a repo's config overrides the global config") BEGIN_TEST(config10, "a repo's config overrides the global config")
git_repository *repo; git_repository *repo;
git_config *cfg; git_config *cfg;
int version; int32_t version;
must_pass(git_repository_open(&repo, REPOSITORY_FOLDER)); must_pass(git_repository_open(&repo, REPOSITORY_FOLDER));
must_pass(git_repository_config(&cfg, repo, GLOBAL_CONFIG, NULL)); must_pass(git_repository_config(&cfg, repo, GLOBAL_CONFIG, NULL));
must_pass(git_config_get_int(cfg, "core.repositoryformatversion", &version)); must_pass(git_config_get_int32(cfg, "core.repositoryformatversion", &version));
must_be_true(version == 0); must_be_true(version == 0);
git_config_free(cfg); git_config_free(cfg);
git_repository_free(repo); git_repository_free(repo);
...@@ -246,11 +246,11 @@ END_TEST ...@@ -246,11 +246,11 @@ END_TEST
BEGIN_TEST(config11, "fall back to the global config") BEGIN_TEST(config11, "fall back to the global config")
git_repository *repo; git_repository *repo;
git_config *cfg; git_config *cfg;
int num; int32_t num;
must_pass(git_repository_open(&repo, REPOSITORY_FOLDER)); must_pass(git_repository_open(&repo, REPOSITORY_FOLDER));
must_pass(git_repository_config(&cfg, repo, GLOBAL_CONFIG, NULL)); must_pass(git_repository_config(&cfg, repo, GLOBAL_CONFIG, NULL));
must_pass(git_config_get_int(cfg, "core.something", &num)); must_pass(git_config_get_int32(cfg, "core.something", &num));
must_be_true(num == 2); must_be_true(num == 2);
git_config_free(cfg); git_config_free(cfg);
git_repository_free(repo); git_repository_free(repo);
...@@ -258,11 +258,11 @@ END_TEST ...@@ -258,11 +258,11 @@ END_TEST
BEGIN_TEST(config12, "delete a value") BEGIN_TEST(config12, "delete a value")
git_config *cfg; git_config *cfg;
int i; int32_t i;
/* By freeing the config, we make sure we flush the values */ /* By freeing the config, we make sure we flush the values */
must_pass(git_config_open_ondisk(&cfg, CONFIG_BASE "/config9")); must_pass(git_config_open_ondisk(&cfg, CONFIG_BASE "/config9"));
must_pass(git_config_set_int(cfg, "core.dummy", 5)); must_pass(git_config_set_int32(cfg, "core.dummy", 5));
git_config_free(cfg); git_config_free(cfg);
must_pass(git_config_open_ondisk(&cfg, CONFIG_BASE "/config9")); must_pass(git_config_open_ondisk(&cfg, CONFIG_BASE "/config9"));
...@@ -270,8 +270,8 @@ BEGIN_TEST(config12, "delete a value") ...@@ -270,8 +270,8 @@ BEGIN_TEST(config12, "delete a value")
git_config_free(cfg); git_config_free(cfg);
must_pass(git_config_open_ondisk(&cfg, CONFIG_BASE "/config9")); must_pass(git_config_open_ondisk(&cfg, CONFIG_BASE "/config9"));
must_be_true(git_config_get_int(cfg, "core.dummy", &i) == GIT_ENOTFOUND); must_be_true(git_config_get_int32(cfg, "core.dummy", &i) == GIT_ENOTFOUND);
must_pass(git_config_set_int(cfg, "core.dummy", 1)); must_pass(git_config_set_int32(cfg, "core.dummy", 1));
git_config_free(cfg); git_config_free(cfg);
END_TEST END_TEST
...@@ -294,12 +294,12 @@ END_TEST ...@@ -294,12 +294,12 @@ END_TEST
BEGIN_TEST(config15, "add a variable in an existing section") BEGIN_TEST(config15, "add a variable in an existing section")
git_config *cfg; git_config *cfg;
int i; int32_t i;
/* By freeing the config, we make sure we flush the values */ /* By freeing the config, we make sure we flush the values */
must_pass(git_config_open_ondisk(&cfg, CONFIG_BASE "/config10")); must_pass(git_config_open_ondisk(&cfg, CONFIG_BASE "/config10"));
must_pass(git_config_set_int(cfg, "empty.tmp", 5)); must_pass(git_config_set_int32(cfg, "empty.tmp", 5));
must_pass(git_config_get_int(cfg, "empty.tmp", &i)); must_pass(git_config_get_int32(cfg, "empty.tmp", &i));
must_be_true(i == 5); must_be_true(i == 5);
must_pass(git_config_delete(cfg, "empty.tmp")); must_pass(git_config_delete(cfg, "empty.tmp"));
git_config_free(cfg); git_config_free(cfg);
...@@ -307,13 +307,13 @@ END_TEST ...@@ -307,13 +307,13 @@ END_TEST
BEGIN_TEST(config16, "add a variable in a new section") BEGIN_TEST(config16, "add a variable in a new section")
git_config *cfg; git_config *cfg;
int i; int32_t i;
git_filebuf buf; git_filebuf buf;
/* By freeing the config, we make sure we flush the values */ /* By freeing the config, we make sure we flush the values */
must_pass(git_config_open_ondisk(&cfg, CONFIG_BASE "/config10")); must_pass(git_config_open_ondisk(&cfg, CONFIG_BASE "/config10"));
must_pass(git_config_set_int(cfg, "section.tmp", 5)); must_pass(git_config_set_int32(cfg, "section.tmp", 5));
must_pass(git_config_get_int(cfg, "section.tmp", &i)); must_pass(git_config_get_int32(cfg, "section.tmp", &i));
must_be_true(i == 5); must_be_true(i == 5);
must_pass(git_config_delete(cfg, "section.tmp")); must_pass(git_config_delete(cfg, "section.tmp"));
git_config_free(cfg); git_config_free(cfg);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment