Changeset 53:f2b76db078b0 in rrlib_util


Ignore:
Timestamp:
16.03.2012 21:39:36 (8 years ago)
Author:
Jens Wettach <wettach@…>
Branch:
default
Phase:
public
Message:

added support for reading/writing a std::vector<T> from/to a bzipped file

Files:
2 edited

Legend:

Unmodified
Added
Removed
  • make.xml

    r44 r53  
    33<targets> 
    44 
    5   <rrlib name="util"> 
     5  <rrlib name="util" 
     6         libs="boost_iostreams"> 
    67    <sources> 
    78      join.h 
  • sFileIOUtils.h

    r52 r53  
    3535#define _rrlib_util_sFileIOUtils_h_ 
    3636 
     37//rrlib includes 
     38#include "rrlib/util/sStringUtils.h" 
     39#include "rrlib/logging/messages.h" 
     40 
    3741//STL includes 
    3842#include <string> 
    3943#include <vector> 
    4044#include <map> 
     45#include <fstream> 
     46#include <iterator> 
     47 
     48//boost includes 
     49#include <boost/iostreams/filtering_stream.hpp> 
     50#include <boost/iostreams/filter/bzip2.hpp> 
    4151 
    4252extern "C" 
     
    111121   */ 
    112122  static void DecompressFile(const std::string& input_filename, const std::string& output_filename); 
     123 
     124 
     125  /*! 
     126   * \brief Writes content of given container to file with given name 
     127   * 
     128   * \returns true iff file could be processed as expected 
     129   * 
     130   * \note If filename has suffix ".bz2" it is compressed accordingly. 
     131   */ 
     132  template <class T> 
     133  static bool WriteContainerToFile(const std::vector<T> &content, const std::string& filename) 
     134  { 
     135    std::ofstream output_file_stream; 
     136    RRLIB_LOG_PRINT(rrlib::logging::eLL_DEBUG_VERBOSE_1, "got file <", filename, ">"); 
     137    if (sStringUtils::EndsWith(filename, ".bz2")) 
     138    { 
     139      RRLIB_LOG_PRINT(rrlib::logging::eLL_DEBUG_VERBOSE_1, "compressing"); 
     140      boost::iostreams::filtering_ostream out; 
     141      out.push(boost::iostreams::bzip2_compressor()); 
     142      output_file_stream.open(filename.c_str()); 
     143      if ( output_file_stream ) 
     144      { 
     145        out.push(output_file_stream); 
     146        std::copy(content.begin(), content.end(), std::ostream_iterator<T>(out)); 
     147        return true; 
     148      } 
     149    } 
     150    else 
     151    { 
     152      output_file_stream.open(filename.c_str()); 
     153      if ( output_file_stream ) 
     154      { 
     155        std::copy(content.begin(), content.end(), std::ostream_iterator<T>(output_file_stream)); 
     156        output_file_stream.close(); 
     157        return true; 
     158      } 
     159    } 
     160    return false; 
     161  } 
     162 
     163  /*! 
     164   * \brief Reads file with given name and writes its content to given container 
     165   * 
     166   * \returns true iff file could be processed as expected 
     167   * 
     168   * \note If filename has suffix ".bz2" it is decompressed accordingly before reading. 
     169   */ 
     170  template <class T> 
     171  static bool ReadContainerFromFile(std::vector<T> &content, const std::string& filename) 
     172  { 
     173    std::ifstream input_file_stream; 
     174    RRLIB_LOG_PRINT(rrlib::logging::eLL_DEBUG_VERBOSE_1, "got file <", filename, ">"); 
     175    if (sStringUtils::EndsWith(filename, ".bz2")) 
     176    { 
     177      RRLIB_LOG_PRINT(rrlib::logging::eLL_DEBUG_VERBOSE_1, "decompressing"); 
     178      boost::iostreams::filtering_istream in; 
     179      in.push(boost::iostreams::bzip2_decompressor()); 
     180      input_file_stream.open(filename.c_str()); 
     181      if (input_file_stream) 
     182      { 
     183        in.push(input_file_stream); 
     184        std::copy(std::istream_iterator<T>(in), std::istream_iterator<T>(), std::back_inserter(content)); 
     185        return true; 
     186      } 
     187    } 
     188    else 
     189    { 
     190      input_file_stream.open(filename.c_str()); 
     191      if (input_file_stream) 
     192      { 
     193        std::copy(std::istream_iterator<T>(input_file_stream), std::istream_iterator<T>(), std::back_inserter(content)); 
     194      } 
     195      input_file_stream.close(); 
     196      return true; 
     197    } 
     198    return false; 
     199  } 
     200 
    113201 
    114202 
Note: See TracChangeset for help on using the changeset viewer.