#include "stdafx.h"
#include "Folder.h"

int IoAFolder::IsExistingFolder( const char *FolderPath )
{
    DWORD FileAttributes = ::GetFileAttributes( FolderPath );

    return FileAttributes != 0xffffffff && ( FileAttributes & FILE_ATTRIBUTE_DIRECTORY ) != 0;
}

int IoAFolder::ClearFolder( const char *FolderPath )
{
	CString WildCard = CString( FolderPath ) + "\\*";

    WIN32_FIND_DATA FindData;

    HANDLE hFind = ::FindFirstFile( WildCard, &FindData );

    if ( hFind == INVALID_HANDLE_VALUE ) return GetLastError( ) == ERROR_FILE_NOT_FOUND;

    int Status = FALSE;

    for ( ; ; )
    {
        CString FileName = FindData.cFileName;

        if ( FileName.Compare( "." ) != 0 && FileName.Compare( ".." ) != 0 )  // skip past . and ..
        {
            CString FindPath = CString( FolderPath ) + "\\" + CString( FileName );

            if ( FindData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
            {
                if ( ! ClearFolder( FindPath ) ) break;  // recursive call

                if ( ! ::RemoveDirectory( FindPath ) ) break;
            }
            else
            {
                if ( ! ::DeleteFile( FindPath ) ) break;
            }
        }

        if ( ! ::FindNextFile( hFind, &FindData ) )
        {
            if ( GetLastError( ) == ERROR_NO_MORE_FILES ) Status = TRUE;

            break;
        }
    }

    if ( ! ::FindClose( hFind ) ) Status = FALSE;

    return Status;
}

int IoAFolder::CreateFolder( const char *FolderPath )
{
    DWORD FileAttributes = ::GetFileAttributes( FolderPath );

    // See if pathname points to anything
    if ( FileAttributes == 0xffffffff )
    {
        // See if call to GetFileAttributes( ) failed
        if ( ::GetLastError( ) != ERROR_FILE_NOT_FOUND ) return FALSE;
    }
    else if ( FileAttributes & FILE_ATTRIBUTE_DIRECTORY )  // existing folder found
    {
        // Delete everything in folder
        if ( ! ClearFolder( FolderPath ) ) return FALSE;

        // Delete folder itself
        if ( ! ::RemoveDirectory( FolderPath ) ) return FALSE;
    }
    else  // file with new folder name found
    {
        // Delete the file
        if ( ! ::DeleteFile( FolderPath ) ) return FALSE;
    }

    // Now that the way is clear, create a new folder
    if ( ! ::CreateDirectory( FolderPath, NULL ) ) return FALSE;

    return TRUE;
}

int IoAFolder::Parse( )
{
	if ( ! IsDefined( ) ) return FALSE;

	CString WildCard = CString( m_FolderPath ) + "\\*";

    WIN32_FIND_DATA FindData;

    HANDLE hFind = ::FindFirstFile( WildCard, &FindData );

    if ( hFind == INVALID_HANDLE_VALUE ) return GetLastError( ) == ERROR_FILE_NOT_FOUND;

    int Status = FALSE;

	int FolderCount = 0;
	int FileCount   = 0;

    for ( ; ; )
    {
        CString FileName = FindData.cFileName;

        if ( FileName.Compare( "." ) != 0 && FileName.Compare( ".." ) != 0 )  // skip past . and ..
        {
            if ( FindData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
            {
                FolderCount += 1;
            }
            else
            {
                FileCount += 1;
            }
        }

        if ( ! ::FindNextFile( hFind, &FindData ) )
        {
            if ( GetLastError( ) == ERROR_NO_MORE_FILES ) Status = TRUE;

            break;
        }
    }

    if ( ! ::FindClose( hFind ) ) Status = FALSE;

    return Status;
}
