From: Chris R. <chr...@us...> - 2004-01-20 07:11:40
|
Update of /cvsroot/perl-ldap/ldap/lib/Net/LDAP In directory sc8-pr-cvs1:/tmp/cvs-serv21424/lib/Net/LDAP Modified Files: LDIF.pm LDIF.pod Log Message: Add objectclass sorting from Peter Marschall Index: LDIF.pm =================================================================== RCS file: /cvsroot/perl-ldap/ldap/lib/Net/LDAP/LDIF.pm,v retrieving revision 1.20 retrieving revision 1.21 diff -u -d -r1.20 -r1.21 --- LDIF.pm 1 Jan 2004 09:43:19 -0000 1.20 +++ LDIF.pm 20 Jan 2004 07:11:33 -0000 1.21 @@ -52,6 +52,7 @@ $opt{'lowercase'} ||= 0; $opt{'change'} ||= 0; + $opt{'sort'} ||= 0; my $self = { changetype => "modify", @@ -305,10 +306,18 @@ } } +# helper function to compare attribute names (sort objectClass first) +sub _cmpAttrs { + ($a =~ /^objectclass$/io) + ? -1 : (($b =~ /^objectclass$/io) ? 1 : ($a cmp $b)); +} + sub _write_attrs { - my($entry,$wrap,$lower) = @_; + my($entry,$wrap,$lower,$sort) = @_; + my @attributes = $entry->attributes(); my $attr; - foreach $attr ($entry->attributes) { + @attributes = sort _cmpAttrs @attributes if ($sort); + foreach $attr (@attributes) { my $val = $entry->get_value($attr, asref => 1); _write_attr($attr,$val,$wrap,$lower); } @@ -350,6 +359,7 @@ my $change = $self->{change}; my $wrap = int($self->{'wrap'}); my $lower = $self->{'lowercase'}; + my $sort = $self->{'sort'}; local($\,$,); # output field and record separators unless ($self->{'fh'}) { @@ -386,7 +396,7 @@ next; } elsif ($type eq 'add') { - _write_attrs($entry,$wrap,$lower); + _write_attrs($entry,$wrap,$lower,$sort); next; } elsif ($type =~ /modr?dn/o) { @@ -423,7 +433,7 @@ print "version: $self->{version}\n\n" if defined $self->{version}; } _write_dn($entry->dn,$self->{'encode'},$wrap); - _write_attrs($entry,$wrap,$lower); + _write_attrs($entry,$wrap,$lower,$sort); } } Index: LDIF.pod =================================================================== RCS file: /cvsroot/perl-ldap/ldap/lib/Net/LDAP/LDIF.pod,v retrieving revision 1.11 retrieving revision 1.12 diff -u -d -r1.11 -r1.12 --- LDIF.pod 20 Jan 2004 07:06:43 -0000 1.11 +++ LDIF.pod 20 Jan 2004 07:11:33 -0000 1.12 @@ -98,6 +98,11 @@ Convert attribute names to lowercase when writing. +=item sort =E<gt> 1 + +Sort attribute names when writing entries according to the rule: +objectclass first then all other attributes alphabetically sorted + =item version =E<gt> '1' Set the LDIF version to write to the resulting LDIF file. |