=head1 NAME

CFX5::Files::Any - semi-transparent handling of arbitary files

=head1 SYNOPSIS

  use CFX5::Files::Any;

  my $file=CFX5::Files::Any->new($filename);
  my @ccl=$file->extract_ccl();

=head1 DESCRIPTION

The CFX5::Files::Any object provides access to various features of the
different types of files supported by ANSYS CFX on a by-request basis,
rather than having to code the same thing separately for different
file types.

=cut

package CFX5::Files::Any;

use strict;

use CFX5::PkgLoader qw(cfx5_require);

use CFX5::Output qw(:all);

use CFX5::PathManip qw(basename);

=head2 CONSTRUCTORS

=head3 new

  my $file=CFX5::Files::Any->new($filename);

Returns a new object representing the named file.

=cut

# we will override our type with that of a derivative class, so we
# need to package them all, hence this magic comment
#packageall CFX5::Files

sub new {
  my $this=shift;
  my $class=ref($this) || $this;
  my $self={};

  # now, what type of file is this?
  my $file=shift;
  # this may do nothing, if we were called on a sub-class's object
  # ... is that right?
  if ($file =~ m!\.(mdef|mres|def|cfg|res|ini|trn|bak|res\.err)$!i) {
    $class =~ s!::Any!::Def!;
  } elsif (basename($file) =~ m!^(def|cfg|res)$!i) {
    $class =~ s!::Any!::Def!;
  } elsif ($file =~ m!\.(cfx)$!i) {
    # XXX should use a different class
    $class =~ s!::Any!::Def!;
  } elsif ($file =~ m!\.(ccl)$!i) {
    $class =~ s!::Any!::CCLText!;
  } else {
    _error("Unable to determine the type of file", $file);
  }

  cfx5_require($class);

  bless $self, $class;

  $self->init($file, @_);

  $self;
}

sub init {
  my $self=shift;
  my $file=shift;

  unless (defined $file) {
    internal_error("No filename in constructor");
  }

  $self->{__PACKAGE__ . '::file'}=$file;
}

=head2 SIMPLE QUERIES

=head3 file

Returns the filename

=cut

sub file {
  my $self=shift;
  return $self->{__PACKAGE__ . '::file'};
}

=head3 description

Returns a description of the file type, for use in error messages
etc.  This should be overridden by subclasses.

=cut

sub description {
  "ANSYS CFX File";
}

1;
