attr.c: refactoring
This splits out a common routine that parses a single line of attribute file and adds it to the attr_stack. It should not change any behaviour, other than attrs array in the attr_stack structure is now grown with alloc_nr() macro, instead of one by one, which relied on xrealloc() to give enough slack to be efficient enough. Signed-off-by: Junio C Hamano <gitster@pobox.com>
Junio C Hamano committed
Aug 14, 2007 at 01:40 UTC
a44131181a13eb599ed35647709692b1699706eb
1 file changed
+41
-26
attr.c
+41
-26
@@ -257,6 +257,7 @@ static struct attr_stack {
257
struct attr_stack *prev;
258
char *origin;
259
unsigned num_matches;
260
+ unsigned alloc;
261
struct match_attr **attrs;
262
} *attr_stack;
263
@@ -287,6 +288,26 @@ static const char *builtin_attr[] = {
288
NULL,
289
};
290
291
+static void handle_attr_line(struct attr_stack *res,
292
+ const char *line,
293
+ const char *src,
294
+ int lineno,
295
+ int macro_ok)
296
+{
297
+ struct match_attr *a;
298
+
299
+ a = parse_attr_line(line, src, lineno, macro_ok);
300
+ if (!a)
301
+ return;
302
+ if (res->alloc <= res->num_matches) {
303
+ res->alloc = alloc_nr(res->num_matches);
304
+ res->attrs = xrealloc(res->attrs,
305
+ sizeof(struct match_attr *) *
306
+ res->alloc);
307
+ }
308
+ res->attrs[res->num_matches++] = a;
309
+}
310
+
311
static struct attr_stack *read_attr_from_array(const char **list)
312
{
313
struct attr_stack *res;
@@ -294,42 +315,34 @@ static struct attr_stack *read_attr_from_array(const char **list)
315
int lineno = 0;
316
317
res = xcalloc(1, sizeof(*res));
297
- while ((line = *(list++)) != NULL) {
298
- struct match_attr *a;
299
-
300
- a = parse_attr_line(line, "[builtin]", ++lineno, 1);
301
- if (!a)
302
- continue;
303
- res->attrs = xrealloc(res->attrs,
304
- sizeof(struct match_attr *) * (res->num_matches + 1));
305
- res->attrs[res->num_matches++] = a;
306
- }
318
+ while ((line = *(list++)) != NULL)
319
+ handle_attr_line(res, line, "[builtin]", ++lineno, 1);
320
return res;
321
}
322
323
static struct attr_stack *read_attr_from_file(const char *path, int macro_ok)
324
{
312
- FILE *fp;
325
+ FILE *fp = fopen(path, "r");
326
struct attr_stack *res;
327
char buf[2048];
328
int lineno = 0;
329
317
- res = xcalloc(1, sizeof(*res));
318
- fp = fopen(path, "r");
330
if (!fp)
320
- return res;
331
+ return NULL;
332
+ res = xcalloc(1, sizeof(*res));
333
+ while (fgets(buf, sizeof(buf), fp))
334
+ handle_attr_line(res, buf, path, ++lineno, macro_ok);
335
+ fclose(fp);
336
+ return res;
337
+}
338
322
- while (fgets(buf, sizeof(buf), fp)) {
323
- struct match_attr *a;
339
+static struct attr_stack *read_attr(const char *path, int macro_ok)
340
+{
341
+ struct attr_stack *res;
342
325
- a = parse_attr_line(buf, path, ++lineno, macro_ok);
326
- if (!a)
327
- continue;
328
- res->attrs = xrealloc(res->attrs,
329
- sizeof(struct match_attr *) * (res->num_matches + 1));
330
- res->attrs[res->num_matches++] = a;
331
- }
332
- fclose(fp);
343
+ res = read_attr_from_file(path, macro_ok);
344
+ if (!res)
345
+ res = xcalloc(1, sizeof(*res));
346
return res;
347
}
348
@@ -370,13 +383,15 @@ static void bootstrap_attr_stack(void)
383
elem->prev = attr_stack;
384
attr_stack = elem;
385
373
- elem = read_attr_from_file(GITATTRIBUTES_FILE, 1);
386
+ elem = read_attr(GITATTRIBUTES_FILE, 1);
387
elem->origin = strdup("");
388
elem->prev = attr_stack;
389
attr_stack = elem;
390
debug_push(elem);
391
392
elem = read_attr_from_file(git_path(INFOATTRIBUTES_FILE), 1);
393
+ if (!elem)
394
+ elem = xcalloc(1, sizeof(*elem));
395
elem->origin = NULL;
396
elem->prev = attr_stack;
397
attr_stack = elem;
@@ -441,7 +456,7 @@ static void prepare_attr_stack(const char *path, int dirlen)
456
memcpy(pathbuf + dirlen, "/", 2);
457
cp = strchr(pathbuf + len + 1, '/');
458
strcpy(cp + 1, GITATTRIBUTES_FILE);
444
- elem = read_attr_from_file(pathbuf, 0);
459
+ elem = read_attr(pathbuf, 0);
460
*cp = '\0';
461
elem->origin = strdup(pathbuf);
462
elem->prev = attr_stack;