Skip to content

Commit

Permalink
Add functions for parsing integers with size suffixes
Browse files Browse the repository at this point in the history
Split out the nnn{k,m,g} parsing code from git_config_int into
git_parse_long, so command-line parameters can enjoy the same
functionality.  Also add get_parse_ulong for unsigned values.

Make git_config_int use git_parse_long, and add get_config_ulong
as well.

Signed-off-by: Brian Downing <bdowning@lavos.net>
Acked-by: Nicolas Pitre <nico@cam.org>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
  • Loading branch information
Brian Downing authored and Junio C Hamano committed Jul 12, 2007
1 parent 11779e7 commit 0b87b6e
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 12 deletions.
3 changes: 3 additions & 0 deletions cache.h
Original file line number Diff line number Diff line change
Expand Up @@ -521,7 +521,10 @@ typedef int (*config_fn_t)(const char *, const char *);
extern int git_default_config(const char *, const char *);
extern int git_config_from_file(config_fn_t fn, const char *);
extern int git_config(config_fn_t fn);
extern int git_parse_long(const char *, long *);
extern int git_parse_ulong(const char *, unsigned long *);
extern int git_config_int(const char *, const char *);
extern unsigned long git_config_ulong(const char *, const char *);
extern int git_config_bool(const char *, const char *);
extern int git_config_set(const char *, const char *);
extern int git_config_set_multivar(const char *, const char *, const char *, int);
Expand Down
58 changes: 46 additions & 12 deletions config.c
Original file line number Diff line number Diff line change
Expand Up @@ -233,21 +233,55 @@ static int git_parse_file(config_fn_t fn)
die("bad config file line %d in %s", config_linenr, config_file_name);
}

int git_config_int(const char *name, const char *value)
static unsigned long get_unit_factor(const char *end)
{
if (!*end)
return 1;
else if (!strcasecmp(end, "k"))
return 1024;
else if (!strcasecmp(end, "m"))
return 1024 * 1024;
else if (!strcasecmp(end, "g"))
return 1024 * 1024 * 1024;
die("unknown unit: '%s'", end);
}

int git_parse_long(const char *value, long *ret)
{
if (value && *value) {
char *end;
long val = strtol(value, &end, 0);
*ret = val * get_unit_factor(end);
return 1;
}
return 0;
}

int git_parse_ulong(const char *value, unsigned long *ret)
{
if (value && *value) {
char *end;
int val = strtol(value, &end, 0);
if (!*end)
return val;
if (!strcasecmp(end, "k"))
return val * 1024;
if (!strcasecmp(end, "m"))
return val * 1024 * 1024;
if (!strcasecmp(end, "g"))
return val * 1024 * 1024 * 1024;
}
die("bad config value for '%s' in %s", name, config_file_name);
unsigned long val = strtoul(value, &end, 0);
*ret = val * get_unit_factor(end);
return 1;
}
return 0;
}

int git_config_int(const char *name, const char *value)
{
long ret;
if (!git_parse_long(value, &ret))
die("bad config value for '%s' in %s", name, config_file_name);
return ret;
}

unsigned long git_config_ulong(const char *name, const char *value)
{
unsigned long ret;
if (!git_parse_ulong(value, &ret))
die("bad config value for '%s' in %s", name, config_file_name);
return ret;
}

int git_config_bool(const char *name, const char *value)
Expand Down

0 comments on commit 0b87b6e

Please sign in to comment.