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

📄 tollqry.cpp

📁 计费查询程序
💻 CPP
字号:
// TOLLQRY.CPP - Implementation file for your Internet Server
//    Tollqry Extension

#include "stdafx.h"
#include "Tollqry.h"

#include "tolldef.h"
#include "tollerinfo.h"

///////////////////////////////////////////////////////////////////////
// The one and only CWinApp object
// NOTE: You may remove this object if you alter your project to no
// longer use MFC in a DLL.

CWinApp theApp;

static const TCHAR szHTMLType[] = _T("HTML");

///////////////////////////////////////////////////////////////////////
// command-parsing map

BEGIN_PARSE_MAP(CTollqryExtension, CHttpServer)
	// TODO: insert your ON_PARSE_COMMAND() and 
	// ON_PARSE_COMMAND_PARAMS() here to hook up your commands.
	// For example:
	ON_PARSE_COMMAND(DoQuery,CTollqryExtension,ITS_PSTR)
	ON_PARSE_COMMAND_PARAMS("loginname")
	ON_PARSE_COMMAND(Default, CTollqryExtension, ITS_EMPTY)
	DEFAULT_PARSE_COMMAND(Default, CTollqryExtension)
END_PARSE_MAP(CTollqryExtension)


///////////////////////////////////////////////////////////////////////
// The one and only CTollqryExtension object

CTollqryExtension theExtension;


///////////////////////////////////////////////////////////////////////
// CTollqryExtension implementation

CTollqryExtension::CTollqryExtension()
{
	sDebugInfo=_T("");

	m_bDatabaseUpdated=FALSE;

	LoadSpecialUserList();

	LoadTollerDatabase();
}

CTollqryExtension::~CTollqryExtension()
{
	FreeTollerList();

	SpecialUserList.RemoveAll();
}

//get toller begin date and toll costperKB from tollset.dat file.
CString CTollqryExtension::GetTollerBeginDate()
{
	CString sDate("");
	CTime time=CTime::GetCurrentTime();

	int nTollFromDate,nHour;
	int nMonth;
/*
	nTollFromDate=::GetPrivateProfileInt("System",
		"TollFromDate",1,"toller.ini");
*/
	CFile file;
	if(!file.Open("c:\\toller\\tollset.dat",CFile::modeRead))
	{
		//error read the date file.	
		nTollFromDate=9;
		nCostPerKB=10;
	}
	else
	{
		int nBytes=file.Read(&nTollFromDate,sizeof(int));
		/*if(nBytes==0)
			nTollFromDate=9;
		*/
		nBytes=file.Read(&nCostPerKB,sizeof(int));
		/*if(nBytes==0)
			nCostPerKB=10;
		*/
		file.Close();
	}
/*
	nHour=::GetPrivateProfileInt("System",
		"UpdateAtHour",5,"toller.ini");
*/
	int nDate=time.GetDay();
	nMonth=time.GetMonth();

	if(nDate<nTollFromDate)
	{
		if(nMonth==1)
			nMonth=12;//it is last year
		else
			nMonth--;
	}

	sDate.Format("%d月%d日",nMonth,nTollFromDate);

	return sDate;
}

//special list API
void	CTollqryExtension::InitSpecialUserList()
{
	SpecialUserList.RemoveAll();
}


void	CTollqryExtension::LoadSpecialUserList()
{
	CFile TheFile;
	
	if(!TheFile.Open("c:\\toller\\specuser.dat",CFile::modeRead))
	{
	//	TheFile.Close();
	//	AfxMessageBox("Unable to open SpecialUser file");
		MessageBeep(0);
		return;
	}
	
	//cycle read all the chars from the tab-seperate username into
	//the SpecialUser List
	
	SpecialUserList.RemoveAll();

	char ch;
	int  nBytesRead;
	CString item("");
	
	while(TRUE)
	{
		nBytesRead=TheFile.Read(&ch,1);
		if(nBytesRead==0)break;

		if((ch!='\n'))//line end with \n
		{
			if(ch=='\r')//bypass '\r'
				continue;
			item+=ch;
		}
		else
		{
			SpecialUserList.AddTail(CString(item));
			item="";
		}
	}

	TheFile.Close();
}

//
//Is the user a special user?
//
BOOL	CTollqryExtension::IsSpecialUser(CString loginName)
{
	CString item;
	POSITION pos;
	
	pos=SpecialUserList.GetHeadPosition();
	if(pos==NULL)
	{
		return FALSE;
	}

	/*
	item=SpecialUserList.GetAt(pos);
	if(item==loginName)return TRUE;
	*/

	while(pos!=NULL)
	{
		item=SpecialUserList.GetNext(pos);
		if(item==loginName)return TRUE;//find the loginname in
		//the special user list
	}	

	//traverse all the list items and not find the loginName
	return FALSE;
}

