Main Page | Class List | File List | Class Members | File Members

replace_fields.c

Go to the documentation of this file.
00001 /*
00002  *  $Id: replace_fields.c,v 1.9 2006/04/17 22:45:19 ghost666 Exp $
00003  *
00004  *  rssbgr - RSS backgroud reader
00005  *  Copyright (C) 2005, 2006 Piotr 'GhosT' Wydrych
00006  *
00007  *  This program is free software; you can redistribute it and/or modify
00008  *  it under the terms of the GNU General Public License as published by
00009  *  the Free Software Foundation; either version 2 of the License, or
00010  *  (at your option) any later version.
00011  *
00012  *  This program is distributed in the hope that it will be useful,
00013  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  *  GNU General Public License for more details.
00016  *
00017  *  You should have received a copy of the GNU General Public License
00018  *  along with this program; if not, write to the Free Software
00019  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00020  */
00021 
00022 #include "replace_fields.h"
00023 
00024 char *replace_all(char *subject, char *this, char *that)
00025 {
00026  size_t pos_sub = 0, len_sub = strlen(subject);
00027  size_t pos_res = 0;
00028  char *pos_fnd = NULL;
00029  size_t len_this = strlen(this), len_that = strlen(that);
00030  char *result = NULL;
00031 
00032  while ((pos_fnd = strstr(subject+pos_sub, this)) != NULL)
00033  {
00034   if ((result = (char *)realloc(result, pos_res+(size_t)pos_fnd-(size_t)(subject+pos_sub)+len_that+1)) == NULL) return NULL;
00035   strlcpy(result+pos_res, subject+pos_sub, (size_t)pos_fnd-(size_t)(subject+pos_sub)+1);
00036   pos_res += (size_t)pos_fnd-(size_t)(subject+pos_sub);
00037   pos_sub += (size_t)pos_fnd-(size_t)(subject+pos_sub);
00038   strlcpy(result+pos_res, that, len_that+1);
00039   pos_res += len_that;
00040   pos_sub += len_this;
00041  }
00042  if ((result = (char *)realloc(result, pos_res+len_sub-pos_sub+1)) == NULL) return NULL;
00043  strlcpy(result+pos_res, subject+pos_sub, len_sub-pos_sub+1);
00044  return result;
00045 }
00046 
00047 char *replace_fields(char *format, char *channel_link, char *channel_title, char *channel_desc, char *item_link, char *item_title, char *item_desc)
00048 {
00049  char *result_old, *result_new;
00050 
00051  if (channel_link == NULL) channel_link = "";
00052  if (channel_title == NULL) channel_title = "";
00053  if (channel_desc == NULL) channel_desc = "";
00054 
00055  if (item_link == NULL) item_link = "";
00056  if (item_title == NULL) item_title = "";
00057  if (item_desc == NULL) item_desc = "";
00058 
00059  result_old = (char *)malloc(strlen(format)+1);
00060 
00061  strcpy(result_old, format);
00062  result_new = replace_all(result_old, "{L}", channel_link);
00063  free(result_old);
00064 
00065  result_old = (char *)malloc(strlen(result_new)+1);
00066  strcpy(result_old, result_new);
00067  free(result_new);
00068 
00069  result_new = replace_all(result_old, "{T}", channel_title);
00070  free(result_old);
00071 
00072  result_old = (char *)malloc(strlen(result_new)+1);
00073  strcpy(result_old, result_new);
00074  free(result_new);
00075 
00076  result_new = replace_all(result_old, "{D}", channel_desc);
00077  free(result_old);
00078 
00079  result_old = (char *)malloc(strlen(result_new)+1);
00080  strcpy(result_old, result_new);
00081  free(result_new);
00082 
00083  result_new = replace_all(result_old, "{l}", item_link);
00084  free(result_old);
00085 
00086  result_old = (char *)malloc(strlen(result_new)+1);
00087  strcpy(result_old, result_new);
00088  free(result_new);
00089 
00090  result_new = replace_all(result_old, "{t}", item_title);
00091  free(result_old);
00092 
00093  result_old = (char *)malloc(strlen(result_new)+1);
00094  strcpy(result_old, result_new);
00095  free(result_new);
00096 
00097  result_new = replace_all(result_old, "{d}", item_desc);
00098  free(result_old);
00099 
00100  result_old = (char *)malloc(strlen(result_new)+1);
00101  strcpy(result_old, result_new);
00102  free(result_new);
00103 
00104  result_new = replace_all(result_old, "\\}", "}");
00105  free(result_old);
00106 
00107  result_old = (char *)malloc(strlen(result_new)+1);
00108  strcpy(result_old, result_new);
00109  free(result_new);
00110 
00111  result_new = replace_all(result_old, "\'", "\\\'");
00112  free(result_old);
00113 
00114  result_old = (char *)malloc(strlen(result_new)+1);
00115  strcpy(result_old, result_new);
00116  free(result_new);
00117 
00118  result_new = replace_all(result_old, "\\\\", "\\");
00119  free(result_old);
00120 
00121  return result_new;
00122 }

Generated on Fri May 26 08:40:18 2006 for RSSbgr by doxygen 1.3.6