// reg.cpp : Defines the class behaviors for the application.
//

#include "stdafx.h"
#include "reg.h"
#include "mainfrm.h"
#include "regview.h"

#ifdef _DEBUG
#undef THIS_FILE
static char BASED_CODE THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CRegApp

BEGIN_MESSAGE_MAP(CRegApp, CWinApp)
	//{{AFX_MSG_MAP(CRegApp)
	//}}AFX_MSG_MAP
	// Standard file based document commands
	ON_COMMAND(ID_FILE_NEW, CWinApp::OnFileNew)
	ON_COMMAND(ID_FILE_OPEN, CWinApp::OnFileOpen)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CRegApp construction

CRegApp::CRegApp()
{
	m_pView = NULL;
	m_pFrame = NULL;
}

/////////////////////////////////////////////////////////////////////////////
// The one and only CRegApp object

CRegApp theApp;

/////////////////////////////////////////////////////////////////////////////
// CRegApp initialization

BOOL CRegApp::InitInstance()
{
	LoadStdProfileSettings();  // Load standard INI file options (including MRU)

	m_pFrame = new CMainFrame;
	m_pView = new CRegView;

    CCreateContext newContext;
	newContext.m_pNewViewClass = NULL;
    newContext.m_pNewDocTemplate = NULL;
    newContext.m_pLastView = NULL;
    newContext.m_pCurrentFrame = NULL;
    newContext.m_pCurrentDoc = NULL;

	DWORD dwStyle = WS_OVERLAPPED;

	// create the frame with 0 dimensions since this is background process
	if (!m_pFrame->Create(NULL, NULL, dwStyle, CRect(0,0,0,0),
		NULL, NULL, 0L, &newContext))
	{
		return FALSE;   // will self destruct on failure normally
	}

	// assingn main app window
 	m_pMainWnd = m_pFrame;

	// create the view with 0 dimensions
	m_pView->Create(NULL,NULL,WS_CHILD,CRect(0,0,0,0),m_pFrame,
		AFX_IDW_PANE_FIRST,&newContext);
	m_pView->OnInitialUpdate();

	// if we got all file names from WWW Server
	if (m_lpCmdLine[0] != '\0')
	{
		CString sIniFile;
		CString sContentFile;
		CString sOutputFile;
		// extracting file names form command line
		ParseCmdLine(sIniFile, sContentFile, sOutputFile);
		// parsing [Form Literal] Section in CGI ini file created by WWW Server
		ParseIniFile(sIniFile.GetBuffer(0));
		// creating appropriate response
		CreateResponse(sOutputFile, sIniFile);
	}
	// let get out 
	PostQuitMessage(0);
	return TRUE;
}

int CRegApp::ExitInstance()
{
	if (m_pView)
		delete m_pView;
	if (m_pFrame)
		delete m_pFrame;
	return 0;
}

void CRegApp::ParseCmdLine(CString& sIniFile, CString& sContentFile, CString& sOutputFile)
{
	CString sCmdLine = m_lpCmdLine;
	int nCmdPos = 0;
	int nWordPos = 0;

	// skip LWS
	while (nCmdPos < sCmdLine.GetLength() &&
		(sCmdLine[nCmdPos] == ' ' || sCmdLine[nCmdPos] == '\t'))
		nCmdPos++;

	nWordPos = nCmdPos;

	// extract ini file path
	while (nCmdPos < sCmdLine.GetLength() &&
		sCmdLine[nCmdPos] != ' ' && sCmdLine[nCmdPos] != '\t')
		nCmdPos++;

	sIniFile = sCmdLine.Mid(nWordPos, (nCmdPos - nWordPos));

	// skip LWS
	while (nCmdPos < sCmdLine.GetLength() &&
		(sCmdLine[nCmdPos] == ' ' || sCmdLine[nCmdPos] == '\t'))
		nCmdPos++;

	nWordPos = nCmdPos;

	// extract ini file path
	while (nCmdPos < sCmdLine.GetLength() &&
		sCmdLine[nCmdPos] != ' ' && sCmdLine[nCmdPos] != '\t')
		nCmdPos++;

	sContentFile = sCmdLine.Mid(nWordPos, (nCmdPos - nWordPos));

	// skip LWS
	while (nCmdPos < sCmdLine.GetLength() &&
		(sCmdLine[nCmdPos] == ' ' || sCmdLine[nCmdPos] == '\t'))
		nCmdPos++;

	nWordPos = nCmdPos;

	// extract ini file path
	while (nCmdPos < sCmdLine.GetLength() &&
		sCmdLine[nCmdPos] != ' ' && sCmdLine[nCmdPos] != '\t')
		nCmdPos++;

	sOutputFile = sCmdLine.Mid(nWordPos, (nCmdPos - nWordPos));
}

void CRegApp::ParseIniFile(CString sIniFile)
{
	int nIniPos = 0;
	int nBufLen;
	CString sLine;
	char lpszBuf[2048];

	nBufLen = GetPrivateProfileSection("Form Literal",(LPSTR)lpszBuf,2048,sIniFile.GetBuffer(0));

	do
	{
		sLine = "";

		while (nIniPos < nBufLen &&	lpszBuf[nIniPos] != '\0')
		{
			sLine += lpszBuf[nIniPos];
			nIniPos++;
		}
		nIniPos++; // let skip null char
			
		// if line is not empty, remember it
		if (sLine != "")
		{
			m_saParams.Add(sLine);
		}

	} while (sLine != "");

}

void CRegApp::CreateResponse(CString sOutputFile, CString sIniFile)
{
	int i;
	int nIndex;
	CString sResponse;
	CFile fOutput;
	CFileStatus fStatus;
	CString sCustomer;
	CString sKey;
	CString sVal;

	// create header and background
	sResponse = "Content-type: text/html\r\n";
	sResponse += "\r\n";
	sResponse += "<HTML>\r\n";
	sResponse += "<BODY>\r\n";
	sResponse += "<HEAD>\r\n";
	sResponse += "<TITLE>ILAR CGI sample</TITLE>\r\n";
	sResponse += "<BODY BACKGROUND=\"/images/back.gif\">\r\n";
	sResponse += "</HEAD>\r\n";

	// do we have all params
	for (i = 0; i < m_saParams.GetSize(); i++)
	{
		sKey.Empty(); // just in case
		sVal.Empty();

		if ((nIndex = m_saParams[i].Find('=')) != -1)
		{
			sKey = m_saParams[i].Left(nIndex);
			sVal = m_saParams[i].Right(m_saParams[i].GetLength() - nIndex - 1);
		}

		if (sVal == "")
		{
			sResponse += "<H1>Sorry, you did not enter all fields</H1>\r\n";
			break;
		}

		sCustomer += "<H3>";
		sCustomer += sKey;
		sCustomer += ": ";
		sCustomer += sVal;
		sCustomer += "</H3>\r\n";
	}

	// yes we have all params
	if (i == m_saParams.GetSize())
	{
		sResponse += "<H1>Registration confirmed:</H1>\r\n";
		sResponse += sCustomer;
	}

	sResponse += "</HTML>\r\n";
	sResponse += "</BODY>\r\n";

	// save our response into output file; name provided by WWW Server
	if (fOutput.Open(sOutputFile.GetBuffer(0),
		CFile::modeCreate | CFile::modeWrite | CFile::typeBinary))
	{
		fOutput.Write(sResponse.GetBuffer(0),sResponse.GetLength());
		fOutput.Close();
	}
}