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

    libdope
    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 <ddstringset.h>
#include <ddbasetime.h>


ddstringset::ddstringset()
{

}

ddstringset::~ddstringset()
{

}

ddstring ddstringset::to_string() const
{
	ddstring result;
	set<ddstring>::const_iterator pos = _blockset.begin();
	for( ; pos != _blockset.end(); ++pos )
  	result += (*pos) + "\n";
	return result;
}


void ddstringset::from_string( const ddstring& src, bool usecase )
{
	ddstring source = src;
	source.replace_all( "\r","" );
	ddstringrow row( source, "\n" );
	for( size_t i=0; i < row.size(); ++i )
		add_entry( usecase ? row[i] : row[i].to_lower() );
}


bool ddstringset::contains_key( const ddstring& key )
{
	ddthreadautomutex tmp( _blockmutex );
	return ( _blockset.count( key ) != 0 );
}


void ddstringset::add_entry( const ddstring& key )
{
	ddthreadautomutex tmp( _blockmutex );			
	_blockset.insert( key );
}


void ddstringset::drop_entry( const ddstring& key )
{
	ddthreadautomutex tmp( _blockmutex );
	_blockset.erase( key );
}


void ddstringset::to_row( ddstringrow& row )
{
	ddthreadautomutex tmp( _blockmutex );
	set<ddstring>::const_iterator pos = _blockset.begin();
	for( ; pos != _blockset.end(); ++pos )
  	row.add( (*pos) );
}


bool ddstringset::find_entry_in( const ddstring& text )
{

	if( text.empty() )
		return false;
	
	ddthreadautomutex tmp( _blockmutex );
	
	// wir holen uns eine liste aus bösen worten
	// und schauen, ob eines davon im posting enthalten
	// ist.
	
	ddstring lowtxt = text.as_lower();	
	set<ddstring>::const_iterator pos = _blockset.begin();
	for( ; pos != _blockset.end(); ++pos )
	{
  	if( lowtxt.contains( (*pos).as_lower() ) )
  		return true;
	}
	
	return false;
}


/**
	Schlimme Worte durch *Z* ersetzen
*/

void ddstringset::translate( ddstring& target, const ddstring& replacement )
{
	// alte version: kann keine teilworte: ficken! bleibt, ficken ! klappt
	
	/*
	
	ddstring result, word;
	size_t i=0; 
	while( i<target.size() )
	{
		// skip mode
		while( i<target.size() && !isalnum( target[i] ) )
	  {
			result += target[i];
			i++;
		}
		
		// read mode
		word = "";
		while( i<target.size() && !isblank( target[i] ) )
	  {
			word+= target[i];
			i++;
		}
		// replace mode
		
		if( !word.empty() )
		{
			size_t found = _blockset.count( word.as_lower() );
			// bad word found
			if( found )
				result += replacement;
			else
				result += word; 	
		}
	}
  target = result;
  
  */
  
	ddthreadautomutex tmp( _blockmutex );
	
	// wir holen uns eine liste aus bösen worten
	// und ersetzen alle im posting enthaltenen
	//
	// ok, viel langsamer kann man dass nicht
	// bauen, wie hieß noch der nette algorithmus mit searchtree ?
	
	
	ddstring lowtxt = target.as_lower();	
	set<ddstring>::const_iterator pos = _blockset.begin();
	// über jedes böse wort
	for( ; pos != _blockset.end(); ++pos )
	{
  	ddstring key = (*pos).as_lower();
  	int beg, index = 0, offset = replacement.length() - key.length();
  	while( ( beg =  lowtxt.find( key, index ) ) >= 0 )
 		{
 			lowtxt.replace( beg, key.length(), replacement );
 			target.replace( beg, key.length(), replacement );
 			index = beg + offset;
 		}
	
	}
	
	
}


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

    libdope
    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.

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


ddstringrowset::ddstringrowset()
{

}


ddstringrowset::~ddstringrowset()
{

}


ddstring ddstringrowset::to_string() const
{
	ddstring result;
	for( size_t i=0; i<_data.size(); ++i )
		result += _data[i] + "\n";
	return result;
}


/**
	Einen String (für die DB) aus allen Einträgen zusammenbauen.
*/

void ddstringrowset::from_string( const ddstring& src )
{
	ddstringrow row( src, "\n" );
	for( size_t i=0; i < row.size(); ++i )
	{
		size_t pos = row[i].find( "|" );
		if( pos != ddstring::npos )
			ddstringset::add_entry( row[i].substr(0,pos) );
		_data.add( row[i] );
	}
}


/**
	Einträge aus einem String (aus der DB) erzeugen.
*/

void ddstringrowset::add_entry( const ddstring& key, const ddstring& comment )
{
	// die ip eintragen
	ddstringset::add_entry( key );
	// wieder locken
	ddthreadautomutex tmp( _blockmutex );			
	ddstring entry = key + "|";
	entry += ddbasedate::now().to_string_de() + "|";
	entry += comment;
	_data.add( entry );
}


void ddstringrowset::drop_entry( const ddstring& key )
{
	ddthreadautomutex tmp( _blockmutex );
	_blockset.erase( key );
	for( size_t i=0; i<_data.size(); ++i )
	{
		if( _data[i].contains( key ) )
		{
			_data.kill_entry( i );
			return;
		}
	}
}



