/*  $Id: provider_factory.cpp,v 1.7 2003/09/19 10:33:02 mbn Exp $
**
**  ClanLib Game SDK
**  Copyright (C) 2003  The ClanLib Team
**  For a total list of contributers see the file CREDITS.
**
**  This library is free software; you can redistribute it and/or
**  modify it under the terms of the GNU Lesser General Public
**  License as published by the Free Software Foundation; either
**  version 2.1 of the License, or (at your option) any later version.
**
**  This library 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
**  Lesser General Public License for more details.
**
**  You should have received a copy of the GNU Lesser General Public
**  License along with this library; if not, write to the Free Software
**  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
*/

#include "Display/display_precomp.h"
#include "API/Display/Providers/provider_factory.h"
#include "API/Display/Providers/provider_type.h"
#include "API/Core/System/error.h"
#include "API/Core/System/clanstring.h"

/////////////////////////////////////////////////////////////////////////////
// CL_ProviderFactory attributes:

std::map<std::string, CL_ProviderType *> CL_ProviderFactory::types;

/////////////////////////////////////////////////////////////////////////////
// CL_ProviderFactory operations:

CL_PixelBuffer *CL_ProviderFactory::load(
	const std::string &filename,
	const std::string &type,
	CL_InputSourceProvider *input_provider)
{
	if (type != "")
	{
		if (types.find(type) == types.end()) throw CL_Error("Unknown image provider type " + type);

		CL_ProviderType *factory = types[type];
		return factory->load(filename, input_provider);
	}

	// Determine file extension and use it to lookup type.
	std::string ext = CL_String::right(filename, 4);
	if (ext.at(0) == '.') ext = CL_String::right(ext, 3);
	ext = CL_String::to_lower(ext);
	if (types.find(ext) == types.end()) throw CL_Error(std::string("Unknown image provider type ") + ext);

	CL_ProviderType *factory = types[ext];
	return factory->load(filename, input_provider);
}