void CTollqryExtension::DoQuery(CHttpServerContext* pCtxt, LPCTSTR pszName)
{
	StartContent(pCtxt);
	WritePageTitle(pCtxt);

	CString sLoginName(pszName);
	CTollerInfo tollerInfo;

	//check if it is 5:00 now.If it is,then the database has updated,so we need to update
	//the database.
/*
	CTime time=CTime::GetCurrentTime();
	if((time.GetHour()==5)&&(!m_bDatabaseUpdated))
	{
*/
		FreeTollerList();
		LoadTollerDatabase();
/*		m_bDatabaseUpdated=TRUE;
	}
	else
	{
		m_bDatabaseUpdated=FALSE;
	}
*/
	//If it is a special user,give the Not found information
	if(IsSpecialUser(sLoginName))
	{
		CString strOutput;
		LoadLongResource(strOutput, IDS_HTML_NOTFOUND);
		*pCtxt << strOutput;
		EndContent(pCtxt);
		return;
	}

	if(Query(sLoginName,tollerInfo))
	{
		//we find the tollerinfo matched to the loginName
		//now we generate the toller report.
	
	//	*pCtxt << sDebugInfo;

		//first the report header
		CString strOutput;
		LoadLongResource(strOutput, IDS_REPORT_HEADER);
		*pCtxt << strOutput;

		//now insert the time stamp 1997/12/01
	/*	CTime time=CTime::GetCurrentTime();
		CString sTime;
		sTime=time.Format("%Y年%m月%d日");
		*pCtxt << sTime;
	*/
		CString sTime;
		sTime=GetTollerBeginDate();

		*pCtxt << sTime;
		*pCtxt << "</P>";
	
		//now the table header
		LoadLongResource(strOutput, IDS_TABLE_HEADER);
		*pCtxt << strOutput;

		//now add and format the data from tollerInfo
		CString sSubTotal("");
		CString sTotal("");
		CString sIPAddr;//=CString(tollerInfo.szIPAddr);
		sIPAddr+=tollerInfo.szIPAddr;
		CString sLoginName(tollerInfo.szLoginName);

		sSubTotal.Format("%dk",tollerInfo.nSubTotal/1000);
		sTotal.Format("%dk",tollerInfo.nTotal/1000);
		
		int     nFee;
		nFee	=tollerInfo.nTotal/1000*nCostPerKB/1000;//0.02yuan per kilo bytes
		CString sFee;
		sFee.Format("%d元",nFee);

		*pCtxt<< "<tr align=\"left\">";
/*
		*pCtxt<< "<td>";
		*pCtxt<< sIPAddr;
		*pCtxt<< "</td>";
*/
		*pCtxt<< "<td>";
		*pCtxt<< sLoginName;
		*pCtxt<< "</td>";
		*pCtxt<< "<td>";
		*pCtxt<< sTotal;
		*pCtxt<< "</td>";
		*pCtxt<< "<td>";
		*pCtxt<< sSubTotal;
		*pCtxt<< "</td>";
		*pCtxt<< "<td>";
		*pCtxt<< sFee;
		*pCtxt<< "</td>";
		*pCtxt<< "</tr>";

		//add the end symbol of table into the report
		*pCtxt << " </table>";

		//now add the report tail to the HTML file
		LoadLongResource(strOutput, IDS_REPORT_TAIL);
		*pCtxt << strOutput;
	}
	else
	{
		//we can't find the tollerinfo matched to the loginName
		//then send the error report.
//		*pCtxt << sDebugInfo;
		CString strOutput;
		LoadLongResource(strOutput, IDS_HTML_NOTFOUND);
		*pCtxt << strOutput;
			
	}

	EndContent(pCtxt);
}

///////////////////////////////////////////////////////////
void CTollqryExtension::LoadTollerDatabase()
{
	tollerList.RemoveAll();
	
	CString sDatabaseName;
	sDatabaseName="c:\\toller\\database\\toller.dbf";

	CFile file;
	if(!file.Open(sDatabaseName,CFile::modeRead))
	{
		MessageBeep(0);
		return;
	}

	TOLLERINFO ti;
	UINT nBytesRead;

	while(TRUE)
	{
		nBytesRead=file.Read(&ti,sizeof(TOLLERINFO));
		if(nBytesRead==0)break;//reach the end of the file
		tollerList.AddTail(new CTollerInfo(&ti));
		TRACE("Toller LoginName=%s\n",ti.szLoginName);
	}

	file.Close();
}

