Index: src/ch/task.cpp =================================================================== diff -u -r56cf39888865c8edb5b5eb02a901557b6aef42e9 -r678a716a4ed83d5790a407ce60dfcf7d8ca703fa --- src/ch/task.cpp (.../task.cpp) (revision 56cf39888865c8edb5b5eb02a901557b6aef42e9) +++ src/ch/task.cpp (.../task.cpp) (revision 678a716a4ed83d5790a407ce60dfcf7d8ca703fa) @@ -502,7 +502,7 @@ void CTask::Load(const CString& strPath, bool bData) { - std::ifstream ifs(strPath); + std::ifstream ifs(strPath, ios_base::in | ios_base::binary); boost::archive::binary_iarchive ar(ifs); m_cs.Lock(); @@ -582,7 +582,7 @@ { CString strPath = m_strTaskBasePath.c_str() + GetUniqueName() + (bData ? _T(".atd") : _T(".atp")); - std::ofstream ofs(strPath); + std::ofstream ofs(strPath, ios_base::out | ios_base::binary); boost::archive::binary_oarchive ar(ofs); if(bData) @@ -1798,7 +1798,7 @@ void CTask::CustomCopyFile(CUSTOM_COPY_PARAMS* pData) { - HANDLE hSrc = INVALID_HANDLE_VALUE, hDst = INVALID_HANDLE_VALUE; + TAutoFileHandle hSrc = INVALID_HANDLE_VALUE, hDst = INVALID_HANDLE_VALUE; ictranslate::CFormat fmt; bool bRetry = false; @@ -1809,7 +1809,7 @@ // Data regarding dest file CFileInfo fiDest; - bool bExist=fiDest.Create(pData->strDstFile, std::numeric_limits::max()); + bool bExist = fiDest.Create(pData->strDstFile, std::numeric_limits::max()); chcore::IFeedbackHandler* piFeedbackHandler = pData->pTask->GetFeedbackHandler(); BOOST_ASSERT(piFeedbackHandler); @@ -2176,8 +2176,8 @@ { // we need to copy rest so do the second pass // close files - CloseHandle(hSrc); - CloseHandle(hDst); + hSrc.Close(); + hDst.Close(); // second pass bFirstPass=false; @@ -2240,18 +2240,16 @@ } // close files - CloseHandle(hSrc); - CloseHandle(hDst); + hSrc.Close(); + hDst.Close(); pData->bProcessed = true; } catch(...) { // close handles - if(hSrc != INVALID_HANDLE_VALUE) - CloseHandle(hSrc); - if(hDst != INVALID_HANDLE_VALUE) - CloseHandle(hDst); + hSrc.Close(); + hDst.Close(); throw; } Index: src/ch/task.h =================================================================== diff -u -r583756058945ab95a48e17b31455b0ed4b213b8e -r678a716a4ed83d5790a407ce60dfcf7d8ca703fa --- src/ch/task.h (.../task.h) (revision 583756058945ab95a48e17b31455b0ed4b213b8e) +++ src/ch/task.h (.../task.h) (revision 678a716a4ed83d5790a407ce60dfcf7d8ca703fa) @@ -137,6 +137,98 @@ bool bProcessed; // has the element been processed ? (false if skipped) }; +/// class encapsulates windows HANDLE, allowing automatic closing it in destructor. +class TAutoFileHandle +{ +public: + // ============================================================================ + /// TAutoFileHandle::TAutoFileHandle + /// @date 2010/08/26 + /// + /// @brief Constructs the TAutoFileHandle object. + // ============================================================================ + TAutoFileHandle() : + m_hHandle(INVALID_HANDLE_VALUE) + { + } + + // ============================================================================ + /// TAutoFileHandle::TAutoFileHandle + /// @date 2010/08/26 + /// + /// @brief Constructs the TAutoFileHandle object with specified handle. + /// @param[in] hHandle - System handle to be managed by this class. + // ============================================================================ + TAutoFileHandle(HANDLE hHandle) : + m_hHandle(hHandle) + { + } + + // ============================================================================ + /// TAutoFileHandle::~TAutoFileHandle + /// @date 2010/08/26 + /// + /// @brief Destructs the TAutoFileHandle object and closes handle if not closed already. + // ============================================================================ + ~TAutoFileHandle() + { + _ASSERTE(m_hHandle == INVALID_HANDLE_VALUE); + Close(); + } + + // ============================================================================ + /// TAutoFileHandle::operator= + /// @date 2010/08/26 + /// + /// @brief Assignment operator. + /// @param[in] hHandle - Handle to be assigned. + /// @return Reference to this object, + // ============================================================================ + TAutoFileHandle& operator=(HANDLE hHandle) + { + if(m_hHandle != hHandle) + { + VERIFY(Close()); + m_hHandle = hHandle; + } + return *this; + } + + // ============================================================================ + /// TAutoFileHandle::operator HANDLE + /// @date 2010/08/26 + /// + /// @brief Retrieves the system handle. + /// @return HANDLE value. + // ============================================================================ + operator HANDLE() + { + return m_hHandle; + } + + // ============================================================================ + /// TAutoFileHandle::Close + /// @date 2010/08/26 + /// + /// @brief Closes the internal handle if needed. + /// @return Result of the CloseHandle() function. + // ============================================================================ + BOOL Close() + { + BOOL bResult = TRUE; + if(m_hHandle != INVALID_HANDLE_VALUE) + { + bResult = CloseHandle(m_hHandle); + m_hHandle = INVALID_HANDLE_VALUE; + } + + return bResult; + } + +private: + HANDLE m_hHandle; ///< System handle +}; + class CTask { public: