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

    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 <ddioobject.h>
#include <unistd.h>	
#include <stdexcept>	

using namespace std;


/**
	Default Constructor
*/

ddioobject::ddioobject()
: _statbuf(0)
{
	_data.resize( url_field_size );
}


/**
	Destructor	
*/

ddioobject::~ddioobject()
{
	if( _statbuf )
		delete _statbuf;
}


/**
	Returns true for local data ( eg. a file )
*/

bool ddioobject::is_local() const 
{
	return test_flag( ddioobject::url_is_local );
}


/**
	Returns true for remote data ( eg. ftp, http )
	== !is_local()
*/

bool ddioobject::is_remote() const 
{
	return !is_local();
}


/**
	Returns true when the description ddstring was parsed
	successfully.
*/

bool ddioobject::is_valid() const 
{
	return test_flag( ddioobject::url_is_valid );
}


/**
	Returns true when the filepath is relative.
*/

bool ddioobject::is_relative() const 
{
	return test_flag( ddioobject::url_is_relative );
}


/**
	Returns true when the object represents a folder (eg. directory).
	@see get _content()	
*/

bool ddioobject::is_folder()
{
	// first, try ::stat if not done yet.
	if( !_statbuf )
		init_stat();
	return test_flag( ddioobject::url_is_folder );
}


/**
	Returns true when the object represents a stream, 
	(eg. character input)
	@see get _content()	
*/

bool ddioobject::is_stream() const 
{
	return test_flag( ddioobject::url_is_stream );
}


/**
	Returns the absolute path.
*/

const ddioobject& ddioobject::make_abs()
{
	if( !is_relative() )
		return *this;
	
	// fraqwürdig
	char tmp[4096];
	if( NULL == ::getcwd( tmp, sizeof(tmp) ) )
		throw runtime_error( "::getcwd( tmp, sizeof(tmp) ) fail!" );

	_data[ddioobject::url_fullpath] = tmp;
  _data[ddioobject::url_fullpath].to_dir_name();
  _data[ddioobject::url_fullpath] += get_path();
 	_data[ddioobject::url_fullpath].to_dir_name();
  _data[ddioobject::url_path] = _data[ddioobject::url_fullpath];
  _data[ddioobject::url_fullpath] += get_full_name();
  clear_flag( ddioobject::url_is_relative );

	return *this;

}


/**
	Returns the path component ( absolute or relativ ).
*/

const ddstring& ddioobject::get_full_path() const
{
	return _data[ddioobject::url_fullpath];
}


/**
	Returns the base path. (might be empty)
	@see ddioinfo
	@see ddioinfo
*/

const ddstring& ddioobject::get_path() const
{
	return _data[ddioobject::url_path];
}


/**
	Returns the full filename.
*/

const ddstring& ddioobject::get_full_name() const
{
	return _data[ddioobject::url_file];
}


/**
	Returns the file suffix (txt, html etc.)
*/

const ddstring& ddioobject::get_file_type() const
{
	return _data[ddioobject::url_file_type];
}


size_t ddioobject::get_file_size()
{
  if( !_statbuf )
		init_stat(); 	
	return _statbuf->st_size;
}


/**
	Returns the file body e.g. index ( from index.html )
*/

const ddstring& ddioobject::get_file_name() const
{
	return _data[ddioobject::url_file_body];
}


/**
	Returns the content.
	@see load_content() has to be called before the file content is accessible.
*/

ddstring&	ddioobject::get_file_content()
{
	return _data[ddioobject::url_content];
}


/**
	Load the file using "from_file( get_fullpath() )".
	@see ddstring::from_file
*/

int ddioobject::load_file_content()
{
	if( is_folder() )
		return 0;
	return get_file_content().from_file( get_full_path() );
}


/**
	Dumps all fields for debugging purposes.
*/

void ddioobject::dump() const
{
}


/**
	Returns a ddstring representation of this url,
	eg. http://panik.zonc.net/moo.txt .
*/

ddstring ddioobject::to_string() const
{
	return get_full_path();
}


/**
	Inits this object using ::stat
	@see ::stat
*/

bool ddioobject::init_stat()
{
	if( !_statbuf )
		_statbuf = new struct stat;
	if( ::stat( get_full_path().c_str(), _statbuf ) < 0 )
		throw runtime_error( "ddioinfo::init_stat(): stat failed: " + get_full_path() );
	if( S_ISDIR( _statbuf->st_mode ) )
		set_flag( url_is_folder );	
	return true;
}



/**
	Return last access time of the file.
	@see ddbasetime
	@see ::stat
*/	

ddbasetime ddioobject::get_last_accessed()
{
	if( !_statbuf )
		init_stat();
	return ddbasetime( _statbuf->st_atime );
}


/**
	Return last modification time of the file.
	@see ddbasetime
	@see ::stat
*/

ddbasetime ddioobject::get_last_modified()
{
	if( !_statbuf )
		init_stat();
	return ddbasetime( _statbuf->st_mtime );
}


/**
	Return last change time of the file.
	@see ddbasetime
	@see ::stat
*/

ddbasetime ddioobject::get_last_changed()
{
	if( !_statbuf )
		init_stat();
	return ddbasetime( _statbuf->st_ctime );
}


// PFUSCH! time/date Aufräumen
ddstring ddioobject::get_last_accessed_str()
{
	if( !_statbuf )
		init_stat(); 	
	ddbasedate date( _statbuf->st_atime );
	return date.to_string_gmt();
}


ddstring ddioobject::get_last_modified_str()
{
	if( !_statbuf )
		init_stat();	
	ddbasedate date( _statbuf->st_mtime );
	return date.to_string_gmt();	
}

ddstring ddioobject::get_last_changed_str()
{
  if( !_statbuf )
		init_stat(); 	
	ddbasedate date( _statbuf->st_ctime );
	return date.to_string_gmt();
}




