[dahdi-commits] tzafrir: tools/trunk r7018 - /tools/trunk/xpp/perl_modules/Dahdi/Config/Gen.pm
SVN commits to the DAHDI project
dahdi-commits at lists.digium.com
Sun Aug 16 12:19:30 CDT 2009
Author: tzafrir
Date: Sun Aug 16 12:19:26 2009
New Revision: 7018
URL: http://svn.asterisk.org/svn-view/dahdi?view=rev&rev=7018
Log:
xpp: Dahdi::Config::Gen.pm: improvements in preperation for T1-CAS:
- New channo_range() method which returns range strings for any list
of channel numbers (handle sorting, validation, etc.)
- Reimplement bchan_range() as a small wrapper around channo_range().
- Another small wrapper is chan_range() that works on channel objects.
- Also a parse_chan_range() does the reverse (from range string to
a list of channel numbers). This isn't used at the moment.
xpp rev: 7306
Modified:
tools/trunk/xpp/perl_modules/Dahdi/Config/Gen.pm
Modified: tools/trunk/xpp/perl_modules/Dahdi/Config/Gen.pm
URL: http://svn.asterisk.org/svn-view/dahdi/tools/trunk/xpp/perl_modules/Dahdi/Config/Gen.pm?view=diff&rev=7018&r1=7017&r2=7018
==============================================================================
--- tools/trunk/xpp/perl_modules/Dahdi/Config/Gen.pm (original)
+++ tools/trunk/xpp/perl_modules/Dahdi/Config/Gen.pm Sun Aug 16 12:19:26 2009
@@ -55,31 +55,87 @@
return $val =~ /^(1|y|yes)$/i;
}
-# Generate channel range strings from span objects
+sub range_string($$) {
+ my ($start, $end) = @_;
+
+ if($start == $end) {
+ sprintf "%d", $start;
+ } else {
+ sprintf "%d-%d", $start, $end;
+ }
+}
+
+# Generate channel range strings from arrays of chan numbers
+# E.g: "63-77,79-93"
+sub channo_range(@) {
+ my @channos = sort { $a <=> $b } @_;
+ my $first_num = $channos[0];
+ my $range_start = $first_num;
+ my @range;
+ my $prev = undef;
+
+ foreach my $c (@channos) {
+ my $curr = $c;
+ if(!defined($prev)) {
+ # First iteration
+ $prev = $curr;
+ } elsif($curr != $prev + 1) {
+ # New range
+ push(@range, range_string($range_start, $prev));
+ $range_start = $curr;
+ }
+ $prev = $curr;
+ }
+ if($prev >= $first_num) {
+ # Last range
+ push(@range, range_string($range_start, $prev));
+ }
+ return join(',', @range);
+}
+
+# Generate channel range strings from chan objects
+# E.g: "63-77,79-93"
+sub chan_range(@) {
+ my @chans = sort { $a->num <=> $b->num } @_;
+ my @channos = map { $_->num } @chans;
+ channo_range(@channos);
+}
+
+# Generate channel range strings from digital span objects
# E.g: "63-77,79-93"
sub bchan_range($) {
my $span = shift || die;
+ die unless $span->is_digital();
my $first_chan = ($span->chans())[0];
my $first_num = $first_chan->num();
- my $range_start = $first_num;
- my @range;
- my $prev = undef;
-
- die unless $span->is_digital();
- foreach my $c (@{$span->bchan_list()}) {
- my $curr = $c + $first_num;
- if(!defined($prev)) {
- $prev = $curr;
- } elsif($curr != $prev + 1) {
- push(@range, sprintf("%d-%d", $range_start, $prev));
- $range_start = $curr;
+ my $bchan_ref = $span->bchan_list();
+ my @channos = map { $_ + $first_num } @{$bchan_ref};
+ channo_range(@channos);
+}
+
+# Returns a channel numbers array from a channel range string
+sub parse_chan_range($) {
+ my $rangestr = shift;
+ $rangestr =~ s/\s*//g; # Squeeze
+ die "Bad characters in '$rangestr'" if $rangestr =~ /[^\d\s,-]/;
+ my @ranges = split(/,/, $rangestr);
+ my @channos;
+ my $last_end;
+
+ foreach my $range (@ranges) {
+ my ($start, $end) = split(/-/, $range, 2);
+ $end = $start unless defined $end;
+ die "Bad characters in '$start'" if $start =~ /\D/;
+ die "Bad characters in '$end'" if $end =~ /\D/;
+ die "Reversed range $end < $start" if $end < $start;
+ die "Channel number < 1" if $start < 1;
+ die "New range begins below previous $start <= $last_end" if defined($last_end) && $last_end >= $start;
+ for(my $i = $start + 0; $i <= $end; $i++) {
+ push(@channos, $i);
}
- $prev = $curr;
- }
- if($prev >= $first_num) {
- push(@range, sprintf("%d-%d", $range_start, $prev));
- }
- return join(',', @range);
+ $last_end = $end;
+ }
+ return sort { $a <=> $b } @channos;
}
sub new($) {
More information about the dahdi-commits
mailing list