safe_ifstream.hh

Go to the documentation of this file.
00001 /* vim: set sw=4 sts=4 et foldmethod=syntax : */
00002 
00003 /*
00004  * Copyright (c) 2009 Ciaran McCreesh
00005  *
00006  * This file is part of the Paludis package manager. Paludis is free software;
00007  * you can redistribute it and/or modify it under the terms of the GNU General
00008  * Public License version 2, as published by the Free Software Foundation.
00009  *
00010  * Paludis is distributed in the hope that it will be useful, but WITHOUT ANY
00011  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00012  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
00013  * details.
00014  *
00015  * You should have received a copy of the GNU General Public License along with
00016  * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
00017  * Place, Suite 330, Boston, MA  02111-1307  USA
00018  */
00019 
00020 #ifndef PALUDIS_GUARD_PALUDIS_UTIL_SAFE_IFSTREAM_HH
00021 #define PALUDIS_GUARD_PALUDIS_UTIL_SAFE_IFSTREAM_HH 1
00022 
00023 #include <paludis/util/attributes.hh>
00024 #include <paludis/util/exception.hh>
00025 #include <paludis/util/fs_entry-fwd.hh>
00026 #include <istream>
00027 
00028 /** \file
00029  * Declarations for SafeIFStream.
00030  *
00031  * \ingroup g_fs
00032  *
00033  * \section Examples
00034  *
00035  * - None at this time.
00036  */
00037 
00038 namespace paludis
00039 {
00040     /**
00041      * Input stream buffer class that can be opened via an FD, and that doesn't
00042      * do retarded things when given a non-file.
00043      *
00044      * See \ref TCppSL Ch. 13.13 for what we're doing here. The buffer code is
00045      * based upon the "io/inbuf1.hpp" example in section 13.13.3.
00046      *
00047      * \ingroup g_fs
00048      * \since 0.34.3
00049      */
00050     class PALUDIS_VISIBLE SafeIFStreamBuf :
00051         public std::streambuf
00052     {
00053         protected:
00054             static const int lookbehind_size = 16;
00055             static const int buffer_size = 512 + lookbehind_size;
00056             char buffer[buffer_size];
00057 
00058             virtual int_type underflow();
00059             virtual pos_type seekoff(off_type, std::ios_base::seekdir, std::ios_base::openmode);
00060             virtual pos_type seekpos(pos_type, std::ios_base::openmode);
00061 
00062         public:
00063             ///\name Basic operations
00064             ///\{
00065 
00066             SafeIFStreamBuf(const int f);
00067 
00068             ///\}
00069 
00070             /// Our file descriptor.
00071             int fd;
00072     };
00073 
00074     /**
00075      * Member from base initialisation for SafeIFStream.
00076      *
00077      * \ingroup g_fs
00078      * \since 0.34.3
00079      */
00080     class PALUDIS_VISIBLE SafeIFStreamBase
00081     {
00082         protected:
00083             /// Our buffer.
00084             SafeIFStreamBuf buf;
00085 
00086         public:
00087             ///\name Basic operations
00088             ///\{
00089 
00090             SafeIFStreamBase(const int fd);
00091 
00092             ///\}
00093     };
00094 
00095     /**
00096      * Input stream buffer class that can be opened via an FD, and that doesn't
00097      * do retarded things when given a non-file.
00098      *
00099      * \ingroup g_fs
00100      * \since 0.34.3
00101      */
00102     class PALUDIS_VISIBLE SafeIFStream :
00103         protected SafeIFStreamBase,
00104         public std::istream
00105     {
00106         private:
00107             const bool _close;
00108 
00109         public:
00110             ///\name Basic operations
00111             ///\{
00112 
00113             SafeIFStream(const int fd);
00114             SafeIFStream(const FSEntry &);
00115             ~SafeIFStream();
00116 
00117             ///\}
00118     };
00119 
00120     /**
00121      * Thrown by SafeIFStream if an error occurs.
00122      *
00123      * \ingroup g_fs
00124      * \since 0.34.3
00125      */
00126     class PALUDIS_VISIBLE SafeIFStreamError :
00127         public Exception
00128     {
00129         public:
00130             SafeIFStreamError(const std::string &) throw ();
00131     };
00132 }
00133 
00134 #endif

Generated on Mon Sep 21 10:36:08 2009 for paludis by  doxygen 1.5.4