bool CDTManager::SetContiInfo( CString file,CString& url,int64& len,int& cur ) { CAtlFile f; if(ERROR_SUCCESS!=f.Create(file,FILE_GENERIC_WRITE,FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,CREATE_ALWAYS)!=ERROR_SUCCESS) { return false; } f.Write(&len,sizeof len); f.Write(&cur,sizeof cur); f.Write(url.GetBuffer(),url.GetLength()*2+2); f.Close(); return true; }
bool Speller::SaveWords( LPCTSTR path, const WordContainer& words ) { CAtlFile file; bool result = false; if (SUCCEEDED(file.Create(path, GENERIC_WRITE, FILE_SHARE_READ, OPEN_ALWAYS))) { WordContainer::const_iterator it = words.begin(); std::vector<char> data; for (; it != words.end(); ++it) { data.clear(); const StringType text = *it + _T("\n"); UTF16toUTF8(text.c_str(), text.size(), data); if (!data.empty()) file.Write(&data[0], static_cast<DWORD>(data.size())); } result = true; } return result; }
DWORD CALLBACK LogViewRE::StreamFileOut(DWORD_PTR dwCookie, LPBYTE pbBuff, LONG cb, LONG *pcb) { CAtlFile* file = (CAtlFile*)dwCookie; if (!file) return ERROR_INVALID_FUNCTION; return file->Write(pbBuff, cb, (DWORD*)pcb); }
BOOL file_put_contents(LPCTSTR lpszFilename, BYTE *pBuffer, INT nLen) { CAtlFile file; if( FAILED( file.Create(lpszFilename, GENERIC_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE, CREATE_ALWAYS) ) ) return FALSE; file.Write( pBuffer, nLen ); file.Close(); return TRUE; }
/** * @param uNotifyCode - notification code if the message is from a control. If the message is from an accelerator, this value is 1. If the message is from a menu, this value is zero. * @param nID - specifies the identifier of the menu item, control, or accelerator. * @param hWndCtl - handle to the control sending the message if the message is from a control. Otherwise, this parameter is NULL. */ void CExpressModeDlg::OnSave(UINT /*uNotifyCode*/, int /*nID*/, HWND /*hWndCtl*/) { try { if (! ValidateXMLDocument()) return; CString strLogFile; GetReportFileName(strLogFile); CCustomFileDialog dlgSaveFile(FALSE, _T("xml"), strLogFile, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT | OFN_PATHMUSTEXIST, _T("Log Files\0*.log\0All Files\0*.*\0"), m_hWnd); if (dlgSaveFile.DoModal(m_hWnd) == IDOK) { CAtlFile fileLog; if (fileLog.Create(dlgSaveFile.m_szFileName, GENERIC_WRITE, 0, CREATE_ALWAYS) == S_OK) { CString strLogText; GetErrorLog(strLogText); CT2CW pchUnicodeText(strLogText); int nDestSize = AtlUnicodeToUTF8(pchUnicodeText, strLogText.GetLength(), NULL, 0); if (nDestSize >= 0) { static const BYTE arrUTF8Preamble[] = { 0xEF, 0xBB, 0xBF }; int nTotalDestSize = sizeof(arrUTF8Preamble) + nDestSize; boost::scoped_array<CHAR> pchDest(new CHAR[nTotalDestSize]); CopyMemory(pchDest.get(), arrUTF8Preamble, sizeof(arrUTF8Preamble)); AtlUnicodeToUTF8(pchUnicodeText, strLogText.GetLength(), pchDest.get() + sizeof(arrUTF8Preamble), nDestSize); fileLog.Write(pchDest.get(), nTotalDestSize); } } } } catch (std::exception& error) { HandleException(error); } }
virtual void _output_file(CAtlString& string) { _file.Write(static_cast<const TCHAR*>(string), string.GetLength()); }
HRESULT FAsyncDownload::FHttpDownloadTP::ProcessDownload(FAsyncDownData *pData) { HRESULT hr = E_FAIL; FString ReqUrl = pData->m_pUrlInfo->m_DownloadUrl; UrlUnescapeInPlace(ReqUrl.GetBuffer(), 0); CUrl url; url.CrackUrl(ReqUrl); const tchar* pszUserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)"; FHInternet hIn = NULL; if (g_AppSettings.m_Proxy.GetLength() > 0) { hIn = InternetOpen(pszUserAgent, INTERNET_OPEN_TYPE_PROXY, g_AppSettings.m_Proxy, g_AppSettings.m_ProxyA, 0); } else { hIn = InternetOpen(pszUserAgent, INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0); } if (NULL == hIn) return E_HTTP_NET_ERROR; FHInternet hCon = InternetConnect(hIn, url.GetHostName(), url.GetPortNumber(), url.GetUserName(), url.GetPassword(), INTERNET_SERVICE_HTTP, 0, 0); if (NULL == hCon) { _DBGAlert("**FAsyncDownload::FHttpDownloadTP::ProcessDownload: InternetConnect() failed: %d\n", GetLastError()); return E_HTTP_NET_ERROR; } ULONG ulRecvTimeout = 15000; InternetSetOption(hCon, INTERNET_OPTION_RECEIVE_TIMEOUT, &ulRecvTimeout, sizeof(ULONG)); FString StrRes = url.GetUrlPath(); StrRes+= url.GetExtraInfo(); FHInternet hReq = HttpOpenRequest(hCon, "GET", StrRes, NULL, NULL, NULL, INTERNET_FLAG_NO_CACHE_WRITE | INTERNET_FLAG_DONT_CACHE, 0); if (NULL == hReq) { _DBGAlert("**FAsyncDownload::FHttpDownloadTP::ProcessDownload: HttpOpenRequest() failed: %d\n", GetLastError()); return E_HTTP_NET_ERROR; } size_type FileSize = 0; if (!(pData->m_pUrlInfo->m_dwDownloadFlags & HTTP_FLAG_NO_RESUME)) FileSize = GetFileSize(pData->m_pUrlInfo->m_DownloadFile); // See if file already exists on the disk. if (FileSize > 0) { FString StrRange; StrRange.Format("Range: bytes=%I64d-", FileSize); HttpAddRequestHeaders(hReq, StrRange, StrRange.GetLength(), HTTP_ADDREQ_FLAG_ADD_IF_NEW); } FString StrVersion; StrVersion.Format("LTV_VERSION: %s", g_AppSettings.m_AppVersion); HttpAddRequestHeaders(hReq, StrVersion, StrVersion.GetLength(), HTTP_ADDREQ_FLAG_ADD_IF_NEW); if (!HttpSendRequest(hReq, NULL, 0, NULL, 0)) { int err = GetLastError(); _DBGAlert("**FAsyncDownload::FHttpDownloadTP::ProcessDownload: HttpSendRequest() failed: %d (0x%x)\n", err, HRESULT_FROM_WIN32(err)); InternetCloseHandle(hCon); InternetCloseHandle(hIn); return E_HTTP_NET_ERROR; } const DWORD dwBufferSize = 8192; char pBuffer[dwBufferSize]; FHttpConnection FConn = hReq; DWORD dwStatusCode = FConn.GetStatusCode(); FString ReqContentType = pData->m_pUrlInfo->m_ContentType; pData->m_pUrlInfo->m_ContentType = FConn.GetHeader(HTTP_QUERY_CONTENT_TYPE); pData->m_pUrlInfo->m_dwStatusCode = dwStatusCode; if (!MatchContentType(ReqContentType, pData->m_pUrlInfo->m_ContentType)) { _DBGAlert("**FAsyncDownload::FHttpDownloadTP::ProcessDownload: Content type mismatch: %s/%s\n", ReqContentType, pData->m_pUrlInfo->m_ContentType); return E_NOINTERFACE; //E_NOINTERFACE = content type mismatch } if (dwStatusCode == 416 && FileSize > 0) { _DBGAlert("FAsyncDownload::FHttpDownloadTP::ProcessDownload: Server status code: %d. Download complete\n", dwStatusCode); return S_OK; } if (dwStatusCode < 200 || dwStatusCode > 206) { _DBGAlert("**FAsyncDownload::FHttpDownloadTP::ProcessDownload: Server status code: %d\n", dwStatusCode); if (dwStatusCode == 404) return E_HTTP_NOTFOUND; return E_HTTP_INVALID_STATUS; } CAtlFile OutFile; if (pData->m_pUrlInfo->m_dwDownloadFlags & HTTP_FLAG_NO_RESUME) DeleteFile(pData->m_pUrlInfo->m_DownloadFile); hr = OutFile.Create(pData->m_pUrlInfo->m_DownloadFile, GENERIC_WRITE, 0, OPEN_ALWAYS); if (FAILED(hr)) { _DBGAlert("**FAsyncDownload::FHttpDownloadTP::ProcessDownload: CreateFile failed: 0x%x, %d : %s\n", hr, GetLastError(), pData->m_pUrlInfo->m_DownloadFile); return E_HTTP_WRITE_FILE; } size_type llTotalRead = 0; size_type llSizeMax = 0; size_type ContentLen = FConn.GetContentLength(); pData->m_pUrlInfo->m_ContentLength = ContentLen; if (dwStatusCode == 206) { FString FStrRange = FConn.GetHeader(HTTP_QUERY_CONTENT_RANGE); if (FStrRange) { //Content-Range: bytes 21010-47021/47022 const char* pszBytes = strstr(FStrRange, "bytes "); if (pszBytes != NULL) { pszBytes+=sizeof("bytes"); LONGLONG llOffset = _strtoi64(pszBytes, NULL, 10); hr = OutFile.Seek(llOffset, FILE_BEGIN); llTotalRead = (size_type)llOffset; if (FAILED(hr)) { _DBGAlert("**FAsyncDownload::FHttpDownloadTP::ProcessDownload: Seek to position %d failed: 0x%x, %d\n", hr, GetLastError()); } const char* pszTotal = strchr(pszBytes, '/'); if (pszTotal != NULL) llSizeMax = _strtoi64(pszTotal + 1, NULL, 10); } } } else { if (ContentLen > 0 && ContentLen == FileSize) { OutFile.Close(); return S_OK; } } if (llSizeMax == 0) llSizeMax = ContentLen; pData->pBindStatusCallback.OnProgress((ULONG)llTotalRead, (ULONG)llSizeMax, BINDSTATUS_BEGINDOWNLOADDATA, L""); DWORD dwBytesRead = 0; for (;;) { if (!InternetReadFile(hReq, pBuffer, dwBufferSize, &dwBytesRead)) { _DBGAlert("**FAsyncDownload::FHttpDownloadTP::ProcessDownload: InternetReadFile() failed: %d\n", GetLastError()); OutFile.Close(); return E_HTTP_NET_ERROR; } if (dwBytesRead == 0) { hr = S_OK; break; } DWORD dwBytesWritten = 0; hr = OutFile.Write(pBuffer, dwBytesRead, &dwBytesWritten); if (FAILED(hr)) { _DBGAlert("**FAsyncDownload::FHttpDownloadTP::ProcessDownload: FileWrite failed: 0x%x, %d\n", hr, GetLastError()); OutFile.Close(); return E_HTTP_WRITE_FILE; } llTotalRead+=dwBytesRead; pData->pBindStatusCallback.OnProgress((ULONG)llTotalRead, llSizeMax > 0 ? (ULONG)llSizeMax : llTotalRead , BINDSTATUS_DOWNLOADINGDATA, L""); if (m_pThis->m_Stopping || pData->pBindStatusCallback.m_Abort) { _DBGAlert("**FAsyncDownload::FHttpDownloadTP::ProcessDownload: Download aborted\n", hr, GetLastError()); hr = E_ABORT; break; } } OutFile.Close(); return hr; }
BOOL CLocalFileDownload::Fetch( INT nCorrurent/*=0*/ ) { m_bStopped = FALSE; try { CFileInStream fin(m_strUrl); if(!fin.Create()) { m_errCode = DLLER_SERVER_FILENOTFOUND; return FALSE; } CString strTmpFile; strTmpFile.Format(_T("%s%s"), m_strFilePath, _T(".tc")); CAtlFile file; if( FAILED( file.Create(strTmpFile, GENERIC_WRITE, FILE_SHARE_WRITE, CREATE_ALWAYS) ) ) { m_errCode = DLERR_CREATEFILE; return FALSE; } m_errCode = DLERR_SUCCESS; m_FileInfo.Reset(fin.GetFileSize(), 0, TRUE); m_DownStat.OnDownBegin(); int64 lastDownloaded = 0, downloadedPercent = m_FileInfo.fileSize/100; const int nBufferSize = 1024; BYTE *pBuffer = new BYTE[nBufferSize]; while(!m_bStopped) { DWORD dwReaded = 0; fin.Read(pBuffer, nBufferSize, &dwReaded); if(dwReaded==0) break; DWORD dwWrited = 0; if( FAILED(file.Write(pBuffer, dwReaded, &dwWrited)) || dwWrited!=dwReaded) { m_errCode = DLERR_WRITEFILE; break; } m_FileInfo.fileDownloaded += dwReaded; if((m_FileInfo.fileDownloaded-lastDownloaded) > downloadedPercent) { m_DownStat.OnDownData(GetTickCount(), (m_FileInfo.fileDownloaded-lastDownloaded)); lastDownloaded = m_FileInfo.fileDownloaded; _Notify(ProcessState_ReceiveData); } } fin.CloseFile(); file.Close(); SAFE_DELETE_ARRAY(pBuffer); m_DownStat.OnDownEnd(); if(m_FileInfo.fileDownloaded==m_FileInfo.fileSize) { MoveFileEx(strTmpFile, m_strFilePath, MOVEFILE_REPLACE_EXISTING); m_errCode = DLERR_SUCCESS; } else { DeleteFile(strTmpFile); m_errCode = DLLER_NETWORK; } } catch (...) { m_errCode = DLERR_WRITEFILE; } return DLERR_SUCCESS==m_errCode; }
HRESULT CZlib::EncodeFile( LPCWSTR lpwszSrcFile, LPCWSTR lpwszDstFile, int level ) { if ( zlib_compress ) { CAtlFile hInFile; HRESULT hr = hInFile.Create( lpwszSrcFile, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_DELETE, OPEN_EXISTING); if (FAILED(hr)) return hr; CAtlFile hOutFile; hr = hOutFile.Create( lpwszDstFile, GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_DELETE, CREATE_ALWAYS); if (FAILED(hr)) return hr; ULONGLONG uInFileSize = 0; hr = hInFile.GetSize(uInFileSize); if (FAILED(hr)) return hr; // 0长度文件不需要压缩 if (0 == uInFileSize) return S_OK; // 太大的文件不压缩 if (uInFileSize > ZLIB_SINGLE_FILE_MAX_SIZE ) //假定压缩比为4:1 return E_FAIL; // 读取输入 CAtlArray<BYTE> bufRead; bufRead.SetCount((size_t)uInFileSize); if (uInFileSize != bufRead.GetCount()) return E_OUTOFMEMORY; hr = hInFile.Read(bufRead.GetData(), (DWORD)bufRead.GetCount()); if (FAILED(hr)) return hr; // 准备压缩 ULONGLONG uOutFileSize = max(uInFileSize, ZLIB_DECOMPRESS_INIT_BUFF_SIZE); CAtlArray<BYTE> bufWrite; try { while (uOutFileSize <= ZLIB_SINGLE_FILE_MAX_SIZE ) { bufWrite.SetCount(0); bufWrite.SetCount((DWORD)uOutFileSize); if (uOutFileSize != bufWrite.GetCount()) return E_OUTOFMEMORY; DWORD dwcompressSize = DWORD(uOutFileSize); int nRet = zlib_compress2( bufWrite.GetData(), &dwcompressSize, bufRead.GetData(), (int)bufRead.GetCount(), level ); if (nRet == Z_OK && dwcompressSize <= uOutFileSize) { bufWrite.SetCount(dwcompressSize); break; } if (nRet != Z_BUF_ERROR) return E_FAIL; uOutFileSize *= 2; } } catch (...) { return E_FAIL; } hr = hOutFile.Write(bufWrite.GetData(), (DWORD)bufWrite.GetCount()); if (FAILED(hr)) return hr; return S_OK; } else { return E_NOINTERFACE; } }