All pastes #613494 Raw Edit

Someone

public text v1 · immutable
·published 2007-07-11 00:35 UTC
rendered paste body
######### FUNCTION THAT CALLS THE DIALOG BOX #########

void CCompilerDlg::OnBnClickedExtractbyZip()
{
	ExtractbyZip dlg;
	dlg.DoModal();
}

######### .H FILE #########


#pragma once

// ExtractbyZip dialog

class ExtractbyZip : public CDialog
{
	DECLARE_DYNAMIC(ExtractbyZip)

public:
	ExtractbyZip(CWnd* pParent = NULL);   // standard constructor
	virtual ~ExtractbyZip();

// Dialog Data
	enum { IDD = IDD_EXTRACTBYZIP };
	//}}AFX_DATA
	CListBox	m_List;

protected:
	DWORD m_ListStyle;
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
public:
	virtual BOOL PreTranslateMessage(MSG* pMsg);
	DECLARE_MESSAGE_MAP()
public:
	afx_msg void OnBnClickedLoadinputfile();
public:
	afx_msg void OnBnClickedLoadzipfile();
public:
	afx_msg void OnBnClickedQuit();
public:
	afx_msg void OnBnClickedAddzip();
public:
	afx_msg void OnBnClickedDeletezip();
public:
	afx_msg void OnBnClickedSavezipfile();
};




######### .CPP FILE BELOW ###########


#pragma once

// ExtractbyZip dialog

class ExtractbyZip : public CDialog
{
	DECLARE_DYNAMIC(ExtractbyZip)

public:
	ExtractbyZip(CWnd* pParent = NULL);   // standard constructor
	virtual ~ExtractbyZip();

// Dialog Data
	enum { IDD = IDD_EXTRACTBYZIP };
	//}}AFX_DATA
	CListBox	m_List;

protected:
	DWORD m_ListStyle;
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
public:
	virtual BOOL PreTranslateMessage(MSG* pMsg);
	DECLARE_MESSAGE_MAP()
public:
	afx_msg void OnBnClickedLoadinputfile();
public:
	afx_msg void OnBnClickedLoadzipfile();
public:
	afx_msg void OnBnClickedQuit();
public:
	afx_msg void OnBnClickedAddzip();
public:
	afx_msg void OnBnClickedDeletezip();
public:
	afx_msg void OnBnClickedSavezipfile();
};




######### .CPP FILE #########



// ExtractbyZip.cpp : implementation file
//

#include "stdafx.h"
#include "Compiler.h"
#include "ExtractbyZip.h"
#include "Purge.h"

CString ZipCodeFile;
CString ZipOutputFile;

CString SimplePickOpenFile(CWnd *pWnd, LPCTSTR pszTitle, LPCTSTR pszFilter);
int VerifyInputFile(const char *File);
int validnumber(const char *string);
int VerifyZipcodeFile(const char *File);
char *strip_invalid_chars(char *string);
CString SimplePickSaveFile(CWnd *pWnd, LPCTSTR pszTitle);

// ExtractbyZip dialog

IMPLEMENT_DYNAMIC(ExtractbyZip, CDialog)

ExtractbyZip::ExtractbyZip(CWnd* pParent /*=NULL*/)
	: CDialog(ExtractbyZip::IDD, pParent)
{

}

ExtractbyZip::~ExtractbyZip()
{
}

void ExtractbyZip::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_ZIPCODES, m_List);
}

BEGIN_MESSAGE_MAP(ExtractbyZip, CDialog)
	ON_BN_CLICKED(IDC_LOADINPUTFILE, &ExtractbyZip::OnBnClickedLoadinputfile)
	ON_BN_CLICKED(IDC_LOADZIPFILE, &ExtractbyZip::OnBnClickedLoadzipfile)
	ON_BN_CLICKED(IDC_QUIT, &ExtractbyZip::OnBnClickedQuit)
	ON_BN_CLICKED(IDC_ADDZIP, &ExtractbyZip::OnBnClickedAddzip)
	ON_BN_CLICKED(IDC_DELETEZIP, &ExtractbyZip::OnBnClickedDeletezip)
	ON_BN_CLICKED(IDC_SAVEZIPFILE, &ExtractbyZip::OnBnClickedSavezipfile)
END_MESSAGE_MAP()


BOOL ExtractbyZip::PreTranslateMessage(MSG* pMsg) 
{
	int idCtrl= this->GetFocus()->GetDlgCtrlID();  
	if(idCtrl == IDC_INPUTZIP && pMsg->message == WM_KEYDOWN && pMsg->wParam == VK_RETURN)
	{
		OnBnClickedAddzip();
		return TRUE;
	} else {
		return CDialog::PreTranslateMessage(pMsg);
	}
}

// ExtractbyZip message handlers

void ExtractbyZip::OnBnClickedLoadinputfile()
{
	CString File;
	LPCTSTR filter = _T("Output Files (*.output)\0*.output\0");

	File = SimplePickOpenFile(NULL, NULL, filter);

	if (File != "" && VerifyInputFile(File))
	{
		if (m_List.GetCount() > 0) {
			GetDlgItem(IDC_START)->EnableWindow(TRUE);
		}

		ZipOutputFile = File;
		SetDlgItemText(IDC_LOADFILE, File);
	} else {
		SetDlgItemText(IDC_LOADFILE, NULL);
		GetDlgItem(IDC_START)->EnableWindow(FALSE);
	}
}

