194 |
|
if( fileDescriptor==0 ) throw std::runtime_error( "ReducedMenuSample initialise from file - couldn't open file" ); |
195 |
|
::UnixFileSentry fileSentry( fileDescriptor ); // Use this as an exception safe way of closing the input file |
196 |
|
google::protobuf::io::FileInputStream fileInput( fileDescriptor ); |
197 |
+ |
|
198 |
+ |
// First read the magic number at the start of the file and make sure it |
199 |
+ |
// matches what I expect. This is uncompressed so I'll wrap it in a block |
200 |
+ |
// to make sure the CodedInputStream is destructed before creating a new |
201 |
+ |
// one with gzip input. |
202 |
+ |
{ |
203 |
+ |
google::protobuf::io::CodedInputStream codedInput( &fileInput ); |
204 |
+ |
|
205 |
+ |
// As a read buffer, I'll create a string the correct size (filled with an arbitrary |
206 |
+ |
// character) and read straight into that. |
207 |
+ |
std::string readMagicNumber; |
208 |
+ |
if( !codedInput.ReadString( &readMagicNumber, FILE_FORMAT_MAGIC_NUMBER.size() ) ) throw std::runtime_error( "ReducedMenuSample initialise from file - error reading magic number" ); |
209 |
+ |
if( readMagicNumber!=FILE_FORMAT_MAGIC_NUMBER ) throw std::runtime_error( "ReducedMenuSample - tried to initialise with a file that is not the correct format" ); |
210 |
+ |
|
211 |
+ |
google::protobuf::uint32 fileformatVersion; |
212 |
+ |
if( !codedInput.ReadVarint32( &fileformatVersion ) ) throw std::runtime_error( "ReducedMenuSample initialise from file - error reading file format version" ); |
213 |
+ |
// So far I only have (and ever expect to have) one version of the file |
214 |
+ |
// format, imaginatively versioned "1". You never know though... |
215 |
+ |
if( fileformatVersion>1 ) std::cerr << "Warning: Attempting to read a ReducedMenuSample with version " << fileformatVersion << " with code that only knows up to version 1." << std::endl; |
216 |
+ |
} |
217 |
+ |
|
218 |
|
google::protobuf::io::GzipInputStream gzipInput( &fileInput ); |
219 |
|
google::protobuf::io::CodedInputStream codedInput( &gzipInput ); |
220 |
|
|
224 |
|
size_t totalBytesLimit=67108864; |
225 |
|
codedInput.SetTotalBytesLimit( totalBytesLimit, -1 ); |
226 |
|
|
206 |
– |
// First read the magic number at the start of the file and make sure it |
207 |
– |
// matches what I expect. As a read buffer, I'll create a string the correct |
208 |
– |
// size (filled with an arbitrary character) and read straight into that. |
209 |
– |
std::string readMagicNumber; |
210 |
– |
if( !codedInput.ReadString( &readMagicNumber, FILE_FORMAT_MAGIC_NUMBER.size() ) ) throw std::runtime_error( "ReducedMenuSample initialise from file - error reading magic number" ); |
211 |
– |
if( readMagicNumber!=FILE_FORMAT_MAGIC_NUMBER ) throw std::runtime_error( "ReducedMenuSample - tried to initialise with a file that is not the correct format" ); |
212 |
– |
|
213 |
– |
google::protobuf::uint32 fileformatVersion; |
214 |
– |
if( !codedInput.ReadVarint32( &fileformatVersion ) ) throw std::runtime_error( "ReducedMenuSample initialise from file - error reading file format version" ); |
215 |
– |
// So far I only have (and ever expect to have) one version of the file |
216 |
– |
// format, imaginatively versioned "1". You never know though... |
217 |
– |
if( fileformatVersion>1 ) std::cerr << "Warning: Attempting to read a ReducedMenuSample with version " << fileformatVersion << " with code that only knows up to version 1." << std::endl; |
218 |
– |
|
227 |
|
google::protobuf::uint64 messageSize; |
228 |
|
|
229 |
|
// Read the size of the header message |
366 |
|
|
367 |
|
// Setup the protobuf file handlers |
368 |
|
google::protobuf::io::FileOutputStream fileOutput( fileDescriptor ); |
369 |
+ |
|
370 |
+ |
// I want the magic number and file format identifier uncompressed, so |
371 |
+ |
// I'll write those before switching to using gzipped output. |
372 |
+ |
{ // Block to make sure codedOutput is destructed before the gzip version is created |
373 |
+ |
google::protobuf::io::CodedOutputStream codedOutput( &fileOutput ); |
374 |
+ |
|
375 |
+ |
// Write a magic number at the start of all files |
376 |
+ |
codedOutput.WriteString( pImple_->FILE_FORMAT_MAGIC_NUMBER ); |
377 |
+ |
// Write an integer that specifies what version of the file format I'm using. I |
378 |
+ |
// have no intention of changing the format but I might as well keep the option |
379 |
+ |
// open. |
380 |
+ |
codedOutput.WriteVarint32( 1 ); |
381 |
+ |
} |
382 |
+ |
|
383 |
|
google::protobuf::io::GzipOutputStream gzipOutput( &fileOutput ); |
384 |
|
google::protobuf::io::CodedOutputStream codedOutput( &gzipOutput ); |
385 |
|
|
364 |
– |
// Write a magic number at the start of all files |
365 |
– |
codedOutput.WriteString( pImple_->FILE_FORMAT_MAGIC_NUMBER ); |
366 |
– |
// Write an integer that specifies what version of the file format I'm using. I |
367 |
– |
// have no intention of changing the format but I might as well keep the option |
368 |
– |
// open. |
369 |
– |
codedOutput.WriteVarint32( 1 ); |
370 |
– |
|
386 |
|
// Write the size of the header message into the file... |
387 |
|
codedOutput.WriteVarint64( pImple_->protobufSampleHeader.ByteSize() ); |
388 |
|
// ...and then write the header |