OSDN Git Service

FileTransform: Remove m_subcode from PackingInfo class
authorTakashi Sawanaka <sdottaka@users.sourceforge.net>
Sat, 15 May 2021 06:20:26 +0000 (15:20 +0900)
committerTakashi Sawanaka <sdottaka@users.sourceforge.net>
Sat, 15 May 2021 06:20:26 +0000 (15:20 +0900)
Src/DiffTextBuffer.cpp
Src/FileTransform.cpp
Src/FileTransform.h
Src/FolderCmp.cpp
Src/Merge7zFormatMergePluginImpl.cpp
Testing/GoogleTest/Plugins/Plugins_test.cpp

index 7fb3915..019b950 100644 (file)
@@ -230,12 +230,11 @@ int CDiffTextBuffer::LoadFromFile(LPCTSTR pszFileNameInit,
 
        // Unpacking the file here, save the result in a temporary file
        m_strTempFileName = pszFileNameInit;
-       if (!FileTransform::Unpacking(infoUnpacker, m_strTempFileName, sToFindUnpacker))
+       if (!FileTransform::Unpacking(infoUnpacker, &m_unpackerSubcode, m_strTempFileName, sToFindUnpacker))
        {
                InitNew(); // leave crystal editor in valid, empty state
                return FileLoadResult::FRESULT_ERROR_UNPACK;
        }
-       m_unpackerSubcode = infoUnpacker->m_subcode;
 
        // we use the same unpacker for both files, so it must be defined after first file
        ASSERT(infoUnpacker->m_PluginOrPredifferMode != PLUGIN_MODE::PLUGIN_AUTO);
@@ -547,58 +546,32 @@ int CDiffTextBuffer::SaveToFile (const String& pszFileName,
                // we need an unpacker/packer, at least a "do nothing" one
                ASSERT(infoUnpacker != nullptr);
                // repack the file here, overwrite the temporary file we did save in
-               String csTempFileName = sIntermediateFilename;
-               infoUnpacker->m_subcode = m_unpackerSubcode;
-               if (!FileTransform::Packing(csTempFileName, *infoUnpacker))
+               bSaveSuccess = FileTransform::Packing(sIntermediateFilename, pszFileName, *infoUnpacker, m_unpackerSubcode);
+               try
                {
-                       try
-                       {
-                               TFile(sIntermediateFilename).remove();
-                       }
-                       catch (Exception& e)
-                       {
-                               LogErrorStringUTF8(e.displayText());
-                       }
-                       // returns now, don't overwrite the original file
-                       return SAVE_PACK_FAILED;
+                       TFile(sIntermediateFilename).remove();
                }
-               // the temp filename may have changed during packing
-               if (csTempFileName != sIntermediateFilename)
+               catch (Exception& e)
                {
-                       try
-                       {
-                               TFile(sIntermediateFilename).remove();
-                       }
-                       catch (Exception& e)
-                       {
-                               LogErrorStringUTF8(e.displayText());
-                       }
-                       sIntermediateFilename = csTempFileName;
+                       LogErrorStringUTF8(e.displayText());
                }
-
-               // Write tempfile over original file
-               try
+               if (!bSaveSuccess)
                {
-                       TFile file1(sIntermediateFilename);
-                       file1.copyTo(pszFileName);
-                       file1.remove();
-                       if (bClearModifiedFlag)
-                       {
-                               SetModified(false);
-                               m_nSyncPosition = m_nUndoPosition;
-                       }
-                       bSaveSuccess = true;
-
-                       // remember revision number on save
-                       m_dwRevisionNumberOnSave = m_dwCurrentRevisionNumber;
-
-                       // redraw line revision marks
-                       UpdateViews (nullptr, nullptr, UPDATE_FLAGSONLY);       
+                       // returns now, don't overwrite the original file
+                       return SAVE_PACK_FAILED;
                }
-               catch (Exception& e)
+
+               if (bClearModifiedFlag)
                {
-                       LogErrorStringUTF8(e.displayText());
+                       SetModified(false);
+                       m_nSyncPosition = m_nUndoPosition;
                }
+
+               // remember revision number on save
+               m_dwRevisionNumberOnSave = m_dwCurrentRevisionNumber;
+
+               // redraw line revision marks
+               UpdateViews (nullptr, nullptr, UPDATE_FLAGSONLY);       
        }
        else
        {
index d1d8951..62d158e 100644 (file)
@@ -66,7 +66,7 @@ bool getPackUnpackPlugin(const String& pluginName, PluginInfo*& plugin, bool& bW
 }
 
 // known handler
-bool Packing(String & filepath, PackingInfo handler)
+bool Packing(String & filepath, const PackingInfo& handler, int handlerSubcode)
 {
        // no handler : return true
        if (handler.m_PluginName.empty())
@@ -91,7 +91,7 @@ bool Packing(String & filepath, PackingInfo handler)
                bHandled = plugin::InvokePackFile(srcFileName,
                        dstFileName,
                        bufferData.GetNChanged(),
-                       piScript, handler.m_subcode);
+                       piScript, handlerSubcode);
                if (bHandled)
                        bufferData.ValidateNewFile();
        }
@@ -99,7 +99,7 @@ bool Packing(String & filepath, PackingInfo handler)
        {
                bHandled = plugin::InvokePackBuffer(*bufferData.GetDataBufferAnsi(),
                        bufferData.GetNChanged(),
-                       piScript, handler.m_subcode);
+                       piScript, handlerSubcode);
                if (bHandled)
                        bufferData.ValidateNewBuffer();
        }
