From 23b885cfbfb443616e3dc27bfe5d854687194d50 Mon Sep 17 00:00:00 2001 From: alex Date: Wed, 12 Jan 2022 20:51:09 -0800 Subject: [PATCH] ... --- inc/tree.h | 1 + src/feed/entries.c | 47 +++++++++++++++++++++++----------------------- src/meta.c | 8 ++++++++ src/tree.c | 13 +++++++++++++ 4 files changed, 46 insertions(+), 23 deletions(-) diff --git a/inc/tree.h b/inc/tree.h index f315945..bfdbad6 100644 --- a/inc/tree.h +++ b/inc/tree.h @@ -27,5 +27,6 @@ void tree_entry_free(struct tree_entry*); int tree_entry_init(struct tree_entry**, struct file*); void tree_free(struct tree*); int tree_init(struct tree**); +size_t tree_length(const struct tree*); #endif diff --git a/src/feed/entries.c b/src/feed/entries.c index 62cf751..e5f090c 100644 --- a/src/feed/entries.c +++ b/src/feed/entries.c @@ -1,10 +1,10 @@ #include -static int entry_default(struct rss_entry*,const char*,struct file*); +static int entry_default(struct rss_entry*,const char*,struct tree_entry*); static int entries_sort(const void*,const void*); -static int entry_default(struct rss_entry *entry, const char *feed_url, struct file *file_p) { - entry->title = strdup(file_p->name); +static int entry_default(struct rss_entry *entry, const char *feed_url, struct tree_entry *p) { + entry->title = strdup(p->name); if(NULL==entry->title) { return -1; } entry->pub_date = *localtime(&(time_t){time(NULL)}); @@ -15,6 +15,7 @@ static int entry_default(struct rss_entry *entry, const char *feed_url, struct f int feed_entries(FILE *fp, struct torrent *torrent) { struct tree_entry *p; struct rss_entry **entries, *entry; + char *path; size_t count, index; if(NULL==fp) { return -1; } @@ -30,27 +31,27 @@ int feed_entries(FILE *fp, struct torrent *torrent) { } index = 0; - for(size_t i=0;ifiles.roots->size;i++) { - p = torrent->files.roots->map[i]; - if(p!=NULL) { - if(rss_entry_init(&entry)<0) { goto clean; } - { - int ret; - if((ret = meta_entry(p->path,entry))<0) { - rss_entry_free(entry); - goto clean; - } - - if(ret==0) { - if(entry_default(entry,torrent_p,p)<0) { - rss_entry_free(entry); - goto clean; - } - } - } - entries[index] = entry; - index++; + p = torrent->tree->entries; + entry = NULL; + while(p!=NULL) { + if(rss_entry_init(&entry)<0) { goto clean; } + + if(entry_default(entry,torrent->feed_url,p)<0) { goto clean; } + + path = concat(torrent->root,p->name); + if(NULL==path) { goto clean; } + + if(meta_entry(path,entry)<0) { + free(path); + goto clean; } + free(path); + + entries[index] = entry; + entry = NULL; + index++; + + p = p->next; } qsort(entries,count,sizeof(struct rss_entry*),&entries_sort); diff --git a/src/meta.c b/src/meta.c index 6ae4cdf..77b3468 100644 --- a/src/meta.c +++ b/src/meta.c @@ -20,8 +20,10 @@ int meta_entry(const char *path, struct rss_entry *entry) { while((i = next_line(fp,&key,&value,buf,META_MAX_LINE_SIZE))>0) { if(strcmp(key,RSS_TAG_TITLE)==0) { + if(entry->title!=NULL) { free(entry->title); } entry->title = strndup(value,i); } else if(strcmp(key,RSS_TAG_LINK)==0) { + if(entry->link!=NULL) { free(entry->link); } entry->link = strndup(value,i); } else if(strcmp(key,RSS_TAG_PUBDATE)==0) { if(NULL==strptime( @@ -30,8 +32,10 @@ int meta_entry(const char *path, struct rss_entry *entry) { &(entry->pub_date) /* struct tm *tm */ )) { return -1; } } else if(strcmp(key,RSS_TAG_DESCRIPTION)==0) { + if(entry->description!=NULL) { free(entry->description); } entry->description = strndup(value,i); } else if(strcmp(key,RSS_TAG_GUID)==0) { + if(entry->guid!=NULL) { free(entry->guid); } entry->guid = strndup(value,i); } else { goto panic; } } @@ -112,12 +116,16 @@ int meta_info(const char *path, struct rss_channel_info *info) { while((i = next_line(fp,&key,&value,buf,META_MAX_LINE_SIZE))>0) { if(strcmp(key,RSS_TAG_TITLE)==0) { + if(info->title!=NULL) { free(info->title); } info->title = strndup(value,i); } else if(strcmp(key,RSS_TAG_LINK)==0) { + if(info->link!=NULL) { free(info->link); } info->link = strndup(value,i); } else if(strcmp(key,RSS_TAG_DESCRIPTION)==0) { + if(info->description!=NULL) { free(info->description); } info->description = strndup(value,i); } else if(strcmp(key,RSS_TAG_LANGUAGE)==0) { + if(info->language!=NULL) { free(info->language); } info->language = strndup(value,i); } else if(strcmp(key,RSS_TAG_LASTBUILDDATE)==0) { if(NULL==strptime( diff --git a/src/tree.c b/src/tree.c index 0da7b55..be39db5 100644 --- a/src/tree.c +++ b/src/tree.c @@ -271,3 +271,16 @@ int tree_init(struct tree **p) { return 1; } + +size_t tree_length(const struct tree *root) { + struct tree_entry *p; + + size_t i = 0; + p = root->entries; + while(p!=NULL) { + i++; + p = p->next; + } + + return i; +} -- 2.30.2