Skip to content

Commit

Permalink
implement --reverse option
Browse files Browse the repository at this point in the history
fixes mlj#41
  • Loading branch information
jetmore committed Jan 22, 2023
1 parent 985145a commit 22231c1
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 1 deletion.
3 changes: 3 additions & 0 deletions castget.1.ronn
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ options as arguments.

### Global options

* `-R`, `--reverse`:
Reverse the order in which items in feeds are processed.

* `-r`, `--resume`:
Resume aborted downloads. Make sure not to use this option if the RSS feed uses the same filename for multiple enclosures as this will corrupt existing downloads.

Expand Down
4 changes: 4 additions & 0 deletions src/castget.c
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ int main(int argc, char **argv)
static gboolean first_only = FALSE;
static gboolean resume = FALSE;
static gboolean debug = FALSE;
static gboolean reverse = FALSE;
static gboolean show_progress_bar = FALSE;
static gchar *rcfile = NULL;
static gchar *filter_regex = NULL;
Expand Down Expand Up @@ -103,6 +104,8 @@ int main(int argc, char **argv)
{ "quiet", 'q', 0, G_OPTION_ARG_NONE, &quiet, "only print error messages" },
{ "first-only", '1', 0, G_OPTION_ARG_NONE, &first_only,
"only process the most recent item from each channel" },
{ "reverse", 'R', 0, G_OPTION_ARG_NONE, &reverse,
"process the channel in reverse order" },
{ "filter", 'f', 0, G_OPTION_ARG_STRING, &filter_regex,
"only process items whose enclosure names match a regular expression" },

Expand Down Expand Up @@ -143,6 +146,7 @@ int main(int argc, char **argv)
opts->first_only = first_only;
opts->resume = resume;
opts->debug = debug;
opts->reverse = reverse;
opts->show_progress_bar = show_progress_bar;

if (catchup) {
Expand Down
45 changes: 44 additions & 1 deletion src/channel.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@
#include <sys/stat.h>
#include <sys/types.h>

void _initialize_index(channel_index *index, int num_items, int reverse);
int _next_index(channel_index *index);

static int _enclosure_pattern_match(enclosure_filter *filter,
const enclosure *enclosure);

Expand Down Expand Up @@ -304,8 +307,12 @@ int channel_update(channel *c, void *user_data, channel_callback cb,
if (!f)
return 1;

channel_index index;
_initialize_index(&index, f->num_items, opts->reverse);

/* Check enclosures in RSS file. */
for (i = 0; i < f->num_items; i++)
while (_next_index(&index)) {
i = index.current;
if (f->items[i]->enclosure) {
if (!g_hash_table_lookup_extended(c->downloaded_enclosures,
f->items[i]->enclosure->url, NULL,
Expand Down Expand Up @@ -343,6 +350,7 @@ int channel_update(channel *c, void *user_data, channel_callback cb,
}
}
}
}

if (!opts->no_mark_read) {
/* Update the RSS last fetched time and save the channel file again. */
Expand All @@ -360,6 +368,41 @@ int channel_update(channel *c, void *user_data, channel_callback cb,
return 0;
}

void _initialize_index(channel_index *index, int num_items, int reverse) {
index->ended = 0;
index->reverse = reverse;
if (reverse) {
index->start = num_items -1;
index->stop = 0;
index->current = num_items;
} else {
index->start = 0;
index->stop = num_items - 1;
index->current = -1;
}
}

int _next_index(channel_index *index) {
if (index->ended)
return 0;

if (index->reverse) {
index->current--;
if (index->current < index->stop) {
index->current = -1;
index->ended = 1;
}
} else {
index->current++;
if (index->current > index->stop) {
index->current = -1;
index->ended = 1;
}
}

return index->ended ? 0 : 1;
}

/* Match the (file) name of an enclosure against a regexp. Letters
in the pattern match both upper and lower case letters if
'caseless' is TRUE. Returns TRUE if the pattern matches, FALSE
Expand Down
9 changes: 9 additions & 0 deletions src/channel.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,21 @@ typedef struct _option_info {
int no_download;
int no_mark_read;
int first_only;
int reverse;
int resume;
enclosure_filter *filter;
int debug;
int show_progress_bar;
} option_info;

typedef struct _channel_index {
int start;
int stop;
int reverse;
int current;
int ended;
} channel_index;

typedef void (*channel_callback)(void *user_data, channel_action action,
channel_info *channel_info,
enclosure *enclosure, const char *filename);
Expand Down

0 comments on commit 22231c1

Please sign in to comment.