Imported Upstream version 17.14.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 #include "zypp/PathInfo.h"
19
20 using std::endl;
21
22 ///////////////////////////////////////////////////////////////////
23 namespace zypp
24 { /////////////////////////////////////////////////////////////////
25
26   ///////////////////////////////////////////////////////////////////
27   namespace
28   { /////////////////////////////////////////////////////////////////
29
30     inline std::streamoff _heplerInitSize( const Pathname & file_r )
31     {
32       PathInfo p( file_r );
33       if ( p.isFile() && filesystem::zipType( file_r ) == filesystem::ZT_NONE )
34         return p.size();
35       return -1;
36     }
37
38     /////////////////////////////////////////////////////////////////
39   } // namespace
40   ///////////////////////////////////////////////////////////////////
41
42   ///////////////////////////////////////////////////////////////////
43   //
44   //    METHOD NAME : InputStream::InputStream
45   //    METHOD TYPE : Constructor
46   //
47   InputStream::InputStream()
48   : _stream( &std::cin, NullDeleter() )
49   , _name( "STDIN" )
50   {}
51
52   ///////////////////////////////////////////////////////////////////
53   //
54   //    METHOD NAME : InputStream::InputStream
55   //    METHOD TYPE : Constructor
56   //
57   InputStream::InputStream( std::istream & stream_r,
58                             const std::string & name_r )
59   : _stream( &stream_r, NullDeleter() )
60   , _name( name_r )
61   {}
62
63   ///////////////////////////////////////////////////////////////////
64   //
65   //    METHOD NAME : InputStream::InputStream
66   //    METHOD TYPE : Constructor
67   //
68   InputStream::InputStream( const Pathname & file_r )
69   : _path( file_r )
70   , _stream( new ifgzstream( _path.asString().c_str() ) )
71   , _name( _path.asString() )
72   , _size( _heplerInitSize( _path ) )
73   {}
74
75   ///////////////////////////////////////////////////////////////////
76   //
77   //    METHOD NAME : InputStream::InputStream
78   //    METHOD TYPE : Constructor
79   //
80   InputStream::InputStream( const Pathname & file_r,
81                             const std::string & name_r )
82   : _path( file_r )
83   , _stream( new ifgzstream( _path.asString().c_str() ) )
84   , _name( name_r )
85   , _size( _heplerInitSize( _path ) )
86   {}
87
88   ///////////////////////////////////////////////////////////////////
89   //
90   //    METHOD NAME : InputStream::InputStream
91   //    METHOD TYPE : Constructor
92   //
93   InputStream::InputStream( const std::string & file_r )
94   : _path( file_r )
95   , _stream( new ifgzstream( _path.asString().c_str() ) )
96   , _name( _path.asString() )
97   , _size( _heplerInitSize( _path ) )
98   {}
99
100   ///////////////////////////////////////////////////////////////////
101   //
102   //    METHOD NAME : InputStream::InputStream
103   //    METHOD TYPE : Constructor
104   //
105   InputStream::InputStream( const std::string & file_r,
106                             const std::string & name_r )
107   : _path( file_r )
108   , _stream( new ifgzstream( _path.asString().c_str() ) )
109   , _name( name_r )
110   , _size( _heplerInitSize( _path ) )
111   {}
112
113   ///////////////////////////////////////////////////////////////////
114   //
115   //    METHOD NAME : InputStream::InputStream
116   //    METHOD TYPE : Constructor
117   //
118   InputStream::InputStream( const char * file_r )
119   : _path( file_r )
120   , _stream( new ifgzstream( _path.asString().c_str() ) )
121   , _name( _path.asString() )
122   , _size( _heplerInitSize( _path ) )
123   {}
124
125   ///////////////////////////////////////////////////////////////////
126   //
127   //    METHOD NAME : InputStream::InputStream
128   //    METHOD TYPE : Constructor
129   //
130   InputStream::InputStream( const char * file_r,
131                             const std::string & name_r )
132   : _path( file_r )
133   , _stream( new ifgzstream( _path.asString().c_str() ) )
134   , _name( name_r )
135   , _size( _heplerInitSize( _path ) )
136   {}
137
138   ///////////////////////////////////////////////////////////////////
139   //
140   //    METHOD NAME : InputStream::~InputStream
141   //    METHOD TYPE : Destructor
142   //
143   InputStream::~InputStream()
144   {}
145
146   /******************************************************************
147    **
148    **   FUNCTION NAME : operator<<
149    **   FUNCTION TYPE : std::ostream &
150   */
151   std::ostream & operator<<( std::ostream & str, const InputStream & obj )
152   {
153     return str << obj.name() << obj.stream();
154   }
155
156   /////////////////////////////////////////////////////////////////
157 } // namespace zypp
158 ///////////////////////////////////////////////////////////////////