/***************************************************************************

    supportware
    Copyright (C) 2001-2008 by c.holzheuer
    c.holzheuer@sourceworx.org

    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 <ddmimemap.h>
//#include <ddpathinfo.h>


using namespace std;



/**
	Default constructor	
*/

ddmimemap::ddmimemap()
:	_default(DDDEFAULT_MIME)
{

}


/**
	Copy constructor	
*/

ddmimemap::ddmimemap( const ddmimemap& src )
: ddbaseobject()
{
	*this = src;
}


/**
	Destructor	
*/

ddmimemap::~ddmimemap()
{

}


/**
	Assignment operator. 
*/

ddmimemap& ddmimemap::operator=( const ddmimemap& src )
{	
	if( this == &src )
		return *this;	
	_data = src._data;
	return *this;
}

/**
	Init this structure by loading the "mime.types" file. 
	@param filename filename, defaults to "mime.types"
	@param path the basepathc, defaults to ""
*/

void ddmimemap::init( const ddstring& filename )
{
	
	ddstring text;
	if( text.from_file( filename ) < 0 )
		throw runtime_error( "ddmimemap::init failed!");
	
	ddstringrow lines;
	ddstringrow entry;
	lines.create_lines( text );
	for( size_t i=0; i<lines.size(); ++i )
	{
		entry.create_lazy( lines[i] );
		if( entry.size() > 1 )
			for( size_t x=1; x<entry.size(); ++x )
				_data[ entry[x] ] = entry[0];
	}
}

const ddstring& ddmimemap::get_entry( const ddstring& key )
{
	ddstringmap::iterator pos = _data.find( key.as_lower() );
	if( pos != _data.end() )
		return (*pos).second;
	return _default;
}

bool ddmimemap::empty() const
{
	return _data.empty();
}

void ddmimemap::dump() const
{
	throw runtime_error( "ddmimemap::dump(): not implemented!" );
}

