Add functions for parsing integers with size suffixes
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>
Brian Downing committed
Jul 12, 2007 at 08:32 UTC
0b87b6e081e75170ffa40e92aebde1e22ad3161e
2 files changed
+49
-12
cache.h
+3
@@ -521,7 +521,10 @@ typedef int (*config_fn_t)(const char *, const char *);
521
extern int git_default_config(const char *, const char *);
522
extern int git_config_from_file(config_fn_t fn, const char *);
523
extern int git_config(config_fn_t fn);
524
+extern int git_parse_long(const char *, long *);
525
+extern int git_parse_ulong(const char *, unsigned long *);
526
extern int git_config_int(const char *, const char *);
527
+extern unsigned long git_config_ulong(const char *, const char *);
528
extern int git_config_bool(const char *, const char *);
529
extern int git_config_set(const char *, const char *);
530
extern int git_config_set_multivar(const char *, const char *, const char *, int);
config.c
+46
-12
@@ -233,21 +233,55 @@ static int git_parse_file(config_fn_t fn)
233
die("bad config file line %d in %s", config_linenr, config_file_name);
234
}
235
236
-int git_config_int(const char *name, const char *value)
236
+static unsigned long get_unit_factor(const char *end)
237
+{
238
+ if (!*end)
239
+ return 1;
240
+ else if (!strcasecmp(end, "k"))
241
+ return 1024;
242
+ else if (!strcasecmp(end, "m"))
243
+ return 1024 * 1024;
244
+ else if (!strcasecmp(end, "g"))
245
+ return 1024 * 1024 * 1024;
246
+ die("unknown unit: '%s'", end);
247
+}
248
+
249
+int git_parse_long(const char *value, long *ret)
250
+{
251
+ if (value && *value) {
252
+ char *end;
253
+ long val = strtol(value, &end, 0);
254
+ *ret = val * get_unit_factor(end);
255
+ return 1;
256
+ }
257
+ return 0;
258
+}
259
+
260
+int git_parse_ulong(const char *value, unsigned long *ret)
261
{
262
if (value && *value) {
263
char *end;
240
- int val = strtol(value, &end, 0);
241
- if (!*end)
242
- return val;
243
- if (!strcasecmp(end, "k"))
244
- return val * 1024;
245
- if (!strcasecmp(end, "m"))
246
- return val * 1024 * 1024;
247
- if (!strcasecmp(end, "g"))
248
- return val * 1024 * 1024 * 1024;
249
- }
250
- die("bad config value for '%s' in %s", name, config_file_name);
264
+ unsigned long val = strtoul(value, &end, 0);
265
+ *ret = val * get_unit_factor(end);
266
+ return 1;
267
+ }
268
+ return 0;
269
+}
270
+
271
+int git_config_int(const char *name, const char *value)
272
+{
273
+ long ret;
274
+ if (!git_parse_long(value, &ret))
275
+ die("bad config value for '%s' in %s", name, config_file_name);
276
+ return ret;
277
+}
278
+
279
+unsigned long git_config_ulong(const char *name, const char *value)
280
+{
281
+ unsigned long ret;
282
+ if (!git_parse_ulong(value, &ret))
283
+ die("bad config value for '%s' in %s", name, config_file_name);
284
+ return ret;
285
}
286
287
int git_config_bool(const char *name, const char *value)