Imported Upstream version 17.22.0
[platform/upstream/libzypp.git] / zypp / base / InputStream.cc
1 /*---------------------------------------------------------------------\
2 |                          ____ _   __ __ ___                          |
3 |                         |__  / \ / / . \ . \                         |
4 |                           / / \ V /|  _/  _/                         |
5 |                          / /__ | | | | | |                           |
6 |                         /_____||_| |_| |_|                           |
7 |                                                                      |
8 \---------------------------------------------------------------------*/
9 /** \file       zypp/base/InputStream.cc
10  *
11 */
12 #include <iostream>
13 #include "zypp/base/LogTools.h"
14
15 #include "zypp/base/InputStream.h"
16 #include "zypp/base/GzStream.h"
17
18 #ifdef ENABLE_ZCHUNK_COMPRESSION
19   #include "zypp/base/ZckStream.h"
20 #endif
21
22 #include "zypp/PathInfo.h"
23
24 using std::endl;
25
26 ///////////////////////////////////////////////////////////////////
27 namespace zypp
28 { /////////////////////////////////////////////////////////////////
29
30   ///////////////////////////////////////////////////////////////////
31   namespace
32   { /////////////////////////////////////////////////////////////////
33
34     inline std::streamoff _helperInitSize( const Pathname & file_r )
35     {
36       PathInfo p( file_r );
37       if ( p.isFile() && filesystem::zipType( file_r ) == filesystem::ZT_NONE )
38         return p.size();
39       return -1;
40     }
41
42     inline shared_ptr<std::istream> streamForFile ( const Pathname & file_r )
43     {
44       const auto zType = filesystem::zipType( file_r );
45
46 #ifdef ENABLE_ZCHUNK_COMPRESSION
47       if ( zType == filesystem::ZT_ZCHNK )
48         return shared_ptr<std::istream>( new ifzckstream( file_r.asString().c_str() ) );
49 #endif
50
51       //fall back to gzstream
52       return shared_ptr<std::istream>( new ifgzstream( file_r.asString().c_str() ) );
53     }
54
55     /////////////////////////////////////////////////////////////////
56   } // namespace
57   ///////////////////////////////////////////////////////////////////
58
59   ///////////////////////////////////////////////////////////////////
60   //
61   //    METHOD NAME : InputStream::InputStream
62   //    METHOD TYPE : Constructor
63   //
64   InputStream::InputStream()
65   : _stream( &std::cin, NullDeleter() )
66   , _name( "STDIN" )
67   {}
68
69   ///////////////////////////////////////////////////////////////////
70   //
71   //    METHOD NAME : InputStream::InputStream
72   //    METHOD TYPE : Constructor
73   //
74   InputStream::InputStream( std::istream & stream_r,
75                             const std::string & name_r )
76   : _stream( &stream_r, NullDeleter() )
77   , _name( name_r )
78   {}
79
80   ///////////////////////////////////////////////////////////////////
81   //
82   //    METHOD NAME : InputStream::InputStream
83   //    METHOD TYPE : Constructor
84   //
85   InputStream::InputStream( const Pathname & file_r )
86   : _path( file_r )
87   , _stream( streamForFile( _path.asString() ) )
88   , _name( _path.asString() )
89   , _size( _helperInitSize( _path ) )
90   {}
91
92   ///////////////////////////////////////////////////////////////////
93   //
94   //    METHOD NAME : InputStream::InputStream
95   //    METHOD TYPE : Constructor
96   //
97   InputStream::InputStream( const Pathname & file_r,
98                             const std::string & name_r )
99   : _path( file_r )
100   , _stream( streamForFile( _path.asString() ) )
101   , _name( name_r )
102   , _size( _helperInitSize( _path ) )
103   {}
104
105   ///////////////////////////////////////////////////////////////////
106   //
107   //    METHOD NAME : InputStream::InputStream
108   //    METHOD TYPE : Constructor
109   //
110   InputStream::InputStream( const std::string & file_r )
111   : _path( file_r )
112   , _stream( streamForFile( _path.asString() ) )
113   , _name( _path.asString() )
114   , _size( _helperInitSize( _path ) )
115   {}
116
117   ///////////////////////////////////////////////////////////////////
118   //
119   //    METHOD NAME : InputStream::InputStream
120   //    METHOD TYPE : Constructor
121   //
122   InputStream::InputStream( const std::string & file_r,
123                             const std::string & name_r )
124   : _path( file_r )
125   , _stream( streamForFile( _path.asString() ) )
126   , _name( name_r )
127   , _size( _helperInitSize( _path ) )
128   {}
129
130   ///////////////////////////////////////////////////////////////////
131   //
132   //    METHOD NAME : InputStream::InputStream
133   //    METHOD TYPE : Constructor
134   //
135   InputStream::InputStream( const char * file_r )
136   : _path( file_r )
137   , _stream( streamForFile( _path.asString() ) )
138   , _name( _path.asString() )
139   , _size( _helperInitSize( _path ) )
140   {}
141
142   ///////////////////////////////////////////////////////////////////
143   //
144   //    METHOD NAME : InputStream::InputStream
145   //    METHOD TYPE : Constructor
146   //
147   InputStream::InputStream( const char * file_r,
148                             const std::string & name_r )
149   : _path( file_r )
150   , _stream( streamForFile( _path.asString() ) )
151   , _name( name_r )
152   , _size( _helperInitSize( _path ) )
153   {}
154
155   ///////////////////////////////////////////////////////////////////
156   //
157   //    METHOD NAME : InputStream::~InputStream
158   //    METHOD TYPE : Destructor
159   //
160   InputStream::~InputStream()
161   {}
162
163   /******************************************************************
164    **
165    **   FUNCTION NAME : operator<<
166    **   FUNCTION TYPE : std::ostream &
167   */
168   std::ostream & operator<<( std::ostream & str, const InputStream & obj )
169   {
170     return str << obj.name() << obj.stream();
171   }
172
173   /////////////////////////////////////////////////////////////////
174 } // namespace zypp
175 ///////////////////////////////////////////////////////////////////