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

    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.

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


#ifndef DDSTRINGROW_H
#define DDSTRINGROW_H

#include <ddbaseobject.h>

using namespace std;

/// ddstringrow implemtiert ein String-Array. 
/**
	Änderung: 
	ddstringrow heisst jetzt ddstringrow, wird also als service
	für ddstring interpretiert und nicht mehr als ddrow<ddstring>.
	
	Dadurch entspricht die Implementierung auch eher ddstringmap bzw.
	(neuer name) ddstringmap.
	
	ddrow ist somit obsolet.
	
	@see ddstring
*/

class ddstringrow : public vector<ddstring>, public ddbaseobject
{

public:

	ddstringrow();
	ddstringrow( size_t size );
	ddstringrow( const char** src );
	ddstringrow( const ddstring& src, const ddstring& sep = ";", size_t minsize=0 );

	virtual ~ddstringrow();

	// FIX! auslagern in creators...
	void create( const char** src );
	void create( const ddstring& src, const ddstring& sep = ";", bool doappend=false, size_t minsize=0 );
	void create_blocks( const ddstring& src, const ddstring& sep, bool doappend=false );
	void create_lazy( const ddstring& src,const ddstring& sep=" \t", bool doappend=false, bool keepseps=false );
	void create_lines( const char* src, size_t len, bool doappend=false );
	void create_lines( const ddstring& src, bool doappend=false );
	
	size_t add( const ddstring& item );
	
	void kill_entry( const ddstring& entry );
	void kill_entry( size_t index );
	
	virtual ddstring join( const ddstring& sep="", size_t fro =0, size_t to=ddstring::npos ) const;
	virtual int index_of( const ddstring& key ) const;
	
	// prototypes
		
	virtual ddstring& operator[]( const ddstring& key )
	{
		throw out_of_range( "stringrow operator[ key ]: prototype" );
		return (*this)[0];
	}

	
	virtual const ddstring& operator[]( const ddstring& key ) const
	{
		throw out_of_range( "stringrow operator[ key ] const: prototype" );
		return (*this)[0];
	}
	
	
	virtual bool contains_key( const ddstring& key ) const
	{
		throw out_of_range( "stringrow contains_key: prototype" );
		return false;
	}
	
	
	/**
		Setzt den Feldwert an der Position key, falls key vorhanden.
	*/
	
	virtual void set_field_value( const ddstring& key, const ddstring& value )
	{
		if( contains_key( key ) )
			(*this)[ key ] = value;
	}
	
	// deppencompiler
	
	ddstring& operator[]( size_t index )
	{
		/*
		if( index < size() )		
			return vector<ddstring>::operator[]( index );
		
		ddstring msg = "ddstringrow operator[ size_t index ]: out of range: ";
		msg += ddstrutil::from_size_t( index ) + " size: " + ddstrutil::from_size_t( size() );
		log_debug( msg );			
		throw out_of_range( msg );
		*/				
		// zu testzwecken lassen wir ihn crashen:
		if( index >= size() )
		{
			ddstring msg = "ddstringrow operator[ size_t index ]: out of range: ";
			msg += ddstrutil::from_size_t( index ) + " size: " + ddstrutil::from_size_t( size() );
			log_debug( msg );
		}
		
		return vector<ddstring>::operator[]( index );
	}

	
	const ddstring& operator[]( size_t index ) const
	{
		
		/*
		if( index < size() )		
			return vector<ddstring>::operator[]( index );
		
		ddstring msg = "ddstringrow operator[ size_t index ]: out of range: ";
		msg += ddstrutil::from_size_t( index ) + " size: " + ddstrutil::from_size_t( size() );
		log_debug( msg );			
		throw out_of_range( msg );
		*/				
		
		// zu testzwecken lassen wir ihn crashen:
		if( index >= size() )
		{
			ddstring msg = "ddstringrow operator[ size_t index ] CONST: out of range: ";
			msg += ddstrutil::from_size_t( index ) + " size: " + ddstrutil::from_size_t( size() );
			log_debug( msg );
		}
		
		return vector<ddstring>::operator[]( index );

	}
	
	
	virtual void dump() const;
	
	
	virtual ddstring to_string() const;

};


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

    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.

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


class ddstringstaterow : public ddstringrow, public ddbasestate
{
public:
};


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

    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.

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


/// ddstringmap: Simpler string-string Container. Basiert auf std::map.
/**
	Zusätzliche wird noch der übliche Krempel wie contains_key etc.
	implementiert.
*/

class ddstringmap : public map<ddstring,ddstring>, public ddbaseobject
{

public:

  /**
   		Destructor. Tut nix.
	*/
	
	virtual ~ddstringmap()
  {
	}


  /**
		Bastelt einen String aus allen Einträgen zum anschauen.
		@see ddbaseobject
	*/

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


  /**
		@returns 'true' wenn ein Eintrag 'key' existiert.
	*/

	virtual bool contains_key( const ddstring& key ) const
	{
		ddstringmap::const_iterator pos = find( key );
		return ( pos != end() );
	}
	
	
	/**
		Setzt den Feldwert an der Position key, falls key vorhanden.
	*/
	
	virtual void set_field_value( const ddstring& key, const ddstring& value )
	{
		if( contains_key( key ) )
			(*this)[ key ] = value;
	}
	

	/**
		Schreibt alle Einträge nach cout.
		@see ddbaseobject
	*/

	virtual void dump() const
	{
		ddstringmap::const_iterator pos = begin();
    for( ; pos != end(); ++pos )
		{
			ddstring line =  "key: <" + (*pos).first + "> value: <" + (*pos).second + ">";
			cout << line << endl;
		}
	}

};


#endif
