How to handle boost::iostreams::gzip_decompressor() exceptions?

Hey all!
I'm trying to add a exception handling code in this function I made:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
   void DecompressLogFile ( std::string source, std::string filename )
   {
      std::ifstream file( source.c_str(), std::ios_base::in | std::ios_base::binary ); //Creates the input stream

      //Tests if the file is being opened correctly.
      if ( !file )
      {
         std::cerr<< "Can't open file: " << source << std::endl;
         return;
      }

      std::ofstream out( filename.c_str(), std::ios_base::out |  std::ios_base::binary ); //Creates the output stream
      boost::iostreams::filtering_streambuf<boost::iostreams::input> in; //Sets the filters to be used in the input stream
      in.push( boost::iostreams::gzip_decompressor() ); //Decompress being used on the file in the input stream
      in.push( file );
      boost::iostreams::copy( in, out ); //Copy the decompressed file to the output stream
   }


To be more precise, I need to handle possible exceptions on this line:
...
in.push( boost::iostreams::gzip_decompressor() );
...

I looked everywhere for examples on how to do this but I can't find anywhere.
So if any of you guys can help me out on this one, code examples would be great too.

Thanks in advance.
You need to decide what you are going to do if the task fails. Will you pass an exception up to the caller? Or will you issue a log message? Or will you quietly ignore it? Or will you take alternative action?
Issue a log message, sorry!
Well you can simply catch the exception like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
void DecompressLogFile ( std::string source, std::string filename )
{
      std::ifstream file( source.c_str(), std::ios_base::in | std::ios_base::binary ); //Creates the input stream

      //Tests if the file is being opened correctly.
      if ( !file )
      {
         std::cerr<< "Can't open file: " << source << std::endl;
         return;
      }

      try
      {
		  std::ofstream out( filename.c_str(), std::ios_base::out |  std::ios_base::binary ); //Creates the output stream
		  boost::iostreams::filtering_streambuf<boost::iostreams::input> in; //Sets the filters to be used in the input stream
		  in.push( boost::iostreams::gzip_decompressor() ); //Decompress being used on the file in the input stream
		  in.push( file );
		  boost::iostreams::copy( in, out ); //Copy the decompressed file to the output stream
      }
      catch(std::exception& e)
      {
    	  std::cerr << e.what() << std::endl;
      }
   }
}
Last edited on
Topic archived. No new replies allowed.