@@ -118,6 +118,26 @@ bool Packing(String & filepath, PackingInfo handler)
        return bSuccess;
 }
 
+bool Packing(const String& srcFilepath, const String& dstFilepath, const PackingInfo& handler, int handlerSubcode)
+{
+       String csTempFileName = srcFilepath;
+       if (!Packing(csTempFileName, handler, handlerSubcode))
+               return false;
+       try
+       {
+               TFile file1(csTempFileName);
+               file1.copyTo(dstFilepath);
+               if (srcFilepath!= csTempFileName)
+                       file1.remove();
+               return true;
+       }
+       catch (Poco::Exception& e)
+       {
+               LogErrorStringUTF8(e.displayText());
+               return false;
+       }
+}
+
 // known handler
 bool Unpacking(String & filepath, const PackingInfo * handler, int * handlerSubcode)
 {
@@ -166,7 +186,8 @@ bool Unpacking(String & filepath, const PackingInfo * handler, int * handlerSubc
                return false;
 
        // valid the subcode
-       *handlerSubcode = subcode;
+       if (handlerSubcode)
+               *handlerSubcode = subcode;
 
        // if the buffer changed, write it before leaving
        bool bSuccess = true;
@@ -197,6 +218,8 @@ bool Unpacking(String & filepath, const String& filteredText, PackingInfo * hand
        storageForPlugins bufferData;
        bufferData.SetDataFileAnsi(filepath);
 
+       // temporary subcode 
+       int subcode = 0;
        // control value
        bool bHandled = false;
 
@@ -213,7 +236,7 @@ bool Unpacking(String & filepath, const String& filteredText, PackingInfo * hand
                bHandled = plugin::InvokeUnpackFile(srcFileName,
                        dstFileName,
                        bufferData.GetNChanged(),
-                       plugin->m_lpDispatch, handler->m_subcode);
+                       plugin->m_lpDispatch, subcode);
                if (bHandled)
                        bufferData.ValidateNewFile();
        }
@@ -229,7 +252,7 @@ bool Unpacking(String & filepath, const String& filteredText, PackingInfo * hand
                        handler->m_PluginName = plugin->m_name;
                        bHandled = plugin::InvokeUnpackBuffer(*bufferData.GetDataBufferAnsi(),
                                bufferData.GetNChanged(),
-                               plugin->m_lpDispatch, handler->m_subcode);
+                               plugin->m_lpDispatch, subcode);
                        if (bHandled)
                                bufferData.ValidateNewBuffer();
                }
@@ -239,7 +262,7 @@ bool Unpacking(String & filepath, const String& filteredText, PackingInfo * hand
        {
                // we didn't find any unpacker, just hope it is normal Ansi/Unicode
                handler->m_PluginName = _T("");
-               handler->m_subcode = 0;
+               subcode = 0;
                bHandled = true;
        }
 
@@ -247,7 +270,8 @@ bool Unpacking(String & filepath, const String& filteredText, PackingInfo * hand
        handler->m_PluginOrPredifferMode = PLUGIN_MODE::PLUGIN_MANUAL;
 
        // assign the sucode
-       *handlerSubcode = handler->m_subcode;
+       if (handlerSubcode)
+               *handlerSubcode = subcode;
 
        // if the buffer changed, write it before leaving
        bool bSuccess = true;
@@ -259,12 +283,12 @@ bool Unpacking(String & filepath, const String& filteredText, PackingInfo * hand
        return bSuccess;
 }
 
-bool Unpacking(PackingInfo *handler, String& filepath, const String& filteredText)
+bool Unpacking(PackingInfo *handler, int * handlerSubcode, String& filepath, const String& filteredText)
 {
        if (handler->m_PluginOrPredifferMode != PLUGIN_MODE::PLUGIN_MANUAL)
-               return Unpacking(filepath, filteredText, handler, &handler->m_subcode);
+               return Unpacking(filepath, filteredText, handler, handlerSubcode);
        else
-               return Unpacking(filepath, handler, &handler->m_subcode);
+               return Unpacking(filepath, handler, handlerSubcode);
 }
 
 ////////////////////////////////////////////////////////////////////////////////
index e53df6d..8248457 100644 (file)
@@ -83,14 +83,11 @@ class PackingInfo : public PluginForFile
 public:
        explicit PackingInfo(PLUGIN_MODE Mode = FileTransform::g_UnpackerMode)
        : PluginForFile(Mode)
-       , m_subcode(0)
        , m_pufile(nullptr)
        , m_bDisallowMixedEOL(false)
        {
        }
 public:
-       /// keep some info from unpacking for packing
-       int                     m_subcode;
        /// text type to override syntax highlighting
        String          m_textType;
        /// custom UniFile
@@ -140,7 +137,7 @@ bool Unpacking(String & filepath, const String& filteredText, PackingInfo * hand
  */
 bool Unpacking(String & filepath, const PackingInfo * handler, int * handlerSubcode);
 
-bool Unpacking(PackingInfo * handler, String & filepath, const String& filteredText);
+bool Unpacking(PackingInfo * handler, int * handlerSubcode, String & filepath, const String& filteredText);
 
 /**
  * @brief Prepare one file for saving, known handler
@@ -152,7 +149,9 @@ bool Unpacking(PackingInfo * handler, String & filepath, const String& filteredT
  * @note Event FILE_PACK
  * Never do Unicode conversion, it was done in SaveFromFile
  */
-bool Packing(String & filepath, PackingInfo handler);
+bool Packing(String & filepath, const PackingInfo& handler, int handlerSubcode);
+
+bool Packing(const String& srcFilepath, const String& dstFilepath, const PackingInfo& handler, int handlerSubcode);
 
 /**
  * @brief Prepare one file for diffing, scan all available plugins (events+filename filtering) 
index 9d54ea9..f41f58c 100644 (file)
@@ -134,7 +134,7 @@ int FolderCmp::prepAndCompareFiles(DIFFITEM &di)
                        // Invoke unpacking plugins
                        if (infoUnpacker && strutils::compare_nocase(filepathUnpacked[nIndex], _T("NUL")) != 0)
                        {
-                               if (!FileTransform::Unpacking(infoUnpacker, filepathUnpacked[nIndex], filteredFilenames))
+                               if (!FileTransform::Unpacking(infoUnpacker, nullptr, filepathUnpacked[nIndex], filteredFilenames))
                                        goto exitPrepAndCompare;
 
                                // we use the same plugins for both files, so they must be defined before second file
index c54fafc..2e5200c 100644 (file)
@@ -49,7 +49,8 @@ HRESULT Merge7zFormatMergePluginImpl::DeCompressArchive(HWND, LPCTSTR path, LPCT
                return E_FAIL;
        paths::CreateIfNeeded(path);
        int nChanged = 0;
-       return plugin::InvokeUnpackFolder(path, folder, nChanged, m_plugin->m_lpDispatch, m_infoUnpacker.m_subcode) ? S_OK : E_FAIL;
+       int subcode = 0;
+       return plugin::InvokeUnpackFolder(path, folder, nChanged, m_plugin->m_lpDispatch, subcode) ? S_OK : E_FAIL;
 }
 
 HRESULT Merge7zFormatMergePluginImpl::CompressArchive(HWND, LPCTSTR path, Merge7z::DirItemEnumerator *)
index 495daf6..5211796 100644 (file)
@@ -55,9 +55,10 @@ namespace
                PrediffingInfo *ip = nullptr;
                PluginManager pm;
                IPluginInfos *ppi = &pm;
+               int subcode = 0;
                ppi->FetchPluginInfos(_T("../../Data/Office/excel.xls|../../Data/Office/excel.xls"), &iu, &ip);
                String file = paths::ConcatPath(oldModulePath, _T("..\\..\\Data\\Office\\excel.xls"));
-               FileTransform::Unpacking(file, _T(".*\\.xls"), iu, &iu->m_subcode);
+               FileTransform::Unpacking(file, _T(".*\\.xls"), iu, &subcode);
        }
 
 }  // namespace