added files
[bcm963xx.git] / userapps / opensource / net-snmp / perl / OID / OID.pm
1 package NetSNMP::OID;
2
3 use strict;
4 use Carp;
5
6 require Exporter;
7 require DynaLoader;
8 use AutoLoader;
9
10 sub compare($$);
11
12 use overload
13     '<=>' => \&compare,
14     'cmp' => \&oidstrcmp,
15     '""' => \&quote_oid,
16     '+' => \&add,
17 ;
18
19 use SNMP;
20
21 sub quote_oid {
22     return $_[0]->{'oidptr'}->to_string();
23 }
24
25 sub length {
26     return $_[0]->{'oidptr'}->length();
27 }
28
29 sub append {
30     my $this = shift;
31     my $str = shift;
32
33     if (ref($str) eq 'NetSNMP::OID') {
34         return $this->{'oidptr'}->append_oid($str->{'oidptr'});
35     }
36     $str = "." . $str if ($str =~ /^\d+/);
37     if ($str =~ /^[.\d]+/) {
38         # oid segment
39         return $this->{'oidptr'}->append($str);
40     }
41     if ($str =~ /^\"(.*)\"$/) {
42         # string index
43         my $newstr = "." . CORE::length($1);
44         map { $newstr .= ".$_" } unpack("c*",$1);
45         return $this->{'oidptr'}->append($newstr);
46     }
47     if ($str =~ /^\'(.*)\'$/) {
48         # string index, implied
49         my $newstr;
50         map { $newstr .= ".$_" } unpack("c*",$1);
51         return $this->{'oidptr'}->append($newstr);
52     }
53     # Just Parse it...
54     return $this->{'oidptr'}->append($str);
55 }
56
57 sub add {
58     my $this = shift;
59     my $str = shift;
60     my ($newoid, %newhash);
61     $newoid = \%newhash;
62     $newoid->{'oidptr'} = $this->{'oidptr'}->clone();
63     bless($newoid, ref($this));
64     $newoid->append($str);
65     return $newoid;
66 }
67
68 use vars qw(@ISA %EXPORT_TAGS @EXPORT_OK @EXPORT $VERSION $AUTOLOAD);
69
70 @ISA = qw(Exporter DynaLoader);
71
72 # Items to export into callers namespace by default. Note: do not export
73 # names by default without a very good reason. Use EXPORT_OK instead.
74 # Do not simply export all your public functions/methods/constants.
75
76 # This allows declaration       use NetSNMP::OID ':all';
77 # If you do not need this, moving things directly into @EXPORT or @EXPORT_OK
78 # will save memory.
79 %EXPORT_TAGS = ( 'all' => [ qw(
80         snmp_oid_compare
81         compare
82 ) ] );
83
84 @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
85
86 @EXPORT = qw(
87         snmp_oid_compare
88         compare
89 );
90 $VERSION = '0.1';
91
92 sub new {
93     my $type = shift;
94     my $arg = shift;
95     if (!$arg) {
96         $arg = $type;
97         $type = "NetSNMP::OID";
98     }
99     SNMP::init_snmp("perl");
100     my $ptr = NetSNMP::OID::newptr($arg);
101     if ($ptr) {
102       return newwithptr($type, $ptr);
103     }
104 }
105
106 sub newwithptr {
107     my $type = shift;
108     my $ptr = shift;
109     my $self = {};
110     if (!$ptr) {
111         $ptr = $type;
112         $type = "NetSNMP::OID";
113     }
114     SNMP::init_snmp("perl");
115     $self->{'oidptr'} = $ptr;
116     bless($self, $type);
117     return $self;
118 }
119
120 sub snmp_oid_compare($$) {
121     my ($oid1, $oid2) = @_;
122     return _snmp_oid_compare($oid1->{oidptr}, $oid2->{oidptr});
123 }
124
125 sub compare($$) {
126     my ($v1, $v2) = @_;
127     snmp_oid_compare($v1, $v2);
128 }
129
130 sub oidstrcmp {
131     my ($v1, $v2) = @_;
132     $v1->{'oidptr'}->to_string cmp $v2->{'oidptr'}->to_string;
133 }
134
135 sub to_array($) {
136     my $self = shift;
137     return $self->{oidptr}->to_array();
138 }
139
140 sub AUTOLOAD {
141     # This AUTOLOAD is used to 'autoload' constants from the constant()
142     # XS function.  If a constant is not found then control is passed
143     # to the AUTOLOAD in AutoLoader.
144
145     my $constname;
146     ($constname = $AUTOLOAD) =~ s/.*:://;
147     croak "& not defined" if $constname eq 'constant';
148     my $val = constant($constname, @_ ? $_[0] : 0);
149     if ($! != 0) {
150         if ($! =~ /Invalid/ || $!{EINVAL}) {
151             $AutoLoader::AUTOLOAD = $AUTOLOAD;
152             goto &AutoLoader::AUTOLOAD;
153         }
154         else {
155             croak "Your vendor has not defined NetSNMP::OID macro $constname";
156         }
157     }
158     {
159         no strict 'refs';
160         # Fixed between 5.005_53 and 5.005_61
161         if ($] >= 5.00561) {
162             *$AUTOLOAD = sub () { $val };
163         }
164         else {
165             *$AUTOLOAD = sub { $val };
166         }
167     }
168     goto &$AUTOLOAD;
169 }
170
171 bootstrap NetSNMP::OID $VERSION;
172
173 # Preloaded methods go here.
174
175 # Autoload methods go after =cut, and are processed by the autosplit program.
176
177 1;
178 __END__
179 # Below is stub documentation for your module. You better edit it!
180
181 =head1 NAME
182
183 NetSNMP::OID - Perl extension for manipulating OIDs
184
185 =head1 SYNOPSIS
186
187   use NetSNMP::OID;
188
189   my $oid = new NetSNMP::OID('sysContact.0');
190
191   if ($oid < new NetSNMP::OID('ifTable')) {
192       do_something();
193   }
194
195   my @numarray = $oid->to_array();
196
197   # appending oids
198   $oid = new NetSNMP::OID('.1.3');
199   $oid += ".6.1";
200   # -> .1.3.6.1
201
202   # appending index strings
203
204   $oid2 = $oid + "\"wes\"";
205   # -> .1.3.6.1.3.119.101.115
206
207   $oid3 = $oid + "\'wes\'";
208   # -> .1.3.6.1.119.101.115
209
210   $len = $oid3->length();
211   # -> 7
212
213 =head1 DESCRIPTION
214
215 The NetSNMP::OID module is a simple wrapper around a C-based net-snmp
216 oid (which is an array of unsigned integers).  The OID is internally
217 stored as a C array of integers for speed purposes when doing
218 comparisons, etc.
219
220 The standard logical expression operators (<, >, ==, ...) are
221 overloaded such that lexographical comparisons may be done with them.
222
223 The + operator is overloaded to allow you to append stuff on to the
224 end of a OID, like index segments of a table, for example.
225
226 =head2 EXPORT
227
228 int snmp_oid_compare(oid1, oid2)
229 int compare(oid1, oid2)
230
231 =head1 AUTHOR
232
233 Wes Hardaker, E<lt>hardaker@users.sourceforge.netE<gt>
234
235 =head1 SEE ALSO
236
237 L<SNMP>, L<perl>.
238
239 =head1 Copyright
240
241 Copyright (c) 2002 Networks Associates Technology, Inc.  All
242 Rights Reserved.  This program is free software; you can
243 redistribute it and/or modify it under the same terms as Perl
244 itself.
245
246 =cut