paludis  Version 2.6.0
safe_ifstream.hh
Go to the documentation of this file.
1 /* vim: set sw=4 sts=4 et foldmethod=syntax : */
2 
3 /*
4  * Copyright (c) 2009, 2010 Ciaran McCreesh
5  *
6  * This file is part of the Paludis package manager. Paludis is free software;
7  * you can redistribute it and/or modify it under the terms of the GNU General
8  * Public License version 2, as published by the Free Software Foundation.
9  *
10  * Paludis is distributed in the hope that it will be useful, but WITHOUT ANY
11  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
13  * details.
14  *
15  * You should have received a copy of the GNU General Public License along with
16  * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
17  * Place, Suite 330, Boston, MA 02111-1307 USA
18  */
19 
20 #ifndef PALUDIS_GUARD_PALUDIS_UTIL_SAFE_IFSTREAM_HH
21 #define PALUDIS_GUARD_PALUDIS_UTIL_SAFE_IFSTREAM_HH 1
22 
25 #include <paludis/util/fs_path-fwd.hh>
26 #include <istream>
27 
28 /** \file
29  * Declarations for SafeIFStream.
30  *
31  * \ingroup g_fs
32  *
33  * \section Examples
34  *
35  * - None at this time.
36  */
37 
38 namespace paludis
39 {
40  /**
41  * Input stream buffer class that can be opened via an FD, and that doesn't
42  * do retarded things when given a non-file.
43  *
44  * See \ref TCppSL Ch. 13.13 for what we're doing here. The buffer code is
45  * based upon the "io/inbuf1.hpp" example in section 13.13.3.
46  *
47  * \ingroup g_fs
48  * \since 0.34.3
49  */
51  public std::streambuf
52  {
53  protected:
54  static const int lookbehind_size = 16;
55  static const int buffer_size = 512 + lookbehind_size;
56  char buffer[buffer_size];
57 
58  virtual int_type underflow();
59  virtual pos_type seekoff(off_type, std::ios_base::seekdir, std::ios_base::openmode);
60  virtual pos_type seekpos(pos_type, std::ios_base::openmode);
61 
62  public:
63  ///\name Basic operations
64  ///\{
65 
66  SafeIFStreamBuf(const int f);
67 
68  ///\}
69 
70  /// Our file descriptor.
71  int fd;
72  };
73 
74  /**
75  * Member from base initialisation for SafeIFStream.
76  *
77  * \ingroup g_fs
78  * \since 0.34.3
79  */
81  {
82  protected:
83  /// Our buffer.
85 
86  public:
87  ///\name Basic operations
88  ///\{
89 
90  SafeIFStreamBase(const int fd);
91 
92  ///\}
93  };
94 
95  /**
96  * Input stream buffer class that can be opened via an FD, and that doesn't
97  * do retarded things when given a non-file.
98  *
99  * \ingroup g_fs
100  * \since 0.34.3
101  */
103  protected SafeIFStreamBase,
104  public std::istream
105  {
106  private:
107  const bool _close;
108 
109  public:
110  ///\name Basic operations
111  ///\{
112 
113  explicit SafeIFStream(const int fd);
114  explicit SafeIFStream(const FSPath &);
115  ~SafeIFStream();
116 
117  ///\}
118  };
119 
120  /**
121  * Thrown by SafeIFStream if an error occurs.
122  *
123  * \ingroup g_fs
124  * \since 0.34.3
125  */
127  public Exception
128  {
129  public:
130  SafeIFStreamError(const std::string &) noexcept;
131  };
132 }
133 
134 #endif
Definition: about_metadata-fwd.hh:23
int fd
Our file descriptor.
Definition: safe_ifstream.hh:71
Definition: safe_ifstream.hh:102
SafeIFStreamBuf buf
Our buffer.
Definition: safe_ifstream.hh:84
Definition: safe_ifstream.hh:80
Definition: safe_ifstream.hh:50
Definition: exception.hh:74
Definition: safe_ifstream.hh:126
Definition: fs_path.hh:33
#define PALUDIS_VISIBLE
Definition: attributes.hh:59