INXString CMarkup::x_TextFromDoc( int nLeft, int nRight ) const { // Convert XML friendly text to text as seen outside XML document // ampersand escape codes replaced with special characters e.g. convert "6>7" to "6>7" // Conveniently the result is always the same or shorter in byte length // static char* szaCode[] = { "lt;","amp;", "gt;", "apos;", "quot;" }; static int anCodeLen[] = { 3,4,3,5,5 }; static char* szSymbol = "<&>\'\""; INXString csText; const char* pSource = (const char*)m_csDoc; int nDestSize = nRight - nLeft + 1; char* pDest = csText.GetBuffer(nDestSize); //LPTSTR pDest = csText.GetBuffer(nDestSize); int nLen = 0; int nCharLen = 0; int nChar = nLeft; while ( nChar <= nRight ) { if ( pSource[nChar] == '&') { // Look for matching &code; bool bCodeConverted = false; for ( int nMatch = 0; nMatch < 5; ++nMatch ) { if ( nChar <= nRight - anCodeLen[nMatch] && strncmp(szaCode[nMatch],&pSource[nChar+1],anCodeLen[nMatch]) == 0 ) { // Insert symbol and increment index past ampersand semi-colon pDest[nLen++] = szSymbol[nMatch]; nChar += anCodeLen[nMatch] + 1; bCodeConverted = true; break; } } // If the code is not converted, leave it as is if ( ! bCodeConverted ) { pDest[nLen++] = _T('&'); ++nChar; } } else // not & { nCharLen = _tclen(&pSource[nChar]); _tccpy( &pDest[nLen], &pSource[nChar] ); nLen += nCharLen; nChar += nCharLen; } } csText.ReleaseBuffer(nLen); return csText; }
CStdString CMarkupSTL::x_TextFromDoc( int nLeft, int nRight ) const { // Convert XML friendly text to text as seen outside XML document // replacing ampersand escape codes with special characters // E.g. convert "6>7" to "6>7" // // Conveniently the result is always the same or shorter in length // static _TCHAR* szaCode[] = { _T("lt;"),_T("amp;"),_T("gt;"),_T("apos;"),_T("quot;") }; static int anCodeLen[] = { 3,4,3,5,5 }; static _TCHAR* szSymbol = _T("<&>\'\""); CStdString csText; const _TCHAR* pSource = m_csDoc; int nDestSize = nRight - nLeft + 1; _TCHAR* pDest = csText.GetBuffer(nDestSize); int nLen = 0; int nCharLen; int nChar = nLeft; while ( nChar <= nRight ) { if ( pSource[nChar] == _T('&') ) { // Look for matching &code; for ( int nMatch = 0; nMatch < 5; ++nMatch ) { if ( nChar <= nRight - anCodeLen[nMatch] && _tcsncmp(szaCode[nMatch],&pSource[nChar+1],anCodeLen[nMatch]) == 0 ) { pDest[nLen++] = szSymbol[nMatch]; nChar += anCodeLen[nMatch] + 1; break; } } // If no match is found it means XML doc is invalid // no devastating harm done, ampersand code will just be left in result if ( nMatch == 5 ) { pDest[nLen++] = _T('&'); ++nChar; } } else { nCharLen = _tclen(&pSource[nChar]); _tccpy( &pDest[nLen], &pSource[nChar] ); nLen += nCharLen; nChar += nCharLen; } } csText.ReleaseBuffer(nLen); return csText; }
CStdString CMarkupSTL::x_TextToDoc( const char * szText, bool bAttrib ) const { // Convert text as seen outside XML document to XML friendly // replacing special characters with ampersand escape codes // E.g. convert "6>7" to "6>7" // // < less than // & ampersand // > greater than // // and for attributes: // // ' apostrophe or single quote // " double quote // static _TCHAR* szaReplace[] = { _T("<"),_T("&"),_T(">"),_T("'"),_T(""") }; const _TCHAR* pFind = bAttrib?_T("<&>\'\""):_T("<&>"); CStdString csText; const _TCHAR* pSource = szText; int nDestSize = _tcslen(pSource); nDestSize += nDestSize / 10 + 7; _TCHAR* pDest = csText.GetBuffer(nDestSize); int nLen = 0; _TCHAR cSource = *pSource; _TCHAR* pFound; while ( cSource ) { if ( nLen > nDestSize - 6 ) { csText.ReleaseBuffer(nLen); nDestSize *= 2; pDest = csText.GetBuffer(nDestSize); } if ( (pFound=_tcschr(pFind,cSource)) != NULL ) { pFound = szaReplace[pFound-pFind]; _tcscpy(&pDest[nLen],pFound); nLen += _tcslen(pFound); } else { _tccpy( &pDest[nLen], pSource ); ++nLen; } pSource += _tclen( pSource ); cSource = *pSource; } csText.ReleaseBuffer(nLen); return csText; }
bool CExampleDlg::ToUCS2(LPTSTR pcOut, int nOutLen, const char* kpcIn) { if (strlen(kpcIn) > 0) { // Do the conversion normally return MultiByteToWideChar(CP_UTF8, 0, kpcIn, -1, pcOut, nOutLen) > 0; } else if (nOutLen > 1) { // Can't distinguish no bytes copied from an error, so handle // an empty input string as a special case. _tccpy(pcOut, _T("")); return true; } else { // Not enough room to do anything! return false; } }
EESTATUS GetExpr ( uint radix, PEEHSTR phStr, ulong *pEnd ) { EESTATUS retval = EENOMEMORY; char *pStr; char *pExprStr; HDEP hExprStr; int len; ulong strIndex; UINT nLen; Unreferenced( radix ); //M00KLUDGE - this routine will eventuall have to walk the bound tree // and format the expression because of ambiguous expressions // use the saved original string if there is one // (in case the expression has been modified) if (pExState->hExStrSav) { hExprStr = pExState->hExStrSav; len = pExState->ExLenSav; strIndex = pExState->strIndexSav; } else { hExprStr = pExState->hExStr; len = pExState->ExLen; strIndex = pExState->strIndex; } pExprStr = (char *) MemLock (hExprStr); nLen = len+1; if (((*phStr = MemAllocate (nLen)) != 0)) { // the expression has been bound and memory allocated char tempBuf[TYPESTRMAX]; UINT nb; UINT nIndex = 0; BOOL fHSYM; char *psz; ulong nAdj = 0; pStr = (char *) MemLock (*phStr); for (psz = pExprStr; (psz < pExprStr + len) && *psz; psz = _tcsinc (psz)) { fHSYM = FALSE; if (*psz == HSYM_MARKER) { HSYM hSym = GetHSYMFromHSYMCode(psz + 1); psz += HSYM_CODE_LEN; // skip embedded HSYM code fHSYM = TRUE; DASSERT (hSym); if (GetNameFromHSYM(tempBuf, hSym) == FALSE) { pExState->err_num = ERR_INTERNAL; MemUnLock(*phStr); MemUnLock(hExprStr); return EEGENERAL; } nb = _tcslen(tempBuf); // compute adjustment for strIndex: // if an HSYM is to the left of strIndex, // strIndex needs to be adjusted if (psz <= pExprStr + strIndex) nAdj += (nb - sizeof (char) - HSYM_CODE_LEN); } else { nb = 1; } // check if there is space in the buffer and // copy nb characters to the destination string if (nIndex + nb > nLen-1) { // there is not enough space, grow buffer MemUnLock(*phStr); nLen += NAMESTRMAX; if ((*phStr = MemReAlloc(*phStr, nLen)) == 0){ MemUnLock(hExprStr); return EENOMEMORY; } pStr = (char *) MemLock (*phStr); } if (fHSYM) { // copy name from tembBuf memcpy(pStr+nIndex, tempBuf, nb); nIndex += nb; } else { // copy a single character from pExprStr _tccpy (pStr + nIndex, psz); nIndex += _tclen (psz); } } pStr[nIndex++] = 0; MemUnLock (*phStr); // Reallocate the buffer in case it is too large DASSERT (nIndex <= nLen); if (nIndex < nLen && (*phStr = MemReAlloc(*phStr, nIndex)) == 0){ MemUnLock(hExprStr); return EENOMEMORY; } retval = EENOERROR; *pEnd = strIndex + nAdj; } MemUnLock (hExprStr); return retval; }