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

    supportware
    Copyright (C) 2001-2007 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 <ddwebredirectmap.h>


void ddwebaliasmap::from_string( const ddstring& source )
{
	ddstring src = source;

	ddstringrow row( src, "\n" );
	for( size_t i=0; i < row.size(); ++i )
	{
		if( !row[i].empty() )
		{
			ddstring newpath, oldpath;
			row[i].split( "|", newpath, oldpath );
			add_entry( newpath, oldpath );
 		}
	}
}


ddstring ddwebaliasmap::to_string() const
{
	ddstring result;
	ddstringmap::const_iterator pos = _new2old.begin();
	for( ; pos != _new2old.end(); ++pos )
  	result += (*pos).first + "|" + (*pos).second + "\n"; 
	return result; 
}


void ddwebaliasmap::add_entry( const ddstring& anewpath, const ddstring& aoldpath )
{
	
	if( !anewpath.empty() && !aoldpath.empty() )
	{		
			// hack: escapes wieder zurück
		ddstring oldpath = aoldpath;
		ddstring newpath = anewpath;
		oldpath.replace_all( "&amp;", "&" );	
		newpath.replace_all( "&amp;", "&" );	
		
		_old2new[ oldpath ] = newpath;
		_new2old[ newpath ] = oldpath;
	}
}


void ddwebaliasmap::drop_entry( const ddstring& newpath )
{
	_old2new.erase( _new2old[ newpath ] );
	_new2old.erase( newpath );
	
}



/**
	einen Request intern umbiegen: /das_leben_ist_toll.html
	nach /faqsthread/xyz
	
	Neu: es sollen auch Pfade erkannt werden:
	
	/moo/blub/alles.html
*/

bool ddwebaliasmap::prepare_request_redirect( ddwebrequest& request )
{
	ddstringrow& splitpath = request.get_split_path();
	
	if( splitpath.size() < 1 )
		return false;
	
	ddstring fpath = request.get_full_path();
	// '/' am ende wegmachen
	size_t idx = fpath.size() - 1;
	if( idx>0 && fpath[idx] == '/' )
		fpath.resize( idx );
	bool isalias = _new2old.contains_key( fpath );
	// es ist ein alias: /das_leben_ist_toll.html;
	if( isalias )
	{
		ddstring path = _new2old[ fpath ];
		if( path == "/" )
			path = "/index.html";
		ddstringrow tmp( path, "/" );
		
		splitpath.clear();
		for( size_t i = 1; i < tmp.size(); ++i )
			splitpath.add( tmp[i] );
			
		//get nicht wg. splitpath.size = 2, tmp.size=3
		//splitpath.swap( tmp );
	
	
	}
	
	return isalias;

}


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

    supportware
    Copyright (C) 2001-2007 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.

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


void ddwebredirectmap::from_string( const ddstring& source )
{
	ddstring src = source;
	// hack: escapes wieder zurück
	src.replace_all( "&amp;", "&" );	
	ddstringrow row( src, "\n" );
	for( size_t i=0; i < row.size(); ++i )
	{
		if( !row[i].empty() )
		{
			ddstring oldpath, newpath;
			row[i].split( "|", oldpath, newpath );
			(*this)[oldpath] = newpath;
		}
	}
	
}

ddstring ddwebredirectmap::to_string() const
{
	ddstring result;
	ddstringmap::const_iterator pos = begin();
	for( ; pos != end(); ++pos )
  	result += (*pos).first + "|" + (*pos).second + "\n"; 
	return result; 
	
}

