www.pudn.com > MINSortDict.zip > MINSortDictDlg.cpp


//************************************************************************* 
// You are free to use/modify this code but leave this header intact. 
// This class is public domain so you are free to use it any of 
// your applications (Freeware,Shareware,Commercial). All I ask is 
// that you let me know so that if you have a real winner I can 
// brag to my buddies that some of my code is in your app. I also 
// wouldn't mind if you sent me a copy of your application since I 
// like to play with new stuff. 
// Author	: DingZhiGang 
// Email	: minidxer@gmail.com 
// HomePage : http://minidx.com 
// Help     : http://minidx.com/forum/ 
//************************************************************************* 
 
#include "stdafx.h" 
#include "MINSortDict.h" 
#include "MINSortDictDlg.h" 
#include "MinidxGUICom.h" 
#include "CppSQLite3.h" 
 
#ifdef _DEBUG 
#define new DEBUG_NEW 
#endif 
 
 
// CAboutDlg dialog used for App About 
 
class CAboutDlg : public CDialog 
{ 
public: 
	CAboutDlg(); 
 
// Dialog Data 
	enum { IDD = IDD_ABOUTBOX }; 
 
	protected: 
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support 
 
// Implementation 
protected: 
	DECLARE_MESSAGE_MAP() 
}; 
 
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD) 
{ 
} 
 
void CAboutDlg::DoDataExchange(CDataExchange* pDX) 
{ 
	CDialog::DoDataExchange(pDX); 
} 
 
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog) 
END_MESSAGE_MAP() 
 
 
// CMINSortDictDlg dialog 
 
 
 
 
CMINSortDictDlg::CMINSortDictDlg(CWnd* pParent /*=NULL*/) 
	: CDialog(CMINSortDictDlg::IDD, pParent) 
{ 
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); 
} 
 
void CMINSortDictDlg::DoDataExchange(CDataExchange* pDX) 
{ 
	CDialog::DoDataExchange(pDX); 
	DDX_Control(pDX, IDC_EDIT_DICT_INPUT, m_edit_input); 
	DDX_Control(pDX, IDC_EDIT_DICT_OUTPUT, m_edit_output); 
	DDX_Control(pDX, IDC_BUTTON_BROWSE_INPUT, m_btn_input_br); 
	DDX_Control(pDX, IDC_BUTTON_BROWSE_OUTPUT, m_btn_output_br); 
	DDX_Control(pDX, IDC_BUTTON_INPUT, m_btn_run_input); 
	DDX_Control(pDX, IDC_BUTTON_INIT, m_btn_init); 
} 
 
BEGIN_MESSAGE_MAP(CMINSortDictDlg, CDialog) 
	ON_WM_SYSCOMMAND() 
	ON_WM_PAINT() 
	ON_WM_QUERYDRAGICON() 
	//}}AFX_MSG_MAP 
	ON_BN_CLICKED(IDC_BUTTON_BROWSE_INPUT, &CMINSortDictDlg::OnBnClickedButtonBrowseInput) 
	ON_BN_CLICKED(IDC_BUTTON_BROWSE_OUTPUT, &CMINSortDictDlg::OnBnClickedButtonBrowseOutput) 
	ON_BN_CLICKED(IDC_BUTTON_INPUT, &CMINSortDictDlg::OnBnClickedButtonInput) 
	ON_BN_CLICKED(IDC_BUTTON_OUTPUT, &CMINSortDictDlg::OnBnClickedButtonOutput) 
	ON_BN_CLICKED(IDC_BUTTON_INIT, &CMINSortDictDlg::OnBnClickedButtonInit) 
END_MESSAGE_MAP() 
 
