libzypp: zypp-core/base/inputstream.cc Source File

libzypp 17.38.7

inputstream.cc
Go to the documentation of this file.
1/*---------------------------------------------------------------------\
2| ____ _ __ __ ___ |
3| |__ / \ / / . \ . \ |
4| / / \ V /| _/ _/ |
5| / /__ | | | | | | |
6| /_____||_| |_| |_| |
7| |
8\---------------------------------------------------------------------*/
12#include <iostream>
13#include <zypp-core/base/LogTools.h>
14
15#include "inputstream.h"
16#include <utility>
17#include <zypp-core/base/GzStream>
18
19#ifdef ENABLE_ZCHUNK_COMPRESSION
20 #include <zypp-core/base/ZckStream>
21#endif
22
23#include <zypp-core/fs/PathInfo.h>
24
25using std::endl;
26
28namespace zypp
29{
30
32 namespace
33 {
34
35 inline std::streamoff _helperInitSize( const Pathname & file_r )
36 {
37 PathInfo p( file_r );
38 if ( p.isFile() && filesystem::zipType( file_r ) == filesystem::ZT_NONE )
39 return p.size();
40 return -1;
41 }
42
43 inline shared_ptr<std::istream> streamForFile ( const Pathname & file_r )
44 {
45#ifdef ENABLE_ZCHUNK_COMPRESSION
46 if ( const auto zType = filesystem::zipType( file_r ); zType == filesystem::ZT_ZCHNK )
47 return shared_ptr<std::istream>( new ifzckstream( file_r.asString().c_str() ) );
48#endif
49
50 //fall back to gzstream
51 return shared_ptr<std::istream>( new ifgzstream( file_r.asString().c_str() ) );
52 }
53
55 } // namespace
57
59 //
60 // METHOD NAME : InputStream::InputStream
61 // METHOD TYPE : Constructor
62 //
63 InputStream::InputStream()
64 : _stream( &std::cin, NullDeleter() )
65 , _name( "STDIN" )
66 {}
67
69 //
70 // METHOD NAME : InputStream::InputStream
71 // METHOD TYPE : Constructor
72 //
73 InputStream::InputStream( std::istream & stream_r,
74 std::string name_r )
75 : _stream( &stream_r, NullDeleter() )
76 , _name(std::move( name_r ))
77 {}
78
80 //
81 // METHOD NAME : InputStream::InputStream
82 // METHOD TYPE : Constructor
83 //
84 InputStream::InputStream( Pathname file_r )
85 : _path(std::move( file_r ))
86 , _stream( streamForFile( _path.asString() ) )
87 , _name( _path.asString() )
88 , _size( _helperInitSize( _path ) )
89 {}
90
92 //
93 // METHOD NAME : InputStream::InputStream
94 // METHOD TYPE : Constructor
95 //
96 InputStream::InputStream( Pathname file_r,
97 std::string name_r )
98 : _path(std::move( file_r ))
99 , _stream( streamForFile( _path.asString() ) )
100 , _name(std::move( name_r ))
101 , _size( _helperInitSize( _path ) )
102 {}
103
105 //
106 // METHOD NAME : InputStream::InputStream
107 // METHOD TYPE : Constructor
108 //
109 InputStream::InputStream( const std::string & file_r )
110 : _path( file_r )
111 , _stream( streamForFile( _path.asString() ) )
112 , _name( _path.asString() )
113 , _size( _helperInitSize( _path ) )
114 {}
115
117 //
118 // METHOD NAME : InputStream::InputStream
119 // METHOD TYPE : Constructor
120 //
121 InputStream::InputStream( const std::string & file_r,
122 std::string name_r )
123 : _path( file_r )
124 , _stream( streamForFile( _path.asString() ) )
125 , _name(std::move( name_r ))
126 , _size( _helperInitSize( _path ) )
127 {}
128
130 //
131 // METHOD NAME : InputStream::InputStream
132 // METHOD TYPE : Constructor
133 //
134 InputStream::InputStream( const char * file_r )
135 : _path( file_r )
136 , _stream( streamForFile( _path.asString() ) )
137 , _name( _path.asString() )
138 , _size( _helperInitSize( _path ) )
139 {}
140
142 //
143 // METHOD NAME : InputStream::InputStream
144 // METHOD TYPE : Constructor
145 //
146 InputStream::InputStream( const char * file_r,
147 std::string name_r )
148 : _path( file_r )
149 , _stream( streamForFile( _path.asString() ) )
150 , _name(std::move( name_r ))
151 , _size( _helperInitSize( _path ) )
152 {}
153
155 //
156 // METHOD NAME : InputStream::~InputStream
157 // METHOD TYPE : Destructor
158 //
159 InputStream::~InputStream()
160 {}
161
162 /******************************************************************
163 **
164 ** FUNCTION NAME : operator<<
165 ** FUNCTION TYPE : std::ostream &
166 */
167 std::ostream & operator<<( std::ostream & str, const InputStream & obj )
168 {
169 return str << obj.name() << obj.stream();
170 }
171
173} // namespace zypp
LogTools.h
zypp::InputStream
Helper to create and pass std::istream.
Definition inputstream.h:57
zypp::InputStream::name
const std::string & name() const
Name of the std::istream.
Definition inputstream.h:107
zypp::InputStream::stream
std::istream & stream() const
The std::istream.
Definition inputstream.h:93
zypp::InputStream::_size
DefaultIntegral< std::streamoff,-1 > _size
Definition inputstream.h:134
zypp::InputStream::~InputStream
~InputStream()
Dtor.
Definition inputstream.cc:159
zypp::InputStream::_path
Pathname _path
Definition inputstream.h:131
zypp::InputStream::_stream
shared_ptr< std::istream > _stream
Definition inputstream.h:132
zypp::InputStream::_name
std::string _name
Definition inputstream.h:133
zypp::InputStream::InputStream
InputStream()
Default ctor providing std::cin.
Definition inputstream.cc:63
zypp::filesystem::PathInfo
Wrapper class for stat/lstat.
Definition PathInfo.h:226
zypp::filesystem::Pathname
Pathname.
Definition Pathname.h:48
inputstream.h
std
Definition ansi.h:855
str
String related utilities and Regular expression matching.
zypp::filesystem::zipType
ZIP_TYPE zipType(const Pathname &file)
Definition PathInfo.cc:1146
zypp::filesystem::ZT_NONE
@ ZT_NONE
Definition PathInfo.h:778
zypp::filesystem::ZT_ZCHNK
@ ZT_ZCHNK
Definition PathInfo.h:778
zypp
Easy-to use interface to the ZYPP dependency resolver.
Definition CodePitfalls.doc:2
zypp::ifzckstream
detail::fXstream< std::istream, detail::ZChunkStreamBuf > ifzckstream
istream reading zchunk files.
Definition zckstream.h:69
zypp::ifgzstream
detail::fXstream< std::istream, gzstream_detail::fgzstreambuf > ifgzstream
istream reading gzip files as well as plain files.
Definition gzstream.h:157
zypp::asString
std::string asString(const Patch::Category &obj)
relates: Patch::Category string representation.
Definition Patch.cc:122
zypp::operator<<
std::ostream & operator<<(std::ostream &str, const Capabilities &obj)
relates: Capabilities Stream output
Definition Capabilities.cc:65
zypp::NullDeleter
shared_ptr custom deleter doing nothing.
Definition PtrTypes.h:84
PathInfo.h