aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/relocation.c132
1 files changed, 27 insertions, 105 deletions
diff --git a/src/relocation.c b/src/relocation.c
index a8157fa..a81a45e 100644
--- a/src/relocation.c
+++ b/src/relocation.c
@@ -4,6 +4,21 @@
#include "relocation.h"
#include "str.h"
+/**
+ * Replace all occurrences of `target` with `replacement` in `original`
+ *
+ * ~~~{.c}
+ * char *str = (char *)calloc(100, sizeof(char));
+ * strcpy(str, "This are a test.");
+ * replace_text(str, "are", "is");
+ * // str is: "This is a test."
+ * free(str);
+ * ~~~
+ *
+ * @param original string to modify
+ * @param target string value to replace
+ * @param replacement string value
+ */
void replace_text(char *original, const char *target, const char *replacement) {
char buffer[OHMYCAL_BUFSIZ];
char *tmp = original;
@@ -29,6 +44,17 @@ void replace_text(char *original, const char *target, const char *replacement) {
strcpy(original, buffer);
}
+/**
+ * Replace `target` with `replacement` in `filename`
+ *
+ * ~~~{.c}
+ * file_replace_text("/path/to/file.txt, "are", "is");
+ * ~~~
+ *
+ * @param filename path to file
+ * @param target string value to replace
+ * @param replacement string
+ */
void file_replace_text(const char* filename, const char* target, const char* replacement) {
FILE *fp = fopen(filename, "r");
if (fp == NULL) {
@@ -60,108 +86,4 @@ void file_replace_text(const char* filename, const char* target, const char* rep
// Replace original with modified copy
remove(filename);
rename(tempfilename, filename);
-}
-
-/**
- * Replace all occurrences of `spattern` with `sreplacement` in `data`
- *
- * ~~~{.c}
- * char *str = (char *)calloc(100, sizeof(char));
- * strcpy(str, "This are a test.");
- * replace_line(str, "are", "is");
- * // str is: "This is a test."
- * free(str);
- * ~~~
- *
- * @param data string to modify
- * @param spattern string value to replace
- * @param sreplacement replacement string value
- * @return success=0, error=-1
- */
-ssize_t replace_line(char *data, const char *spattern, const char *sreplacement) {
- if (data == NULL || spattern == NULL || sreplacement == NULL) {
- return -1;
- }
-
- if (strlen(spattern) == 0 || strlen(sreplacement) == 0) {
- return 0;
- }
-
- ssize_t count_replaced = 0;
-
- char *token = NULL;
- char buf[OHMYCAL_BUFSIZ];
- char *bufp = buf;
- char output[OHMYCAL_BUFSIZ];
- memset(output, 0, sizeof(output));
- strcpy(buf, data);
- for (size_t i = 0; (token = strsep(&bufp, "\n")) != NULL; i++) {
- char *match = strstr(token, spattern);
- if (match) {
- strncat(output, token, strlen(token) - strlen(match));
- strcat(output, sreplacement);
- strcat(output, "\n");
- count_replaced++;
- } else {
- strcat(output, token);
- strcat(output, "\n");
- }
- }
-
- strcpy(data, output);
- return count_replaced;
-}
-
-/**
- * Replace all occurrences of `oldstr` in file `path` with `newstr`
- * @param filename file to modify
- * @param oldstr string to replace
- * @param newstr replacement string
- * @return success=0, failure=-1, or value of `ferror()`
- */
-int file_replace_line(char *filename, const char *spattern, const char *sreplacement) {
- char data[OHMYCAL_BUFSIZ];
- char tempfile[PATH_MAX];
- FILE *fp = NULL;
- if ((fp = fopen(filename, "r")) == NULL) {
- perror(filename);
- return -1;
- }
-
- sprintf(tempfile, "%s.replacement", filename);
- FILE *tfp = NULL;
- if ((tfp = fopen(tempfile, "w+")) == NULL) {
- fclose(fp);
- perror(tempfile);
- return -1;
- }
-
- // Zero the data buffer
- memset(data, '\0', OHMYCAL_BUFSIZ);
- while(fgets(data, OHMYCAL_BUFSIZ, fp) != NULL) {
- replace_line(data, spattern, sreplacement);
- fprintf(tfp, "%s", data);
- memset(data, 0, sizeof(data));
- }
- fclose(fp);
- fflush(tfp);
- rewind(tfp);
-
- // Truncate the original file
- if ((fp = fopen(filename, "w+")) == NULL) {
- perror(filename);
- return -1;
- }
- // Zero the data buffer once more
- memset(data, '\0', OHMYCAL_BUFSIZ);
- // Dump the contents of the temporary file into the original file
- while(fgets(data, OHMYCAL_BUFSIZ, tfp) != NULL) {
- fprintf(fp, "%s", data);
- }
- fclose(fp);
- fclose(tfp);
-
- // Remove temporary file
- unlink(tempfile);
- return 0;
-}
+} \ No newline at end of file