📄 cedtelement.cpp
字号:
#include "stdafx.h"
#include "cedtElement.h"
#include "Encode.h"
#include "Utility.h"
#include <strstrea.h>
#include <ctype.h>
#define _SWAP_UCHAR(A, B) { _uchar_temp = (A); (A) = (B); (B) = _uchar_temp; }
static UCHAR _uchar_temp;
CString ENCODING_TYPE_DESCRIPTION_FULL[] = {
/* ENCODING_TYPE_UNKNOWN */ "Unknown Encoding",
/* ENCODING_TYPE_ASCII */ "ASCII Encoding",
/* ENCODING_TYPE_UNICODE_LE */ "Unicode Encoding (Little Endian)",
/* ENCODING_TYPE_UNICODE_BE */ "Unicode Encoding (Big Endian)",
/* ENCODING_TYPE_UTF8_WBOM */ "UTF-8 Encoding (with BOM)",
/* ENDOCING_TYPE_UTF8_XBOM */ "UTF-8 Encoding (w/o BOM)",
};
CString ENCODING_TYPE_DESCRIPTION_SHORT[] = {
/* ENCODING_TYPE_UNKNOWN */ "N.A.",
/* ENCODING_TYPE_ASCII */ "ASCII",
/* ENCODING_TYPE_UNICODE_LE */ "Unicode",
/* ENCODING_TYPE_UNICODE_BE */ "Unicode",
/* ENCODING_TYPE_UTF8_WBOM */ "UTF-8",
/* ENCODING_TYPE_UTF8_XBOM */ "UTF-8",
};
CString FILE_FORMAT_DESCRIPTION_FULL[] = {
/* FILE_FORMAT_UNKNOWN */ "Unknown Format",
/* FILE_FORMAT_DOS */ "DOS Format",
/* FILE_FORMAT_UNIX */ "UNIX Format",
/* FILE_FORMAT_MAC */ "MAC Format",
};
CString FILE_FORMAT_DESCRIPTION_SHORT[] = {
/* FILE_FORMAT_UNKNOWN */ "N.A.",
/* FILE_FORMAT_DOS */ "DOS",
/* FILE_FORMAT_UNIX */ "UNIX",
/* FILE_FORMAT_MAC */ "MAC",
};
static UCHAR _GetRangeType(LPCTSTR lpszRange)
{
if ( ! _strnicmp(lpszRange, "RANGE1", 6) ) return RT_RANGE1;
else if( ! _strnicmp(lpszRange, "RANGE2", 6) ) return RT_RANGE2;
else if( ! _strnicmp(lpszRange, "!R1&R2", 6) ) return RT_NR1AR2;
else if( ! _strnicmp(lpszRange, "!RNGE1", 6) ) return RT_NRNGE1;
else if( ! _strnicmp(lpszRange, "!RNGE2", 6) ) return RT_NRNGE2;
else if( ! _strnicmp(lpszRange, "R1||R2", 6) ) return RT_R1ORR2;
return RT_GLOBAL;
}
BOOL DetectEncodingTypeAndFileFormat(LPCTSTR lpszPathName, INT & nEncodingType, INT & nFileFormat)
{
try {
CFile file(lpszPathName, CFile::modeRead | CFile::typeBinary);
UCHAR szBuffer[4096]; INT nCount = file.Read( szBuffer, 4096 );
file.Close();
DetectEncodingType(szBuffer, nCount, nEncodingType);
DetectFileFormat(szBuffer, nCount, nFileFormat);
TRACE2("EncodingType: %d, FileFormat: %d\n", nEncodingType, nFileFormat);
return TRUE;
} catch( CException * ex ) {
nEncodingType = ENCODING_TYPE_ASCII;
nFileFormat = FILE_FORMAT_DOS;
ex->Delete();
return FALSE;
}
}
BOOL DetectEncodingType(LPVOID lpContents, INT nLength, INT & nEncodingType)
{
LPBYTE lpBuffer = (LPBYTE)lpContents;
if( nLength >= 2 && lpBuffer[0] == 0xFF && lpBuffer[1] == 0xFE ) { nEncodingType = ENCODING_TYPE_UNICODE_LE; return TRUE; }
if( nLength >= 2 && lpBuffer[0] == 0xFE && lpBuffer[1] == 0xFF ) { nEncodingType = ENCODING_TYPE_UNICODE_BE; return TRUE; }
if( nLength >= 3 && lpBuffer[0] == 0xEF && lpBuffer[1] == 0xBB && lpBuffer[2] == 0xBF ) { nEncodingType = ENCODING_TYPE_UTF8_WBOM; return TRUE; }
nEncodingType = ENCODING_TYPE_ASCII;
return TRUE;
}
BOOL DetectFileFormat(LPVOID lpContents, INT nLength, INT & nFileFormat)
{
LPBYTE lpBuffer = (LPBYTE)lpContents;
BOOL bHasCR = FALSE, bHasLF = FALSE;
for( INT i = 0; i < nLength; i++ ) {
if( lpBuffer[i] == '\r' ) bHasCR = TRUE;
if( lpBuffer[i] == '\n' ) bHasLF = TRUE;
if( bHasCR && bHasLF ) { nFileFormat = FILE_FORMAT_DOS; return TRUE; }
}
if( ! bHasCR && bHasLF ) nFileFormat = FILE_FORMAT_UNIX;
else if( bHasCR && ! bHasLF ) nFileFormat = FILE_FORMAT_MAC;
else nFileFormat = FILE_FORMAT_DOS; // default file format
return TRUE;
}
// CLangSpec
void CLangSpec::ResetContents()
{
m_bCaseSensitive = TRUE; m_bMultiLineStringConstant = m_bVariableHighlightInString = FALSE;
m_szDelimiters = "(){}[]<>+-*/%=\"'~!@#$^&|\\?:;,."; // Omited '_' from default delimiters 2004.08.08
m_szKeywordPrefix = m_szHexaDecimalMark = "";
m_szVariablePrefix = m_szVariableEnclosedBy = m_szSpecialVariableChars = "";
m_chEscapeChar = 0x00;
m_chQuotationMark1 = m_chQuotationMark2 = m_chQuotationMark3 = 0x00;
m_chIndentationOn = m_chIndentationOff = 0x00;
m_szLineComment1OnFirstPosition = m_szLineComment2OnFirstPosition = "";
m_szLineComment1 = m_szLineComment2 = "";
m_szBlockComment1On = m_szBlockComment1Off = "";
m_szBlockComment2On = m_szBlockComment2Off = "";
m_szShadowOn = m_szShadowOff = "";
m_szHighlightOn = m_szHighlightOff = "";
m_szRange1Beg = m_szRange1End = "";
m_szRange2Beg = m_szRange2End = "";
m_szPairs1 = m_szPairs2 = m_szPairs3 = "";
m_ucQuotationMarkRange = m_ucLineCommentRange = m_ucBlockCommentRange = RT_GLOBAL;
}
BOOL CLangSpec::FileLoad(LPCTSTR lpszPathName)
{
TCHAR szBuffer[4096];
ResetContents(); // clear previous settings
ifstream fin(lpszPathName, ios::in | ios::nocreate);
if( ! fin.is_open() ) return FALSE;
while( fin.good() ) {
fin.getline(szBuffer, 4096);
if( szBuffer[0] != '$' ) continue;
TCHAR * ptr1 = strtok(szBuffer, "=");
TCHAR * ptr2 = strtok(NULL, "\n");
if ( ptr2 && ! _stricmp(ptr1, "$CASESENSITIVE") ) { m_bCaseSensitive = (ptr2[0] == 'Y' || ptr2[0] == 'y') ? TRUE : FALSE; }
else if( ptr2 && ! _stricmp(ptr1, "$MULTILINESTRINGCONSTANT") ) { m_bMultiLineStringConstant = (ptr2[0] == 'Y' || ptr2[0] == 'y') ? TRUE : FALSE; }
else if( ptr2 && ! _stricmp(ptr1, "$VARIABLEHIGHLIGHTINSTRING") ) { m_bVariableHighlightInString = (ptr2[0] == 'Y' || ptr2[0] == 'y') ? TRUE : FALSE; }
else if( ptr2 && ! _stricmp(ptr1, "$DELIMITERS") ) { m_szDelimiters = ptr2; }
else if( ptr2 && ! _stricmp(ptr1, "$PREFIX") ) { m_szKeywordPrefix = ptr2; }
else if( ptr2 && ! _stricmp(ptr1, "$KEYWORDPREFIX") ) { m_szKeywordPrefix = ptr2; }
else if( ptr2 && ! _stricmp(ptr1, "$HEXADECIMALMARK") ) { m_szHexaDecimalMark = ptr2; }
else if( ptr2 && ! _stricmp(ptr1, "$VARIABLEPREFIX") ) { m_szVariablePrefix = ptr2; }
else if( ptr2 && ! _stricmp(ptr1, "$VARIABLEENCLOSEDBY") ) { m_szVariableEnclosedBy = ptr2; }
else if( ptr2 && ! _stricmp(ptr1, "$SPECIALVARIABLECHARS") ) { m_szSpecialVariableChars = ptr2; }
else if( ptr2 && ! _stricmp(ptr1, "$ESCAPECHAR") ) { m_chEscapeChar = ptr2[0]; }
else if( ptr2 && ! _stricmp(ptr1, "$QUOTATIONMARK1") ) { m_chQuotationMark1 = ptr2[0]; }
else if( ptr2 && ! _stricmp(ptr1, "$QUOTATIONMARK2") ) { m_chQuotationMark2 = ptr2[0]; }
else if( ptr2 && ! _stricmp(ptr1, "$QUOTATIONMARK3") ) { m_chQuotationMark3 = ptr2[0]; }
else if( ptr2 && ! _stricmp(ptr1, "$INDENTATIONON" ) ) { m_chIndentationOn = ptr2[0]; }
else if( ptr2 && ! _stricmp(ptr1, "$INDENTATIONOFF") ) { m_chIndentationOff = ptr2[0]; }
else if( ptr2 && ! _stricmp(ptr1, "$LINECOMMENTONFIRSTPOSITION" ) ) { m_szLineComment1OnFirstPosition = ptr2; }
else if( ptr2 && ! _stricmp(ptr1, "$LINECOMMENT1ONFIRSTPOSITION") ) { m_szLineComment1OnFirstPosition = ptr2; }
else if( ptr2 && ! _stricmp(ptr1, "$LINECOMMENT2ONFIRSTPOSITION") ) { m_szLineComment2OnFirstPosition = ptr2; }
else if( ptr2 && ! _stricmp(ptr1, "$LINECOMMENT" ) ) { m_szLineComment1 = ptr2; }
else if( ptr2 && ! _stricmp(ptr1, "$LINECOMMENT1") ) { m_szLineComment1 = ptr2; }
else if( ptr2 && ! _stricmp(ptr1, "$LINECOMMENT2") ) { m_szLineComment2 = ptr2; }
else if( ptr2 && ! _stricmp(ptr1, "$BLOCKCOMMENTON" ) ) { m_szBlockComment1On = ptr2; }
else if( ptr2 && ! _stricmp(ptr1, "$BLOCKCOMMENTOFF" ) ) { m_szBlockComment1Off = ptr2; }
else if( ptr2 && ! _stricmp(ptr1, "$BLOCKCOMMENT1ON" ) ) { m_szBlockComment1On = ptr2; }
else if( ptr2 && ! _stricmp(ptr1, "$BLOCKCOMMENT1OFF") ) { m_szBlockComment1Off = ptr2; }
else if( ptr2 && ! _stricmp(ptr1, "$BLOCKCOMMENT2ON" ) ) { m_szBlockComment2On = ptr2; }
else if( ptr2 && ! _stricmp(ptr1, "$BLOCKCOMMENT2OFF") ) { m_szBlockComment2Off = ptr2; }
else if( ptr2 && ! _stricmp(ptr1, "$SHADOWON" ) ) { m_szShadowOn = ptr2; }
else if( ptr2 && ! _stricmp(ptr1, "$SHADOWOFF") ) { m_szShadowOff = ptr2; }
else if( ptr2 && ! _stricmp(ptr1, "$HIGHLIGHTON" ) ) { m_szHighlightOn = ptr2; }
else if( ptr2 && ! _stricmp(ptr1, "$HIGHLIGHTOFF") ) { m_szHighlightOff = ptr2; }
else if( ptr2 && ! _stricmp(ptr1, "$RANGE1BEG") ) { m_szRange1Beg = ptr2; }
else if( ptr2 && ! _stricmp(ptr1, "$RANGE1END") ) { m_szRange1End = ptr2; }
else if( ptr2 && ! _stricmp(ptr1, "$RANGE2BEG") ) { m_szRange2Beg = ptr2; }
else if( ptr2 && ! _stricmp(ptr1, "$RANGE2END") ) { m_szRange2End = ptr2; }
else if( ptr2 && ! _stricmp(ptr1, "$PAIRS1") ) { m_szPairs1 = ptr2; }
else if( ptr2 && ! _stricmp(ptr1, "$PAIRS2") ) { m_szPairs2 = ptr2; }
else if( ptr2 && ! _stricmp(ptr1, "$PAIRS3") ) { m_szPairs3 = ptr2; }
else if( ptr2 && ! _stricmp(ptr1, "$QUOTATIONMARKRANGE") ) { m_ucQuotationMarkRange = _GetRangeType(ptr2); }
else if( ptr2 && ! _stricmp(ptr1, "$LINECOMMENTRANGE" ) ) { m_ucLineCommentRange = _GetRangeType(ptr2); }
else if( ptr2 && ! _stricmp(ptr1, "$BLOCKCOMMENTRANGE" ) ) { m_ucBlockCommentRange = _GetRangeType(ptr2); }
}
fin.close();
return TRUE;
}
// CKeywords
BOOL CKeywords::FileLoad(LPCTSTR lpszPathName, BOOL bCaseSensitive)
{
TCHAR szBuffer[4096], szWord[1024];
UCHAR ucType = 0x00, ucRange = RT_GLOBAL;
RemoveAll(); // clear hash table first
ifstream fin(lpszPathName, ios::in | ios::nocreate);
if( ! fin.is_open() ) return FALSE;
while( fin.good() ) {
fin.getline(szBuffer, 4096);
if( szBuffer[0] == '#' ) continue;
if( ! _strnicmp(szBuffer, "[-COMMENT", 9) ) {
ucType = 0x00;
ucRange = RT_GLOBAL;
} else if( ! _strnicmp(szBuffer, "[KEYWORDS", 9) ) {
TCHAR * ptr1 = strtok(szBuffer, ":");
TCHAR * ptr2 = strtok(NULL, "\n");
ucType = WT_KEYWORD0 + (ptr1[9] - '0');
ucRange = _GetRangeType(ptr2);
} else if( ucType ) {
istrstream sin(szBuffer);
SHORT siValue;
while( sin.good() ) {
sin.eatwhite(); if( ! sin.good() ) break;
sin >> szWord; if( szWord[0] == '\0' ) break;
if( ! bCaseSensitive ) _strlwr(szWord);
if( ! Lookup(szWord, siValue) ) {
siValue = (SHORT)MAKEWORD(ucType, ucRange);
SetAt(szWord, siValue);
}
}
}
}
fin.close();
return TRUE;
}
// CDictionary
BOOL CDictionary::FileLoad(LPCTSTR lpszPathName, CALLBACK_FUNCTION fcnCallback)
{
TCHAR szWord[1024]; SHORT siValue; UINT nCount = 0;
ifstream fin(lpszPathName, ios::in | ios::nocreate);
if( ! fin.is_open() ) return FALSE;
while( fin.good() ) {
fin.eatwhite(); if( fin.eof() ) break;
fin >> szWord; _strlwr(szWord);
if( ! Lookup( szWord, siValue ) ) {
SetAt( szWord, siValue = WT_WORD );
nCount++; m_nWordCount++;
}
// call a callback function if exist
if( ! (nCount % 100) && fcnCallback ) fcnCallback( fin.tellg() );
}
fin.close();
return TRUE;
}
BOOL CDictionary::AddWord(LPCTSTR lpszWord)
{
TCHAR szWord[1024]; SHORT siValue;
strcpy(szWord, lpszWord); _strlwr(szWord);
if( ! Lookup( szWord, siValue ) ) {
SetAt( szWord, siValue = WT_WORD );
m_nWordCount++;
} else return FALSE;
return TRUE;
}
// CAnalyzedString
CAnalyzedString & CAnalyzedString::operator=(const CAnalyzedString & stringSrc) {
CString::operator=(stringSrc);
delete [] m_pWordInfo; m_pWordInfo = NULL;
m_sWordCount = 0; /* m_sInfoFlags = 0x00; */
return * this;
}
// CFormatedString
CFormatedString & CFormatedString::operator=(const CFormatedString & stringSrc) {
m_pString = stringSrc.m_pString;
delete [] m_pWordInfo; m_pWordInfo = NULL;
m_sWordCount = 0; m_sInfoFlags = 0x00; m_sLineSplitIndex = 0;
return * this;
}
// CMemText
BOOL CMemText::FileLoad(LPCTSTR lpszPathName)
{
try {
CFile file(lpszPathName, CFile::modeRead | CFile::typeBinary | CFile::shareDenyNone);
RemoveAll(); AddTail(""); // initialize contents
CHAR szBuffer[FILE_READ_BUFFER_SIZE+1];
int i, nCount, nTotal = 0; BOOL bDelimFount = FALSE;
while( nCount = file.Read( szBuffer, FILE_READ_BUFFER_SIZE ) ) { // read file contents
for( bDelimFount = FALSE, i = 0; i <= nCount-1; i++ ) {
if( szBuffer[i] == '\n' ) { bDelimFount = TRUE; i++; break; }
}
nCount = i; nTotal += nCount;
szBuffer[nCount] = 0x00;
if( nCount >= 1 && szBuffer[nCount-1] == '\n' ) { szBuffer[nCount-1] = 0x00; nCount--; }
if( nCount >= 1 && szBuffer[nCount-1] == '\r' ) { szBuffer[nCount-1] = 0x00; nCount--; }
GetTail() += szBuffer;
if( bDelimFount ) AddTail("");
file.Seek(nTotal, CFile::begin);
}
file.Close();
} catch( CException * ex ) {
ex->ReportError( MB_OK | MB_ICONSTOP );
ex->Delete(); return FALSE;
}
return TRUE;
}
BOOL CMemText::FileSave(LPCTSTR lpszPathName)
{
try {
CFile file(lpszPathName, CFile::modeReadWrite | CFile::typeBinary | CFile::shareDenyWrite);
POSITION pos = GetHeadPosition();
while( pos ) {
CString & rString = GetNext(pos);
INT nLength = rString.GetLength();
file.Write( rString, nLength );
if( pos ) file.Write( "\r\n", 2 );
}
file.Close();
} catch( CException * ex ) {
ex->ReportError( MB_OK | MB_ICONSTOP );
ex->Delete(); return FALSE;
}
return TRUE;
}
void CMemText::AppendText(CMemText & rBlock)
{
POSITION posBeg = rBlock.GetHeadPosition();
if( ! posBeg ) { return; }
POSITION posEnd = GetTailPosition();
if( ! posEnd ) { AddTail( & rBlock ); return; }
GetAt(posEnd) += rBlock.GetNext(posBeg);
for(INT i = 1; i < rBlock.GetCount(); i++) AddTail( rBlock.GetNext(posBeg) );
}
CMemText & CMemText::operator=(const CMemText & rBlock) {
RemoveAll();
POSITION pos = rBlock.GetHeadPosition();
while( pos ) AddTail( rBlock.GetNext(pos) );
return * this;
}
void CMemText::MakeUpperCase()
{
POSITION pos = GetHeadPosition();
while( pos ) {
CString & rString = GetNext( pos );
rString.MakeUpper();
}
}
void CMemText::MakeLowerCase()
{
POSITION pos = GetHeadPosition();
while( pos ) {
CString & rString = GetNext( pos );
rString.MakeLower();
}
}
void CMemText::MakeInvertCase()
{
POSITION pos = GetHeadPosition();
while( pos ) {
CString & rString = GetNext( pos );
::MakeInvertCase( rString );
}
}
void CMemText::MakeCapitalize()
{
POSITION pos = GetHeadPosition();
while( pos ) {
CString & rString = GetNext( pos );
::MakeCapitalize( rString );
}
}
INT CMemText::GetMaxLength()
{
INT nLen, nMaxLen = 0;
POSITION pos = GetHeadPosition();
while( pos ) {
CString & rString = GetNext( pos );
nLen = rString.GetLength();
if( nLen > nMaxLen ) nMaxLen = nLen;
}
return nMaxLen;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -