From 24fbad1fafb0e7c0cd65afb199a29a8676db5b5e Mon Sep 17 00:00:00 2001 From: Jean-Philippe Orsini Date: Thu, 24 Apr 2014 11:34:40 +0200 Subject: [PATCH] & must be encoded as & --- src/Makefile.am | 2 ++ src/Makefile.in | 10 ++++-- src/main.c | 27 ++++++++++++--- src/pstr.c | 100 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/pstr.h | 35 ++++++++++++++++++++ 5 files changed, 166 insertions(+), 8 deletions(-) create mode 100644 src/pstr.c create mode 100644 src/pstr.h diff --git a/src/Makefile.am b/src/Makefile.am index cfd0803..58ff475 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -20,6 +20,8 @@ prss_SOURCES = main.c \ list.h \ plog.c \ plog.h \ + pstr.c \ + pstr.h \ ptime.c \ ptime.h \ ttrss.c \ diff --git a/src/Makefile.in b/src/Makefile.in index 09633ba..51e0aba 100644 --- a/src/Makefile.in +++ b/src/Makefile.in @@ -93,9 +93,10 @@ CONFIG_CLEAN_VPATH_FILES = am__installdirs = "$(DESTDIR)$(bindir)" "$(DESTDIR)$(man1dir)" PROGRAMS = $(bin_PROGRAMS) am_prss_OBJECTS = main.$(OBJEXT) http.$(OBJEXT) io.$(OBJEXT) \ - list.$(OBJEXT) plog.$(OBJEXT) ptime.$(OBJEXT) ttrss.$(OBJEXT) \ - ttrss_cache.$(OBJEXT) ttrss_model.$(OBJEXT) ttrss_ws.$(OBJEXT) \ - ttrss_wsasync.$(OBJEXT) url.$(OBJEXT) webbrowser.$(OBJEXT) + list.$(OBJEXT) plog.$(OBJEXT) pstr.$(OBJEXT) ptime.$(OBJEXT) \ + ttrss.$(OBJEXT) ttrss_cache.$(OBJEXT) ttrss_model.$(OBJEXT) \ + ttrss_ws.$(OBJEXT) ttrss_wsasync.$(OBJEXT) url.$(OBJEXT) \ + webbrowser.$(OBJEXT) prss_OBJECTS = $(am_prss_OBJECTS) prss_LDADD = $(LDADD) AM_V_P = $(am__v_P_@AM_V@) @@ -374,6 +375,8 @@ prss_SOURCES = main.c \ list.h \ plog.c \ plog.h \ + pstr.c \ + pstr.h \ ptime.c \ ptime.h \ ttrss.c \ @@ -486,6 +489,7 @@ distclean-compile: @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/list.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/main.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/plog.Po@am__quote@ +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/pstr.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/ptime.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/ttrss.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/ttrss_cache.Po@am__quote@ diff --git a/src/main.c b/src/main.c index 20b9b14..b9e6860 100644 --- a/src/main.c +++ b/src/main.c @@ -32,6 +32,7 @@ #include #include "http.h" #include +#include #include "ttrss.h" #include "ttrss_ws.h" #include "webbrowser.h" @@ -282,12 +283,25 @@ static struct headline *get_selected_headline(GtkTreeIter *iter) return NULL; } +static char *headline_to_markup(struct headline *h) +{ + return strrepg(h->title, "&", "&"); +} + static char *headline_get_formated_headline(struct headline *h) { + char *tmp, *res; + + tmp = headline_to_markup(h); + if (h->unread) - return g_strdup_printf("%s", h->title); + res = g_strdup_printf("%s", tmp); else - return strdup(h->title); + res = strdup(tmp); + + free(tmp); + + return res; } static char *headline_get_date(struct headline *h) @@ -367,7 +381,7 @@ int headline_cursor_changed_cbk(GtkTreeView *treeview, gpointer data) GtkTreeIter iter; GtkTreeModel *model; struct headline *headline; - char *str, *date; + char *str, *date, *title; log_debug("headline_cursor_changed_cbk()"); @@ -377,7 +391,9 @@ int headline_cursor_changed_cbk(GtkTreeView *treeview, gpointer data) headline = get_selected_headline(&iter); if (headline) { - gtk_label_set_text(w_headline_title, headline->title); + title = headline_to_markup(headline); + gtk_label_set_markup(w_headline_title, title); + gtk_label_set_text(w_headline_date, ctime(&headline->date)); str = ttrss_get_headline_content(headline); @@ -398,11 +414,12 @@ int headline_cursor_changed_cbk(GtkTreeView *treeview, gpointer data) date = headline_get_date(headline); gtk_list_store_set(GTK_LIST_STORE(model), &iter, - COL_HEADLINE_TITLE, headline->title, + COL_HEADLINE_TITLE, title, COL_HEADLINE_DATE, date, COL_HEADLINE_EPOCH_TIME, headline->date, -1); free(date); + free(title); } else { log_debug("headline not found"); } diff --git a/src/pstr.c b/src/pstr.c new file mode 100644 index 0000000..fb4f937 --- /dev/null +++ b/src/pstr.c @@ -0,0 +1,100 @@ +/* + Copyright (C) 2011-2014 jeanfi@gmail.com + + 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 (at your option) 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 + +char *strrep(char *str, const char *old, const char *new) +{ + char *p, *res; + int pos; + + if (!str) + return NULL; + + if (!*str || !old || !*old || !new || !strcmp(old, new)) + return str; + + p = strstr(str, old); + + if (!p) + return str; + + res = malloc(strlen(str) + (new ? strlen(new) : 0) - strlen(old) + 1); + + pos = p - str; + + strncpy(res, str, pos); + res[pos] = '\0'; + + if (new) + strcat(res + pos, new); + + strcat(res, str + pos + strlen(old)); + + return res; +} + +/* + Derivated from http://stackoverflow.com/questions/779875/what-is-the-function-to-replace-string-in-c +*/ +char *strrepg(char *orig, const char *rep, const char *with) { + char *result; // the return string + char *ins; // the next insert point + char *tmp; // varies + int len_rep; // length of rep + int len_with; // length of with + int len_front; // distance between rep and end of last rep + int count; // number of replacements + + if (!orig) + return NULL; + if (!rep) + rep = ""; + len_rep = strlen(rep); + if (!with) + with = ""; + len_with = strlen(with); + + ins = orig; + for(count = 0; (tmp = strstr(ins, rep)); count++) { + ins = tmp + len_rep; + } + + // first time through the loop, all the variable are set correctly + // from here on, + // tmp points to the end of the result string + // ins points to the next occurrence of rep in orig + // orig points to the remainder of orig after "end of rep" + tmp = result = malloc(strlen(orig) + (len_with - len_rep) * count + 1); + + if (!result) + return NULL; + + while (count--) { + ins = strstr(orig, rep); + len_front = ins - orig; + tmp = strncpy(tmp, orig, len_front) + len_front; + tmp = strcpy(tmp, with) + len_with; + orig += len_front + len_rep; // move to next "end of rep" + } + strcpy(tmp, orig); + return result; +} diff --git a/src/pstr.h b/src/pstr.h new file mode 100644 index 0000000..f54508e --- /dev/null +++ b/src/pstr.h @@ -0,0 +1,35 @@ +/* + Copyright (C) 2011-2014 jeanfi@gmail.com + + 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 (at your option) 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 + */ + +#ifndef _P_STR_H_ +#define _P_STR_H_ + +#define _P_STR_VER 2 + +/* + Replaces the first occurence of the substring 'old' by 'new' in the + string 'str'. + + Returns a new allocated string only if an occurence is found. + */ +char *strrep(char *str, const char *old, const char *new); + +char *strrepg(char *str, const char *old, const char *new); + +#endif -- 2.7.4