📄 temporaryfile.cc
字号:
// Tools Library//// Copyright (C) 2004 Navel Ltd.//// This library is free software; you can redistribute it and/or// modify it under the terms of the GNU Lesser General Public// License as published by the Free Software Foundation; either// version 2.1 of the License, or (at your option) any later version.//// This library is distributed in the hope that it will be useful,// but WITHOUT ANY WARRANTY; without even the implied warranty of// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU// Lesser General Public License for more details.//// You should have received a copy of the GNU Lesser General Public// License along with this library; if not, write to the Free Software// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA//// Contact information:// Mailing address:// Marios Hadjieleftheriou// University of California, Riverside// Department of Computer Science// Surge Building, Room 310// Riverside, CA 92521//// Email:// marioh@cs.ucr.edu
#include <stdio.h>
#include <unistd.h>
#include <Tools.h>
Tools::TemporaryFile::TemporaryFile()
: m_currentFile(0), m_fileSize(0), m_bEOF(false)
{
char p[5 + 6] = "tmpfXXXXXX";
int fd = mkstemp(p);
if (fd == -1) throw IllegalStateException("Tools::TemporaryFile::TemporaryFile: Cannot create tmp file.");
close(fd);
m_file.open(p, std::fstream::out | std::fstream::trunc | std::fstream::binary);
if (! m_file) throw IllegalStateException("Tools::TemporaryFile::TemporaryFile: Cannot open tmp file.");
m_strFileName.push_back(std::string(p));
}
Tools::TemporaryFile::~TemporaryFile()
{
m_file.close();
bool bFailed = false;
for (unsigned long cFile = 0; cFile < m_strFileName.size(); cFile++)
{
if (remove(m_strFileName[cFile].c_str()) == -1) bFailed = true;
}
if (bFailed) throw IllegalStateException("Tools::TemporaryFile::~TemporaryFile: Cannot remove tmp file.");
}
void Tools::TemporaryFile::storeNextObject(unsigned long len, const byte* const data)
{
if (m_fileSize > 1073741824L)
{
char p[5 + 6] = "tmpfXXXXXX";
int fd = mkstemp(p);
if (fd == -1) throw IllegalStateException("Tools::TemporaryFile::storeNextObject: Cannot create tmp file.");
close(fd);
m_file.close();
m_file.clear();
m_file.open(p, std::fstream::out | std::fstream::trunc | std::fstream::binary);
if (! m_file) throw IllegalStateException("Tools::TemporaryFile::storeNextObject: Cannot open tmp file.");
m_strFileName.push_back(std::string(p));
m_currentFile++;
m_fileSize = 0;
}
m_file.write(reinterpret_cast<char*>(&len), sizeof(unsigned long));
m_file.write(reinterpret_cast<const char*>(data), len);
if (! m_file.good()) throw IllegalStateException("Tools::TemporaryFile::storeNextObject: Cannot store object.");
m_fileSize += len + sizeof(unsigned long);
}
void Tools::TemporaryFile::storeNextObject(ISerializable* r)
{
unsigned long len;
byte* data;
r->storeToByteArray(len, &data);
try
{
storeNextObject(len, data);
delete[] data;
}
catch (...)
{
delete[] data;
throw;
}
}
void Tools::TemporaryFile::loadNextObject(unsigned long& len, byte** data) throw (EndOfStreamException, IllegalStateException)
{
if (m_bEOF) throw EndOfStreamException("Tools::TemporaryFile::loadNextObject: End of file.");
m_file.read(reinterpret_cast<char*>(&len), sizeof(unsigned long));
if (! m_file.good())
{
if (m_currentFile == m_strFileName.size() - 1)
{
m_bEOF = true;
throw EndOfStreamException("Tools::TemporaryFile::loadNextObject: End of file.");
}
m_currentFile++;
m_file.close();
m_file.clear();
m_file.open(m_strFileName[m_currentFile].c_str(), std::fstream::in | std::fstream::binary);
if (! m_file) throw IllegalStateException("Tools::TemporaryFile::loadNextObject: Cannot open tmp file.");
m_fileSize = 0;
m_file.read(reinterpret_cast<char*>(&len), sizeof(unsigned long));
if (! m_file.good()) throw IllegalStateException("Tools::TemporaryFile::loadNextObject: Cannot load length.");
}
*data = new byte[len];
m_file.read(reinterpret_cast<char*>(*data), len);
if (! m_file.good())
{
delete[] *data;
throw IllegalStateException("Tools::TemporaryFile::loadNextObject: Cannot load data.");
}
}
void Tools::TemporaryFile::loadNextObject(ISerializable* r) throw (EndOfStreamException, IllegalStateException)
{
unsigned long len;
byte* data;
loadNextObject(len, &data);
r->loadFromByteArray(data);
delete[] data;
}
bool Tools::TemporaryFile::rewindForReading()
{
m_file.close();
m_file.clear();
m_file.open(m_strFileName[0].c_str(), std::fstream::in | std::fstream::binary);
if (! m_file) return false;
m_currentFile = 0;
m_fileSize = 0;
m_bEOF = false;
return true;
}
bool Tools::TemporaryFile::rewindForWriting()
{
bool bFailed = false;
for (unsigned long cFile = 0; cFile < m_strFileName.size(); cFile++)
{
if (remove(m_strFileName[cFile].c_str()) == -1) bFailed = true;
}
if (bFailed) throw IllegalStateException("Tools::TemporaryFile::rewindForWriting: Cannot remove tmp file.");
std::string str = m_strFileName[0];
m_strFileName.clear();
m_file.close();
m_file.clear();
m_file.open(str.c_str(), std::fstream::out | std::fstream::trunc | std::fstream::binary);
if (! m_file) return false;
m_strFileName.push_back(str);
m_currentFile = 0;
m_fileSize = 0;
m_bEOF = false;
return true;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -