00001 /* vim: set sw=4 sts=4 et foldmethod=syntax : */ 00002 00003 /* 00004 * Copyright (c) 2006, 2007, 2008, 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_OFSTREAM_HH 00021 #define PALUDIS_GUARD_PALUDIS_UTIL_SAFE_OFSTREAM_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 <ostream> 00027 00028 /** \file 00029 * Declarations for SafeOFStream. 00030 * 00031 * \ingroup g_fs 00032 * 00033 * \section Examples 00034 * 00035 * - None at this time. 00036 */ 00037 00038 namespace paludis 00039 { 00040 /** 00041 * Output 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/outbuf2.hpp" example in section 13.13.3. 00046 * 00047 * \ingroup g_fs 00048 * \since 0.34.3 00049 */ 00050 class PALUDIS_VISIBLE SafeOFStreamBuf : 00051 public std::streambuf 00052 { 00053 protected: 00054 virtual int_type 00055 overflow(int_type c); 00056 00057 virtual std::streamsize 00058 xsputn(const char * s, std::streamsize num); 00059 00060 public: 00061 ///\name Basic operations 00062 ///\{ 00063 00064 SafeOFStreamBuf(const int f); 00065 00066 ///\} 00067 00068 /// Our file descriptor. 00069 int fd; 00070 }; 00071 00072 /** 00073 * Member from base initialisation for SafeOFStream. 00074 * 00075 * \ingroup g_fs 00076 * \since 0.34.3 00077 */ 00078 class PALUDIS_VISIBLE SafeOFStreamBase 00079 { 00080 protected: 00081 /// Our buffer. 00082 SafeOFStreamBuf buf; 00083 00084 public: 00085 ///\name Basic operations 00086 ///\{ 00087 00088 SafeOFStreamBase(const int fd); 00089 00090 ///\} 00091 }; 00092 00093 /** 00094 * Output stream buffer class that can be opened via an FD, and that doesn't 00095 * do retarded things when given a non-file. 00096 * 00097 * \ingroup g_fs 00098 * \since 0.34.3 00099 */ 00100 class PALUDIS_VISIBLE SafeOFStream : 00101 protected SafeOFStreamBase, 00102 public std::ostream 00103 { 00104 private: 00105 const bool _close; 00106 00107 public: 00108 ///\name Basic operations 00109 ///\{ 00110 00111 SafeOFStream(const int fd); 00112 SafeOFStream(const FSEntry &, const int open_flags = -1); 00113 ~SafeOFStream(); 00114 00115 ///\} 00116 }; 00117 00118 /** 00119 * Thrown by SafeOFStream if an error occurs. 00120 * 00121 * \ingroup g_fs 00122 * \since 0.34.3 00123 */ 00124 class PALUDIS_VISIBLE SafeOFStreamError : 00125 public Exception 00126 { 00127 public: 00128 SafeOFStreamError(const std::string &) throw (); 00129 }; 00130 } 00131 00132 #endif