3
// Copyright (C) 2004 Navel Ltd.
5
// This library is free software; you can redistribute it and/or
6
// modify it under the terms of the GNU Lesser General Public
7
// License as published by the Free Software Foundation; either
8
// version 2.1 of the License, or (at your option) any later version.
10
// This library is distributed in the hope that it will be useful,
11
// but WITHOUT ANY WARRANTY; without even the implied warranty of
12
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
// Lesser General Public License for more details.
15
// You should have received a copy of the GNU Lesser General Public
16
// License along with this library; if not, write to the Free Software
17
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29
int mkstemp( char* prefix )
31
char* filename = tempnam( ".", prefix );
32
if ( filename == NULL )
34
int fd = open( filename, O_RDWR | O_BINARY | O_CREAT, 0444 );
41
Tools::TemporaryFile::TemporaryFile()
46
char p[5 + 6] = "tmpfXXXXXX";
47
int fd = mkstemp( p );
49
throw IllegalStateException(
50
"Tools::TemporaryFile::TemporaryFile: Cannot create tmp file."
54
m_file.open( p, std::ios::in | std::ios::out | std::ios::trunc | std::ios::binary );
57
throw IllegalStateException(
58
"Tools::TemporaryFile::TemporaryFile: Cannot open tmp file."
60
m_strFileName.push_back( std::string( p ) );
63
Tools::TemporaryFile::~TemporaryFile()
68
for ( unsigned long cFile = 0; cFile < m_strFileName.size(); cFile++ )
70
if ( remove( m_strFileName[cFile].c_str() ) == -1 ) bFailed = true;
74
throw IllegalStateException(
75
"Tools::TemporaryFile::~TemporaryFile: Cannot remove tmp file."
79
void Tools::TemporaryFile::storeNextObject(
81
const byte* const data
84
if ( m_fileSize > 1073741824L )
86
char p[5 + 6] = "tmpfXXXXXX";
87
int fd = mkstemp( p );
89
throw IllegalStateException(
90
"Tools::TemporaryFile::storeNextObject: Cannot create tmp file."
96
m_file.open( p, std::ios::in | std::ios::out | std::fstream::trunc | std::fstream::binary );
98
throw IllegalStateException(
99
"Tools::TemporaryFile::storeNextObject: Cannot open tmp file."
102
m_strFileName.push_back( std::string( p ) );
107
m_file.write( reinterpret_cast<char*>( &len ), sizeof( unsigned long ) );
108
m_file.write( reinterpret_cast<const char*>( data ), len );
110
if ( ! m_file.good() )
111
throw IllegalStateException(
112
"Tools::TemporaryFile::storeNextObject: Cannot store object."
115
m_fileSize += len + sizeof( unsigned long );
118
void Tools::TemporaryFile::storeNextObject( ISerializable* r )
122
r->storeToByteArray( &data, len );
127
storeNextObject( len, data );
137
void Tools::TemporaryFile::loadNextObject( byte** data, unsigned long& len )
140
throw EndOfStreamException(
141
"Tools::TemporaryFile::loadNextObject: End of file."
144
m_file.read( reinterpret_cast<char*>( &len ), sizeof( unsigned long ) );
146
if ( ! m_file.good() )
148
if ( m_currentFile == m_strFileName.size() - 1 )
151
throw EndOfStreamException(
152
"Tools::TemporaryFile::loadNextObject: End of file."
160
m_strFileName[m_currentFile].c_str(),
161
std::ios::in | std::ios::out | std::ios::binary
165
throw IllegalStateException(
166
"Tools::TemporaryFile::loadNextObject: Cannot open tmp file."
171
m_file.read( reinterpret_cast<char*>( &len ), sizeof( unsigned long ) );
173
if ( ! m_file.good() )
174
throw IllegalStateException(
175
"Tools::TemporaryFile::loadNextObject: Cannot load length."
179
*data = new byte[len];
180
m_file.read( reinterpret_cast<char*>( *data ), len );
182
if ( ! m_file.good() )
185
throw IllegalStateException(
186
"Tools::TemporaryFile::loadNextObject: Cannot load data."
191
void Tools::TemporaryFile::loadNextObject( ISerializable* r )
195
loadNextObject( &data, len );
196
r->loadFromByteArray( data );
200
void Tools::TemporaryFile::rewindForReading()
205
m_strFileName[0].c_str(),
206
std::ios::in | std::ios::out | std::ios::binary
210
throw IllegalStateException(
211
"Tools::TemporaryFile::rewindForReading: "
212
"Cannot open file" + m_strFileName[0]
220
void Tools::TemporaryFile::rewindForWriting()
222
bool bFailed = false;
223
for ( unsigned long cFile = 0; cFile < m_strFileName.size(); cFile++ )
225
if ( remove( m_strFileName[cFile].c_str() ) == -1 ) bFailed = true;
229
throw IllegalStateException(
230
"Tools::TemporaryFile::rewindForWriting: Cannot remove tmp file."
233
std::string str = m_strFileName[0];
234
m_strFileName.clear();
240
std::ios::in | std::ios::out | std::ios::trunc | std::ios::binary
244
throw IllegalStateException(
245
"Tools::TemporaryFile::rewindForWriting: "
246
"Cannot open file " + str
249
m_strFileName.push_back( str );