00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
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 }