2 * Copyright 2006 Jon Loeliger
5 #include "git-compat-util.h"
6 #include "interpolate.h"
9 void interp_set_entry(struct interp *table, int slot, const char *value)
11 char *oldval = table[slot].value;
18 newval = xstrdup(value);
20 table[slot].value = newval;
24 void interp_clear_table(struct interp *table, int ninterps)
28 for (i = 0; i < ninterps; i++) {
29 interp_set_entry(table, i, NULL);
35 * Convert a NUL-terminated string in buffer orig
36 * into the supplied buffer, result, whose length is reslen,
37 * performing substitutions on %-named sub-strings from
38 * the table, interps, with ninterps entries.
42 * { "%H", "example.org"},
47 * Returns 0 on a successful substitution pass that fits in result,
48 * Returns a number of bytes needed to hold the full substituted
52 unsigned long interpolate(char *result, unsigned long reslen,
54 const struct interp *interps, int ninterps)
56 const char *src = orig;
58 unsigned long newlen = 0;
59 const char *name, *value;
60 unsigned long namelen, valuelen;
64 memset(result, 0, reslen);
68 /* Try to match an interpolation string. */
69 for (i = 0; i < ninterps; i++) {
70 name = interps[i].name;
71 namelen = strlen(name);
72 if (strncmp(src, name, namelen) == 0)
76 /* Check for valid interpolation. */
78 value = interps[i].value;
79 valuelen = strlen(value);
81 if (newlen + valuelen + 1 < reslen) {
83 strncpy(dest, value, valuelen);
91 /* Straight copy one non-interpolation character. */
92 if (newlen + 1 < reslen)
98 if (newlen + 1 < reslen)