int VerifyZipcodeFile(const char *File)
{
	CString error, temp;

	const int BUFSIZE = 1024;
	char buf[BUFSIZE];

	FILE *zfile;

	fopen_s(&zfile, File, "r");

	if (zfile == NULL)
	{
		error.Format("ERROR: Unable to open %s.", File);
		AfxMessageBox(error);
		return 0;
	}

	while (fgets(buf, BUFSIZE, zfile))
	{
		strcpy_s(buf, strip_invalid_chars(buf));
		if (!validnumber(buf)) {
			error.Format("ERROR: The zip code file you selected isn't a valid zip code file!");
			AfxMessageBox(error);
			fclose(zfile);
			return 0;
		}
	}	
	fclose(zfile);
	return 1;
}

void ExtractbyZip::OnBnClickedLoadzipfile()
{
	CString File, AddrFile;
	LPCTSTR filter = _T("Zip Code Files (*.zipcode)\0*.zipcode\0");
	const int BUFSIZE = 1024;
	char buf[BUFSIZE];
	FILE *zfile;

	File = SimplePickOpenFile(NULL, NULL, filter);

	if (File != "" && VerifyZipcodeFile(File)) {
		SetDlgItemText(IDC_ZIPCODEFILE, File);
		m_List.ResetContent();
		ZipCodeFile = File;

		fopen_s(&zfile, File, "r");
		while (fgets(buf, BUFSIZE, zfile)) {
			strcpy_s(buf, strip_invalid_chars(buf));
			m_List.AddString(buf);
		}
		fclose(zfile);

		if (m_List.GetCount() > 0) {
			GetDlgItemText(IDC_ADDRFILE, AddrFile);

			if (AddrFile != "")
				GetDlgItem(IDC_START)->EnableWindow(TRUE);

			GetDlgItem(IDC_SAVEZIPFILE)->EnableWindow(TRUE);
		} else {
			GetDlgItem(IDC_START)->EnableWindow(FALSE);
			GetDlgItem(IDC_SAVEZIPFILE)->EnableWindow(FALSE);
		}

	} else {
		SetDlgItemText(IDC_ZIPCODEFILE, NULL);
	}
}

void ExtractbyZip::OnBnClickedQuit()
{
	EndDialog(IDD_EXTRACTBYZIP);
}

void ExtractbyZip::OnBnClickedAddzip()
{
	CString input, AddrFile;

	GetDlgItemText(IDC_INPUTZIP, input);

	if (input == "") {
		AfxMessageBox("You haven't input a zip code!");
	} else {
		if (!validnumber(input) || (strlen(input) != 5)) {
			AfxMessageBox("You have input an invalid zip code.");
		} else if (!m_List.FindString(-1, input)) {
			AfxMessageBox("Zip code already exists!");
		} else {
			GetDlgItemText(IDC_ADDRFILE, AddrFile);
			m_List.AddString(input);

			if (m_List.GetCount() > 0 && AddrFile != "")
				GetDlgItem(IDC_START)->EnableWindow(TRUE);

			SetDlgItemText(IDC_INPUTZIP, NULL);
			GetDlgItem(IDC_SAVEZIPFILE)->EnableWindow(TRUE);
		}
	}
}
void ExtractbyZip::OnBnClickedDeletezip()
{
	int idx = m_List.GetCurSel();

	m_List.DeleteString(idx);

	if (m_List.GetCount() == 0) {
		GetDlgItem(IDC_SAVEZIPFILE)->EnableWindow(FALSE);
		GetDlgItem(IDC_START)->EnableWindow(FALSE);
	}
}

void ExtractbyZip::OnBnClickedSavezipfile()
{
	CString error, tmp;
	FILE *zfile;
	int n;

	if (ZipOutputFile == "") ZipOutputFile = SimplePickSaveFile(NULL, NULL);
	if (ZipOutputFile == "") {
		AfxMessageBox("Warning: No file was selected; zip code file has NOT been saved!");
		return;
	}

	fopen_s(&zfile, ZipOutputFile, "wb");

	if (zfile == NULL)
	{
		error.Format("ERROR: Unable to open %s.", ZipOutputFile);
		AfxMessageBox(error);
		return;
	}

	for (int i=0; i < m_List.GetCount(); i++)
	{
		n = m_List.GetTextLen(i);
		m_List.GetText(i, tmp.GetBuffer(n));
		fprintf(zfile, "%s\r\n", tmp);
		tmp.ReleaseBuffer();
	}
	fclose(zfile);
	SetDlgItemText(IDC_ZIPCODEFILE, ZipOutputFile);
	AfxMessageBox("Zip code list has been successfully saved to file!");
}


CString SimplePickSaveFile(CWnd *pWnd, LPCTSTR pszTitle)
{
	OPENFILENAME ofn;

	TCHAR pszFile[MAX_PATH*2];

	strcpy_s(pszFile, _T(""));
	memset( &ofn, 0, sizeof(ofn) );

	ofn.lStructSize = sizeof(ofn);
	ofn.hwndOwner = pWnd ? pWnd->m_hWnd : GetActiveWindow();
	ofn.lpstrFile = pszFile;
	ofn.lpstrDefExt = "zipcode";
	ofn.nMaxFile = sizeof(pszFile);
	ofn.Flags = OFN_HIDEREADONLY | OFN_NONETWORKBUTTON | OFN_FILEMUSTEXIST;
	ofn.lpstrFilter = _T("Zipcode Output Files (*.zipcode)\0*.zipcode");
	ofn.lpstrTitle = pszTitle ? pszTitle : _T("Please select a file");

	if (GetSaveFileName( &ofn ))
		return pszFile;
	else
		return CString();
}