/// get database path 
static CString get_db_path() 
{ 
	CString   dbPath, appPath;   
	GetModuleFileName(NULL, appPath.GetBuffer(MAX_PATH), MAX_PATH);   
	appPath.ReleaseBuffer();   
	int n = appPath.ReverseFind('\\');   
	appPath = appPath.Left(n);   
	TCHAR c = appPath.GetAt(n-1);   
	if(c != '\\') appPath += "\\"; 
	dbPath = appPath + _T("db.dat"); 
 
	return dbPath; 
} 
 
// init database  
static void init_db(void) 
{ 
	CString   dbPath = get_db_path(); 
 
	if(_taccess(dbPath, 0)) 
	{ 
		CppSQLite3DB db; 
		db.open(dbPath); 
		char* sql = "CREATE TABLE M_TABLE(M_T_DATA TEXT);"; 
		db.execDML(sql); 
	} 
} 
 
static bool isExistWord(CppSQLite3DB *db, const char *iWord) 
{ 
	//CppSQLite3DB db; 
	//db.open(get_db_path()); 
	char sql[200] = "select M_T_DATA from M_TABLE WHERE M_T_DATA='"; 
	::strcat(sql, iWord); 
	::strcat(sql, "');"); 
	CppSQLite3Query q = db->execQuery(sql); 
	while (!q.eof()){ 
		return true; 
	} 
	return false; 
} 
 
// CMINSortDictDlg message handlers 
 
BOOL CMINSortDictDlg::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 
 
	// initialization database 
	init_db(); 
 
	CString   inPath, outPath, appPath;   
	GetModuleFileName(NULL, appPath.GetBuffer(MAX_PATH), MAX_PATH);   
	appPath.ReleaseBuffer();   
	int n = appPath.ReverseFind('\\');   
	appPath = appPath.Left(n);   
	TCHAR c = appPath.GetAt(n-1);   
	if(c != '\\') appPath += "\\"; 
	inPath = appPath + _T("src.txt"); 
	outPath = appPath + _T("result.txt"); 
 
	SetDlgItemText(IDC_EDIT_DICT_INPUT, inPath.GetBuffer()); 
	SetDlgItemText(IDC_EDIT_DICT_OUTPUT, outPath.GetBuffer()); 
 
	return TRUE;  // return TRUE  unless you set the focus to a control 
} 
 
