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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
|
/* 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 <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <utime.h>
#include <stats.h>
#include "gallery-utils.h"
#define STRING_ALLOC_SIZE 4096
/*
* str_replace: replace substring 'search' with a 'replace' string
* - multiple occurrences of the string are replaced
* - reallocates the original string
*/
void
str_replace (gchar **dst, const gchar *search, const gchar *replace)
{
gchar *d;
gchar *src;
gchar *found;
int i;
int alloc_size;
int used;
if (! dst || ! search || strlen (*dst) == 0 || strlen (search) == 0 || strstr (*dst, search) == NULL)
return;
/* let's suppose just one occurrence of replace, realloc if needed */
used = strlen (*dst) + 1;
alloc_size = (((used + strlen (replace)) / STRING_ALLOC_SIZE) + 1) * STRING_ALLOC_SIZE;
d = g_malloc (alloc_size);
i = 0;
src = *dst;
while (strstr (src, search)) {
found = strstr (src, search);
used += strlen (replace) - strlen (search);
if (used > alloc_size) {
alloc_size = ((used / STRING_ALLOC_SIZE) + 1) * STRING_ALLOC_SIZE;
d = g_realloc (d, alloc_size);
}
/* copy the data between search string */
if (found > src) {
memcpy (d + i, src, found - src);
i += found - src;
}
/* copy replace string instead */
if (strlen (replace) > 0) {
memcpy (d + i, replace, strlen (replace));
i += strlen (replace);
}
src = found + strlen (search);
}
/* copy the rest */
if (src) {
memcpy (d + i, src, strlen (src));
i += strlen (src);
}
d[i] = 0x0;
/* return fixed string */
g_free (*dst);
*dst = g_strdup (d);
g_free (d);
}
/*
* copy_file: copy file from src to dst
*/
gboolean
copy_file (const gchar *src, const gchar *dst)
{
#define BUFFER_SIZE 65536
FILE *fin;
FILE *fout;
void *buffer;
int size_r;
struct stat st;
struct utimbuf ut;
size_t written;
fin = fopen (src, "r");
if (fin == NULL) {
log_error ("copy_file: error reading file \"%s\": %s\n", src, strerror (errno));
return FALSE;
}
fout = fopen (dst, "w");
if (fout == NULL) {
log_error ("copy_file: error writing to file \"%s\": %s\n", dst, strerror (errno));
fclose (fin);
return FALSE;
}
buffer = g_malloc0 (BUFFER_SIZE);
size_r = BUFFER_SIZE;
while (! feof (fin) && size_r == BUFFER_SIZE) {
size_r = fread (buffer, 1, BUFFER_SIZE, fin);
written = fwrite (buffer, 1, size_r, fout);
if (written < size_r) {
log_error ("copy_file: error writing to file \"%s\": %s\n", dst, strerror (errno));
break;
}
}
fclose (fout);
fclose (fin);
g_free (buffer);
/* copy timestamps */
memset (&st, 0, sizeof (st));
if (stat (src, &st) == -1) {
log_error ("copy_file: cannot stat source file \"%s\": %s\n", src, strerror (errno));
return TRUE;
}
memset (&ut, 0, sizeof (ut));
ut.actime = st.st_atime;
ut.modtime = st.st_mtime;
if (utime (dst, &ut) == -1)
log_error ("copy_file: cannot set timestamps on target file \"%s\": %s\n", dst, strerror (errno));
return TRUE;
}
/*
* make_string: make string of 'substr' substrings
* - returns newly allocated string
*/
gchar *
make_string (const gchar *substr, int count)
{
int i;
gchar *s;
if (count < 0)
count = 0;
s = g_malloc0 (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;
}
/*
* fix_entities: replace all invalid & entities with &
* - returns newly allocated string
*/
void
fix_entities (gchar **str)
{
gchar *d;
gchar *src;
gchar *found;
gchar *scan;
int i;
int alloc_size;
int used;
if (! *str || strstr (*str, "&") == NULL)
return;
i = 0;
src = *str;
used = strlen (src) + 1;
alloc_size = ((used / STRING_ALLOC_SIZE) + 1) * STRING_ALLOC_SIZE;
d = g_malloc (alloc_size);
while (strstr (src, "&")) {
found = strstr (src, "&");
/* copy the data between search string */
memcpy (d + i, src, found - src + 1);
i += found - src + 1;
/* scroll to next whitespace */
scan = found + 1;
while (scan && (
(*scan >= 0x41 && *scan <= 0x5a) || (*scan >= 0x61 && *scan <= 0x7a) || /* A-Z, a-z */
(*scan >= 0x30 && *scan <= 0x39) || (*scan == 0x23) /* 0-9, # */
))
scan++;
if (scan && (*scan == 0x3b)) {
/* this is semicolon, correctly closed entity */
/* -- ignore */
}
else {
/* replace with & */
used += 4;
if (used > alloc_size) {
alloc_size = ((used / STRING_ALLOC_SIZE) + 1) * STRING_ALLOC_SIZE;
d = g_realloc (d, alloc_size);
}
memcpy (d + i, "amp;", 4);
i += 4;
}
src = found + 1;
}
/* copy the rest */
if (src) {
memcpy (d + i, src, strlen (src));
i += strlen (src);
}
d[i] = 0x0;
/* return fixed string */
g_free (*str);
*str = g_strdup (d);
g_free (d);
}
/*
* remove_tags: remove all occurences of tags beginning with tag_begin and ending with tag_end
* - e.g. remove_tags (&x, "<!--", "-->") will remove all comments
* - returns newly allocated string
*/
void
remove_tags (gchar **str, const gchar *tag_begin, const gchar *tag_end)
{
gchar *src;
gchar *found;
gchar *found2;
gchar *dest;
if (! *str || ! tag_begin || ! tag_end || strlen (*str) == 0 || strlen (tag_begin) == 0 || strlen (tag_end) == 0)
return;
src = *str;
while ((found = strstr (src, tag_begin)) != NULL) {
found2 = strstr (found, tag_end);
if (found2) {
found2 += strlen (tag_end);
dest = g_malloc0 (strlen (src) - (found2 - found) + 1);
memcpy (dest, src, found - src);
memcpy (dest + (found - src), found2, strlen (found2) + 1);
g_free (src);
src = g_strdup (dest);
g_free (dest);
}
else {
/* break in case of malformed tag structure, avoid endless loop */
log_error ("remove_tags: malformed tag structure detected, strange things may happen\n");
break;
}
}
*str = src;
}
/*
* count_dir_levels: returns number of path elements
*/
int
count_dir_levels (const gchar *path)
{
int i;
int level;
if (path == NULL || strlen (path) == 0)
return 0;
level = 1;
for (i = strlen (path) - 1; i > 0; i--) {
if (G_IS_DIR_SEPARATOR (*(path + i)) && i > 0 && i < strlen (path))
level++;
}
return level;
}
/*
* extract_file_ext: returns pointer to filename extension
*/
const gchar *
extract_file_ext (const gchar *filename)
{
g_return_val_if_fail (filename != NULL, NULL);
g_return_val_if_fail (strlen (filename) > 0, NULL);
return strrchr (filename, '.') + 1;
}
/*
* log_error: prints an error and increments stats
*/
/* this function can possibly store error in some list and print them all at the end again */
void
log_error (const gchar *format, ...)
{
va_list args;
va_start (args, format);
vfprintf (stderr, format, args);
va_end (args);
stats_errors_inc ();
}
/*
* needs_update: returns TRUE if the destionation file needs updating, also when missing
*/
gboolean
needs_update (const gchar *source, const gchar *dest)
{
struct stat src_stat;
struct stat dst_stat;
memset (&src_stat, 0, sizeof (src_stat));
memset (&dst_stat, 0, sizeof (dst_stat));
/* if we can't stat the source file, return FALSE to prevent further errors during update */
if (stat (source, &src_stat) == -1) {
log_error ("needs_update: cannot stat source file \"%s\": %s\n", source, strerror (errno));
return FALSE;
}
/* if we can't stat the destination file, we need update anyway */
if (stat (dest, &dst_stat) == -1)
return TRUE;
/* destination file size should not be zero */
if (dst_stat.st_size <= 0)
return TRUE;
return (src_stat.st_mtime > dst_stat.st_mtime);
}
|