UINT CBoxScript::ParseScriptTextCodePage(LPCSTR pstrText, int nCount) { static struct { char *pstrName; int nSize; }CmdName[] = { {"#codepage", 9} }; #define CMD_COUNT (sizeof(CmdName) / sizeof(CmdName[0])) int i; LPCSTR pstrTemp, pstrTemp1; int nTempCount; int nLineCount = 1; while(nCount > 0 && IsBlank(pstrText[0])) { if(pstrText[0] == '\n') nLineCount ++; pstrText ++; nCount --; } while(nCount > 0 && pstrText[0] == '#') { for(i = 0; i < CMD_COUNT; i ++) if(nCount > CmdName[i].nSize && !_strnicmp(pstrText, CmdName[i].pstrName, CmdName[i].nSize) && IsBlankChar(pstrText[CmdName[i].nSize])) break; if(i == CMD_COUNT) break; pstrText += CmdName[i].nSize; nCount -= CmdName[i].nSize; while(nCount > 0 && IsBlankChar(pstrText[0])) { pstrText ++; nCount --; } if(nCount > 0 && pstrText[0] == '\"') { pstrText ++; nCount --; } pstrTemp = pstrText; nTempCount = nCount; while(nTempCount > 0 && !IsLineChar(pstrTemp[0])) { pstrTemp ++; nTempCount --; } pstrTemp1 = pstrTemp; while(pstrTemp1 > pstrText && IsBlankChar(pstrTemp1[0])) pstrTemp1 --; if(pstrTemp1 > pstrText && pstrTemp1[-1] == '\"') pstrTemp1 --; CStringA strValue; strValue.SetString(pstrText, (int)(pstrTemp1 - pstrText)); pstrText = pstrTemp; nCount = nTempCount; if (i == 0) return atoi(strValue); while(nCount > 0 && IsBlank(pstrText[0])) { if(pstrText[0] == '\n') nLineCount ++; pstrText ++; nCount --; } } return 0; }
int CBoxScript::ParseScriptText(LPCSTR pstrText, int nCount, CStringA& strScriptText, int nIncludeFlagIndex) { CStringA strTempText; if(nCount >= 2 && (BYTE)pstrText[0] == 0xff && (BYTE)pstrText[1] == 0xfe) { /* int _nTempCount = WideCharToMultiByte(_AtlGetConversionACP(), 0, LPWSTR(pstrText + 2), (nCount - 2) / 2, NULL, 0, NULL, NULL); LPSTR _pstr = strTempText.GetBuffer(_nTempCount); WideCharToMultiByte(_AtlGetConversionACP(), 0, LPWSTR(pstrText + 2), (nCount - 2) / 2, _pstr, _nTempCount, NULL, NULL); strTempText.ReleaseBuffer(_nTempCount); */ //发现是Unicode则检查是否已经存在CodePage,存在就转换到当前CodePage,否则转换成UTF8同时设定m_CodePage为65001 if (m_uiCodePage == 0) m_uiCodePage = CP_UTF8; int _nTempCount = WideCharToMultiByte(m_uiCodePage, 0, LPWSTR(pstrText + 2), (nCount - 2) / 2, NULL, 0, NULL, NULL); LPSTR _pstr = strTempText.GetBuffer(_nTempCount); WideCharToMultiByte(m_uiCodePage, 0, LPWSTR(pstrText + 2), (nCount - 2) / 2, _pstr, _nTempCount, NULL, NULL); strTempText.ReleaseBuffer(_nTempCount); pstrText = strTempText; nCount = strTempText.GetLength(); } else if(nCount >= 3 && (BYTE)pstrText[0] == 0xEF && (BYTE)pstrText[1] == 0xBB && (BYTE)pstrText[2] == 0xBF) { pstrText += 3; nCount -= 3; if (m_uiCodePage && m_uiCodePage != CP_UTF8) { CStringW strTempTextW; int _nTempCount = MultiByteToWideChar(CP_UTF8, 0, pstrText, nCount, NULL, 0); LPWSTR _pstrW = strTempTextW.GetBuffer(_nTempCount); MultiByteToWideChar(CP_UTF8, 0, pstrText, nCount, _pstrW, _nTempCount); strTempTextW.ReleaseBuffer(_nTempCount); _nTempCount = WideCharToMultiByte(m_uiCodePage, 0, strTempTextW, strTempTextW.GetLength(), NULL, 0, NULL, NULL); LPSTR _pstr = strTempText.GetBuffer(_nTempCount); WideCharToMultiByte(m_uiCodePage, 0, strTempTextW, strTempTextW.GetLength(), _pstr, _nTempCount, NULL, NULL); strTempText.ReleaseBuffer(_nTempCount); pstrText = strTempText; nCount = strTempText.GetLength(); } else m_uiCodePage = CP_UTF8; } else { UINT uiCodePage = ParseScriptTextCodePage(pstrText, nCount); if (uiCodePage == 0) uiCodePage = GetACP(); //if (m_uiCodePage == 0) // m_uiCodePage = CP_UTF8; if (m_uiCodePage && m_uiCodePage != uiCodePage) { CStringW strTempTextW; int _nTempCount = MultiByteToWideChar(uiCodePage, 0, pstrText, nCount, NULL, 0); LPWSTR _pstrW = strTempTextW.GetBuffer(_nTempCount); MultiByteToWideChar(uiCodePage, 0, pstrText, nCount, _pstrW, _nTempCount); strTempTextW.ReleaseBuffer(_nTempCount); _nTempCount = WideCharToMultiByte(m_uiCodePage, 0, strTempTextW, strTempTextW.GetLength(), NULL, 0, NULL, NULL); LPSTR _pstr = strTempText.GetBuffer(_nTempCount); WideCharToMultiByte(m_uiCodePage, 0, strTempTextW, strTempTextW.GetLength(), _pstr, _nTempCount, NULL, NULL); strTempText.ReleaseBuffer(_nTempCount); pstrText = strTempText; nCount = strTempText.GetLength(); } else { m_uiCodePage = uiCodePage; } } static struct { char *pstrName; int nSize; }CmdName[] = { {"#include", 8}, {"#language", 9}, {"#debug", 6}, {"#timeout", 8}, {"#transaction", 12}, {"#codepage", 9} }; #define CMD_COUNT (sizeof(CmdName) / sizeof(CmdName[0])) int i; LPCSTR pstrTemp, pstrTemp1; int nTempCount; int nLineCount = 1; while(nCount > 0 && IsBlank(pstrText[0])) { if(pstrText[0] == '\n') nLineCount ++; pstrText ++; nCount --; } while(nCount > 0 && pstrText[0] == '#') { for(i = 0; i < CMD_COUNT; i ++) if(nCount > CmdName[i].nSize && !_strnicmp(pstrText, CmdName[i].pstrName, CmdName[i].nSize) && IsBlankChar(pstrText[CmdName[i].nSize])) break; if(i == CMD_COUNT) break; pstrText += CmdName[i].nSize; nCount -= CmdName[i].nSize; while(nCount > 0 && IsBlankChar(pstrText[0])) { pstrText ++; nCount --; } if(nCount > 0 && pstrText[0] == '\"') { pstrText ++; nCount --; } pstrTemp = pstrText; nTempCount = nCount; while(nTempCount > 0 && !IsLineChar(pstrTemp[0])) { pstrTemp ++; nTempCount --; } pstrTemp1 = pstrTemp; while(pstrTemp1 > pstrText && IsBlankChar(pstrTemp1[0])) pstrTemp1 --; if(pstrTemp1 > pstrText && pstrTemp1[-1] == '\"') pstrTemp1 --; CStringA strValue; strValue.SetString(pstrText, (int)(pstrTemp1 - pstrText)); pstrText = pstrTemp; nCount = nTempCount; switch(i) { case 0: strValue.MakeLower(); if(LoadScriptFile(BOX_CA2CT(strValue), strScriptText, nLineCount)) return 500; break; case 1: m_strLanguage = strValue; break; case 2: m_bEnableDebug = !strValue.CompareNoCase("on") || !strValue.CompareNoCase("true"); if(!strValue.CompareNoCase("step")) m_bStepDebug = TRUE; break; case 3: m_pHost->m_nTimeout = atoi(strValue); break; case 4: if(!strValue.CompareNoCase("Required")) m_nTransaction = 3; else if(!strValue.CompareNoCase("Requires_New")) m_nTransaction = 2; else if(!strValue.CompareNoCase("Supported")) m_nTransaction = 1; else if(!strValue.CompareNoCase("Not_Supported")) m_nTransaction = 0; break; case 5: //(uiCodePage) break; } while(nCount > 0 && IsBlank(pstrText[0])) { if(pstrText[0] == '\n') nLineCount ++; pstrText ++; nCount --; } } AddLineMap(nIncludeFlagIndex, nLineCount); strScriptText.Append(pstrText, nCount); strScriptText += _T("\r\n"); m_nScriptLine ++; pstrTemp = pstrText; nTempCount = nCount; while(nTempCount > 0) { if(pstrTemp[0] == '\n') m_nScriptLine ++; pstrTemp ++; nTempCount --; } return 0; }