summaryrefslogtreecommitdiff
path: root/replace.c
blob: dcfd1ed5e10d333c8cf847e781a1b3c08d605382 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/*
 * 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 <http://www.gnu.org/licenses/>.
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

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
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)
{
	/*
	 * 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);
			fprintf(destination, "%s", replacement);
		}
		ch = filebuffer[i];
		fputc(ch, destination);
	}
	fclose(template);
	fclose(destination);
	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;
}