mirror of
https://github.com/git/git
synced 2024-10-30 04:01:21 +00:00
85023577a8
This is a mechanical clean-up of the way *.c files include system header files. (1) sources under compat/, platform sha-1 implementations, and xdelta code are exempt from the following rules; (2) the first #include must be "git-compat-util.h" or one of our own header file that includes it first (e.g. config.h, builtin.h, pkt-line.h); (3) system headers that are included in "git-compat-util.h" need not be included in individual C source files. (4) "git-compat-util.h" does not have to include subsystem specific header files (e.g. expat.h). Signed-off-by: Junio C Hamano <junkio@cox.net>
106 lines
2 KiB
C
106 lines
2 KiB
C
/*
|
|
* Copyright 2006 Jon Loeliger
|
|
*/
|
|
|
|
#include "git-compat-util.h"
|
|
#include "interpolate.h"
|
|
|
|
|
|
void interp_set_entry(struct interp *table, int slot, const char *value)
|
|
{
|
|
char *oldval = table[slot].value;
|
|
char *newval = NULL;
|
|
|
|
if (oldval)
|
|
free(oldval);
|
|
|
|
if (value)
|
|
newval = xstrdup(value);
|
|
|
|
table[slot].value = newval;
|
|
}
|
|
|
|
|
|
void interp_clear_table(struct interp *table, int ninterps)
|
|
{
|
|
int i;
|
|
|
|
for (i = 0; i < ninterps; i++) {
|
|
interp_set_entry(table, i, NULL);
|
|
}
|
|
}
|
|
|
|
|
|
/*
|
|
* Convert a NUL-terminated string in buffer orig
|
|
* into the supplied buffer, result, whose length is reslen,
|
|
* performing substitutions on %-named sub-strings from
|
|
* the table, interps, with ninterps entries.
|
|
*
|
|
* Example interps:
|
|
* {
|
|
* { "%H", "example.org"},
|
|
* { "%port", "123"},
|
|
* { "%%", "%"},
|
|
* }
|
|
*
|
|
* Returns 1 on a successful substitution pass that fits in result,
|
|
* Returns 0 on a failed or overflowing substitution pass.
|
|
*/
|
|
|
|
int interpolate(char *result, int reslen,
|
|
const char *orig,
|
|
const struct interp *interps, int ninterps)
|
|
{
|
|
const char *src = orig;
|
|
char *dest = result;
|
|
int newlen = 0;
|
|
char *name, *value;
|
|
int namelen, valuelen;
|
|
int i;
|
|
char c;
|
|
|
|
memset(result, 0, reslen);
|
|
|
|
while ((c = *src) && newlen < reslen - 1) {
|
|
if (c == '%') {
|
|
/* Try to match an interpolation string. */
|
|
for (i = 0; i < ninterps; i++) {
|
|
name = interps[i].name;
|
|
namelen = strlen(name);
|
|
if (strncmp(src, name, namelen) == 0) {
|
|
break;
|
|
}
|
|
}
|
|
|
|
/* Check for valid interpolation. */
|
|
if (i < ninterps) {
|
|
value = interps[i].value;
|
|
valuelen = strlen(value);
|
|
|
|
if (newlen + valuelen < reslen - 1) {
|
|
/* Substitute. */
|
|
strncpy(dest, value, valuelen);
|
|
newlen += valuelen;
|
|
dest += valuelen;
|
|
src += namelen;
|
|
} else {
|
|
/* Something's not fitting. */
|
|
return 0;
|
|
}
|
|
|
|
} else {
|
|
/* Skip bogus interpolation. */
|
|
*dest++ = *src++;
|
|
newlen++;
|
|
}
|
|
|
|
} else {
|
|
/* Straight copy one non-interpolation character. */
|
|
*dest++ = *src++;
|
|
newlen++;
|
|
}
|
|
}
|
|
|
|
return newlen < reslen - 1;
|
|
}
|