OSDN Git Service

Move decompressing code from MainFrm.cpp to 7zCommon.cpp
authorsdottaka <sdottaka@sourceforge.net>
Sat, 19 Jul 2014 11:21:43 +0000 (20:21 +0900)
committersdottaka <sdottaka@sourceforge.net>
Sat, 19 Jul 2014 11:21:43 +0000 (20:21 +0900)
--HG--
branch : stable

Src/7zCommon.cpp
Src/7zCommon.h
Src/MainFrm.cpp

index 692929c..b6b4566 100644 (file)
@@ -1121,3 +1121,123 @@ void CDirView::DirItemEnumerator::CollectFiles(String &strBuffer)
        }
        ASSERT(pchBuffer - &strBuffer[0] == cchBuffer);
 }
+
+DecompressResult DecompressArchive(HWND hWnd, const PathContext& files)
+{
+       DecompressResult res(files, NULL, IS_EXISTING_DIR);
+       try
+       {
+               String path;
+               USES_CONVERSION;
+               // Handle archives using 7-zip
+               if (Merge7z::Format *piHandler = ArchiveGuessFormat(res.files[0].c_str()))
+               {
+                       res.pTempPathContext = new CTempPathContext;
+                       path = env_GetTempChildPath();
+                       for (int index = 0; index < res.files.GetSize(); index++)
+                               res.pTempPathContext->m_strDisplayRoot[index] = res.files[index];
+                       if (res.files[0] == res.files[1])
+                       {
+                               res.files[1].erase();
+                               if (res.files.GetSize() > 2)
+                               {
+                                       res.files[2].erase();
+                               }
+                       }
+                       do
+                       {
+                               if (FAILED(piHandler->DeCompressArchive(hWnd, res.files[0].c_str(), path.c_str())))
+                                       break;
+                               if (res.files[0].find(path) == 0)
+                               {
+                                       VERIFY(::DeleteFile(res.files[0].c_str()) || (LogErrorString(string_format(_T("DeleteFile(%s) failed"), res.files[0].c_str())), false));
+                               }
+                               BSTR pTmp = piHandler->GetDefaultName(hWnd, res.files[0].c_str());
+                               res.files[0] = OLE2T(pTmp);
+                               SysFreeString(pTmp);
+                               res.files[0].insert(0, _T("\\"));
+                               res.files[0].insert(0, path);
+                       } while (piHandler = ArchiveGuessFormat(res.files[0].c_str()));
+                       res.files[0] = path;
+               }
+               if (Merge7z::Format *piHandler = ArchiveGuessFormat(res.files[1].c_str()))
+               {
+                       if (!res.pTempPathContext)
+                       {
+                               res.pTempPathContext = new CTempPathContext;
+                               for (int index = 0; index < res.files.GetSize(); index++)
+                                       res.pTempPathContext->m_strDisplayRoot[index] = res.files[index];
+                       }
+                       path = env_GetTempChildPath();
+                       do
+                       {
+                               if (FAILED(piHandler->DeCompressArchive(hWnd, res.files[1].c_str(), path.c_str())))
+                                       break;;
+                               if (res.files[1].find(path) == 0)
+                               {
+                                       VERIFY(::DeleteFile(res.files[1].c_str()) || (LogErrorString(string_format(_T("DeleteFile(%s) failed"), res.files[1].c_str())), false));
+                               }
+                               BSTR pTmp = piHandler->GetDefaultName(hWnd, res.files[1].c_str());
+                               res.files[1] = OLE2T(pTmp);
+                               SysFreeString(pTmp);
+                               res.files[1].insert(0, _T("\\"));
+                               res.files[1].insert(0, path);
+                       } while (piHandler = ArchiveGuessFormat(res.files[1].c_str()));
+                       res.files[1] = path;
+               }
+               if (res.files.GetSize() > 2)
+               {
+                       if (Merge7z::Format *piHandler = ArchiveGuessFormat(res.files[2].c_str()))
+                       {
+                               if (!res.pTempPathContext)
+                               {
+                                       res.pTempPathContext = new CTempPathContext;
+                                       for (int index = 0; index < res.files.GetSize(); index++)
+                                               res.pTempPathContext->m_strDisplayRoot[index] = res.files[index];
+                               }
+                               path = env_GetTempChildPath();
+                               do
+                               {
+                                       if (FAILED(piHandler->DeCompressArchive(hWnd, res.files[2].c_str(), path.c_str())))
+                                               break;;
+                                       if (res.files[2].find(path) == 0)
+                                       {
+                                               VERIFY(::DeleteFile(res.files[2].c_str()) || (LogErrorString(string_format(_T("DeleteFile(%s) failed"), res.files[2].c_str())), false));
+                                       }
+                                       BSTR pTmp = piHandler->GetDefaultName(hWnd, res.files[1].c_str());
+                                       res.files[2] = OLE2T(pTmp);
+                                       SysFreeString(pTmp);
+                                       res.files[2].insert(0, _T("\\"));
+                                       res.files[2].insert(0, path);
+                               } while (piHandler = ArchiveGuessFormat(res.files[2].c_str()));
+                               res.files[2] = path;
+                       }
+               }
+               if (res.files[1].empty())
+               {
+                       // assume Perry style patch
+                       res.files[1] = path;
+                       res.files[0] += _T("\\ORIGINAL");
+                       res.files[1] += _T("\\ALTERED");
+                       if (!PathFileExists(res.files[0].c_str()) || !PathFileExists(res.files[1].c_str()))
+                       {
+                               // not a Perry style patch: diff with itself...
+                               res.files[0] = res.files[1] = path;
+                               if (res.files.GetSize() > 2)
+                                       res.files[2] = path;
+                       }
+                       else
+                       {
+                               res.pTempPathContext->m_strDisplayRoot[0] += _T("\\ORIGINAL");
+                               res.pTempPathContext->m_strDisplayRoot[1] += _T("\\ALTERED");
+                       }
+               }
+       }
+       catch (CException *e)
+       {
+               e->ReportError(MB_ICONSTOP);
+               e->Delete();
+       }
+       return res;
+}
+
index 3e6ddc8..db6c3f2 100644 (file)
@@ -6,6 +6,7 @@
 #include "../ArchiveSupport/Merge7z/Merge7z.h"
 
 #include "DirView.h"
+#include "paths.h"
 #include <list>
 
 class DirView;
@@ -91,6 +92,18 @@ void NTAPI Recall7ZipMismatchError();
 BOOL NTAPI IsMerge7zEnabled();
 DWORD NTAPI VersionOf7z(BOOL bLocal = FALSE);
 
+struct DecompressResult
+{
+       DecompressResult(const PathContext& files, CTempPathContext *pTempPathContext, PATH_EXISTENCE pathsType) :
+               files(files), pTempPathContext(pTempPathContext), pathsType(pathsType)
+       {
+       }
+       PathContext files;
+       CTempPathContext *pTempPathContext;
+       PATH_EXISTENCE pathsType;
+};
+DecompressResult DecompressArchive(HWND hWnd, const PathContext& infiles);
+
 /**
  * @brief assign BSTR to String, and return BSTR for optional SysFreeString()
  */
index f631d1c..eb486ca 100644 (file)
@@ -1013,120 +1013,11 @@ BOOL CMainFrame::DoFileOpen(PathContext * pFiles /*=NULL*/,
                }
        }
 
-       CTempPathContext *pTempPathContext = NULL;
-       try
+       DecompressResult res = DecompressArchive(m_hWnd, files);
+       if (res.pTempPathContext)
        {
-               String path;
-               USES_CONVERSION;
-               // Handle archives using 7-zip
-               if (Merge7z::Format *piHandler = ArchiveGuessFormat(files[0].c_str()))
-               {
-                       pTempPathContext = new CTempPathContext;
-                       path = env_GetTempChildPath();
-                       for (int index = 0; index < files.GetSize(); index++)
-                               pTempPathContext->m_strDisplayRoot[index] = files[index];
-                       pathsType = IS_EXISTING_DIR;
-                       if (files[0] == files[1])
-                       {
-                               files[1].erase();
-                               if (files.GetSize() > 2)
-                               {
-                                       files[2].erase();
-                               }
-                       }
-                       do
-                       {
-                               if (FAILED(piHandler->DeCompressArchive(m_hWnd, files[0].c_str(), path.c_str())))
-                                       break;
-                               if (files[0].find(path) == 0)
-                               {
-                                       VERIFY(::DeleteFile(files[0].c_str()) || (LogErrorString(string_format(_T("DeleteFile(%s) failed"), files[0].c_str())), false));
-                               }
-                               BSTR pTmp = piHandler->GetDefaultName(m_hWnd, files[0].c_str());
-                               files[0] = OLE2T(pTmp);
-                               SysFreeString(pTmp);
-                               files[0].insert(0, _T("\\"));
-                               files[0].insert(0, path);
-                       } while (piHandler = ArchiveGuessFormat(files[0].c_str()));
-                       files[0] = path;
-               }
-               if (Merge7z::Format *piHandler = ArchiveGuessFormat(files[1].c_str()))
-               {
-                       if (!pTempPathContext)
-                       {
-                               pTempPathContext = new CTempPathContext;
-                               for (int index = 0; index < files.GetSize(); index++)
-                                       pTempPathContext->m_strDisplayRoot[index] = files[index];
-                       }
-                       path = env_GetTempChildPath();
-                       do
-                       {
-                               if (FAILED(piHandler->DeCompressArchive(m_hWnd, files[1].c_str(), path.c_str())))
-                                       break;;
-                               if (files[1].find(path) == 0)
-                               {
-                                       VERIFY(::DeleteFile(files[1].c_str()) || (LogErrorString(string_format(_T("DeleteFile(%s) failed"), files[1].c_str())), false));
-                               }
-                               BSTR pTmp = piHandler->GetDefaultName(m_hWnd, files[1].c_str());
-                               files[1] = OLE2T(pTmp);
-                               SysFreeString(pTmp);
-                               files[1].insert(0, _T("\\"));
-                               files[1].insert(0, path);
-                       } while (piHandler = ArchiveGuessFormat(files[1].c_str()));
-                       files[1] = path;
-               }
-               if (files.GetSize() > 2)
-               {
-                       if (Merge7z::Format *piHandler = ArchiveGuessFormat(files[2].c_str()))
-                       {
-                               if (!pTempPathContext)
-                               {
-                                       pTempPathContext = new CTempPathContext;
-                                       for (int index = 0; index < files.GetSize(); index++)
-                                               pTempPathContext->m_strDisplayRoot[index] = files[index];
-                               }
-                               path = env_GetTempChildPath();
-                               do
-                               {
-                                       if (FAILED(piHandler->DeCompressArchive(m_hWnd, files[2].c_str(), path.c_str())))
-                                               break;;
-                                       if (files[2].find(path) == 0)
-                                       {
-                                               VERIFY(::DeleteFile(files[2].c_str()) || (LogErrorString(string_format(_T("DeleteFile(%s) failed"), files[2].c_str())), false));
-                                       }
-                                       BSTR pTmp = piHandler->GetDefaultName(m_hWnd, files[1].c_str());
-                                       files[2] = OLE2T(pTmp);
-                                       SysFreeString(pTmp);
-                                       files[2].insert(0, _T("\\"));
-                                       files[2].insert(0, path);
-                               } while (piHandler = ArchiveGuessFormat(files[2].c_str()));
-                               files[2] = path;
-                       }
-               }
-               if (files[1].empty())
-               {
-                       // assume Perry style patch
-                       files[1] = path;
-                       files[0] += _T("\\ORIGINAL");
-                       files[1] += _T("\\ALTERED");
-                       if (!PathFileExists(files[0].c_str()) || !PathFileExists(files[1].c_str()))
-                       {
-                               // not a Perry style patch: diff with itself...
-                               files[0] = files[1] = path;
-                               if (files.GetSize() > 2)
-                                       files[2] = path;
-                       }
-                       else
-                       {
-                               pTempPathContext->m_strDisplayRoot[0] += _T("\\ORIGINAL");
-                               pTempPathContext->m_strDisplayRoot[1] += _T("\\ALTERED");
-                       }
-               }
-       }
-       catch (CException *e)
-       {
-               e->ReportError(MB_ICONSTOP);
-               e->Delete();
+               pathsType = res.pathsType;
+               files = res.files;
        }
 
        // Determine if we want new a dirview open now that we know if it was
@@ -1163,7 +1054,7 @@ BOOL CMainFrame::DoFileOpen(PathContext * pFiles /*=NULL*/,
                        }
                        // Anything that can go wrong inside InitCompare() will yield an
                        // exception. There is no point in checking return value.
-                       pDirDoc->InitCompare(files, bRecurse, pTempPathContext);
+                       pDirDoc->InitCompare(files, bRecurse, res.pTempPathContext);
 
                        pDirDoc->SetDescriptions(theApp.m_strDescriptions);
                        pDirDoc->SetTitle(NULL);