<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;"># Copyright Â© 2007-2010 RaphaÃ«l Hertzog &lt;hertzog@debian.org&gt;
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see &lt;https://www.gnu.org/licenses/&gt;.

package Dpkg::Control::Info;

use strict;
use warnings;

our $VERSION = '1.00';

use Dpkg::Control;
use Dpkg::ErrorHandling;
use Dpkg::Gettext;

use parent qw(Dpkg::Interface::Storable);

use overload
    '@{}' =&gt; sub { return [ $_[0]-&gt;{source}, @{$_[0]-&gt;{packages}} ] };

=encoding utf8

=head1 NAME

Dpkg::Control::Info - parse files like debian/control

=head1 DESCRIPTION

It provides an object to access data of files that follow the same
syntax as F&lt;debian/control&gt;.

=head1 FUNCTIONS

=over 4

=item $c = Dpkg::Control::Info-&gt;new($file)

Create a new Dpkg::Control::Info object for $file. If $file is omitted, it
loads F&lt;debian/control&gt;. If file is "-", it parses the standard input.

=cut

sub new {
    my ($this, $arg) = @_;
    my $class = ref($this) || $this;
    my $self = {
	source =&gt; undef,
	packages =&gt; [],
    };
    bless $self, $class;
    if ($arg) {
	$self-&gt;load($arg);
    } else {
	$self-&gt;load('debian/control');
    }
    return $self;
}

=item $c-&gt;reset()

Resets what got read.

=cut

sub reset {
    my $self = shift;
    $self-&gt;{source} = undef;
    $self-&gt;{packages} = [];
}

=item $c-&gt;load($file)

Load the content of $file. Exits in case of errors. If file is "-", it
loads from the standard input.

=item $c-&gt;parse($fh, $description)

Parse a control file from the given filehandle. Exits in case of errors.
$description is used to describe the filehandle, ideally it's a filename
or a description of where the data comes from. It's used in error
messages.

=cut

sub parse {
    my ($self, $fh, $desc) = @_;
    $self-&gt;reset();
    my $cdata = Dpkg::Control-&gt;new(type =&gt; CTRL_INFO_SRC);
    return if not $cdata-&gt;parse($fh, $desc);
    $self-&gt;{source} = $cdata;
    unless (exists $cdata-&gt;{Source}) {
	$cdata-&gt;parse_error($desc, _g('first block lacks a source field'));
    }
    while (1) {
	$cdata = Dpkg::Control-&gt;new(type =&gt; CTRL_INFO_PKG);
        last if not $cdata-&gt;parse($fh, $desc);
	push @{$self-&gt;{packages}}, $cdata;
	unless (exists $cdata-&gt;{Package}) {
	    $cdata-&gt;parse_error($desc, _g("block lacks the '%s' field"),
	                        'Package');
	}
	unless (exists $cdata-&gt;{Architecture}) {
	    $cdata-&gt;parse_error($desc, _g("block lacks the '%s' field"),
	                        'Architecture');
	}

    }
}

=item $c-&gt;[0]

=item $c-&gt;get_source()

Returns a Dpkg::Control object containing the fields concerning the
source package.

=cut

sub get_source {
    my $self = shift;
    return $self-&gt;{source};
}

=item $c-&gt;get_pkg_by_idx($idx)

Returns a Dpkg::Control object containing the fields concerning the binary
package numbered $idx (starting at 1).

=cut

sub get_pkg_by_idx {
    my ($self, $idx) = @_;
    return $self-&gt;{packages}[--$idx];
}

=item $c-&gt;get_pkg_by_name($name)

Returns a Dpkg::Control object containing the fields concerning the binary
package named $name.

=cut

sub get_pkg_by_name {
    my ($self, $name) = @_;
    foreach my $pkg (@{$self-&gt;{packages}}) {
	return $pkg if ($pkg-&gt;{Package} eq $name);
    }
    return;
}


=item $c-&gt;get_packages()

Returns a list containing the Dpkg::Control objects for all binary packages.

=cut

sub get_packages {
    my $self = shift;
    return @{$self-&gt;{packages}};
}

=item $c-&gt;output($filehandle)

Dump the content into a filehandle.

=cut

sub output {
    my ($self, $fh) = @_;
    my $str;
    $str .= $self-&gt;{source}-&gt;output($fh);
    foreach my $pkg (@{$self-&gt;{packages}}) {
	print { $fh } "\n";
	$str .= "\n" . $pkg-&gt;output($fh);
    }
    return $str;
}

=item "$c"

Return a string representation of the content.

=item @{$c}

Return a list of Dpkg::Control objects, the first one is corresponding to
source information and the following ones are the binary packages
information.

=back

=head1 AUTHOR

RaphaÃ«l Hertzog &lt;hertzog@debian.org&gt;.

=cut

1;
</pre></body></html>