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

    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 <ddiourl.h>


/**
	Default constructor.
*/

ddiourl::ddiourl()
{
}


/**
	Destructor.
*/

ddiourl::~ddiourl()
{
}


/**
	Creates the data members from the source strings.
	@param	path 	the src  string
	@param 	base	   the base path (optional)
*/


bool ddiourl::init_path( const ddstring& path )
{
	_data.clear();
	_data.resize( url_field_size );
	
	ddstring fullpath = path;
	
	// chop cgi vars, if any
	size_t pos = fullpath.rfind( '?' );
	if( pos != ddstring::npos )
		fullpath = fullpath.substr( 0, pos );
	
	if( fullpath.size() >0 && fullpath[0] != '/' )
		set_flag( ddioobject::url_is_relative  );
	_data[ddioobject::url_fullpath]  = fullpath;

	// path is the path without file
	pos = fullpath.rfind( '/' );
	// path does exist
	if( pos != ddstring::npos && pos != 0 )
	{
		_data[ddioobject::url_path] = fullpath.substr( 0, pos+1 );
		_data[ddioobject::url_file] = fullpath.substr( pos+1 );
	}
	else   // path does not exist
	{
		_data[ddioobject::url_file] = fullpath;
	}

	ddstring& src = ( _data[ddioobject::url_file] );
	size_t nxt = src.rfind( '.' );
	if( nxt != ddstring::npos )
	{
		_data[ddioobject::url_file_type] = src.substr( nxt+1 );
		_data[ddioobject::url_file_body] = src.substr( 0, nxt );
	}
	else
	{
		_data[ddioobject::url_file_body] = src;			
	}
	
		// cleanup
	if( _statbuf )
		delete _statbuf;
	_statbuf = 0;
	
	return true;
	
}

