summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--replace.c46
-rw-r--r--replace.h6
2 files changed, 52 insertions, 0 deletions
diff --git a/replace.c b/replace.c
index 1860d9e..dcfd1ed 100644
--- a/replace.c
+++ b/replace.c
@@ -37,6 +37,14 @@ getfilelength(FILE *fp)
return retval;
}
+void
+replaceappendchar(char *dest, char ch)
+{
+ int len = strlen(dest);
+ dest[len] = ch;
+ dest[len+1] = '\0';
+}
+
int
replaceinfile(char *originalfile, char *destinationfile, char *placeholder, char *replacement)
{
@@ -78,3 +86,41 @@ replaceinfile(char *originalfile, char *destinationfile, char *placeholder, char
free(filebuffer);
return 1;
}
+
+char *
+replaceinmemory(char *src, char *placeholder, char *replacement)
+{
+ /*
+ * replace every placeholder with replacement in source src
+ * return pointer to string, NULL if no placement found
+ * user must free the returned memory
+ */
+ char *dest = NULL;
+ size_t destsize;
+ char *substr = strstr(src, placeholder);
+ if (!substr)
+ return NULL;
+ /* initial dest size if only one placeholder exists */
+ destsize = (strlen(src)-strlen(placeholder))+strlen(replacement+1);
+ dest = malloc(destsize);
+ if (!dest)
+ return NULL;
+ dest[0] = '\0'; /* junk in memory, we need [0] to be null for strlen() later */
+ for (size_t i = 0; i < strlen(src); i++)
+ {
+ if (i == (substr-src))
+ {
+ i += strlen(placeholder);
+ substr++;
+ substr = strstr(substr, placeholder);
+ if (substr)
+ {
+ destsize = (destsize-strlen(placeholder))+strlen(replacement)+1;
+ dest = realloc(dest, destsize);
+ }
+ strcat(dest, replacement);
+ }
+ replaceappendchar(dest, src[i]);
+ }
+ return dest;
+}
diff --git a/replace.h b/replace.h
index 1b1d4a7..8336660 100644
--- a/replace.h
+++ b/replace.h
@@ -19,7 +19,13 @@
size_t
getfilelength(FILE *fp);
+void
+replaceappendchar(char *dest, char ch);
+
int
replaceinfile(char *originalfile, char *destinationfile, char *placeholder, char *replacement);
+char *
+replaceinmemory(char *src, char *placeholder, char *replacement);
+
#endif