Skip to content
Snippets Groups Projects
Commit 13f5aafd authored by Marco De Marco's avatar Marco De Marco
Browse files

Clean up method added to file wrapper

parent 091f50bd
No related branches found
No related tags found
No related merge requests found
...@@ -11,30 +11,30 @@ FileWrapper::FileWrapper(Tango::DeviceImpl* deviceImpl_p, ...@@ -11,30 +11,30 @@ FileWrapper::FileWrapper(Tango::DeviceImpl* deviceImpl_p,
std::string fileName, boost::uint64_t totalFileSize) throw(std::logic_error) std::string fileName, boost::uint64_t totalFileSize) throw(std::logic_error)
: Tango::LogAdapter(deviceImpl_p), m_storagePath(storagePath), : Tango::LogAdapter(deviceImpl_p), m_storagePath(storagePath),
m_filePath(filePath), m_fileVersion(fileVersion), m_fileName(fileName), m_filePath(filePath), m_fileVersion(fileVersion), m_fileName(fileName),
m_totalFileSize(totalFileSize) m_expectedFileSize(totalFileSize)
{ {
DEBUG_STREAM << "FileWrapper::FileWrapper()" << endl; DEBUG_STREAM << "FileWrapper::FileWrapper()" << endl;
boost::filesystem::path destPath(storagePath); m_outputFilePath /= storagePath;
destPath /= filePath; m_outputFilePath /= filePath;
std::stringstream fileStream; std::stringstream fileStream;
fileStream << "/" << fileVersion; fileStream << "/" << fileVersion;
destPath /= fileStream.str(); m_outputFilePath /= fileStream.str();
if(!boost::filesystem::exists(destPath)) if(!boost::filesystem::exists(m_outputFilePath))
boost::filesystem::create_directories(destPath); boost::filesystem::create_directories(m_outputFilePath);
if(!boost::filesystem::is_directory(destPath)) if(!boost::filesystem::is_directory(m_outputFilePath))
throw std::logic_error("Destination path \'" throw std::logic_error("Destination path \'"
+ destPath.string() + "\' is not a directory" ); + m_outputFilePath.string() + "\' is not a directory" );
destPath /= fileName; m_outputFilePath /= fileName;
m_outputFileStream.open(destPath.string(), std::ios::binary); m_outputFileStream.open(m_outputFilePath.string(), std::ios::binary);
} }
//============================================================================== //==============================================================================
...@@ -122,7 +122,7 @@ bool FileWrapper::isBad() ...@@ -122,7 +122,7 @@ bool FileWrapper::isBad()
//============================================================================== //==============================================================================
bool FileWrapper::isCompleted() bool FileWrapper::isCompleted()
{ {
return (boost::uint64_t)m_outputFileStream.tellp() >= m_totalFileSize; return (boost::uint64_t)m_outputFileStream.tellp() >= m_expectedFileSize;
} }
//============================================================================== //==============================================================================
...@@ -130,15 +130,31 @@ bool FileWrapper::isCompleted() ...@@ -130,15 +130,31 @@ bool FileWrapper::isCompleted()
//============================================================================== //==============================================================================
boost::uint64_t FileWrapper::getLeftToWrite() boost::uint64_t FileWrapper::getLeftToWrite()
{ {
return m_totalFileSize - (boost::uint64_t)m_outputFileStream.tellp(); return m_expectedFileSize - (boost::uint64_t)m_outputFileStream.tellp();
} }
//============================================================================== //==============================================================================
// FileWrapper::read() // FileWrapper::write()
//============================================================================== //==============================================================================
void FileWrapper::write(std::vector<char>& writeBuff, boost::uint64_t& recvBytes) void FileWrapper::write(std::vector<char>& writeBuff, boost::uint64_t& recvBytes)
{ {
m_outputFileStream.write(&writeBuff[0], (std::streamsize)recvBytes); m_outputFileStream.write(&writeBuff[0], (std::streamsize)recvBytes);
} }
//==============================================================================
// FileWrapper::cleanUp()
//==============================================================================
void FileWrapper::cleanUp()
{
DEBUG_STREAM << "FileWrapper::cleanUp()" << endl;
if(m_outputFileStream.is_open())
m_outputFileStream.close();
boost::system::error_code errorCode;
if(boost::filesystem::exists(m_outputFilePath))
boost::filesystem::remove(m_outputFilePath, errorCode);
} }
} //namespace
...@@ -64,6 +64,11 @@ public: ...@@ -64,6 +64,11 @@ public:
virtual void write(std::vector<char>&, boost::uint64_t&); virtual void write(std::vector<char>&, boost::uint64_t&);
//------------------------------------------------------------------------------
// [Public] Clean up method
//------------------------------------------------------------------------------
virtual void cleanUp();
protected: protected:
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
// [Protected] Class variables // [Protected] Class variables
...@@ -80,10 +85,13 @@ protected: ...@@ -80,10 +85,13 @@ protected:
//File name property //File name property
const std::string m_fileName; const std::string m_fileName;
//Input file size //Expected file size
boost::uint64_t m_totalFileSize; boost::uint64_t m_expectedFileSize;
//Output file path
boost::filesystem::path m_outputFilePath;
//Input file stream //Output file stream
std::ofstream m_outputFileStream; std::ofstream m_outputFileStream;
}; };
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment