⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 loginfoview.cpp

📁 用于数据挖掘
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// LogInfoView.cpp : CLogInfoView 类的实现
//



#include "stdafx.h"
#include "LogInfo.h"

#include "LogInfoDoc.h"
#include "LogInfoView.h"
#include ".\loginfoview.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// CLogInfoView

IMPLEMENT_DYNCREATE(CLogInfoView, CListView)

BEGIN_MESSAGE_MAP(CLogInfoView, CListView)
	ON_COMMAND(ID_DataInput, OnDatainput)
	ON_COMMAND(ID_TestFile, OnTestfile)
	ON_COMMAND(ID_StringTest, OnStringtest)
	ON_COMMAND(ID_MOVE_NEXT, OnMoveNext)
	ON_COMMAND(ID_CreateUser, OnCreateuser)
	ON_COMMAND(ID_InputLog1, OnInputlog1)
	ON_COMMAND(ID_ModifyTime, OnModifytime)
	ON_COMMAND(ID_ModifyAddress, OnModifyaddress)
	ON_COMMAND(ID_User_Identify, OnUserIdentify)
	ON_COMMAND(ID_Delete_nUser, OnDeletenuser)
	ON_COMMAND(ID_Session_Identify, OnSessionIdentify)
	ON_COMMAND(ID_Identify_SameWeb, OnIdentifySameweb)
END_MESSAGE_MAP()


CString CLogInfoView::m_Column[]=
{
	"用户IP","用户登录的时间","连接端口","用户访问的网址",
		"用户数据1","用户数据2","用户访问的第二个网址","用户操作系统信息"
};

// CLogInfoView 构造/析构

CLogInfoView::CLogInfoView()
{
	// TODO: 在此处添加构造代码
	m_pUserSet.CreateInstance(__uuidof(Recordset));
	m_pUserSet1.CreateInstance(__uuidof(Recordset));
	m_pCommand.CreateInstance(__uuidof(Command));
	IntPageNo=0;

}

CLogInfoView::~CLogInfoView()
{
}

BOOL CLogInfoView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: 在此处通过修改 CREATESTRUCT cs 来修改窗口类或
	// 样式
	cs.style|=LVS_REPORT	|
		LVS_SINGLESEL		|
		LVS_SHOWSELALWAYS	|
		LVS_ICON;

	return CListView::PreCreateWindow(cs);
}

void CLogInfoView::OnInitialUpdate()
{
	CListView::OnInitialUpdate();
	
	CLogInfoDoc* pDoc=GetDocument();
	ASSERT_VALID(pDoc);
	
	CListCtrl &lc=GetListCtrl();
	int Column;
	LV_COLUMN LVisualColumn;

	DWORD dwStyle=ListView_GetExtendedListViewStyle(GetListCtrl());

	dwStyle |=LVS_EX_FULLROWSELECT |LVS_EX_GRIDLINES |
		LVS_EX_HEADERDRAGDROP | LVS_EX_TRACKSELECT;
	ListView_SetExtendedListViewStyle(GetListCtrl(),dwStyle);

	lc.DeleteAllItems();
	 //LVisualColumn.mask=LVCF_WIDTH|LVCF_FMT|LVCF_TEXT|LVCF_SUBITEM;
	
	for(Column=0;(Column<8);Column++)
	{
		CString HeaderString;

		LVisualColumn.iSubItem=Column;
		LVisualColumn.pszText=(LPTSTR)(LPCTSTR)m_Column[Column];
		LVisualColumn.cx=120;
		LVisualColumn.mask=LVCF_WIDTH|LVCF_TEXT;
		lc.InsertColumn(Column,&LVisualColumn);
	}
	pDoc->UpdateAllViews(NULL);
	// TODO: 调用 GetListCtrl() 直接访问 ListView 的列表控件,
	// 从而可以用项填充 ListView。
}


// CLogInfoView 诊断

#ifdef _DEBUG
void CLogInfoView::AssertValid() const
{
	CListView::AssertValid();
}

void CLogInfoView::Dump(CDumpContext& dc) const
{
	CListView::Dump(dc);
}

CLogInfoDoc* CLogInfoView::GetDocument() const // 非调试版本是内联的
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CLogInfoDoc)));
	return (CLogInfoDoc*)m_pDocument;
}
#endif //_DEBUG


// CLogInfoView 消息处理程序

void CLogInfoView::OnDatainput()
{
	// TODO: 在此添加命令处理程序代码
	CListCtrl& lc=GetListCtrl();
	try
	{
		CString str;
		//打开文件准备读
		CStdioFile File;
		File.Open("F:\\vc\\vc7\\weblog.txt",CFile::modeRead);
		//打开数据库,准备将记录写入数据库
		m_pUserSet->Open("select UserIP,UserLogTime,UserLogIPPort,UserVisitAddress1,UserData1,UserData2,UserVisitAddress2, UserWindowsInfo from TableLog",
			theApp.m_pConnection.GetInterfacePtr(),
			adOpenDynamic,
			adLockOptimistic,
			adCmdText);
		m_pFieldsPtr=m_pUserSet->Fields;

		CString strTemp[8];
		while(File.ReadString(str))
		{
			//解析字符串
			AnalysisString(str,strTemp);		
	
			//将读取出来的格式化后的记录写入数据库
			m_pUserSet->AddNew();
			m_pFieldPtr=m_pFieldsPtr->Item["UserIP"];
			if(strTemp[0].IsEmpty())
				strTemp[0]=" ";
			m_pFieldPtr->Value=(_variant_t)strTemp[0];

			m_pFieldPtr=m_pFieldsPtr->Item["UserLogTime"];
			if(strTemp[1].IsEmpty())
				strTemp[1]=" ";
			m_pFieldPtr->Value=(_variant_t)strTemp[1];

			m_pFieldPtr=m_pFieldsPtr->Item["UserLogIPPort"];
			if(strTemp[2].IsEmpty())
				strTemp[2]="0";
			m_pFieldPtr->Value=(_variant_t)strTemp[2];

			m_pFieldPtr=m_pFieldsPtr->Item["UserVisitAddress1"];
			if(strTemp[3].IsEmpty())
				strTemp[3]=" ";
			m_pFieldPtr->Value=(_variant_t)strTemp[3];

			m_pFieldPtr=m_pFieldsPtr->Item["UserData1"];
			if(strTemp[4].IsEmpty())
				strTemp[4]="0";
			m_pFieldPtr->Value=(_variant_t)strTemp[4];

			m_pFieldPtr=m_pFieldsPtr->Item["UserData2"];
			if(strTemp[5].IsEmpty())
				strTemp[5]="0";
			m_pFieldPtr->Value=(_variant_t)strTemp[5];

			m_pFieldPtr=m_pFieldsPtr->Item["UserVisitAddress2"];
			if(strTemp[6].IsEmpty())
				strTemp[6]=" ";
			m_pFieldPtr->Value=(_variant_t)strTemp[6];

			m_pFieldPtr=m_pFieldsPtr->Item["UserWindowsInfo"];
			if(strTemp[7].IsEmpty())
				strTemp[7]=" ";
			m_pFieldPtr->Value=(_variant_t)strTemp[7];
			m_pUserSet->Update();

		}//end while

		m_pUserSet->Update();
		m_pUserSet->Close();
		
		GetDocument()->UpdateAllViews(NULL);

		AfxMessageBox("记录添加成功!");

	}
	catch(_com_error *e)
	{
		AfxMessageBox(e->ErrorMessage());
	}
	catch(...)
	{
		AfxMessageBox("访问数据服务器时发生异常.\n请与系统管理员联系,谢谢使用.");
		return;
	}
		
}

void CLogInfoView::OnUpdate(CView* /*pSender*/, LPARAM /*lHint*/, CObject* /*pHint*/)
{
	// TODO: 在此添加专用代码和/或调用基类
	ShowDataInList(IntPageNo);
	//int i=100;
	//CString x;
	//x.Format("%d",i);
	//AfxMessageBox(x);

}
void CLogInfoView::ShowDataInList(int PageNo)
{

	CListCtrl &lc=GetListCtrl();
	lc.DeleteAllItems();
	try
	{
		int index=0;
		m_pUserSet->Open("select UserIP,UserLogTime,UserLogIPPort,UserVisitAddress1,UserData1,UserData2,UserVisitAddress2, UserWindowsInfo from TableLog",
			theApp.m_pConnection.GetInterfacePtr(),
			adOpenDynamic,
			adLockOptimistic,
			adCmdText);
		int i=0;
		m_pUserSet->Move(200*PageNo);
		MaxPageNo=m_pUserSet->GetRecordCount()/200;
		while(!m_pUserSet->adoEOF && i<200)
		{
			i++;
			m_pFieldsPtr=m_pUserSet->Fields;
			m_pFieldPtr=m_pFieldsPtr->Item["UserIP"];
			CString str="";
			if(m_pFieldPtr->Value.vt==VT_NULL) str="";
			else
				str.Format((char*)_bstr_t(m_pFieldPtr->Value));
			int itemIndex=lc.InsertItem(LVIF_TEXT|LVIF_PARAM,index,str,0,0,0,0);

			int SubitemIndex;

			CString m_Column1[8]={"UserIP","UserLogTime","UserLogIPPort",
				"UserVisitAddress1",
				"UserData1","UserData2",
				"UserVisitAddress2", "UserWindowsInfo"};


			for(SubitemIndex=1;SubitemIndex<8;SubitemIndex++)
			{
				m_pFieldPtr=
					m_pFieldsPtr->Item[_variant_t(m_Column1[SubitemIndex])];
				if(m_pFieldPtr->Value.vt==VT_NULL)str="";
				else str.Format((char*)_bstr_t(m_pFieldPtr->Value));

				lc.SetItem(itemIndex,SubitemIndex,LVIF_TEXT,str,0,0,0,0);
			}
			m_pUserSet->MoveNext();
			index++;
		}
		m_pUserSet->Close();
        
	}
	catch(_com_error *e)
	{
		AfxMessageBox(e->ErrorMessage());
	}
	catch(...)
	{
		AfxMessageBox("访问数据服务器时发生异常.\n请与系统管理员联系,谢谢使用.");
		return;
	}
}

void CLogInfoView::OnTestfile()
{
	// TODO: 在此添加命令处理程序代码
	try
	{
		/////*FileStream * MyFile=new FileStream("weblog.txt",FileMode::Open,FileAccess::Read);
		////StreamReader * In=new StreamReader(MyFile);
		////String str=In->ReadLine();
		////In->Close();*/
		CString str;
		CStdioFile File;
		File.Open("F:\\vc\\vc7\\weblog.txt",CFile::modeRead);
		File.ReadString(str);
		AfxMessageBox(str);
		File.Close();
	}
	catch(...)
	{
		AfxMessageBox("文件读取错误!");
	}

}
void CLogInfoView::AnalysisString(CString _str,CString _strTemp[])
{
	int pos=-1;
	//while((pos=_str.Find("1",0))!=-1)
	//{
	//	AfxMessageBox(_str.Left(pos));
	//	_str=_str.Right(_str.GetLength()-pos-1);
	//	AfxMessageBox(_str);
	//}
	//得到IP
	pos=_str.Find(" ",0);
	_strTemp[0]=_str.Left(pos);
	_str=_str.Right(_str.GetLength()-pos-1);
	//得到时间
	pos=_str.Find("[",0);
	_str=_str.Right(_str.GetLength()-pos-1);
	pos=_str.Find("+",0);
	_strTemp[1]=_str.Left(pos);
	//得到端口号
	_str=_str.Right(_str.GetLength()-pos-1);
	pos=_str.Find("]",0);
	_strTemp[2]=_str.Left(pos);
	//得到第一个地址
	_str=_str.Right(_str.GetLength()-pos-1);
	pos=_str.Find("h",0);
	pos--;
	_str=_str.Right(_str.GetLength()-pos-1);
	pos=_str.Find("\"",0);
	_strTemp[3]=_str.Left(pos);
	//得到第一个数据
	_str=_str.Right(_str.GetLength()-pos-2);
	pos=_str.Find(" ",0);
	_strTemp[4]=_str.Left(pos);
	//得到第二个数据
	_str=_str.Right(_str.GetLength()-pos-1);
	pos=_str.Find(" ",0);
	_strTemp[5]=_str.Left(pos);
	//得到第二个地址
	_str=_str.Right(_str.GetLength()-pos-1);
	if(_str.GetAt(1)=='-')
	{
		_strTemp[6]=" ";
		pos=_str.Find("\"",3);
		_str=_str.Right(_str.GetLength()-pos-1);
		pos=_str.Find("\"",0);
		_strTemp[7]=_str.Left(pos);
	}
	else
	{
		_str=_str.Right(_str.GetLength()-1);
		pos=_str.Find("\"",0);
		_strTemp[6]=_str.Left(pos);
		_str=_str.Right(_str.GetLength()-pos-3);
		pos=_str.Find("\"",0);
		_strTemp[7]=_str.Left(pos);
	}
}

void CLogInfoView::OnStringtest()
{
	// TODO: 在此添加命令处理程序代码
	try
	{
		//m_pUserSet->Open("select UserIP,UserLogTime,UserLogIPPort,UserVisitAddress1,UserData1,UserData2,UserVisitAddress2, UserWindowsInfo from TableLog",
		//	theApp.m_pConnection.GetInterfacePtr(),
		//	adOpenDynamic,
		//	adLockOptimistic,
		//	adCmdText);
		//m_pFieldsPtr=m_pUserSet->Fields;
		m_pUserSet->Open("select UserVisitAddress1,UserVisitAddress2 from TableLog",
			theApp.m_pConnection.GetInterfacePtr(),
			adOpenDynamic,
			adLockOptimistic,
			adCmdText);
		m_pFieldsPtr=m_pUserSet->Fields;
		int i=0;
		CString str1,str2;
		m_pUserSet->MoveFirst();
		str1.Format("%d",m_pUserSet->GetRecordCount());
		AfxMessageBox(str1);
		while(!m_pUserSet->adoEOF)
		{
			//i=false;
			m_pFieldPtr=m_pFieldsPtr->Item["UserVisitAddress1"];
			str1=m_pFieldPtr->Value;
			if(str1.GetLength()>255)
				i++;
			m_pFieldPtr=m_pFieldsPtr->Item["UserVisitAddress2"];
			str2=m_pFieldPtr->Value;
			if(str2.GetLength()>255)
				i++;
			//if(i==false)
			//{
				m_pUserSet->MoveNext();
		//	}
		//	else
		//	{
		//		CString strSql="delete from TableLog where UserVisitAddress1='"
		//			+ str1 + "' and UserVisitAddress2='" + str2 + "'";
		//		m_pCommand->ActiveConnection=theApp.m_pConnection;
		//		m_pCommand->CommandText=(_bstr_t)strSql;
		//		m_pCommand->CommandType=adCmdText;
		//		m_pCommand->CommandTimeout=15;
		//		m_pCommand->Execute(NULL,NULL,adCmdText);
		//		//m_pUserSet->Delete();
		//		//m_pUserSet->Update();
		//	}
		}
		//m_pUserSet->Update();
		m_pUserSet->Close();
		str1.Format("%d",i);
		AfxMessageBox(str1);

	}
	catch(...)
	{
		AfxMessageBox("读取数据库时发生错误");
	}

}

void CLogInfoView::OnMoveNext()
{
	// TODO: 在此添加命令处理程序代码
	IntPageNo++;
	if(IntPageNo<=MaxPageNo)
		ShowDataInList(IntPageNo);
	else

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -