All pastes #777105 Raw Edit

Vermischtes

public text v1 · immutable
·published 2007-11-16 15:14 UTC
rendered paste body
#include "CDir.h"

CDir::CDir( void )
{
	this->m_Dir			= NULL;
	this->m_DirList		= new CDirList;

	this->m_DirHandle	= 0;

	this->setLastError( ECDIR_OK );
	
	memset( &(this->m_FileInformation), 0x00, sizeof( WIN32_FIND_DATAA ) );
}

CDir::~CDir( void )
{
	this->m_DirList->clear();
	delete this->m_DirList;

	if( this->m_Dir != NULL )
	{
		free( this->m_Dir );
	}
	else
	{
		/* NOTHING TO DO */
	}

	this->m_DirList = NULL;
	this->m_Dir = NULL;
}

int CDir::scanDir( void )
{
	if( false == this->dirSet() )
	{
		this->setLastError( ECDIR_E_NO_DIR );
		return ECDIR_E_NO_DIR;
	}
	else
	{
		return scanDir( this->getDir() );
	}
}

int CDir::scanDir( const char *dir )
{
	int   ret    = 0;
	char* dirArg = NULL;
	char* buffer = NULL;

	if( this->m_DirHandle != 0 )
	{
		FindClose( this->m_DirHandle );
	}
	else
	{
		/* NOTHING TO DO */
	}

	if( NULL == dir )
	{
		//printf("dir!\n");
		this->setLastError( ECDIR_E_DIR );
		return ECDIR_E_DIR;
	}
	else
	{
		this->clearContainer();

		int size = (int)strlen(dir)+(3*sizeof(char));
		
		//printf( "%s\n", dir );
		//ret = this->setDir( dir );

		if( ret != ECDIR_OK )
		{
			this->setLastError( ret );
		//	printf( "setdir!!\n");
			return ret;
		}

		//printf("\ndir to scan: %s\n",dir);

		dirArg = (char*)malloc( size );

		if( dirArg == NULL )
		{
			this->setLastError( ECDIR_E_MEMORY );
			return ECDIR_E_MEMORY;
		}
		else
		{
			/* NOTHING TO DO */
		}
		
		if( strstr( dir, "\\" ) )
		{
			_snprintf( dirArg, size, "%s*", dir );
		}
		else
		{
			_snprintf( dirArg, size, "%s\\*", dir );
		}
	}
	
	this->m_DirHandle = FindFirstFileA( dirArg, &(this->m_FileInformation) );
	buffer = (char*)malloc( sizeof(this->m_FileInformation.cFileName) );
	
	if( buffer == NULL )
	{
		this->setLastError( ECDIR_E_MEMORY );
		return ECDIR_E_MEMORY;
	}
	else
	{
		strncpy( buffer, this->m_FileInformation.cFileName, sizeof(this->m_FileInformation.cFileName) );
		this->m_DirList->push_back( CDirAttr( std::string(buffer), (this->m_FileInformation) ) );

		free(buffer);
	}

	if( INVALID_HANDLE_VALUE != this->m_DirHandle )
	{ 
		while( FindNextFileA( this->m_DirHandle, &(this->m_FileInformation) ) != 0 )
		{
			buffer = (char*)malloc( sizeof(this->m_FileInformation.cFileName) );

			if( buffer == NULL )
			{
				this->setLastError( ECDIR_E_MEMORY );
				return ECDIR_E_MEMORY;
			}
			else
			{
				strncpy( buffer, this->m_FileInformation.cFileName, sizeof(this->m_FileInformation.cFileName) );
				this->m_DirList->push_back( CDirAttr( std::string(buffer), (this->m_FileInformation) ) );

				free(buffer);
			}
		}
	}
	else
	{
		this->setLastError( ECDIR_E_DIR );
		//printf("invhand!!\n");
		return ECDIR_E_DIR;
	}

	free( dirArg );

	return ECDIR_OK;
}

pCDirList CDir::getIndex( void )
{
	return this->m_DirList;
}

int CDir::setDir( const char* dir )
{
	int size = (int)strlen(dir)+sizeof(char);

	//printf("dir to set: %s\n", dir );

	if( size == 1 )
	{
		this->setLastError( ECDIR_E_DIR );
		return ECDIR_E_DIR;
	}
	else
	{
		/* NOTHING TO DO */
	}

	if( NULL != this->m_Dir )
	{
		//printf("prev. dir: %s\n", this->m_Dir );
		free( this->m_Dir );

		this->m_Dir = NULL;
	}
	else
	{
		/* NOTHING TO DO */
	}

	if( this->m_Dir == NULL )
	{
		this->m_Dir = (char*)malloc( size );
	}
	
	if( this->m_Dir == NULL )
	{
		this->setLastError( ECDIR_E_MEMORY );
		return ECDIR_E_MEMORY;
	}
	else
	{
		/* NOTHING TO DO */
	}

	memcpy( this->m_Dir, dir, size );
	//strncpy( this->m_Dir, dir, size );

//	printf("new dir: %s\n", dir );

	return ECDIR_OK;
}

