VisualBCutterDlg.cpp

 /**
* Version 0 of bcutter was command line based this is a new
* window based version of the original BCutter.
*
* Version 1.0.0 of VisulBCutter used the BCutter's algorithm
* to chop file, added a functionality to creation of a batch
* file for restoring the file. This was left out on BCutter
*
* Version 1.0.1 fixed a bug that caused the VisualBCutter to
* hang. Still working on the help file (maybe i should leave it out).
*
* Version 1.1.0 uses a thread for choping the file in the
* background because the dialog could not repaint itself
* when it was chopping of huge files.
* Commented out the help dialog, maybe it will be included
* in the future. Replaced it with the about dialog.
*
* Version 1.1.0.1 ;)  Added the ability to cancel the process
* during chopping. This introduced a couple (or more) of bugs
* This version was not real released but it fixes
* a lot of bugs that caused this program to crash when the use
* cancels an operation.
*
* The only problem I seem to find with this program is that when
* you try to cancel the chopping while the thread is reading from
* the file, the program hangs. To get around this I disable the
* cancel button when the file is beign read.
*
*
*/


// VisualBCutterDlg.cpp : implementation file
//

#include "stdafx.h"
#include "VisualBCutter.h"
#include "VisualBCutterDlg.h"
#include "helpdialog.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
CAboutDlg();

// Dialog Data
//{{AFX_DATA(CAboutDlg)
enum { IDD = IDD_ABOUTBOX };
//}}AFX_DATA

// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CAboutDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
//}}AFX_VIRTUAL

// Implementation
protected:
//{{AFX_MSG(CAboutDlg)
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
//{{AFX_DATA_INIT(CAboutDlg)
//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()


/***
*
* GLOBAL THREAD FUNCTION
* I could not use chop file as a thread function
* maybe it is because it is the member of a class
* so i will use this global function (sic) to do the
* job and pass it the CVisualBCutterDlg class as a parameter
*/

UINT GlobalChop(LPVOID pParam){

CVisualBCutterDlg* pObject = (CVisualBCutterDlg*)pParam;

if(pObject == NULL || !pObject->IsKindOf(RUNTIME_CLASS(CVisualBCutterDlg)))
return 1;

return pObject->ChopFile(pParam);
}



/////////////////////////////////////////////////////////////////////////////
// CVisualBCutterDlg dialog

IMPLEMENT_DYNAMIC(CVisualBCutterDlg,CDialog)

CVisualBCutterDlg::CVisualBCutterDlg(CWnd* pParent /*=NULL*/)
: CDialog(CVisualBCutterDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CVisualBCutterDlg)
m_szSliceName = _T("");
m_szFileName = _T("");
m_lSliceSize = 0;
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CVisualBCutterDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CVisualBCutterDlg)
DDX_Control(pDX, IDC_COMBO1, m_cComboBox);
DDX_Control(pDX, IDC_PROGRESS1, m_cProgress);
DDX_Text(pDX, IDC_ESLICENAME, m_szSliceName);
DDX_Text(pDX, IDC_FILENAME, m_szFileName);
DDX_Text(pDX, IDC_SLICESIZE, m_lSliceSize);
//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CVisualBCutterDlg, CDialog)
//{{AFX_MSG_MAP(CVisualBCutterDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_FILE_OPEN, OnFileOpen)
ON_BN_CLICKED(IDC_SLICEFILEOPEN, OnSlicefileopen)
ON_BN_CLICKED(IDC_CONTINUE, OnContinue)
ON_BN_CLICKED(IDC_HELPBUTTON, OnHelp)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CVisualBCutterDlg message handlers

BOOL CVisualBCutterDlg::OnInitDialog()
{
CDialog::OnInitDialog();

// Add "About..." menu item to system menu.

// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);

CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}

// Set the icon for this dialog.  The framework does this automatically
//  when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon

// TODO: Add extra initialization here

m_cComboBox.AddString("Megabytes");
m_cComboBox.AddString("Gigabytes");
m_bThreadRunning = false;

return TRUE;  // return TRUE  unless you set the focus to a control
}

void CVisualBCutterDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CVisualBCutterDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting

SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;

// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CVisualBCutterDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}

void CVisualBCutterDlg::OnFileOpen()
{
CFileDialog fd(TRUE);
if(fd.DoModal() == IDOK)
{
m_szFileName =  fd.GetPathName();
m_szInputName = fd.GetFileName();

SetDlgItemText(IDC_FILENAME,m_szFileName);
}


}

void CVisualBCutterDlg::OnSlicefileopen()
{
CFileDialog fd(FALSE);
if(fd.DoModal() == IDOK)
{
m_szSliceName = fd.GetFileName();
m_szSlicePath = fd.GetPathName();
SetDlgItemText(IDC_ESLICENAME,m_szSlicePath);

int pos = m_szSlicePath.Find(m_szSliceName,0);
m_szSlicePath.Delete(pos,m_szSliceName.GetLength());
}
}

void CVisualBCutterDlg::OnContinue()
{
//Disable controls while the thread is running
EnableControls(FALSE);
SetDlgItemText(IDCANCEL,"&Cancel");
m_pThread = AfxBeginThread(GlobalChop,this);
m_bThreadRunning = true;


}


