From 76df4d98d4eb88caf16bdf903978c32feba563ef Mon Sep 17 00:00:00 2001 From: daniel-Jones Date: Sun, 14 Oct 2018 13:23:35 +1030 Subject: first code push --- input.txt | 10 +++++++ main.c | 12 +++++++++ replace.c | 90 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ replace.h | 28 ++++++++++++++++++++ 4 files changed, 140 insertions(+) create mode 100644 input.txt create mode 100644 main.c create mode 100644 replace.c create mode 100644 replace.h diff --git a/input.txt b/input.txt new file mode 100644 index 0000000..06eba69 --- /dev/null +++ b/input.txt @@ -0,0 +1,10 @@ +Normal text file + +%PLACEHOLDER% +%PLACEHOLDER% +%PLACEHOLDER% + +With lines and whatever, %PLACEHOLDER% + +123%PLACEHOLDER%123 +ok %PLACEHOLDER% ok diff --git a/main.c b/main.c new file mode 100644 index 0000000..8efc66c --- /dev/null +++ b/main.c @@ -0,0 +1,12 @@ +#include +#include "replace.h" + +int +main(void) +{ + int ret; + ret = replaceinfile("input.txt", "output.txt", "%PLACEHOLDER%", "replacement string"); + if (ret == 0) + puts("failed"); + return 0; +} diff --git a/replace.c b/replace.c new file mode 100644 index 0000000..d9a0157 --- /dev/null +++ b/replace.c @@ -0,0 +1,90 @@ +/* + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include +#include +#include + +size_t +getfilelength(FILE *fp) +{ + /* + * return the number of bytes in file fp + * value of 0 returned is empty or error + * it doesn't matter for our purpose + */ + size_t retval; + if (!fp) + retval = 0; + else + { + fseek(fp, 0, SEEK_END); + retval = ftell(fp); + fseek(fp, 0, SEEK_SET); + } + return retval; +} + +void +appendchar(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) +{ + /* + * write originalfile into destinationfile replacing every instance + * of placeholder with replacement + */ + + /* open files */ + FILE *template = fopen(originalfile, "r"); + FILE *destination = fopen(destinationfile, "w"); + if (!template) + { + fclose(destination); + return 0; + } + int ch; + char *substr; + size_t filesize = getfilelength(template); + char *filebuffer = malloc(filesize+1); + if (!filebuffer) + return 0; + fread(filebuffer, 1, filesize, template); + substr = strstr(filebuffer, placeholder); + for (size_t i = 0; i < strlen(filebuffer); i++) + { + if (i == (substr-filebuffer)) + { + i += strlen(placeholder); + substr++; + substr = strstr(substr, placeholder); + printf("%s", replacement); + fprintf(destination, "%s", replacement); + } + ch = filebuffer[i]; + putchar(ch); + fputc(ch, destination); + } + fclose(template); + fclose(destination); + free(filebuffer); + return 1; +} diff --git a/replace.h b/replace.h new file mode 100644 index 0000000..8b0089c --- /dev/null +++ b/replace.h @@ -0,0 +1,28 @@ +/* + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#ifndef REPLACE_H +#define REPLACE_H + +size_t +getfilelength(FILE *fp); + +void +appendchar(char *dest, char ch); + +int +replaceinfile(char *originalfile, char *destinationfile, char *placeholder, char *replacement); + +#endif -- cgit v1.2.3