OpenTREP Logo  0.07.18
C++ Open Travel Request Parsing Library
Loading...
Searching...
No Matches
PORFileHelper.cpp
Go to the documentation of this file.
1// //////////////////////////////////////////////////////////////////////
2// Import section
3// //////////////////////////////////////////////////////////////////////
4// STL
5#include <cassert>
6#include <string>
7#include <istream>
8#include <exception>
9// Boost
10#include <boost/filesystem.hpp>
11#include <boost/filesystem/fstream.hpp>
12#include <boost/tokenizer.hpp>
13#include <boost/iostreams/device/file.hpp>
14#include <boost/iostreams/filtering_stream.hpp>
15#include <boost/iostreams/filter/gzip.hpp>
16#include <boost/iostreams/filter/bzip2.hpp>
17// OpenTrep
20
21namespace OPENTREP {
22
23 // //////////////////////////////////////////////////////////////////////
24 PORFileHelper::PORFileHelper() : _iStreamPtr (NULL) {
25 }
26
27 // //////////////////////////////////////////////////////////////////////
29 : _iStreamPtr (iPORFileHelper._iStreamPtr) {
30 }
31
32 // //////////////////////////////////////////////////////////////////////
34 : _iStreamPtr (NULL) {
35 init (iPORFilePath);
36 }
37
38 // //////////////////////////////////////////////////////////////////////
40 delete _iStreamPtr; _iStreamPtr = NULL;
41 }
42
43 // //////////////////////////////////////////////////////////////////////
44 std::istream& PORFileHelper::getFileStreamRef() const {
45 assert (_iStreamPtr != NULL);
46 return *_iStreamPtr;
47 }
48
49 // //////////////////////////////////////////////////////////////////////
50 void PORFileHelper::init (const PORFilePath_T& iPORFilePath) {
51 // DEBUG
52 OPENTREP_LOG_DEBUG ("Checking whether the POR file ('" << iPORFilePath
53 << "') exists, is readable and/or compressed.");
54
55 // Check whether the POR file exists and is readable.
56 boost::filesystem::path lPORFilePath (iPORFilePath.begin(),
57 iPORFilePath.end());
58 if (!(boost::filesystem::exists (lPORFilePath)
59 && boost::filesystem::is_regular_file (lPORFilePath))) {
60 OPENTREP_LOG_ERROR ("The POR file " << iPORFilePath
61 << " does not exist or cannot be open." << std::endl);
62
63 throw FileNotFoundException ("The POR file " + iPORFilePath
64 + " does not exist or cannot be read");
65 }
66
74 const boost::filesystem::path& lPORFileExtPath = lPORFilePath.extension();
75 const std::string& lPORFileExt = lPORFileExtPath.string();
76 if (lPORFileExt == ".bz2") {
77 // Open the file
78 boost::iostreams::file_source cprdPORFile (iPORFilePath, std::ios_base::in
79 | std::ios_base::binary);
80
81 // Uncompress the file with the BZ2 library and its Boost wrapper
82 boost::iostreams::filtering_istream* bunzip2Filter_ptr =
83 new boost::iostreams::filtering_istream();
84 bunzip2Filter_ptr->push (boost::iostreams::bzip2_decompressor());
85 bunzip2Filter_ptr->push (cprdPORFile);
86 _iStreamPtr = bunzip2Filter_ptr;
87
88 } else if (lPORFileExt == ".gz") {
89 // Open the file
90 boost::iostreams::file_source cprdPORFile (iPORFilePath, std::ios_base::in
91 | std::ios_base::binary);
92
93 // Uncompress the file with the BZ2 library and its Boost wrapper
94 boost::iostreams::filtering_istream* gunzipFilter_ptr =
95 new boost::iostreams::filtering_istream();
96 gunzipFilter_ptr->push (boost::iostreams::gzip_decompressor());
97 gunzipFilter_ptr->push (cprdPORFile);
98 _iStreamPtr = gunzipFilter_ptr;
99
100 } else if (lPORFileExt == ".csv") {
101 // Open the file
102 _iStreamPtr = new boost::filesystem::ifstream (iPORFilePath,
103 std::ios_base::in);
104
105 } else {
106 //
107 std::ostringstream errorStr;
108 errorStr << "The POR file " << iPORFilePath
109 << " has got an unknown extension ('" << lPORFileExt
110 << "'). Recognised extensions: .csv, .bz2, .gz";
111 OPENTREP_LOG_ERROR (errorStr.str());
112 throw FileExtensionUnknownException (errorStr.str());
113 }
114 }
115
116}
#define OPENTREP_LOG_ERROR(iToBeLogged)
Definition Logger.hpp:24
#define OPENTREP_LOG_DEBUG(iToBeLogged)
Definition Logger.hpp:33
std::istream & getFileStreamRef() const
PORFileHelper(const PORFilePath_T &)