void	CTollqryExtension::FreeTollerList()
{
	//free all the items in the list
	POSITION pos;
	CTollerInfo* pTollerInfo;
	
	if((pos=tollerList.GetHeadPosition())==NULL)
	{
		//no items in the list
		//free the tollerlist pointers
		tollerList.RemoveAll();	
		return;
	}


	while(pos!=NULL)
	{
		pTollerInfo=(CTollerInfo*)tollerList.GetNext(pos);
		delete pTollerInfo;
		pTollerInfo=NULL;
	}

	//free the tollerList pointers
	tollerList.RemoveAll();	
}


BOOL CTollqryExtension::Query(CString sLoginName,
			   CTollerInfo& tollerInfo)
{
	POSITION pos;
	CTollerInfo* pti;

//	sDebugInfo+=sLoginName;

	if((pos=tollerList.GetHeadPosition())==NULL)
	{
		
//		sDebugInfo+="  pos=NULL.";
		return FALSE;
	}

	while(pos!=NULL)
	{
		pti=(CTollerInfo*)tollerList.GetNext(pos);
		if(pti->GetLoginName()==sLoginName)
		{
			tollerInfo.Put(*pti);
			sDebugInfo+="  Find loginName matched.";
			return TRUE;
		}
	}
	return FALSE;//can't find the matched loginName in the list.
}




BOOL CTollqryExtension::GetExtensionVersion(HSE_VERSION_INFO* pVer)
{
	// Call default implementation for initialization
	CHttpServer::GetExtensionVersion(pVer);

	// Load description string
	TCHAR sz[HSE_MAX_EXT_DLL_NAME_LEN+1];
	ISAPIVERIFY(::LoadString(AfxGetResourceHandle(),
			IDS_SERVER, sz, HSE_MAX_EXT_DLL_NAME_LEN));
	_tcscpy(pVer->lpszExtensionDesc, sz);
	return TRUE;
}

///////////////////////////////////////////////////////////////////////
// CTollqryExtension command handlers

void CTollqryExtension::Default(CHttpServerContext* pCtxt)
{
	StartContent(pCtxt);
	WriteTitle(pCtxt);

	*pCtxt << _T("This default message was produced by the Internet");
	*pCtxt << _T(" Server DLL Wizard. Edit your CTollqryExtension::Default()");
	*pCtxt << _T(" implementation to change it.\r\n");

	EndContent(pCtxt);
}

void CTollqryExtension::WritePageTitle(CHttpServerContext* pCtxt)
{
	CString strTitle;

	strTitle.LoadString(IDS_PAGE_TITLE);

	*pCtxt << "<title>";
	*pCtxt << strTitle;
	*pCtxt << "</title>";
}

BOOL CTollqryExtension::LoadLongResource(CString& str, UINT nID)
{
	HRSRC hRes;
	HINSTANCE hInst = AfxGetResourceHandle();
	BOOL bResult = FALSE;
	
	hRes = FindResource(hInst, MAKEINTRESOURCE(nID), "#23");
	if (hRes == NULL)
		ISAPITRACE1("Error: Resource %d could not be found\r\n", nID);
	else
	{
		DWORD dwSize = SizeofResource(hInst, hRes);
		if (dwSize == 0)
		{
			str.Empty();
			bResult = TRUE;
		}
		else
		{
			LPTSTR pszStorage = str.GetBufferSetLength(dwSize);

			HGLOBAL hGlob = LoadResource(hInst, hRes);
			if (hGlob != NULL)
			{
				LPVOID lpData = LockResource(hGlob);

				if (lpData != NULL)
				{
					memcpy(pszStorage, lpData, dwSize);
					bResult = TRUE;
				}

				FreeResource(hGlob);
			}
		}
	}

#ifdef _DEBUG
	if (!bResult)
		str.Format(_T("<b>Could not find string %d</b>"), nID);
#endif

	return bResult;
}


// Do not edit the following lines, which are needed by ClassWizard.
#if 0
BEGIN_MESSAGE_MAP(CTollqryExtension, CHttpServer)
	//{{AFX_MSG_MAP(CTollqryExtension)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()
#endif	// 0



///////////////////////////////////////////////////////////////////////
// If your extension will not use MFC, you'll need this code to make
// sure the extension objects can find the resource handle for the
// module.  If you convert your extension to not be dependent on MFC,
// remove the comments arounn the following AfxGetResourceHandle()
// and DllMain() functions, as well as the g_hInstance global.

/****

static HINSTANCE g_hInstance;

HINSTANCE AFXISAPI AfxGetResourceHandle()
{
	return g_hInstance;
}

BOOL WINAPI DllMain(HINSTANCE hInst, ULONG ulReason,
					LPVOID lpReserved)
{
	if (ulReason == DLL_PROCESS_ATTACH)
	{
		g_hInstance = hInst;
	}

	return TRUE;
}

****/

⌨️ 快捷键说明

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