]> git.ipfire.org Git - thirdparty/git.git/blame - interpolate.c
commit-tree: lift completely arbitrary limit of 16 parents
[thirdparty/git.git] / interpolate.c
CommitLineData
49ba83fb
JL
1/*
2 * Copyright 2006 Jon Loeliger
3 */
4
eb30aed7 5#include "git-compat-util.h"
49ba83fb
JL
6#include "interpolate.h"
7
8
a2838377 9void interp_set_entry(struct interp *table, int slot, const char *value)
eb30aed7
JL
10{
11 char *oldval = table[slot].value;
a2838377 12 char *newval = NULL;
eb30aed7 13
8e0f7003 14 free(oldval);
eb30aed7
JL
15
16 if (value)
17 newval = xstrdup(value);
18
19 table[slot].value = newval;
20}
21
22
23void interp_clear_table(struct interp *table, int ninterps)
24{
25 int i;
26
27 for (i = 0; i < ninterps; i++) {
28 interp_set_entry(table, i, NULL);
29 }
30}
31
32
49ba83fb
JL
33/*
34 * Convert a NUL-terminated string in buffer orig
35 * into the supplied buffer, result, whose length is reslen,
36 * performing substitutions on %-named sub-strings from
37 * the table, interps, with ninterps entries.
38 *
39 * Example interps:
40 * {
41 * { "%H", "example.org"},
42 * { "%port", "123"},
43 * { "%%", "%"},
44 * }
45 *
4acfd1b7
PH
46 * Returns the length of the substituted string (not including the final \0).
47 * Like with snprintf, if the result is >= reslen, then it overflowed.
49ba83fb
JL
48 */
49
80583c0e 50unsigned long interpolate(char *result, unsigned long reslen,
4dafd7d2
AR
51 const char *orig,
52 const struct interp *interps, int ninterps)
49ba83fb 53{
4dafd7d2 54 const char *src = orig;
49ba83fb 55 char *dest = result;
80583c0e 56 unsigned long newlen = 0;
3a55602e 57 const char *name, *value;
80583c0e 58 unsigned long namelen, valuelen;
49ba83fb
JL
59 int i;
60 char c;
61
80583c0e 62 while ((c = *src)) {
49ba83fb
JL
63 if (c == '%') {
64 /* Try to match an interpolation string. */
65 for (i = 0; i < ninterps; i++) {
66 name = interps[i].name;
67 namelen = strlen(name);
80583c0e 68 if (strncmp(src, name, namelen) == 0)
49ba83fb 69 break;
49ba83fb
JL
70 }
71
72 /* Check for valid interpolation. */
73 if (i < ninterps) {
74 value = interps[i].value;
55246aac
MV
75 if (!value) {
76 src += namelen;
77 continue;
78 }
49ba83fb 79
55246aac 80 valuelen = strlen(value);
4acfd1b7 81 if (newlen + valuelen < reslen) {
49ba83fb 82 /* Substitute. */
4acfd1b7 83 memcpy(dest, value, valuelen);
49ba83fb 84 dest += valuelen;
49ba83fb 85 }
80583c0e
JH
86 newlen += valuelen;
87 src += namelen;
88 continue;
49ba83fb 89 }
49ba83fb 90 }
80583c0e
JH
91 /* Straight copy one non-interpolation character. */
92 if (newlen + 1 < reslen)
93 *dest++ = *src;
94 src++;
95 newlen++;
49ba83fb
JL
96 }
97
4acfd1b7
PH
98 /* XXX: the previous loop always keep room for the ending NUL,
99 we just need to check if there was room for a NUL in the first place */
100 if (reslen > 0)
101 *dest = '\0';
102 return newlen;
49ba83fb 103}