Click here to Skip to main content
15,892,643 members
Articles / Desktop Programming / MFC

Classes for Splitting and Joining files

Rate me:
Please Sign up or sign in to vote.
4.75/5 (7 votes)
16 Aug 20023 min read 82.2K   3K   32  
This article describes two classes, one for splitting a file into smaller chunks and another for merging smaller files into one. Also there is a utility written using these classes.
// FileSplitPage.cpp : implementation file
//

#include "stdafx.h"
#include "SplitMerge.h"
#include "FileSplitPage.h"
#include "FileSplit.h"

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

/////////////////////////////////////////////////////////////////////////////
// CFileSplitPage property page

IMPLEMENT_DYNCREATE(CFileSplitPage, CPropertyPage)

CFileSplitPage::CFileSplitPage() : CPropertyPage(CFileSplitPage::IDD)
{
	//{{AFX_DATA_INIT(CFileSplitPage)
	m_nFileSize = 0;
	m_InputFile = _T("");
	m_nFiles = 0;
	m_OutputDir = _T("");
	m_nOutputtype = -1;
	//}}AFX_DATA_INIT

	m_nOutputtype = 0;
	m_nFiles = 10;
	m_nFileSize = 1024;
	m_bIsWorking = false;
}

CFileSplitPage::~CFileSplitPage()
{
}

void CFileSplitPage::DoDataExchange(CDataExchange* pDX)
{
	CPropertyPage::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CFileSplitPage)
	DDX_Control(pDX, IDC_BTN_SELINPUTFILE, m_SelFileBtn);
	DDX_Control(pDX, IDC_BTN_SELOUTPUTDIR, m_SelDirBtn);
	DDX_Control(pDX, IDC_PROGRESS_SPLIT, m_Progress);
	DDX_Text(pDX, IDC_EDIT_FILESIZE, m_nFileSize);
	DDX_Text(pDX, IDC_EDIT_INPUTFILE, m_InputFile);
	DDX_Text(pDX, IDC_EDIT_NFILES, m_nFiles);
	DDX_Text(pDX, IDC_EDIT_OUTPUTDIR, m_OutputDir);
	DDX_Radio(pDX, IDC_RDO_NFILES, m_nOutputtype);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CFileSplitPage, CPropertyPage)
	//{{AFX_MSG_MAP(CFileSplitPage)
	ON_BN_CLICKED(IDC_BTN_SELINPUTFILE, OnBtnSelinputfile)
	ON_BN_CLICKED(IDC_BTN_SELOUTPUTDIR, OnBtnSeloutputdir)
	ON_BN_CLICKED(IDC_BTN_STARTSPLIT, OnBtnStartsplit)
	ON_BN_CLICKED(IDC_RDO_FILESIZE, OnRdoFilesize)
	ON_BN_CLICKED(IDC_RDO_NFILES, OnRdoNfiles)
	//}}AFX_MSG_MAP
	ON_MESSAGE(WM_FS_UPDATESTATUS, OnUpdateStatus)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CFileSplitPage message handlers

void CFileSplitPage::OnBtnSelinputfile() 
{
	// TODO: Add your control notification handler code here
	CFileDialog dlg(TRUE);
	if(dlg.DoModal()==IDOK)
	{
		UpdateData(TRUE);
		m_InputFile = dlg.GetPathName();
		if(m_OutputDir.IsEmpty())
		{
			m_OutputDir = m_InputFile.Left(m_InputFile.GetLength()-dlg.GetFileName().GetLength());
		}
		UpdateData(FALSE);
	}
	
}

void CFileSplitPage::OnBtnSeloutputdir() 
{

	// TODO: Add your control notification handler code here
	LPMALLOC pMalloc;
	/* Gets the Shell's default allocator */
	if (::SHGetMalloc(&pMalloc) == NOERROR)
	{
		UpdateData(TRUE);
		BROWSEINFO bi;
		char pszBuffer[MAX_PATH];
		LPITEMIDLIST pidl;
		// Get help on BROWSEINFO struct - it's got all the bit settings.
		bi.hwndOwner = GetSafeHwnd();
		bi.pidlRoot = NULL;
		bi.pszDisplayName = pszBuffer;
		bi.lpszTitle = _T("Select Path For Output Files");
		bi.ulFlags = BIF_RETURNFSANCESTORS | BIF_RETURNONLYFSDIRS;
		bi.lpfn = NULL;
		bi.lParam = 0;
		// This next call issues the dialog box.
		if ((pidl = ::SHBrowseForFolder(&bi)) != NULL)
		{
			if (::SHGetPathFromIDList(pidl, pszBuffer))
			{ 
			// At this point pszBuffer contains the selected path */.
				m_OutputDir = pszBuffer;
				UpdateData(FALSE);
			}
			// Free the PIDL allocated by SHBrowseForFolder.
			pMalloc->Free(pidl);
		}
		// Release the shell's allocator.
		pMalloc->Release();
	}
	
}

