* @brief Save file */
| 314 | * @brief Save file |
| 315 | */ |
| 316 | HRESULT CHexMergeView::SaveFile(const tchar_t* path, bool packing) |
| 317 | { |
| 318 | // Write data to an intermediate file |
| 319 | String tempPath = env::GetTemporaryPath(); |
| 320 | String sIntermediateFilename = env::GetTemporaryFileName(tempPath, _T("MRG_"), 0); |
| 321 | if (sIntermediateFilename.empty()) |
| 322 | return E_FAIL; //Nothing to do if even tempfile name fails |
| 323 | HANDLE h = CreateFile(sIntermediateFilename.c_str(), GENERIC_WRITE, FILE_SHARE_READ, |
| 324 | 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0); |
| 325 | HRESULT hr = SE(h != INVALID_HANDLE_VALUE); |
| 326 | if (h == INVALID_HANDLE_VALUE) |
| 327 | return hr; |
| 328 | size_t length = GetLength(); |
| 329 | void *buffer = GetBuffer(length); |
| 330 | if (buffer == 0) |
| 331 | { |
| 332 | CloseHandle(h); |
| 333 | return E_POINTER; |
| 334 | } |
| 335 | size_t pos = 0; |
| 336 | while (pos < length && SUCCEEDED(hr)) |
| 337 | { |
| 338 | DWORD cb = 0; |
| 339 | hr = SE(WriteFile(h, reinterpret_cast<const BYTE*>(buffer) + pos, |
| 340 | length - pos < 0x10000000 ? static_cast<DWORD>(length - pos) : 0x10000000, &cb, 0)); |
| 341 | pos += cb; |
| 342 | } |
| 343 | CloseHandle(h); |
| 344 | if (hr != S_OK) |
| 345 | return hr; |
| 346 | |
| 347 | CHexMergeDoc* pDoc = static_cast<CHexMergeDoc*>(GetDocument()); |
| 348 | if (packing && !m_unpackerSubcodes.empty()) |
| 349 | { |
| 350 | if (!pDoc->GetUnpacker()->Packing(m_nThisPane, sIntermediateFilename, path, m_unpackerSubcodes, { path })) |
| 351 | { |
| 352 | String str = CMergeApp::GetPackingErrorMessage(m_nThisPane, pDoc->m_nBuffers, path, *pDoc->GetUnpacker()); |
| 353 | int answer = AfxMessageBox(str.c_str(), MB_OKCANCEL | MB_ICONWARNING); |
| 354 | if (answer == IDOK) |
| 355 | { |
| 356 | pDoc->SaveAs(m_nThisPane, false); |
| 357 | return S_OK; |
| 358 | } |
| 359 | return S_OK; |
| 360 | } |
| 361 | } |
| 362 | else |
| 363 | { |
| 364 | hr = SE(CopyFile(sIntermediateFilename.c_str(), path, FALSE)); |
| 365 | if (hr != S_OK) |
| 366 | return hr; |
| 367 | } |
| 368 | |
| 369 | m_fileInfo.Update(path); |
| 370 | SetSavePoint(); |
| 371 | hr = SE(DeleteFile(sIntermediateFilename.c_str())); |
| 372 | if (hr != S_OK) |
| 373 | { |
no test coverage detected