/* Cataract - Static web photo gallery generator * Copyright (C) 2008 Tomas Bzatek * * 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 #include #include #include #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; }