/* ParaView Plugin Reader for demonstration purposes, 
   reading simply a time step from an ASCII file named *.foo */

#include "vtkFooReader.h"
#include "vtkInformation.h"
#include "vtkInformationVector.h"
#include "vtkObjectFactory.h"
#include "vtkUnstructuredGrid.h"

vtkCxxRevisionMacro(vtkFooReader, "$Revision: 0.1 $");
vtkStandardNewMacro(vtkFooReader);


//----------------------------------------------------------------------------
vtkFooReader::vtkFooReader()
{
  this->FileName = 0;

  this->SetNumberOfInputPorts(0);
  this->SetNumberOfOutputPorts(1);
}


//----------------------------------------------------------------------------
vtkFooReader::~vtkFooReader()
{
  this->SetFileName(0);
}


//----------------------------------------------------------------------------
int vtkFooReader::RequestData(vtkInformation *request, vtkInformationVector **inputVector, vtkInformationVector *outputVector)
{
  vtkInformation *outInfo = outputVector->GetInformationObject(0);
  vtkUnstructuredGrid *output = 
    vtkUnstructuredGrid::SafeDownCast(outInfo->Get(vtkDataObject::DATA_OBJECT()));

  this->UpdateProgress(0.0);

  if (outInfo->Has(vtkStreamingDemandDrivenPipeline::UPDATE_TIME_STEPS()))
    {
    double requestedTimeValue =
      outInfo->Get(vtkStreamingDemandDrivenPipeline::UPDATE_TIME_STEPS())[0];

    std::cout << "RequestData: requested time value: " << requestedTimeValue << endl;

    output->GetInformation()->Set(vtkDataObject::DATA_TIME_STEPS(),
                                  &requestedTimeValue, 1);
    }

  std::cout << "RequestData: Got request for opening file <" << this->FileName << ">..."  << endl;
  this->UpdateProgress(1.0);

  return 1;
}


//----------------------------------------------------------------------------
int vtkFooReader::RequestInformation(vtkInformation *request, vtkInformationVector **inputVector, vtkInformationVector *outputVector)
{
  vtkInformation *outInfo = outputVector->GetInformationObject(0);
  char line[256];
  std::ifstream infile;
  double timeStepValue;

  infile.open(this->FileName);
  infile >> timeStepValue;
  infile.close();

  outInfo->Set(vtkStreamingDemandDrivenPipeline::TIME_STEPS(), 
               &timeStepValue, 1);
  double timeRange[2];
  timeRange[0] = timeStepValue;
  timeRange[1] = timeStepValue;
  outInfo->Set(vtkStreamingDemandDrivenPipeline::TIME_RANGE(), timeRange, 2);

  return 1;
}


//----------------------------------------------------------------------------
void vtkFooReader::PrintSelf(ostream& os, vtkIndent indent)
{
  this->Superclass::PrintSelf(os, indent);
}


//----------------------------------------------------------------------------
int vtkFooReader::CanReadFile(const char *fname)
{
  FILE *INFILE;

  // First make sure the file exists.  This prevents an empty file
  // from being created on older compilers.
  struct stat fs;
  if(stat(fname, &fs) != 0) 
    { 
    return 0; 
    }

  INFILE = fopen(fname, "r");
  if (INFILE == NULL)
    {
    // Cannot open file
    fclose(INFILE);
    return 0;
    }
  fclose(INFILE);

  return 1;
}
