~ubuntu-branches/ubuntu/wily/qgis/wily

« back to all changes in this revision

Viewing changes to src/core/spatialindex/tools/TemporaryFile.cc

  • Committer: Bazaar Package Importer
  • Author(s): Johan Van de Wauw
  • Date: 2010-07-11 20:23:24 UTC
  • mfrom: (3.1.4 squeeze)
  • Revision ID: james.westby@ubuntu.com-20100711202324-5ktghxa7hracohmr
Tags: 1.4.0+12730-3ubuntu1
* Merge from Debian unstable (LP: #540941).
* Fix compilation issues with QT 4.7
* Add build-depends on libqt4-webkit-dev 

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Tools Library
 
2
//
 
3
// Copyright (C) 2004  Navel Ltd.
 
4
//
 
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.
 
9
//
 
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.
 
14
//
 
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
 
18
//
 
19
//  Email:
 
20
//    mhadji@gmail.com
 
21
 
 
22
#include <stdio.h>
 
23
#include <unistd.h>
 
24
 
 
25
#include <Tools.h>
 
26
 
 
27
#ifdef WIN32
 
28
#include <fcntl.h>
 
29
int mkstemp( char* prefix )
 
30
{
 
31
  char* filename = tempnam( ".", prefix );
 
32
  if ( filename == NULL )
 
33
    return -1;
 
34
  int fd = open( filename, O_RDWR | O_BINARY | O_CREAT, 0444 );
 
35
  if ( fd < 0 )
 
36
    return -1;
 
37
  return fd;
 
38
}
 
39
#endif
 
40
 
 
41
Tools::TemporaryFile::TemporaryFile()
 
42
    : m_currentFile( 0 ),
 
43
    m_fileSize( 0 ),
 
44
    m_bEOF( false )
 
45
{
 
46
  char p[5 + 6] = "tmpfXXXXXX";
 
47
  int fd = mkstemp( p );
 
48
  if ( fd == -1 )
 
49
    throw IllegalStateException(
 
50
      "Tools::TemporaryFile::TemporaryFile: Cannot create tmp file."
 
51
    );
 
52
  close( fd );
 
53
 
 
54
  m_file.open( p, std::ios::in | std::ios::out | std::ios::trunc | std::ios::binary );
 
55
 
 
56
  if ( ! m_file )
 
57
    throw IllegalStateException(
 
58
      "Tools::TemporaryFile::TemporaryFile: Cannot open tmp file."
 
59
    );
 
60
  m_strFileName.push_back( std::string( p ) );
 
61
}
 
62
 
 
63
Tools::TemporaryFile::~TemporaryFile()
 
64
{
 
65
  m_file.close();
 
66
 
 
67
  bool bFailed = false;
 
68
  for ( unsigned long cFile = 0; cFile < m_strFileName.size(); cFile++ )
 
69
  {
 
70
    if ( remove( m_strFileName[cFile].c_str() ) == -1 ) bFailed = true;
 
71
  }
 
72
 
 
73
  if ( bFailed )
 
74
    throw IllegalStateException(
 
75
      "Tools::TemporaryFile::~TemporaryFile: Cannot remove tmp file."
 
76
    );
 
77
}
 
78
 
 
79
void Tools::TemporaryFile::storeNextObject(
 
80
  unsigned long len,
 
81
  const byte* const data
 
82
)
 
83
{
 
84
  if ( m_fileSize > 1073741824L )
 
85
  {
 
86
    char p[5 + 6] = "tmpfXXXXXX";
 
87
    int fd = mkstemp( p );
 
88
    if ( fd == -1 )
 
89
      throw IllegalStateException(
 
90
        "Tools::TemporaryFile::storeNextObject: Cannot create tmp file."
 
91
      );
 
92
    close( fd );
 
93
 
 
94
    m_file.close();
 
95
    m_file.clear();
 
96
    m_file.open( p, std::ios::in | std::ios::out | std::fstream::trunc | std::fstream::binary );
 
97
    if ( ! m_file )
 
98
      throw IllegalStateException(
 
99
        "Tools::TemporaryFile::storeNextObject: Cannot open tmp file."
 
100
      );
 
101
 
 
102
    m_strFileName.push_back( std::string( p ) );
 
103
    m_currentFile++;
 
104
    m_fileSize = 0;
 
105
  }
 
106
 
 
107
  m_file.write( reinterpret_cast<char*>( &len ), sizeof( unsigned long ) );
 
108
  m_file.write( reinterpret_cast<const char*>( data ), len );
 
109
 
 
110
  if ( ! m_file.good() )
 
111
    throw IllegalStateException(
 
112
      "Tools::TemporaryFile::storeNextObject: Cannot store object."
 
113
    );
 
114
 
 
115
  m_fileSize += len + sizeof( unsigned long );
 
116
}
 
117
 
 
118
void Tools::TemporaryFile::storeNextObject( ISerializable* r )
 
119
{
 
120
  unsigned long len;
 
121
  byte* data;
 
122
  r->storeToByteArray( &data, len );
 
123
 
 
124
  try
 
125
  {
 
126
    assert( len > 0 );
 
127
    storeNextObject( len, data );
 
128
    delete[] data;
 
129
  }
 
130
  catch ( ... )
 
131
  {
 
132
    delete[] data;
 
133
    throw;
 
134
  }
 
135
}
 
136
 
 
137
void Tools::TemporaryFile::loadNextObject( byte** data, unsigned long& len )
 
138
{
 
139
  if ( m_bEOF )
 
140
    throw EndOfStreamException(
 
141
      "Tools::TemporaryFile::loadNextObject: End of file."
 
142
    );
 
143
 
 
144
  m_file.read( reinterpret_cast<char*>( &len ), sizeof( unsigned long ) );
 
145
 
 
146
  if ( ! m_file.good() )
 
147
  {
 
148
    if ( m_currentFile == m_strFileName.size() - 1 )
 
149
    {
 
150
      m_bEOF = true;
 
151
      throw EndOfStreamException(
 
152
        "Tools::TemporaryFile::loadNextObject: End of file."
 
153
      );
 
154
    }
 
155
 
 
156
    m_currentFile++;
 
157
    m_file.close();
 
158
    m_file.clear();
 
159
    m_file.open(
 
160
      m_strFileName[m_currentFile].c_str(),
 
161
      std::ios::in | std::ios::out | std::ios::binary
 
162
    );
 
163
 
 
164
    if ( ! m_file )
 
165
      throw IllegalStateException(
 
166
        "Tools::TemporaryFile::loadNextObject: Cannot open tmp file."
 
167
      );
 
168
 
 
169
    m_fileSize = 0;
 
170
 
 
171
    m_file.read( reinterpret_cast<char*>( &len ), sizeof( unsigned long ) );
 
172
 
 
173
    if ( ! m_file.good() )
 
174
      throw IllegalStateException(
 
175
        "Tools::TemporaryFile::loadNextObject: Cannot load length."
 
176
      );
 
177
  }
 
178
 
 
179
  *data = new byte[len];
 
180
  m_file.read( reinterpret_cast<char*>( *data ), len );
 
181
 
 
182
  if ( ! m_file.good() )
 
183
  {
 
184
    delete[] *data;
 
185
    throw IllegalStateException(
 
186
      "Tools::TemporaryFile::loadNextObject: Cannot load data."
 
187
    );
 
188
  }
 
189
}
 
190
 
 
191
void Tools::TemporaryFile::loadNextObject( ISerializable* r )
 
192
{
 
193
  unsigned long len;
 
194
  byte* data;
 
195
  loadNextObject( &data, len );
 
196
  r->loadFromByteArray( data );
 
197
  delete[] data;
 
198
}
 
199
 
 
200
void Tools::TemporaryFile::rewindForReading()
 
201
{
 
202
  m_file.close();
 
203
  m_file.clear();
 
204
  m_file.open(
 
205
    m_strFileName[0].c_str(),
 
206
    std::ios::in | std::ios::out | std::ios::binary
 
207
  );
 
208
 
 
209
  if ( ! m_file )
 
210
    throw IllegalStateException(
 
211
      "Tools::TemporaryFile::rewindForReading: "
 
212
      "Cannot open file" + m_strFileName[0]
 
213
    );
 
214
 
 
215
  m_currentFile = 0;
 
216
  m_fileSize = 0;
 
217
  m_bEOF = false;
 
218
}
 
219
 
 
220
void Tools::TemporaryFile::rewindForWriting()
 
221
{
 
222
  bool bFailed = false;
 
223
  for ( unsigned long cFile = 0; cFile < m_strFileName.size(); cFile++ )
 
224
  {
 
225
    if ( remove( m_strFileName[cFile].c_str() ) == -1 ) bFailed = true;
 
226
  }
 
227
 
 
228
  if ( bFailed )
 
229
    throw IllegalStateException(
 
230
      "Tools::TemporaryFile::rewindForWriting: Cannot remove tmp file."
 
231
    );
 
232
 
 
233
  std::string str = m_strFileName[0];
 
234
  m_strFileName.clear();
 
235
 
 
236
  m_file.close();
 
237
  m_file.clear();
 
238
  m_file.open(
 
239
    str.c_str(),
 
240
    std::ios::in | std::ios::out | std::ios::trunc | std::ios::binary
 
241
  );
 
242
 
 
243
  if ( ! m_file )
 
244
    throw IllegalStateException(
 
245
      "Tools::TemporaryFile::rewindForWriting: "
 
246
      "Cannot open file " + str
 
247
    );
 
248
 
 
249
  m_strFileName.push_back( str );
 
250
  m_currentFile = 0;
 
251
  m_fileSize = 0;
 
252
  m_bEOF = false;
 
253
}
 
254