void load_file2(std::wstring& wstrOut, std::wistream& wistreamIn)
{
	wstrOut.erase();
	//bad() 如果出现错误则返回true 
	if(wistreamIn.bad()) 
		return;
	wstrOut.reserve(wistreamIn.rdbuf()->in_avail());
	wchar_t c;
	//get() 读取字符 
	while(wistreamIn.get(c))
	{
		if(wstrOut.capacity() == wstrOut.size())
			wstrOut.reserve(wstrOut.capacity() * 3);
		wstrOut.append(1, c);
	}
}
예제 #2
0
파일: 1.cpp 프로젝트: dennis714/tracer
void func (std::string a, std::wstring b)
{
	printf ("%s\n", a.c_str());
	printf ("%d\n", a.size());
	printf ("%d\n", a.capacity());

	wprintf (L"%s\n", b.c_str());
	printf ("%d\n", b.size());
	printf ("%d\n", b.capacity());
};
예제 #3
0
BOOL DownloadWebPageUnicode(std::wstring& page, HINTERNET hInternet, LPCWSTR url)
{
	TRACEST(_T("DownloadWebPageUnicode"), CW2CT(url));
	BOOL bDownloadSuccess = FALSE;
	if (hInternet == NULL)
		return FALSE;
	HINTERNET hFile = InternetOpenUrl(hInternet, url, NULL, 0, INTERNET_FLAG_NO_CACHE_WRITE, 0);
	if (hFile)
	{   
		page.clear();
		DWORD dwSize = 0;
		INT headerCP = 0;//Latin1
		INT pageCP = 0;//Latin1
		INT workingCP = 1252;//Latin1
		if(!HttpQueryInfo(hFile, HTTP_QUERY_RAW_HEADERS_CRLF, 0, &dwSize, 0))
		{
			if (GetLastError()==ERROR_INSUFFICIENT_BUFFER)
			{
				SetLastError(0);
				LPVOID lpOutBuffer = new CHAR[dwSize];
				if (HttpQueryInfo(hFile, HTTP_QUERY_RAW_HEADERS_CRLF, lpOutBuffer, &dwSize, 0))
				{
					//TRACE((LPCTSTR)lpOutBuffer);
					headerCP = GetWindowsCodePageW((LPCTSTR)lpOutBuffer);
				}
				delete[] lpOutBuffer;
			}		
		}

		DWORD pageSize = 0;
		DWORD pageSizeLen = sizeof(pageSize);
		if (::HttpQueryInfo(hFile, HTTP_QUERY_CONTENT_LENGTH, &pageSize, &pageSizeLen, NULL))
		{
			if (pageSize > 0)
				page.reserve(pageSize + 10);
		}
		if (page.capacity() < 1000)
			page.reserve(50000);
		const int bufferSize = 8192;
		const int bufferSizeU = 2 * bufferSize;
		CHAR bf[bufferSize + 1];
		TCHAR bfU[bufferSizeU];
		unsigned long nSize = 0;
		BOOL bReadSuccess = TRUE;
		BOOL bFirstTime = TRUE;
		while(bReadSuccess)
		{
			bReadSuccess = InternetReadFile(hFile, bf, bufferSize, &nSize);
			if (bReadSuccess)
			{
				if (nSize == 0)
				{
					//TRACE(_T("@3 DownloadWebPage. InternetReadFile Finished\r\n"));
					bDownloadSuccess = TRUE;
					break;
				}
				if (bFirstTime)
				{
					bFirstTime = FALSE;
					bf[nSize] = 0;
					pageCP = GetWindowsCodePageA(bf);
					if (headerCP != pageCP)
					{
						TRACE(_T("@3 DownloadWebPage. CodePage Differs (header:%d - page:%d)\r\n"),
							headerCP, pageCP);
					}
					if (pageCP > 0)	//===Choose the Codepage detected from page if available
						workingCP = pageCP;
					else if (headerCP > 0) //===Else select the Codepage detected from headers if available
						workingCP = headerCP;
					//===Otherwise keep the original 1252 (latin 1)
				}
				if (nSize > 0)
				{
					INT bfLen = MultiByteToWideChar(workingCP, 0, bf, nSize, 0, 0);
					if (bfLen < bufferSizeU)
					{
						MultiByteToWideChar(workingCP, 0, bf, nSize, bfU, bfLen);
						page.append(bfU, bfLen);
					}
					else
					{
						TRACE(_T("@1 DownloadWebPageUnicode. Unicode buffer not enough\r\n"));
						bReadSuccess = FALSE;
					}
				}
			}
			else
				HandleInternetError(_T("DownloadWebPageUnicode. InternetReadFile"));
		}
		InternetCloseHandle(hFile);
	}
	else
		HandleInternetError(_T("DownloadWebPageUnicode. InternetOpenUrl"));
	return bDownloadSuccess;
}