/*
 * ImageReaderFactory.cpp
 *
 * (c) 2002 by Thorsten Scheuermann <tscheuermann@gmx.net>
 *
 * 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.
 */

#include "ImageReaderFactory.h"
#include "ImageReader.h"
#include "JPEGReader.h"
#include <qstring.h>
#include <stdio.h>

ImageReader *ImageReaderFactory::getReader(const QString& fileName)
{
  int p;
  QString ext;
  FILE *fp;
  bool accessible = true;

  p = fileName.findRev('.');
  if(p == -1)
  {
    // unrecognized file format
    return NULL;
  }
  ext = fileName.mid(p+1).lower();

  fp = fopen(fileName, "r");
  if(!fp)
  {
    // cannot open file
    accessible = false;
  }
  else
    fclose(fp);

  if((ext == "jpg") || (ext == "jpeg"))
  {
    if(!accessible)
    {
      // cannot open file
      return NULL;
    }
    return new JPEGReader(fileName);
  }
  else
  {
    // unrecognized file format
    return NULL;
  }
  
  return NULL;
}
