Imported Upstream version 17.14.0
[platform/upstream/libzypp.git] / zypp / base / InputStream.h
1 /*---------------------------------------------------------------------\
2 |                          ____ _   __ __ ___                          |
3 |                         |__  / \ / / . \ . \                         |
4 |                           / / \ V /|  _/  _/                         |
5 |                          / /__ | | | | | |                           |
6 |                         /_____||_| |_| |_|                           |
7 |                                                                      |
8 \---------------------------------------------------------------------*/
9 /** \file       zypp/base/InputStream.h
10  *
11 */
12 #ifndef ZYPP_BASE_INPUTSTREAM_H
13 #define ZYPP_BASE_INPUTSTREAM_H
14
15 #include <iosfwd>
16
17 #include "zypp/base/PtrTypes.h"
18 #include "zypp/base/DefaultIntegral.h"
19 #include "zypp/Pathname.h"
20
21 ///////////////////////////////////////////////////////////////////
22 namespace zypp
23 { /////////////////////////////////////////////////////////////////
24
25   ///////////////////////////////////////////////////////////////////
26   //
27   //    CLASS NAME : InputStream
28   //
29   /** Helper to create and pass std::istream.
30    * The provided std::istream may either be std::cin,
31    * sone (gziped) file or an aleady existig \c std::istream.
32    *
33    * An optional \c name arument may be passed to the ctor,
34    * to identify the stream in log messages, even if it is
35    * not a file.
36    *
37    * Per default the name is "STDIN", the path to an input file
38    * or empty.
39    *
40    * \code
41    * void parse( const InputStream & input = InputStream() )
42    * {
43    *   // process input.stream() and refer to input.name()
44    *   // in log messages.
45    * }
46    *
47    * parse();                  // std::cin
48    * parse( "/some/file" );    // file
49    * parse( "/some/file.gz" ); // gziped file
50    * std::istream & mystream;
51    * parse( mystream );        // some existing stream
52    * parse( InputStream( mystream,
53    *                     "my stream's name" ) );
54    * \endcode
55   */
56   class InputStream
57   {
58   public:
59     /** Default ctor providing \c std::cin. */
60     InputStream();
61
62     /** Ctor providing an aleady existig \c std::istream. */
63     InputStream( std::istream & stream_r,
64                  const std::string & name_r = std::string() );
65
66     /** Ctor for reading a (gziped) file. */
67     InputStream( const Pathname & file_r );
68
69     /** Ctor for reading a (gziped) file. */
70     InputStream( const Pathname & file_r,
71                  const std::string & name_r );
72
73     /** Ctor for reading a (gziped) file. */
74     InputStream( const std::string & file_r );
75
76     /** Ctor for reading a (gziped) file. */
77     InputStream( const std::string & file_r,
78                  const std::string & name_r );
79
80     /** Ctor for reading a (gziped) file. */
81     InputStream( const char * file_r );
82
83     /** Ctor for reading a (gziped) file. */
84     InputStream( const char * file_r,
85                  const std::string & name_r );
86
87     /** Dtor. */
88     ~InputStream();
89
90     /** The std::istream.
91      * \note The provided std::istream is never \c const.
92     */
93     std::istream & stream() const
94     { return *_stream; }
95
96     /** Allow implicit conversion to std::istream.*/
97     operator std::istream &() const
98     { return *_stream; }
99
100     /** Name of the std::istream.
101      * Per default this is "STDIN", the path to an input file or
102      * empty. A custom string may be provided to the ctor.
103      *
104      * This may be used in log messages to identify the stream even
105      * even if it is not a file.
106     */
107     const std::string & name() const
108     { return _name; }
109
110     /** Path to the input file or empty if no file. */
111     const Pathname & path() const
112     { return _path; }
113
114     /** Size of the input stream (informal).
115      * If constructed from an uncompressed file, the file size.
116      * Otherwise \c -1. See \ref setSize;
117     */
118     std::streamoff size() const
119     { return _size; }
120
121     /** Set the size of the input stream.
122      * You may set it to whatever vaule is appropriate. E.g.
123      * <tt>*=10</tt> to compensate gzip comression. or the
124      * number of items, lines, ... The value is not used here,
125      * just provided.
126     */
127     void setSize( std::streamoff val_r )
128     { _size = val_r; }
129
130   private:
131     Pathname                 _path;
132     shared_ptr<std::istream> _stream;
133     std::string              _name;
134     DefaultIntegral<std::streamoff,-1> _size;
135   };
136   ///////////////////////////////////////////////////////////////////
137
138   /** \relates InputStream Stream output */
139   std::ostream & operator<<( std::ostream & str, const InputStream & obj );
140
141   /////////////////////////////////////////////////////////////////
142 } // namespace zypp
143 ///////////////////////////////////////////////////////////////////
144 #endif // ZYPP_BASE_INPUTSTREAM_H