From 5fcb8865b75a8054dc32a64f0cded018af91b934 Mon Sep 17 00:00:00 2001 From: Stefan Schantl Date: Sun, 11 Sep 2022 15:52:48 +0200 Subject: [PATCH] Add pure perl get_set_data function(). This function will create an object with the set data of a given set and allows easy access to the single parts by using different methods. Signed-off-by: Stefan Schantl --- lib/IPSet.pm | 140 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 140 insertions(+) diff --git a/lib/IPSet.pm b/lib/IPSet.pm index 649e267..cc13ba0 100644 --- a/lib/IPSet.pm +++ b/lib/IPSet.pm @@ -27,6 +27,146 @@ require XSLoader; XSLoader::load('IPSet', $VERSION); # Preloaded methods go here. +sub get_set_data($$) { + my ($session, $setname) = @_; + + # Hash to store and access the set data. + my %data = (); + + # Array to store and access the set members. + my @members = (); + + # Call XS function to get the set data. + my $data_raw = &get_set_data_raw($session, $setname); + + # Split the data from the members part. + my ($data, $members) = split("Members:", $data_raw); + + # Cut the data part into single pieces for easy parsing. + my @tmp = split("\n", $data); + my $header; + + # Extract and assign values to the data hash. + foreach (@tmp) { + $data{'name'} = $1 if($_ =~ /Name: (.*)/); + $data{'type'} = $1 if($_ =~ /Type: (.*)/); + $data{'revision'} = $1 if ($_ =~ /Revision: (.*)/); + $data{'memsize'} = $1 if ($_ =~ /memory: (.*)/); + $data{'references'} = $1 if ($_ =~ /References: (.*)/); + $data{'entries'} = $1 if ($_ =~ /entries: (.*)/); + + # Grab and store the header. + $header = $1 if ($_ =~ /Header: (.*)/); + } + + # Cut the heade string into single pieces. + @tmp = split(/ /, $header); + + # Add the header values to the data hash. + $data{'family'} = $tmp[1]; + $data{'hashsize'} = $tmp[3]; + $data{'maxelem'} = $tmp[5]; + + # Check if the members part contains data. + if($members) { + # Cut the list of members into sigle ones and + # assign them to the members array. + @tmp = split("\n", $members); + + foreach (@tmp) { + # Remove any remain newlines. + chomp($_); + + # Skip empty elements. + next unless ($_); + + # Add member to the members array. + push(@members, $_); + } + + # Add the members array to the data hash. + $data{'members'} = \@members; + } + + # Create object. + my $self = bless \%data, $setname; +} + +# +# Methods to get single data elements. +# (In alphabetical order) +# + +# Amount of entries a set has. +sub entries { + my $self = shift; + + return $self->{entries}; +} + +# The family of the set. +sub family { + my $self = shift; + + return $self->{family}; +} + +# The configured hashsize. +sub hashsize { + my $self = shift; + + return $self->{hashsize}; +} + +# A list of current members of the set. +# Stored as array. +sub members { + my $self = shift; + + return $self->{members}; +} + +# The amount of maximum elements a set can have. +sub maxelem { + my $self = shift; + + return $self->{maxelem}; +} + +# The size of a set in memory. +sub memsize { + my $self = shift; + + return $self->{memsize}; +} + +# The name of a set. +sub name { + my $self = shift; + + return $self->{name}; +} + +# The amount how often the set is referenced. +sub references { + my $self = shift; + + return $self->{references}; +} + +# The revison number of a given set. +sub revision { + my $self = shift; + + return $self->{revision}; +} + +# The type of the set. +sub type { + my $self = shift; + + return $self->{type}; +} 1; __END__ -- 2.47.2