diff options
Diffstat (limited to 'gallery-utils.c')
| -rw-r--r-- | gallery-utils.c | 122 |
1 files changed, 122 insertions, 0 deletions
diff --git a/gallery-utils.c b/gallery-utils.c new file mode 100644 index 0000000..6de0224 --- /dev/null +++ b/gallery-utils.c @@ -0,0 +1,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; +} + + |