void CFileSplitPage::OnBtnStartsplit() 
{
	// TODO: Add your control notification handler code here
	if(!m_bIsWorking)
	{
		if(UpdateData(TRUE))
		{
			//validate
			if(m_InputFile.IsEmpty())
				return;
			int nSplitVal = m_nOutputtype==0?m_nFiles:(m_nFileSize*1024);
			if(nSplitVal==0)
				return;
			SetDlgItemText(IDC_SP_STATIC,"Creating Output Files...");
			EnableAllControls(FALSE);
			GetDlgItem(IDC_BTN_STARTSPLIT)->SetWindowText("Cancel");
			m_pFS = (CFileSplit*)(AfxBeginThread(RUNTIME_CLASS(CFileSplit)));
			m_pFS->m_pMainWnd = this;//AfxGetMainWnd();
			m_pFS->m_InputFileName = m_InputFile;
			m_pFS->m_OutputDirName = m_OutputDir;
			m_pFS->m_nSplitMode    = m_nOutputtype;
			m_pFS->m_nSplitVal	   = nSplitVal;
			
			m_pFS->PostThreadMessage(WM_FS_START,0,0);
			m_bIsWorking = true;
		}
	}
	else
	{
		//Cancel...
		m_pFS->PostThreadMessage(WM_FS_CANCEL,0,0);
		EnableAllControls(TRUE);
	}
	
}

void CFileSplitPage::OnRdoFilesize() 
{
	// TODO: Add your control notification handler code here
	GetDlgItem(IDC_EDIT_FILESIZE)->EnableWindow(TRUE);
	GetDlgItem(IDC_EDIT_NFILES)->EnableWindow(FALSE);
	
}

void CFileSplitPage::OnRdoNfiles() 
{
	// TODO: Add your control notification handler code here
	GetDlgItem(IDC_EDIT_FILESIZE)->EnableWindow(FALSE);
	GetDlgItem(IDC_EDIT_NFILES)->EnableWindow(TRUE);
	
}

void CFileSplitPage::EnableAllControls(BOOL bEnable)
{
	GetDlgItem(IDC_EDIT_INPUTFILE)->EnableWindow(bEnable);
	GetDlgItem(IDC_EDIT_OUTPUTDIR)->EnableWindow(bEnable);
	GetDlgItem(IDC_BTN_SELINPUTFILE)->EnableWindow(bEnable);
	GetDlgItem(IDC_BTN_SELOUTPUTDIR)->EnableWindow(bEnable);
	GetDlgItem(IDC_RDO_NFILES    )->EnableWindow(bEnable);
	GetDlgItem(IDC_RDO_FILESIZE  )->EnableWindow(bEnable);
	GetDlgItem(IDC_EDIT_FILESIZE)->EnableWindow(m_nOutputtype!=0 && bEnable);
	GetDlgItem(IDC_EDIT_NFILES)->EnableWindow(m_nOutputtype==0 && bEnable);
}



void CFileSplitPage::OnUpdateStatus(WPARAM wP, LPARAM lP)
{
	if(lP==0)
		m_Progress.SetPos((int)wP);
	if(lP==-1)
	{
		m_Progress.SetPos(0);
	MessageBox("Error splitting file, please check filenames", "Error",MB_ICONERROR);

		EnableAllControls(TRUE);
		GetDlgItem(IDC_BTN_STARTSPLIT)->SetWindowText("Start");
		GetDlgItem(IDC_SP_STATIC)->SetWindowText("Done");
		m_bIsWorking = false;

	}
	if(lP==1)
	{
		m_Progress.SetPos(100);
		EnableAllControls(TRUE);
		GetDlgItem(IDC_BTN_STARTSPLIT)->SetWindowText("Start");
		GetDlgItem(IDC_SP_STATIC)->SetWindowText("Done");
		m_bIsWorking = false;
	}

}

BOOL CFileSplitPage::OnInitDialog() 
{
	CPropertyPage::OnInitDialog();
	HICON hIcon = AfxGetApp()->LoadIcon(IDI_ICON_FOPEN);
	m_SelFileBtn.SetIcon(hIcon);
	m_SelDirBtn.SetIcon(hIcon);
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Architect
United Kingdom United Kingdom
Has been programming for 7 years, mostly in Visual C++, C# .NET and occasionally in Java

Comments and Discussions