char* CDir::getDir( void )
{
	return this->m_Dir;
}

bool CDir::dirSet( void )
{
	if( this->m_Dir != NULL )
	{
		return true;
	}
	else
	{
		/* NOTHING TO DO */
	}

	return false;
}

int CDir::createDir( void )
{
	return this->createDir( this->getDir() );
}

int CDir::createDir( const char* dir )
{
	int size = (int)strlen( dir );
	SECURITY_ATTRIBUTES secAtt;

	secAtt.nLength = sizeof( SECURITY_ATTRIBUTES );
	secAtt.lpSecurityDescriptor = NULL;
	secAtt.bInheritHandle = FALSE;

	if( 0 != size )
	{
		if( FALSE == CreateDirectoryA( dir, &secAtt ) )
		{
			this->setLastError( ECDIR_E_DIR_SET );
			return ECDIR_E_DIR_SET;
		}
		else
		{
			return ECDIR_OK;
		}
	}
	else
	{
		return ECDIR_E_NO_DIR;
	}
}

int CDir::removeDir( void )
{
	return this->removeDir( this->getDir() );
}

int CDir::removeDir( const char* dir )
{
	int size = (int)strlen( dir );

	if( 0 != size )
	{
		if( FALSE == RemoveDirectoryA( dir ) )
		{
			this->setLastError( ECDIR_E_DIR );
			return ECDIR_E_DIR;
		}
		else
		{
			return ECDIR_OK;
		}
	}
	else
	{
		this->setLastError( ECDIR_E_NO_DIR );
		return ECDIR_E_NO_DIR;
	}
}

char* CDir::getWorkingDir( void )
{
	char*	buffer	= NULL;
	int		size	= GetCurrentDirectoryA( 0, NULL );

	buffer = (char*)malloc( size );
	
	if( NULL == buffer )
	{
		this->setLastError( ECDIR_E_MEMORY );
		return NULL;
	}

	GetCurrentDirectoryA( size, buffer );

	return buffer; /* MUST be free'd by user !! */
}

bool CDir::fileExists( const char* dir, const char* file )
{
	if( NULL == dir || NULL == file )
	{
		this->setLastError( ECDIR_E_DIR );
		return false;
	}

	if( 0 != strcmp( this->getDir(), dir ) )
	{
		this->setDir( dir );
	}
	
	if( ECDIR_OK != this->scanDir( this->getDir() ) )
	{
	//	printf( "%d\n", this->getLastError() );	
		return false;
	}

	for( unsigned int i=0; i<this->m_DirList->size(); i++ )
	{
	//	printf( "%s :: %s\n", this->m_DirList->at( i ).first.c_str(), file );
		if( 0 == strcmp( this->m_DirList->at( i ).first.c_str(), file ) )
		{
			return true;
		}
	}

	this->setLastError( ECDIR_E_DIR );

	return false;
}

bool CDir::fileExists( const char* file )
{
	return this->fileExists( this->getDir(), file );
}

void CDir::clearContainer( void )
{
	if( this->m_DirList->size() != 0 )
	{
		this->m_DirList->clear();
	}
}

int CDir::moveFile( const char* srcFile, const char* destPath )
{
	return this->moveFile( this->getDir(), srcFile, destPath );
}

int CDir::moveFile( const char* srcPath, const char* srcFile, const char* destPath )
{
	char*	buffer	= NULL;
	int		bufSize = (int)( strlen( srcPath )+strlen( srcFile ) );

	if( NULL == srcPath || NULL == srcFile || NULL == destPath )
	{
		this->setLastError( ECDIR_E_DIR );
		return ECDIR_E_DIR;
	}
	else
	{
		/* NOTHING TO DO */
	}

	buffer = (char*)malloc( bufSize );

	if( NULL == buffer )
	{
		this->setLastError( ECDIR_E_MEMORY );
		return ECDIR_E_MEMORY;
	}
	else
	{
		if( srcPath[ strlen(srcPath) ] == '\\' )
		{
			sprintf( buffer, "%s%s", srcPath, srcFile );
		}
		else
		{
			buffer = (char*)realloc( buffer, bufSize+1 );

			if( NULL == buffer )
			{
				this->setLastError( ECDIR_E_MEMORY );
				return ECDIR_E_MEMORY;
			}

			sprintf( buffer, "%s\\%s", srcPath, srcFile );
		}
	}

	if( FALSE == MoveFileA( buffer, destPath ) )
	{
		this->setLastError( ECDIR_E_FILE );
		return ECDIR_E_FILE;
	}

	free( buffer );

	return ECDIR_OK;
}


void CDir::setLastError( int err )
{
	if( err > ECDIR_E_FILE || err < ECDIR_OK )
	{
		return;
	}

	this->m_lastErr = err;
}

int	CDir::getLastError( void )
{
	return this->m_lastErr;
}