📄 cpp2htmldlg.cpp
字号:
/*===源文件描述===
文件: Cpp2HtmlDlg.cpp
作者: Anel Lee (mailto: indep@263.net)
日期: 2003年5月13日 12:41
---
版权声明:本文件版权归Crazing Studio所有,请保留此声明
---
用途:
===源文件描述===*/
/*=============早期发现以下串处理有问题,都已经纠正了==================
1.C/C++文件:字符串分析错误,颜色不对
const char g_ch = '\0';
const char *g_str = "\\'\"''''''''''''\"\"\"\"\\\"\\\\\"";
const char *g_str2 = "\"<A HREF=\"\");";
2.文本文件:出现乱码,且颜色不对
"PhotoImpact 6.0中文版"
"AutoDesk的3DMax等等,如雷贯耳,不用多说吧?"
=====================================================================*/
#include "stdafx.h"
#include "Cpp2Html.h"
#include "Cpp2HtmlDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
class CAboutDlg : public CDialog
{
public:
CAboutDlg();
// Dialog Data
//{{AFX_DATA(CAboutDlg)
enum { IDD = IDD_ABOUTBOX };
//}}AFX_DATA
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CAboutDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
// Implementation
protected:
//{{AFX_MSG(CAboutDlg)
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
//{{AFX_DATA_INIT(CAboutDlg)
//}}AFX_DATA_INIT
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CCpp2HtmlDlg dialog
CCpp2HtmlDlg::CCpp2HtmlDlg(CWnd* pParent /*=NULL*/)
: CDialog(CCpp2HtmlDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CCpp2HtmlDlg)
m_bBoldKryword = FALSE;
m_bItalicComment = FALSE;
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CCpp2HtmlDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CCpp2HtmlDlg)
DDX_Check(pDX, IDC_BOLDKEYWORD, m_bBoldKryword);
DDX_Check(pDX, IDC_ITALICCOMMENT, m_bItalicComment);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CCpp2HtmlDlg, CDialog)
//{{AFX_MSG_MAP(CCpp2HtmlDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_CONVERT, OnConvert)
ON_BN_CLICKED(IDC_BROWSECPP, OnBrowseCpp)
ON_BN_CLICKED(IDC_BROWSEHTML, OnBrowseHtml)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
const char *CCpp2HtmlDlg::m_strKeywords[99] =
{
"__int32", //0
"__int64",
"asm",
"auto",
"BOOL", //4
"bool",
"break",
"BSTR",
"BYTE",
"case", //9
"CATCH",
"catch",
"char",
"class",
"const", //14
"const_cast",
"continue",
"default",
"define",
"delete", //19
"disable",
"do",
"double",
"DWORD",
"DWORD", //24
"dynamic_cast",
"else",
"enum",
"error",
"explicit", //29
"extern",
"FALSE",
"false",
"float",
"for", //34
"friend",
"goto",
"HANDLE",
"if",
"ifdef", //39
"ifndef",
"include",
"inline",
"int",
"INT", //44
"LONG",
"long",
"LPCSTR",
"LPCTSRT",
"mutable", //49
"namespace",
"new",
"NULL",
"operator",
"PBYTE", //54
"PDWORD",
"pragma",
"private",
"protected",
"PSTR", //59
"public",
"PWORD",
"register",
"reinterpret_cast",
"return", //64
"short",
"signed",
"sizeof",
"static",
"static_cast", //69
"struct",
"switch",
"template",
"this",
"throw", //74
"true",
"TRUE",
"try",
"TRY",
"typedef", //79
"typeid",
"uchar",
"uint",
"ulong",
"ULONG", //84
"union",
"unsigned",
"ushort",
"USHORT",
"using", //89
"virtual",
"void",
"volatile",
"warning",
"wchar_t", //94
"while",
"WORD", //96
"undef",
"endif",
};
/////////////////////////////////////////////////////////////////////////////
// CCpp2HtmlDlg message handlers
BOOL CCpp2HtmlDlg::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
ShowStatus("Select C++ source file please");
m_bItalicComment = TRUE;
m_bBoldKryword = TRUE;
CheckDlgButton(IDC_ITALICCOMMENT,TRUE);
CheckDlgButton(IDC_BOLDKEYWORD,TRUE);
return TRUE; // return TRUE unless you set the focus to a control
}
void CCpp2HtmlDlg::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 CCpp2HtmlDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, (WPARAM) 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 to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CCpp2HtmlDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CCpp2HtmlDlg::OnOK()
{
}
void CCpp2HtmlDlg::OnConvert()
{
if(!UpdateData(TRUE))
{
ShowStatus("invalidate input!");
return;
}
char src[512];
char dst[512];
int n = GetDlgItemText(IDC_CPPFILE,src,512);
if(n<0 || !*src)
{
ShowStatus("Select C++ source file please");
CWnd *pWnd = GetDlgItem(IDC_CPPFILE);
pWnd->SetFocus();
return;
}
n = GetDlgItemText(IDC_HTMLFILE,dst,512);
if(n<0 || !*dst)
{
ShowStatus("specify converted HTML file please");
CWnd *pWnd = GetDlgItem(IDC_HTMLFILE);
pWnd->SetFocus();
return;
}
WIN32_FIND_DATA fd;
HANDLE hFind = FindFirstFile(src,&fd);
if(!hFind || hFind==INVALID_HANDLE_VALUE)
{
ShowStatus("***input file doesn't existing");
return;
}
if(!fd.nFileSizeLow && !fd.nFileSizeHigh)
{
ShowStatus("***empty input file encountered, no convertion needed");
return;
}
HANDLE hSrc = CreateFile(src,GENERIC_READ,FILE_SHARE_READ,NULL,OPEN_EXISTING,0,NULL);
if(hSrc==INVALID_HANDLE_VALUE)
{
ShowStatus("***can't open input file");
return;
}
PSTR pBuf = (PSTR)GlobalAlloc(GMEM_FIXED,fd.nFileSizeLow + 4);
if(!pBuf)
{
ShowStatus("***can't alloc memory");
CloseHandle(hSrc);
return;
}
memset(pBuf,0,fd.nFileSizeLow + 4);
DWORD dw = 0;
ReadFile(hSrc,pBuf,fd.nFileSizeLow,&dw,NULL);
CloseHandle(hSrc);
if(dw<fd.nFileSizeLow)
{
GlobalFree(pBuf);
ShowStatus("***read file error");
return;
}
HANDLE hDst = CreateFile(dst,GENERIC_WRITE,FILE_SHARE_READ,NULL,CREATE_ALWAYS,0,NULL);
if(hDst==INVALID_HANDLE_VALUE)
{
ShowStatus("***can't open output file");
GlobalFree(pBuf);
return;
}
ShowStatus("开始转换");
PSTR strFileName = strrchr(src,'\\');
strFileName++;
//generate HTML file header
WriteString(hDst,"<HEAD>\r\n");
WriteString(hDst,"<TITLE>");
WriteString(hDst,strFileName);
WriteString(hDst,"</TITLE>\r\n");
WriteString(hDst,"</HEAD>\r\n");
//generate HTML file body properties
WriteString(hDst,"<BODY BACKGROUND=#ffffff FACE=\"FixedSys\" SIZE=12>\r\n");
WriteString(hDst,"<CENTER><H1>");
WriteString(hDst,strFileName);
WriteString(hDst,"</H1></CENTER>");
WriteString(hDst,"<SCRIPT LANGUAGE=\"JavaScript\">\r\n");
WriteString(hDst,"<!-- Hide script contents from old browsers\r\n");
WriteString(hDst,"document.write(\"最后修改时间: \" + document.lastModified)\r\n");
WriteString(hDst,"// End hiding of contents from old browsers -->\r\n");
WriteString(hDst,"</SCRIPT>\r\n");
WriteString(hDst,"<HR SIZE=5>\r\n");
// Indicate that the output text will be preformatted fixed-width text.
WriteString(hDst,"<PRE>\r\n");
char line[4100];
char strToken[128];
char html[4100];
char tmp[4100];
PSTR p;
BOOL bParseNumber = FALSE;
BOOL bParseToken = FALSE;
BOOL bInComment = FALSE;
BOOL bInString = FALSE;
BOOL bInEOLComment = FALSE;
BOOL bEscapeChar = FALSE;
BOOL bStartString = FALSE;
BOOL bPreprocessor = FALSE;
BOOL bContinueString = FALSE;
PSTR pStrStartPos = NULL;
PSTR strInclude;
PSTR pQuotes;
BOOL bInAStr = FALSE;
BOOL bESC;
BOOL bInFloat = FALSE;
PSTR pStr;
int j;
strToken[0] = '\0';
const char *strAlpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_";
const char *strNumber = "0123456789";
const char *strOperator = "&<>(){}[];:!%^*-+=|,";
PSTR pPos = pBuf;
for(;;)
{
html[0] = '\0';
n = LineRead(pPos,line);
if(n<=0) break;
p = line;
for(;n>0;--n,++p)
{
if(bParseNumber && !bParseToken)
{
//If the current character is not a hexadecimal digit and it is not
//an 'x' or 'X', then the number has been completed and its color
//formatting will be turned off.
//if(!isxdigit(*p) && *p != 'x' && *p != 'X')
if(!((*p>='A' && *p<='F') || (*p>='a' && *p<='f') || *p=='x' || *p=='X' || (*p>='0' && *p<='9')))
{
strcat(html,"</FONT>");
bParseNumber = FALSE;
bInFloat = FALSE;
}
else if(*p=='.' && !bInFloat)
{
bInFloat = TRUE;
//Add the character to the HTML output line.
StrAppend(html,*p);
continue;
}
else if(*p=='L')
{
//Add the character to the HTML output line.
StrAppend(html,*p);
strcat(html,"</FONT>");
bParseNumber = FALSE;
bInFloat = FALSE;
continue;
}
StrAppend(html,*p);
continue;
}
// Process the current character.
// Catch all alphabetic characters and the underscore. The underscore
// is a legal character in variable names and keywords.
if(strchr(strAlpha,*p))
{
// If this character is not in a comment or a string, then add it to
// the token string and indicate that a token has been found.
if(!bInComment && !bInString)
{
if(bParseNumber)
{
strcat(html,"</FONT>");
bParseNumber = FALSE;
bInFloat = FALSE;
}
StrAppend(strToken,*p);
bParseToken = TRUE;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -