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
|
/* Cataract - Static web photo gallery generator
* Copyright (C) 2008 Tomas Bzatek <tbzatek@users.sourceforge.net>
*
* 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 2
* of the License, or 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, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include "gallery-utils.h"
/*
* str_replace: replace substring 'search' with a 'replace' string
* - only one occurence of the string is replaced
* - reallocates the original string
*/
void
str_replace (char **dst, const char *search, const char *replace)
{
char *res;
char *tms;
int new_str_len;
/* is 'search' present in 'dst'? */
res = strstr (*dst, search);
if (res == NULL)
return;
new_str_len = strlen (*dst) - strlen (search) + strlen (replace) + 1;
tms = malloc (new_str_len);
memset (tms, 0, new_str_len);
memcpy (tms, *dst, res - *dst);
memcpy ((void*) ((long int) tms + (long int) res - (long int) *dst),
replace,
strlen (replace));
memcpy ((void*) ((long int) tms + (long int) res - (long int) *dst + strlen (replace)),
(void*) ((long int) res + strlen (search)),
strlen (res) - strlen (search));
/* return fixed string */
free (*dst);
*dst = tms;
}
/*
* copy_file: copy file from src to dst
*/
gboolean
copy_file (const char *src, const char *dst)
{
#define BUFFER_SIZE 65536
FILE *fin;
FILE *fout;
void *buffer;
int size_r;
fin = fopen (src, "r");
if (fin == NULL) {
fprintf (stderr, "copy_file: error reading file \"%s\": %s\n", src, strerror (errno));
return FALSE;
}
fout = fopen (dst, "w");
if (fout == NULL) {
fprintf (stderr, "copy_file: error writing to file \"%s\": %s\n", dst, strerror (errno));
fclose (fin);
return FALSE;
}
buffer = malloc (BUFFER_SIZE);
memset (buffer, 0, BUFFER_SIZE);
size_r = BUFFER_SIZE;
while ((! feof (fin)) && (size_r == BUFFER_SIZE)) {
size_r = fread (buffer, 1, BUFFER_SIZE, fin);
fwrite (buffer, 1, size_r, fout);
}
fclose (fout);
fclose (fin);
free (buffer);
return TRUE;
}
/*
* make_string: make string of 'substr' substrings
* - returns newly allocated string
*/
char *
make_string (const char* substr, const int count)
{
int i;
char *s;
s = malloc (strlen (substr) * count + 1);
for (i = 0; i < count; i++)
memcpy (s + (strlen (substr) * i), substr, strlen (substr));
s[strlen (substr) * count] = 0;
return s;
}
|