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

    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 DDSTRING_H
#define DDSTRING_H

#include <string>
#include <dddefines.h>
#include <sstream>
#include <iomanip>


using namespace std;


/// ddstring


/// namespace ddstring contains utility functions for	ddstring.

 		
		

class ddstringrow;

int hexit( char c );

class ddstring : public string
{

public:

	ddstring();
	ddstring( const ddstring& src );
	ddstring& operator=( const ddstring& src );
	
	ddstring( const string& src );
  ddstring( const char* src );
  
  ddstring( size_t len, char entry );
  ddstring( const char* entry, size_t len );

	virtual ~ddstring();
	
	ddstring& operator=( const string& src );
  ddstring& operator=( const char* src );
  
  // wild: in zahl umwandeln, erhöhen und als string speichern
  ddstring& operator++( int );
	
	// wild: in zahl umwandeln, decrementieren und als string speichern
  ddstring& operator--( int );

	// unklar, ob das funktioniert
	
	ddstring& operator=( bool num )
	{
		return from_bool( num );
	}
	
  ddstring& operator=( int num )
 	{
		return from_int( num );
	}
 	
 	ddstring& operator=( size_t num )
 	{
		return from_size_t( num );
	}
	
	ddstring& operator=( double num )
  {
		return from_double( num );
	}
	
	inline ddstring dup() const
	{
		return ddstring( *this );
	}
		
	inline int last_byte()
	{
		if( this->size()>0 )
			return (*this)[this->size()-1];
		return 0; //??
	}
	
	inline bool contains( const ddstring& key ) const
	{
		return find( key ) != ddstring::npos;
	}
	
	inline bool contains( const char key ) const
	{
		return find( key ) != ddstring::npos;
	}
	
	ddstring& from_bool( bool num );
	ddstring& from_int( int num );
  ddstring& from_size_t( size_t num );   	
	ddstring& from_double( double num, int precision = 6 );
	int    	  from_file( const ddstring& filename );
	
	ddstring& to_dir_name();
	bool			to_file( const ddstring& filename );
	ddstring& to_upper();
	ddstring& to_lower();
	ddstring& to_split_text( size_t asize );
	
	bool   		is_number() const;
	
	ddstring& trim_evil();	
	ddstring& trim();
	ddstring& trim_text( size_t asize );	
	ddstring	trim_pattern( const ddstring& pattern ) const;	
	ddstring& trim_all_of( const ddstring& pattern );
	ddstring& blank_all_of( const ddstring& pattern );

	bool      split( const ddstring& pattern, ddstring& part1, ddstring& part2 ) const;
	
	ddstring& replace_all( const ddstring& oldstr, const ddstring& newstr );
	ddstring& replace_all( char oldchar, char newchar );
	size_t		count_all( const ddstring& txt );
	size_t 		count_words();
	
	
	
	ddstring& escape();
	ddstring& unescape();

	// Konvertierung _ohne_ Änderung
	
	inline ddstring as_upper() const
	{
		return dup().to_upper();
	}
	
	inline ddstring as_lower() const
	{
		return dup().to_lower();
	}

	
	bool   		as_bool() const;
	int	 	  	as_int() const;
	double  	as_double() const;
	size_t		as_size_t() const;
	
	ddstring as_split_text( size_t asize ) const;
	ddstring as_cleaned_text( size_t asize = ddstring::npos ) const;
	ddstring as_valid_nickname() const;
	ddstring as_plain_ascii() const;
	ddstring as_codeless_text() const;

};


namespace ddstrutil
{	
	ddstring to_hex( int num, int bytes, bool uppercase=true );
	
	ddstring from_bool( bool num );
	ddstring from_int( int num );
	ddstring from_size_t( size_t num );
	ddstring from_double( double num, int precision=6 );
	ddstring from_hex( int num, int bytes=0 );
	ddstring from_file( const ddstring& filename );

} //namespace ddstring




#endif
