]> mj.ucw.cz Git - pciids.git/blob - PciIds/Address/Pci.pm
33e4afc0e210120e5c0112d11be4d32a05864c2e
[pciids.git] / PciIds / Address / Pci.pm
1 package PciIds::Address::Pci;
2 use strict;
3 use warnings;
4 use PciIds::Address::Toplevel;
5 use base 'PciIds::Address::Base';
6
7 sub new( $ ) {
8         my( $address ) = @_;
9         return PciIds::Address::Toplevel::new( $address ) if( $address =~ /^PC\/?$/ );
10         return bless PciIds::Address::Base::new( $address );
11 }
12
13 sub pretty( $ ) {
14         my $self = shift;
15         $_ = $self->get();
16         s/^PC\/?//;
17         s/\//:/g;
18         s/([0-9a-f]{4})([0-9a-f]{4})/$1 $2/g;
19         my $prefix = '';
20         if( /:.*:/ ) {
21                 $prefix = 'Subsystem';
22         } elsif( /:/ ) {
23                 $prefix = 'Device';
24         } else {
25                 $prefix = 'Vendor';
26         }
27         return $prefix.' '. $_;
28 }
29
30 sub tail( $ ) {
31         my( $new ) = ( shift->get() );
32         $new =~ s/.*\/(.)/$1/;
33         $new =~ s/([0-9a-f]{4})([0-9a-f]{4})/$1 $2/g;
34         return $new;
35 }
36
37 sub restrictRex( $$ ) {
38         my( $self, $restrict ) = @_;
39         my( $result ) = ( $restrict =~ /^([a-f0-9]{1,4})/ );#TODO every time?
40         return $result;
41 }
42
43 sub leaf( $ ) {
44         return ( shift->get() =~ /\/.*\/.*\// );
45 }
46
47 sub append( $$ ) {
48         my( $self, $suffix ) = @_;
49         return ( undef, 'You can not add to leaf node' ) if( $self->leaf() );
50         $suffix =~ s/ //g;
51         return ( undef, "Invalid ID syntax" ) unless ( ( ( $self->get() !~ /^PC\/.*\// ) && ( $suffix =~ /^[0-9a-f]{4}$/ ) ) || ( ( $self->get() =~ /^PC\/.*\// ) && ( $suffix =~ /^[0-9a-f]{8}$/ ) ) );
52         return ( PciIds::Address::new( $self->{'value'} . ( ( $self->{'value'} =~ /\/$/ ) ? '' : '/' ) . $suffix ), undef );
53 }
54
55 sub path( $ ) {
56         my( $self ) = @_;
57         my $result = PciIds::Address::Base::path( $self );
58         my( $vid ) = ( $self->get() =~ /^PC\/[0-9a-f]{4}\/[0-9a-f]{4}\/([0-9a-f]{4})/ );
59         splice @{$result}, 2, 0, PciIds::Address::new( "PC/$vid" ) if( defined $vid ) && ( $result->[1]->get() ne "PC/$vid" );
60         return $result;
61 }
62
63 1;