#!/usr/bin/perl -w use strict; #----------------------------------------------------------------------------- my %newsrc; my $testing_unmark = ''; my $testing_mark = 'yes'; #----------------------------------------------------------------------------- sub newsrc_unmark { my ($group, $artno) = @_; if (exists $newsrc{':'}->{$group}) { @{$newsrc{':'}->{$group}} = map { my ($lo, $hi) = split(/-/); $hi || ($hi = $lo); if ($artno == $lo) { if ($artno == $hi) { (); } elsif ($artno+1 < $hi) { (($artno+1) . '-' . $hi); } else { ($hi); } } elsif ($artno == $hi) { if ($artno-1 > $lo) { ($lo . '-' . ($artno-1)); } else { ($lo); } } elsif (($artno > $lo) && ($artno < $hi)) { if ($artno-1 == $lo) { if ($artno+1 == $hi) { ($lo, $hi); } else { ($lo, ($artno+1) . '-' . $hi); } } elsif ($artno+1 == $hi) { ($lo . '-' . ($artno-1), $hi); } else { ($lo . '-' . ($artno-1), ($artno+1) . '-' . $hi); } } else { ($_); } } @{$newsrc{':'}->{$group}}; } } #----------------------------------------------------------------------------- sub newsrc_mark { my ($group, $artno) = @_; if (exists $newsrc{':'}->{$group}) { # ensure $artno isn't inside a range my $found = ''; foreach (@{$newsrc{':'}->{$group}}) { my ($lo, $hi) = split(/-/); $hi || ($hi = $lo); if (($artno >= $lo) && ($artno <= $hi)) { $found = 'yes'; last; } } # add artno, and combine ranges unless ($found) { local $^W=0; push(@{$newsrc{':'}->{$group}}, $artno); @{$newsrc{':'}->{$group}} = map { s/^(\d+)-.*?-(\d+)$/$1-$2/; $_; } split(/,/, join('-', map { if (my($lo,$hi) = (/^(\d+),(\d+)$/)) { if ($hi-$lo == 1) { ($lo . '-' . $hi); } else { ($_); } } else { ($_); } } split(/-/, join(',', sort { $a <=> $b } @{$newsrc{':'}->{$group}} ) ) ) ); } } } #----------------------------------------------------------------------------- sub setup_unmark_test { $newsrc{':'}->{'test'} = [ qw(1011-1015 1111-1113 1211-1212) ]; } if ($testing_unmark) { my @unmarks = qw(1011 1012 1013 1014 1015 1111 1112 1113 1211 1212); my $unmark; foreach $unmark (@unmarks) { print "------- unmarking $unmark\n"; &fix_test(); print "before: ", join(',', @{$newsrc{':'}->{'test'}}), "\n"; &newsrc_unmark('test', $unmark); print "after : ", join(',', @{$newsrc{':'}->{'test'}}), "\n"; } } sub setup_mark_test { $newsrc{':'}->{'test'} = [ ]; } if ($testing_mark) { &setup_mark_test(); my @marks = qw(1 2 3 4 5 6 7 8 10 12 14 16 18 11 13 15 17 100 99 98 101 102); my $mark; foreach $mark (@marks) { &newsrc_mark('test', $mark); print "marked $mark\t", join(',', @{$newsrc{':'}->{'test'}}), "\n"; } }