aboutsummaryrefslogtreecommitdiff
path: root/interpolate.c
diff options
context:
space:
mode:
authorJon Loeliger <jdl@jdl.com>2006-09-19 20:31:51 -0500
committerJunio C Hamano <junkio@cox.net>2006-09-20 11:15:39 -0700
commit49ba83fb67d9e447b86953965ce5f949c6a93b81 (patch)
tree94b3f1bd68329ca9c7d61b572c4283aafc04f2aa /interpolate.c
parent62e27f273d66afa996cb7aee6cdb25fbedc053f6 (diff)
downloadgit-49ba83fb67d9e447b86953965ce5f949c6a93b81.tar.gz
git-49ba83fb67d9e447b86953965ce5f949c6a93b81.tar.xz
Add virtualization support to git-daemon
Signed-off-by: Jon Loeliger Signed-off-by: Junio C Hamano <junkio@cox.net>
Diffstat (limited to 'interpolate.c')
-rw-r--r--interpolate.c82
1 files changed, 82 insertions, 0 deletions
diff --git a/interpolate.c b/interpolate.c
new file mode 100644
index 000000000..d82f1b51b
--- /dev/null
+++ b/interpolate.c
@@ -0,0 +1,82 @@
+/*
+ * Copyright 2006 Jon Loeliger
+ */
+
+#include <string.h>
+
+#include "interpolate.h"
+
+
+/*
+ * 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,
+ char *orig,
+ struct interp *interps, int ninterps)
+{
+ 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;
+}