CStringW TextFormat::UTF8ToUnicode(const CString& in)
{
  int len = ::UTF8ToUnicode(in,in.GetLength(),NULL,0);
  CStringW out;
  LPWSTR ptr = out.GetBufferSetLength(len);
  ::UTF8ToUnicode(in,in.GetLength(),ptr,len);
  out.ReleaseBuffer(len);
  return out;
}
CStringW TextFormat::Latin1ToUnicode(const CString& in)
{
  CStringW text;
  int len = in.GetLength();
  LPWSTR textPtr = text.GetBufferSetLength(len);
  for (int i = 0; i < len; i++)
    textPtr[i] = (wchar_t)(unsigned char)in[i];
  text.ReleaseBuffer(len);
  return text;
}
Exemple #3
0
BOOL CStdioFileEx::ReadWideString(CStringW& rString)
{
   _ASSERTE(m_pStream);
   rString = L"";// empty string without deallocating
   
   if(m_bIsUnicodeText)
   {
      // If at position 0, discard byte-order mark before reading
      if(GetPosition() == 0)
      {
         wchar_t bom;
         Read(&bom, sizeof(wchar_t));
      }
      const int nMaxSize = 128;
      LPWSTR lpsz = rString.GetBuffer(nMaxSize);
      LPWSTR lpszResult;
      int nLen = 0;
      for (;;)
      {
         lpszResult = fgetws(lpsz, nMaxSize+1, m_pStream);
         rString.ReleaseBuffer();
         
         // handle error/eof case
         if (lpszResult == NULL && !feof(m_pStream))
         {
            Afx_clearerr_s(m_pStream);
            AfxThrowFileException(CFileException::genericException, _doserrno,
               m_strFileName);
         }
         
         // if string is read completely or EOF
         if (lpszResult == NULL || (nLen = (int)lstrlenW(lpsz)) < nMaxSize || lpsz[nLen-1] == '\n')
            break;
         
         nLen = rString.GetLength();
         lpsz = rString.GetBuffer(nMaxSize + nLen) + nLen;
      }
      //remove crlf if exist.
      nLen = rString.GetLength();
      if (nLen > 1 && rString.Mid(nLen-2) == L"\r\n")
      {
         rString.GetBufferSetLength(nLen-2);
      }
      return rString.GetLength() > 0;
   }
   else
   {
      CStringA ansiString;
      BOOL bRetval = ReadAnsiString(ansiString);
      //setlocale(LC_ALL, "chs_chn.936");//no need
      rString = ansiString;
      return bRetval;
   }
}
Exemple #4
0
CStringW Util::String::GBKToUnicode( CStringA gbk )
{
	CStringW strUnicode;
	DWORD dwMinSize = 0;
	dwMinSize = MultiByteToWideChar(936, NULL, gbk, gbk.GetLength(),NULL, 0);
	strUnicode.GetBufferSetLength(dwMinSize);
	LPWSTR lpszStr =  strUnicode.GetBuffer();
	INT ok = MultiByteToWideChar(936, NULL, gbk, gbk.GetLength(), lpszStr, dwMinSize);
	strUnicode.ReleaseBuffer();
	return strUnicode;
}
Exemple #5
0
void OutputStream::PutString(LPCWSTR fmt, ...)
{
    CStringW str;

    va_list args;
    va_start(args, fmt);
    int len = _vscwprintf(fmt, args) + 1;
    if(len > 0) vswprintf_s(str.GetBufferSetLength(len), len, fmt, args);
    va_end(args);

    LPCWSTR s = str;
    while(*s) PutChar(*s++);
}
Exemple #6
0
LPCWSTR StringUTF8ToWChar(LPCSTR sUTF8, CStringW &sWChar, int iChars/* = -1*/)
{
	if (!sUTF8)
		return NULL;

	sWChar.Empty();
	int iLen = MultiByteToWideChar(CP_UTF8, 0, sUTF8, iChars, NULL, 0);
	if (iLen > 0) {
		LPWSTR sBuf = sWChar.GetBufferSetLength(iLen);
		iLen = MultiByteToWideChar(CP_UTF8, 0, sUTF8, iChars, sBuf, iLen);
		sWChar.ReleaseBufferSetLength(sBuf[iLen - 1] ? iLen : iLen - 1);
		return (iLen > 0) ? sWChar.GetString() : NULL;
	}

	return *sUTF8 != 0 ? sWChar.GetString() : NULL;
}
Exemple #7
0
LPCWSTR StringCharToWChar(LPCSTR sChar, CStringW &sWChar, int iChars/* = -1*/, UINT codepage/* = CP_ACP*/)
{
	if (!sChar)
		return NULL;

	sWChar.Empty();
	int iLen = MultiByteToWideChar(codepage, 0, sChar, iChars, NULL, 0);
	if (iLen > 0) {
		LPWSTR sBuf = sWChar.GetBufferSetLength(iLen);
		MultiByteToWideChar(codepage, 0, sChar, iChars, sBuf, iLen);
		sWChar.ReleaseBufferSetLength(sBuf[iLen - 1] ? iLen : iLen - 1);
		return (iLen > 0) ? sWChar.GetString() : NULL;
	}

	return (*sChar != 0) ? sWChar.GetString() : NULL;
}
CString TextFormat::ToXML_UTF8(const CStringW& in)
{
  // First escape any characters that will cause problems in the XML
  CStringW escText;
  int escLen = ::EscapeXML(in,in.GetLength(),NULL,0);
  LPWSTR escPtr = escText.GetBufferSetLength(escLen);
  ::EscapeXML(in,in.GetLength(),escPtr,escLen);
  escText.ReleaseBuffer(escLen);

  // Convert the escaped text to UTF-8
  CString utfText;
  int utfLen = ::AtlUnicodeToUTF8(escText,escText.GetLength(),NULL,0);
  LPSTR utfPtr = utfText.GetBufferSetLength(utfLen);
  ::AtlUnicodeToUTF8(escText,escText.GetLength(),utfPtr,utfLen);
  utfText.ReleaseBuffer(utfLen);
  return utfText;
}