📄 cplusplus.cpp
字号:
#include "stdafx.h"
#include "SdbmsDemoView.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
// SQL KeyWords
LPTSTR g_strSQLKeyWordList[] =
{
_T("AND"),
_T("OR"),
_T("NOT"),
_T("CONNECT"),
_T("CREATE"),
_T("DROP"),
_T("ADD"),
_T("DEL"),
_T("CHANGE"),
_T("ROLE"),
_T("GRANT"),
_T("REVOKE"),
_T("SET"),
_T("AUDIT"),
_T("NOAUDIT"),
_T("OBJRIGHT"),
_T("SYSRIGHT"),
_T("SELECT"),
_T("INSERT"),
_T("UPDATE"),
_T("DELETE"),
_T("NULL"),
_T("INTO"),
_T("VALUES"),
_T("WHERE"),
_T("ORDER"),
_T("ASC"),
_T("DESC"),
_T("BELIEVED"),
_T("SELF"),
_T("DATABASE"),
_T("TABLE"),
_T("VIEW"),
_T("INDEX"),
_T("CHAR"),
_T("INT"),
_T("BOOL"),
_T("PRIMARY"),
_T("KEY"),
_T("WITH"),
_T("AS"),
_T("OF"),
_T("TO"),
_T("ON"),
_T("BY"),
_T("FROM"),
_T("FOR"),
_T("USER"),
_T("ROLE"),
_T("CHILD"),
_T("PARENT"),
_T("TOP"),
_T("PASSWORD"),
_T("EXECUTED"),
_T("OPTION"),
_T("CASCADE"),
_T("RESTRICT"),
_T("SECLEVEL"),
_T("SECURITY"),
_T("LEVEL"),
_T("ALLOW"),
_T("CURRENT"),
_T("TRUE"),
_T("FALSE"),
NULL
};
BOOL IsKeyWord(LPCTSTR pszChars, int nLength)
{
for (int i=0; g_strSQLKeyWordList[i] != NULL; i++)
{
if (_strnicmp(g_strSQLKeyWordList[i], pszChars, nLength) == 0 && //忽略大小写
g_strSQLKeyWordList[i][nLength] == 0)
return TRUE;
}
return FALSE;
}
BOOL IsNumber(LPCTSTR pszChars, int nLength)
{
if (nLength > 2 && pszChars[0] == '0' && pszChars[1] == 'x') //16进制
{
for (int i=2; i<nLength; i++)
{
if (isdigit(pszChars[i]) || (pszChars[i] >= 'A' && pszChars[i] <= 'F') ||
(pszChars[i] >= 'a' && pszChars[i] <= 'f'))
continue;
return FALSE;
}
return TRUE;
}
if (!isdigit(pszChars[0])) //第一个字符不是数字
return FALSE;
for (int i=1; i<nLength; i++)
{
if (!isdigit(pszChars[i]) && //pszChars[i] != '+' && pszChars[i] != '-' &&
pszChars[i] != '.' && pszChars[i] != 'e' && pszChars[i] != 'E')
return FALSE;
}
return TRUE;
}
//该宏被用于函数ParseLine中
#define DEFINE_BLOCK(pos, colorindex) \
ASSERT((pos) >= 0 && (pos) <= nLength);\
if (pBuf != NULL)\
{\
if (nActualItems == 0 || pBuf[nActualItems - 1].m_nCharPos <= (pos)){\
pBuf[nActualItems].m_nCharPos = (pos);\
pBuf[nActualItems].m_nColorIndex = (colorindex);\
nActualItems ++;}\
}
#define COOKIE_COMMENT 0x0001
//#define COOKIE_PREPROCESSOR 0x0002 //不需要预编译语句
#define COOKIE_EXT_COMMENT 0x0004
#define COOKIE_STRING 0x0008
#define COOKIE_CHAR 0x0010
DWORD CSdbmsDemoView::ParseLine(DWORD dwCookie, int nLineIndex, TEXTBLOCK *pBuf, int &nActualItems)
{
int nLength = GetLineLength(nLineIndex);
if (nLength <= 0)
return dwCookie & COOKIE_EXT_COMMENT;
LPCTSTR pszChars = GetLineChars(nLineIndex);
BOOL bFirstChar = (dwCookie & ~COOKIE_EXT_COMMENT) == 0;
BOOL bRedefineBlock = TRUE;
BOOL bDecIndex = FALSE;
int nIdentBegin = -1;
for (int i = 0; ; i++)
{
if (bRedefineBlock)
{
int nPos = i;
if (bDecIndex)
nPos--;
if (dwCookie & (COOKIE_COMMENT | COOKIE_EXT_COMMENT))
{
DEFINE_BLOCK(nPos, COLORINDEX_COMMENT);
}
else
if (dwCookie & (COOKIE_CHAR | COOKIE_STRING))
{
DEFINE_BLOCK(nPos, COLORINDEX_STRING);
}
else
/* if (dwCookie & COOKIE_PREPROCESSOR)
{
DEFINE_BLOCK(nPos, COLORINDEX_PREPROCESSOR);
}
else
*/ {
DEFINE_BLOCK(nPos, COLORINDEX_NORMALTEXT);
}
bRedefineBlock = FALSE;
bDecIndex = FALSE;
}
if (i == nLength)
break;
if (dwCookie & COOKIE_COMMENT)
{
DEFINE_BLOCK(i, COLORINDEX_COMMENT);
dwCookie |= COOKIE_COMMENT;
break;
}
// String constant "...."
if (dwCookie & COOKIE_STRING)
{
if (pszChars[i] == '"' && (i == 0 || pszChars[i - 1] != '\\'))
{
dwCookie &= ~COOKIE_STRING;
bRedefineBlock = TRUE;
}
continue;
}
// Char constant '..'
if (dwCookie & COOKIE_CHAR)
{
if (pszChars[i] == '\'' && (i == 0 || pszChars[i - 1] != '\\'))
{
dwCookie &= ~COOKIE_CHAR;
bRedefineBlock = TRUE;
}
continue;
}
// Extended comment /*....*/
if (dwCookie & COOKIE_EXT_COMMENT)
{
if (i > 0 && pszChars[i] == '/' && pszChars[i - 1] == '*')
{
dwCookie &= ~COOKIE_EXT_COMMENT;
bRedefineBlock = TRUE;
}
continue;
}
if (i > 0 && pszChars[i] == '/' && pszChars[i - 1] == '/')
{
DEFINE_BLOCK(i - 1, COLORINDEX_COMMENT);
dwCookie |= COOKIE_COMMENT;
break;
}
/* // Preprocessor directive #....
if (dwCookie & COOKIE_PREPROCESSOR)
{
if (i > 0 && pszChars[i] == '*' && pszChars[i - 1] == '/')
{
DEFINE_BLOCK(i - 1, COLORINDEX_COMMENT);
dwCookie |= COOKIE_EXT_COMMENT;
}
continue;
}
*/
// Normal text
if (pszChars[i] == '"')
{
DEFINE_BLOCK(i, COLORINDEX_STRING);
dwCookie |= COOKIE_STRING;
continue;
}
if (pszChars[i] == '\'')
{
DEFINE_BLOCK(i, COLORINDEX_STRING);
dwCookie |= COOKIE_CHAR;
continue;
}
if (i > 0 && pszChars[i] == '*' && pszChars[i - 1] == '/')
{
DEFINE_BLOCK(i - 1, COLORINDEX_COMMENT);
dwCookie |= COOKIE_EXT_COMMENT;
continue;
}
if (bFirstChar)
{
/* if (pszChars[i] == '#')
{
DEFINE_BLOCK(i, COLORINDEX_PREPROCESSOR);
dwCookie |= COOKIE_PREPROCESSOR;
continue;
}
*/ if (! isspace(pszChars[i]))
bFirstChar = FALSE;
}
if (pBuf == NULL)
continue; // We don't need to extract KeyWords,
// for faster parsing skip the rest of loop
if (isalnum(pszChars[i]) || pszChars[i] == '_' || pszChars[i] == '.')
{
if (nIdentBegin == -1)
nIdentBegin = i;
}
else
{
if (nIdentBegin >= 0)
{
if (IsKeyWord(pszChars + nIdentBegin, i - nIdentBegin))
{
DEFINE_BLOCK(nIdentBegin, COLORINDEX_KEYWORD);
}
else
if (IsNumber(pszChars + nIdentBegin, i - nIdentBegin))
{
DEFINE_BLOCK(nIdentBegin, COLORINDEX_NUMBER);
}
bRedefineBlock = TRUE;
bDecIndex = TRUE;
nIdentBegin = -1;
}
}
}
if (nIdentBegin >= 0)
{
if (IsKeyWord(pszChars + nIdentBegin, i - nIdentBegin))
{
DEFINE_BLOCK(nIdentBegin, COLORINDEX_KEYWORD);
}
else
if (IsNumber(pszChars + nIdentBegin, i - nIdentBegin))
{
DEFINE_BLOCK(nIdentBegin, COLORINDEX_NUMBER);
}
}
if (pszChars[nLength - 1] != '\\')
dwCookie &= COOKIE_EXT_COMMENT;
return dwCookie;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -