#include "stdafx.h"
#include "HTMLBuilder.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
static char szOperator[] = "!%&()*+,-./:;<=>?[]^{|}~\0";
static char szKeywords[][32] =
{
"for","if","else","continue","do","while","goto",
"switch","case","break","default","return",
"new","delete","inline",
"bool","char","double","float","int","long","short","void",
"false","true",
"const","unsigned","signed","volatile","mutable",
"auto","extern","static","register",
"#include","#if","#ifdef","#ifndef","#else","#elif","#endif","#define","#undef",
"#pragma","once","defined",
"struct","union","enum","typedef","sizeof",
"this","explicit","operator","private","public","protected","friend","class","virtual",
"template","using","namespace","typename","typeid","uuid","__uuidof","interface",
"const_cast","static_cast","dynamic_cast","reinterpret_cast",
"__asm","__based","__cdecl","__declspec","__fastcall","__inline","__stdcall","naked",
"__single_inheritance","__multiple_inheritance","__virtual_inheritance",
"__int8","__int16","__int32","__int64",
"dllexport","dllimport",
"thread","throw","try","catch",
"__try","__leave","__finally","__except",
NULL
};
CHTMLBuilder::CHTMLBuilder(Mythos::MemFile& MemFile) : m_MemFile(MemFile)
{
}
CHTMLBuilder::~CHTMLBuilder()
{
}
void CHTMLBuilder::WriteHeader(const char* pszFile)
{
if(!m_OutputFile.Open(pszFile,Mythos::IFile::_WRITE_TEXT_)) return;
m_OutputFile.PutString("<html>\n");
m_OutputFile.PutString("<head>\n");
m_OutputFile.PutString("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\">\n");
m_OutputFile.PutString("<link rel=\"stylesheet\" type=\"text/css\" href=\"mystyles.css\">\n");
m_OutputFile.PutString("</head>\n");
m_OutputFile.PutString("\n");
m_OutputFile.PutString("<body>\n");
m_OutputFile.PutString("<table border=0 cellspacing=0 cellpadding=0 width=\"100%\">\n");
m_OutputFile.PutString("<tr><td bgcolor=\"#ffffff\">\n");
m_OutputFile.PutString("<span class=source>\n");
m_OutputFile.PutString("<pre>\n");
}
void CHTMLBuilder::WriteFooter()
{
m_OutputFile.PutString("</pre>\n");
m_OutputFile.PutString("</span>\n");
m_OutputFile.PutString("</td></tr></table>\n");
m_OutputFile.PutString("\n");
m_OutputFile.PutString("</body>\n");
m_OutputFile.PutString("</html>\n");
m_OutputFile.Close();
}
void CHTMLBuilder::Parse()
{
char szLine[1024];
m_boCommBlock = false;
while(true)
{
if(!m_MemFile.GetString(szLine,1024)) break;
ParseLine(szLine);
m_OutputFile.PutChar('\n');
}
}
void CHTMLBuilder::ParseLine(char* pszLine)
{
char* pszChar = pszLine;
bool boString = false;
bool boQuote = false;
DWORD dwAnti = 0;
char cChar = 0;
char cPrev;
m_dwWordLen = 0;
while(*pszChar)
{
cPrev = cChar;
cChar = *pszChar++;
if(cPrev == '\\') dwAnti++;
else dwAnti = 0;
if(cChar == ' ')
{
PutWord();
m_OutputFile.PutChar(' ');
continue;
}
if(cChar == 9)
{
PutWord();
m_OutputFile.PutChar(cChar);
continue;
}
if(m_boCommBlock)
{
if((cChar != '*') || (*pszChar != '/'))
{
m_OutputFile.PutChar(cChar);
continue;
}
m_OutputFile.PutString("*/</span>");
pszChar++;
m_boCommBlock = false;
continue;
}
if(boString)
{
if((cChar != '"') || (dwAnti & 1))
{
m_OutputFile.PutChar(cChar);
continue;
}
m_OutputFile.PutString("\"</span>");
boString = false;
continue;
}
if(boQuote)
{
if((cChar != '\'') || (dwAnti & 1))
{
m_OutputFile.PutChar(cChar);
continue;
}
m_OutputFile.PutString("'</span>");
boQuote = false;
continue;
}
if(cChar == '/')
{
if(*pszChar == '/')
{
PutWord();
m_OutputFile.PutString("<span class=comment>//");
m_OutputFile.PutString(pszChar+1);
m_OutputFile.PutString("</span>");
return;
}
if(*pszChar == '*')
{
PutWord();
m_OutputFile.PutString("<span class=commblock>/*");
m_boCommBlock = true;
pszChar++;
continue;
}
}
if(cChar == '"')
{
PutWord();
m_OutputFile.PutString("<span class=string>\"");
boString = true;
continue;
}
if(cChar == '\'')
{
PutWord();
m_OutputFile.PutString("<span class=string>'");
boQuote = true;
continue;
}
if(strchr(szOperator,cChar))
{
if((cChar != '.') || !IsDigit(*pszChar))
{
PutWord();
m_OutputFile.PutString("<span class=operator>");
m_OutputFile.PutChar(cChar);
while(*pszChar && strchr(szOperator,*pszChar))
{
if((*pszChar == '.') && IsDigit(*(pszChar+1))) break;
cPrev = cChar;
cChar = *pszChar++;
m_OutputFile.PutChar(cChar);
}
m_OutputFile.PutString("</span>");
continue;
}
}
m_szWord[m_dwWordLen++] = cChar;
}
PutWord();
}
bool CHTMLBuilder::IsDigit(const char cChar)
{
return((cChar >= '0') && (cChar <= '9'));
}
bool CHTMLBuilder::IsKeyword()
{
for(DWORD dwI = 0; szKeywords[dwI][0] != 0; dwI++)
{
if(!strcmp(szKeywords[dwI],m_szWord))
{
return true;
}
}
return false;
}
bool CHTMLBuilder::IsNumber()
{
return(IsDigit(m_szWord[0]) || (m_szWord[0] == '.'));
}
void CHTMLBuilder::PutWord()
{
if(!m_dwWordLen) return;
m_szWord[m_dwWordLen] = 0;
if(IsKeyword())
{
m_OutputFile.PutString("<span class=keyword>");
m_OutputFile.PutString(m_szWord);
m_OutputFile.PutString("</span>");
}
else if(IsNumber())
{
m_OutputFile.PutString("<span class=number>");
m_OutputFile.PutString(m_szWord);
m_OutputFile.PutString("</span>");
}
else
{
m_OutputFile.PutString(m_szWord);
}
m_dwWordLen = 0;
}
|