void CMINSortDictDlg::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 CMINSortDictDlg::OnPaint() 
{ 
	if (IsIconic()) 
	{ 
		CPaintDC dc(this); // device context for painting 
 
		SendMessage(WM_ICONERASEBKGND, reinterpret_cast(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 function to obtain the cursor to display while the user drags 
//  the minimized window. 
HCURSOR CMINSortDictDlg::OnQueryDragIcon() 
{ 
	return static_cast(m_hIcon); 
} 
 
 
void CMINSortDictDlg::OnBnClickedButtonBrowseInput() 
{ 
	// select input file dialog 
	CFileDialog dlg(TRUE, NULL, NULL, OFN_FILEMUSTEXIST,  
		_T("All Files (*.*)|*.*||"), this); 
 
	if (dlg.DoModal() == IDOK)  
	{ 
		m_str_input = dlg.GetPathName();		 
		m_edit_input.SetWindowText(m_str_input); 
	} 
} 
 
void CMINSortDictDlg::OnBnClickedButtonBrowseOutput() 
{ 
	// select output file dialog 
	CFileDialog dlg(TRUE, NULL, NULL, OFN_FILEMUSTEXIST,  
		_T("All Files (*.*)|*.*||"), this); 
 
	if (dlg.DoModal() == IDOK)  
	{ 
		m_str_output = dlg.GetPathName();		 
		m_edit_output.SetWindowText(m_str_output); 
	} 
} 
 
 
/// input data 
void CMINSortDictDlg::OnBnClickedButtonInput() 
{ 
	GetDlgItemText(IDC_EDIT_DICT_INPUT, m_str_input); 
	 
	if ( m_str_input.IsEmpty() ) 
	{ 
		MessageBox(_T("Please input source file name.")); 
		return; 
	} 
	 
	FILE * f = _tfopen(m_str_input, _T("rb")); 
	if (!f) 
	{ 
		//str.Format(_T("Cannot find %s"), m_str_input); 
		//return str; 
	} 
 
	char szBuf[300];		// line buffer 
 
	CppSQLite3DB db; 
 
	db.open(get_db_path().GetBuffer()); 
	 
	//db.execDML("BEGIN;"); 
 
	long count = 0; 
 
	//while (_fgetts(szBuf, sizeof(szBuf)/sizeof(TCHAR)-2, f)) 
	while (fgets(szBuf, sizeof(szBuf)/sizeof(char)-1, f)) 
	{ 
		bool isExist = false; 
		char s[200] = "select * from M_TABLE WHERE M_T_DATA='"; 
		::strcat(s, szBuf); 
		::strcat(s, "'"); 
		s[strlen(s)] = 0; 
		CppSQLite3Query q = db.execQuery(s); 
		while (!q.eof()){ 
			isExist = true; 
			break; 
		} 
 
		// check exist 
		if( isExist == false ) 
		{ 
 
			// if not exist,add it 
			char *str = "INSERT INTO M_TABLE VALUES('"; 
			char *sql = new char[800]; sql[0] = 0; 
			// ufv-->u''v 
			char *str_content = CMinidxGUICom::st_replace(szBuf); 
			::strcat(sql, str); 
			::strcat(sql, str_content); 
			::strcat(sql, "');"); 
			db.execDML(sql); 
			if(str_content) delete[]str_content; str_content = NULL; 
			if(sql) delete[]sql; sql = NULL; 
			count++; 
		} 
	} 
 
	 
	db.execDML("COMMIT;"); 
 
	fclose(f); 
 
	CString msg; 
	msg.Format(_T(" Import data success! [%i]"), count); 
	MessageBox(msg); 
	 
	//OnOK(); 
} 
 
/// output data 
void CMINSortDictDlg::OnBnClickedButtonOutput() 
{ 
	GetDlgItemText(IDC_EDIT_DICT_OUTPUT, m_str_output); 
 
	if ( m_str_output.IsEmpty() ) 
	{ 
		MessageBox(_T("Please input target file name.")); 
		return; 
	} 
 
	FILE * f = _tfopen(m_str_output, _T("wb")); 
	if (!f) 
	{ 
		//str.Format(_T("Cannot find %s"), m_str_input); 
		//return str; 
	} 
	// get data froom db 
	CppSQLite3DB db; 
	db.open(get_db_path().GetBuffer()); 
	char *sql = "select M_T_DATA from M_TABLE ORDER BY M_T_DATA;"; 
	CppSQLite3Query q = db.execQuery(sql); 
	long count = 0; 
	while (!q.eof()){ 
		fprintf(f, "%s", q.fieldValue(0)); 
		q.nextRow(); 
		count++; 
	} 
 
	fclose(f); 
 
	CString msg; 
	//d   int   Signed   decimal   integer.     
	//i   int   Signed   decimal   integer.     
	//o   int   Unsigned   octal   integer.     
	//u   int   Unsigned   decimal   integer.     
	//x   int   Unsigned   hexadecimal   integer,   using   gabcdef.h     
	//X   int   Unsigned   hexadecimal   integer,   using   gABCDEF.h  
	msg.Format(_T(" Export data success! [%i]"), count); 
	MessageBox(msg); 
} 
 
/// remove all data 
void CMINSortDictDlg::OnBnClickedButtonInit() 
{ 
	CString   dbPath = get_db_path(); 
 
	if(_taccess(dbPath, 0)) 
	{ 
		CppSQLite3DB db; 
		db.open(dbPath); 
		char* sql = "DELETE * FROM TABLE;"; 
		db.execDML(sql); 
	} 
	CString msg; 
	msg.Format(_T(" Init database success!")); 
	MessageBox(msg); 
}