long CVisualBCutterDlg::GetSliceSize(CString str)
{
long retVal = 1024*1024;

if(str == "Bytes")
retVal = 1;
if(str == "Kilobytes")
retVal = 1024;
if(str == "Megabytes")
retVal = 1024*1024;
if(str == "Gigabytes")
retVal = 1024*1024*1024;

return retVal;
}

void CVisualBCutterDlg::OnHelp()
{
//CHelpDialog hdlg;

//hdlg.DoModal();
CAboutDlg dlg;
dlg.DoModal();

}

UINT CVisualBCutterDlg::ChopFile(LPVOID lParam)
{
CRITICAL_SECTION  csCritical;
char temp[20];
CString size;
CString batchString = "copy /B ";

m_cInputFile.Open(m_szFileName,CFile::modeRead);
long fileSize = m_cInputFile.GetLength();


GetDlgItem(IDC_SLICESIZE)->GetWindowText(size);
m_lSliceSize = atol(size);
m_cComboBox.GetWindowText(size);
m_lSliceSize *= GetSliceSize(size);

char *buff = new char[m_lSliceSize];

if(m_lSliceSize <= 0)
{
AfxMessageBox("Invalid Slice size");
EnableControls(TRUE);
m_cInputFile.Close();
return -1;
}

long numSlices = fileSize/m_lSliceSize;

if(numSlices != 0)
{


long remSlice = fileSize%m_lSliceSize;

if(remSlice != 0)
m_cProgress.SetRange(0,(int)numSlices+1);
else
m_cProgress.SetRange(0,(int)numSlices);

long i =1;
for(;i<= numSlices; i++)
{
InitializeCriticalSection (&csCritical);
EnterCriticalSection (&csCritical);
m_cProgress.SetPos((int)i);
CString slice = m_szSliceName +"."+ ltoa(i,temp,10);
batchString = batchString+slice + "+";

GetDlgItem(IDCANCEL)->EnableWindow(FALSE);
m_cInputFile.Read(buff,m_lSliceSize);
GetDlgItem(IDCANCEL)->EnableWindow(TRUE);


m_cSliceFile.open(m_szSlicePath+slice,ios::binary);
m_cSliceFile.write(buff,m_lSliceSize);
m_cSliceFile.close();
LeaveCriticalSection (&csCritical);

}

if(remSlice !=0)
{
InitializeCriticalSection (&csCritical);
EnterCriticalSection (&csCritical);
m_cProgress.SetPos((int)i);
CString slice = m_szSliceName +"."+ ltoa(i,temp,10);
batchString = batchString+slice +" ";

GetDlgItem(IDCANCEL)->EnableWindow(FALSE);
m_cInputFile.Read(buff,remSlice);
GetDlgItem(IDCANCEL)->EnableWindow(TRUE);

m_cSliceFile.open(m_szSlicePath+slice,ios::binary);
m_cSliceFile.write(buff,m_lSliceSize);
m_cSliceFile.close();
LeaveCriticalSection (&csCritical);
}


batchString = batchString +" "+m_szInputName;
m_cRestoreBatFile.open(m_szSlicePath+m_szSliceName+".bat");
m_cRestoreBatFile<<batchString<<endl;
m_cRestoreBatFile.close();

AfxMessageBox("Done.");
EnableControls(TRUE);

//Clear controls
SetDlgItemText(IDC_FILENAME,"");
SetDlgItemText(IDC_ESLICENAME,"");
SetDlgItemText(IDC_SLICESIZE,"0");
m_cProgress.SetPos(0);


}
else
{
AfxMessageBox("Invalid slice size",MB_ICONSTOP,0);

//Enable controls
EnableControls(TRUE);

}

m_cInputFile.Close();
return 0;

}



void CVisualBCutterDlg::OnCancel()
{


SetWindowText("OnCancel");

CRITICAL_SECTION  csCritical;

if(m_bThreadRunning)
{
InitializeCriticalSection (&csCritical);
EnterCriticalSection (&csCritical);

if(m_pThread != NULL)
{
m_pThread->SuspendThread();
}

delete m_pThread;
m_pThread = NULL;

m_cInputFile.Abort();

LeaveCriticalSection (&csCritical);

//Clear controls
SetDlgItemText(IDC_FILENAME,"");
SetDlgItemText(IDC_ESLICENAME,"");
SetDlgItemText(IDC_SLICESIZE,"0");
SetDlgItemText(IDCANCEL,"&Close");
m_cProgress.SetPos(0);
EnableControls(TRUE);

}
else
{

CDialog::OnCancel();
::ExitProcess(0);
}
}

void CVisualBCutterDlg::EnableControls(BOOL state)
{

GetDlgItem(IDC_CONTINUE)->EnableWindow(state);
GetDlgItem(IDC_FILENAME)->EnableWindow(state);
GetDlgItem(IDC_ESLICENAME)->EnableWindow(state);
GetDlgItem(IDC_SLICESIZE)->EnableWindow(state);
GetDlgItem(IDC_COMBO1)->EnableWindow(state);
GetDlgItem(IDC_FILE_OPEN)->EnableWindow(state);
GetDlgItem(IDC_SLICEFILEOPEN)->EnableWindow(state);
GetDlgItem(IDC_HELPBUTTON)->EnableWindow(state);
SetDlgItemText(IDCANCEL,"&Close");
m_bThreadRunning = false;
}

Project Homepage: