=head1 NAME

CFX5::Files::Partition - Handling of partitioning files

=head1 SYNOPSIS

  use CFX5::Files::Partition;

=head1 DESCRIPTION

This library implements some very basic handling of partition
information files, based on the CFX5::DefFile class.

=cut

package CFX5::Files::Partition;

use strict;

use CFX5::Config qw(%Config);
use CFX5::Output qw(:all);

use base q(CFX5::DefFile);

=head2 CONSTRUCTORS

=head3 new

  my $par=CFX5::Files::Partition->new('file.par');

Returns an object representing the named file, which must exist.

=cut

=head2 QUERIES

=head3 num_parts

  my $partitions=$par->num_parts();

Returns the number of partitions that this file contains, or undef if
it cannot be worked out.

=cut

sub num_parts {
  my $self=shift;

  my @dslist;

  # first check if this is an old par-file and abort if yes
  @dslist=$self->have_dataset(-what => 'S/INFO_PAR',
                              -where => 'EVERY');
  if (@dslist) {
    _abort("The use of old par-files (version 11 or earlier) is not supported.");
  }

  @dslist=$self->have_dataset(-what => 'S/NPAR',
                              -where => 'EVERY');
  unless (@dslist) {
    _trace("No S/NPAR dataset");
    return undef;
  }

  # we always take the first one, there should only ever be one
  if (@dslist > 1) {
    _trace("Multiple S/NPAR datasets");
  }

  my $ds=$self->read_dataset(-what => $dslist[0][0],
                             -where => $dslist[0][1],
                             -when => $dslist[0][2]);

  unless (defined $ds) {
    _trace("Error reading dataset");
    return undef;
  }

  my $np=($ds->data())[0];

  return $np;
}

1;
