aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAvatar Ciaran McCreesh <ciaran.mccreesh@googlemail.com> 2006-01-22 08:21:48 +0000
committerAvatar Ciaran McCreesh <ciaran.mccreesh@googlemail.com> 2006-01-22 08:21:48 +0000
commitf14f6c8017dbcb53c8596623ae3581e3aa058b30 (patch)
tree5137fc16f8cc2b4f8a50fdb3002de17fbd6dea38
parent17305ce435a37f6dce7a108a190ae362aff0411e (diff)
downloadpaludis-f14f6c8017dbcb53c8596623ae3581e3aa058b30.tar.gz
paludis-f14f6c8017dbcb53c8596623ae3581e3aa058b30.tar.xz
Use base from member to avoid initialising pstream's std::istream with an uninitialised variable. Move pstream buffer and error classes into the main pstream files.
-rw-r--r--paludis/files.m42
-rw-r--r--paludis/pstream.cc60
-rw-r--r--paludis/pstream.hh112
-rw-r--r--paludis/pstream_TEST.cc1
-rw-r--r--paludis/pstream_error.cc29
-rw-r--r--paludis/pstream_error.hh48
-rw-r--r--paludis/pstream_in_buf.cc81
-rw-r--r--paludis/pstream_in_buf.hh97
8 files changed, 162 insertions, 268 deletions
diff --git a/paludis/files.m4 b/paludis/files.m4
index aece6c1..19f1bc6 100644
--- a/paludis/files.m4
+++ b/paludis/files.m4
@@ -77,8 +77,6 @@ add(`paludis', `hh', `cc')
add(`portage_repository', `hh', `cc')
add(`private_implementation_pattern', `hh', `cc')
add(`pstream', `hh', `cc', `test')
-add(`pstream_error', `hh', `cc')
-add(`pstream_in_buf', `hh', `cc')
add(`qualified_package_name', `hh', `cc', `test')
add(`qualified_package_name_collection', `hh', `cc')
add(`qualified_package_name_error', `hh', `cc')
diff --git a/paludis/pstream.cc b/paludis/pstream.cc
index b153d58..4c631c1 100644
--- a/paludis/pstream.cc
+++ b/paludis/pstream.cc
@@ -19,4 +19,64 @@
*/
#include "pstream.hh"
+#include <errno.h>
+using namespace paludis;
+
+PStreamError::PStreamError(const std::string & message) throw () :
+ Exception(message)
+{
+}
+
+PStreamInBuf::int_type
+PStreamInBuf::underflow()
+{
+ if (0 == fd)
+ return EOF;
+
+ if (gptr() < egptr())
+ return *gptr();
+
+ int num_putback = gptr() - eback();
+ if (num_putback > putback_size)
+ num_putback = putback_size;
+ std::memmove(buffer + putback_size - num_putback,
+ gptr() - num_putback, num_putback);
+
+ size_t n = fread(buffer + putback_size, 1, buffer_size - putback_size, fd);
+ if (n <= 0)
+ return EOF;
+
+ setg(buffer + putback_size - num_putback, buffer + putback_size,
+ buffer + putback_size + n);
+
+ return *gptr();
+}
+
+PStreamInBuf::PStreamInBuf(const std::string & command) :
+ _command(command),
+ fd(popen(command.c_str(), "r"))
+{
+ if (0 == fd)
+ throw PStreamError("popen('" + _command + "', 'r') failed: " +
+ strerror(errno));
+
+ setg(buffer + putback_size, buffer + putback_size, buffer + putback_size);
+}
+
+PStreamInBuf::~PStreamInBuf()
+{
+ if (0 != fd)
+ pclose(fd);
+}
+
+int
+PStreamInBuf::exit_status()
+{
+ if (0 != fd)
+ {
+ _exit_status = pclose(fd);
+ fd = 0;
+ }
+ return _exit_status;
+}
diff --git a/paludis/pstream.hh b/paludis/pstream.hh
index ea9c195..d83f7ae 100644
--- a/paludis/pstream.hh
+++ b/paludis/pstream.hh
@@ -21,34 +21,126 @@
#ifndef PALUDIS_GUARD_PALUDIS_PSTREAM_HH
#define PALUDIS_GUARD_PALUDIS_PSTREAM_HH 1
-#include <paludis/pstream_in_buf.hh>
#include <paludis/instantiation_policy.hh>
+#include <paludis/exception.hh>
+#include <streambuf>
+#include <limits>
+#include <string>
+#include <cstdio>
#include <istream>
/** \file
- * Declarations for the PStream class.
+ * Declarations for the PStream and PStreamInBuf classes, and related
+ * utilities.
*
* \ingroup PStream
+ * \ingroup Exception
*/
namespace paludis
{
/**
- * A PStream class is a standard input stream class whose contents comes
- * from the output of an executed command.
+ * Thrown if a PStream or PStreamInBuf encounters an error.
*
* \ingroup PStream
+ * \ingroup Exception
*/
- class PStream :
- public std::istream,
- private InstantiationPolicy<PStream, instantiation_method::NonCopyableTag>
+ class PStreamError : public Exception
+ {
+ public:
+ PStreamError(const std::string & message) throw ();
+ };
+
+ /**
+ * Input buffer class for a process, invoked using popen(3).
+ *
+ * Bidirectional I/O isn't supported since we haven't needed it yet, and
+ * because popen on Linux is unidirectional.
+ *
+ * See Josuttis' "The C++ Standard Library" Ch. 13.13 for what we're doing
+ * here. The buffer code is based upon the "io/inbuf1.hpp" example in
+ * section 13.13.3.
+ *
+ * \ingroup PStream
+ */
+ class PStreamInBuf :
+ public std::streambuf,
+ private InstantiationPolicy<PStreamInBuf, instantiation_method::NonCopyableTag>
{
+ private:
+ const std::string _command;
+
+ int _exit_status;
+
protected:
+ FILE * fd;
+
+ static const int putback_size = std::numeric_limits<unsigned>::digits >> 3;
+
+ static const int buffer_size = 3 * putback_size;
+
+ char buffer[buffer_size];
+
+ virtual int_type underflow();
+
+ public:
/**
- * Our buffer.
+ * Constructor.
+ *
+ * \param command The command to run. See PStream for discussion.
+ */
+ PStreamInBuf(const std::string & command);
+
+ /**
+ * Destructor.
*/
+ ~PStreamInBuf();
+
+ /**
+ * What was our command?
+ */
+ const std::string & command() const
+ {
+ return _command;
+ }
+
+ /**
+ * What is our exit status?
+ */
+ int exit_status();
+ };
+
+ /**
+ * For internal use by PStream classes.
+ */
+ namespace pstream_internals
+ {
+ /**
+ * Avoid base from member issues for PStream.
+ */
+ struct PStreamInBufBase :
+ private paludis::InstantiationPolicy<PStreamInBufBase, instantiation_method::NonCopyableTag>
+ {
PStreamInBuf buf;
+ PStreamInBufBase(const std::string & command) :
+ buf(command)
+ {
+ }
+ };
+ }
+
+ /**
+ * A PStream class is a standard input stream class whose contents comes
+ * from the output of an executed command.
+ *
+ * \ingroup PStream
+ */
+ class PStream :
+ private InstantiationPolicy<PStream, instantiation_method::NonCopyableTag>,
+ protected pstream_internals::PStreamInBufBase,
+ public std::istream
+ {
public:
/**
* Constructor.
@@ -58,8 +150,8 @@ namespace paludis
* as part of the command.
*/
PStream(const std::string & command) :
- std::istream(&buf),
- buf(command)
+ PStreamInBufBase(command),
+ std::istream(&buf)
{
}
diff --git a/paludis/pstream_TEST.cc b/paludis/pstream_TEST.cc
index bb4a873..13cef2d 100644
--- a/paludis/pstream_TEST.cc
+++ b/paludis/pstream_TEST.cc
@@ -19,7 +19,6 @@
*/
#include "pstream.hh"
-#include "pstream_error.hh"
#include <test/test_framework.hh>
#include <test/test_runner.hh>
diff --git a/paludis/pstream_error.cc b/paludis/pstream_error.cc
deleted file mode 100644
index 9563cfd..0000000
--- a/paludis/pstream_error.cc
+++ /dev/null
@@ -1,29 +0,0 @@
-/* vim: set sw=4 sts=4 et foldmethod=syntax : */
-
-/*
- * Copyright (c) 2006 Ciaran McCreesh <ciaranm@gentoo.org>
- *
- * This file is part of the Paludis package manager. Paludis is free software;
- * you can redistribute it and/or modify it under the terms of the GNU General
- * Public License as published by the Free Software Foundation; either version
- * 2 of the License, or (at your option) any later version.
- *
- * Paludis is distributed in the hope that it will be useful, but WITHOUT ANY
- * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
- * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
- * details.
- *
- * You should have received a copy of the GNU General Public License along with
- * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
- * Place, Suite 330, Boston, MA 02111-1307 USA
- */
-
-#include "pstream_error.hh"
-
-using namespace paludis;
-
-PStreamError::PStreamError(const std::string & message) throw () :
- Exception(message)
-{
-}
-
diff --git a/paludis/pstream_error.hh b/paludis/pstream_error.hh
deleted file mode 100644
index f107059..0000000
--- a/paludis/pstream_error.hh
+++ /dev/null
@@ -1,48 +0,0 @@
-/* vim: set sw=4 sts=4 et foldmethod=syntax : */
-
-/*
- * Copyright (c) 2006 Ciaran McCreesh <ciaranm@gentoo.org>
- *
- * This file is part of the Paludis package manager. Paludis is free software;
- * you can redistribute it and/or modify it under the terms of the GNU General
- * Public License as published by the Free Software Foundation; either version
- * 2 of the License, or (at your option) any later version.
- *
- * Paludis is distributed in the hope that it will be useful, but WITHOUT ANY
- * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
- * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
- * details.
- *
- * You should have received a copy of the GNU General Public License along with
- * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
- * Place, Suite 330, Boston, MA 02111-1307 USA
- */
-
-#ifndef PALUDIS_GUARD_PALUDIS_PSTREAM_ERROR_HH
-#define PALUDIS_GUARD_PALUDIS_PSTREAM_ERROR_HH 1
-
-#include <paludis/exception.hh>
-
-/** \file
- * Declarations for the PStreamError class.
- *
- * \ingroup PStream
- * \ingroup Exception
- */
-
-namespace paludis
-{
- /**
- * Thrown if a PStream or PStreamInBuf encounters an error.
- *
- * \ingroup PStream
- * \ingroup Exception
- */
- class PStreamError : public Exception
- {
- public:
- PStreamError(const std::string & message) throw ();
- };
-}
-
-#endif
diff --git a/paludis/pstream_in_buf.cc b/paludis/pstream_in_buf.cc
deleted file mode 100644
index 6556bc3..0000000
--- a/paludis/pstream_in_buf.cc
+++ /dev/null
@@ -1,81 +0,0 @@
-/* vim: set sw=4 sts=4 et foldmethod=syntax : */
-
-/*
- * Copyright (c) 2006 Ciaran McCreesh <ciaranm@gentoo.org>
- *
- * This file is part of the Paludis package manager. Paludis is free software;
- * you can redistribute it and/or modify it under the terms of the GNU General
- * Public License as published by the Free Software Foundation; either version
- * 2 of the License, or (at your option) any later version.
- *
- * Paludis is distributed in the hope that it will be useful, but WITHOUT ANY
- * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
- * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
- * details.
- *
- * You should have received a copy of the GNU General Public License along with
- * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
- * Place, Suite 330, Boston, MA 02111-1307 USA
- */
-
-#include "pstream_in_buf.hh"
-#include "pstream_error.hh"
-#include "stringify.hh"
-#include <cstring>
-#include <errno.h>
-#include <exception>
-
-using namespace paludis;
-
-PStreamInBuf::int_type
-PStreamInBuf::underflow()
-{
- if (0 == fd)
- return EOF;
-
- if (gptr() < egptr())
- return *gptr();
-
- int num_putback = gptr() - eback();
- if (num_putback > putback_size)
- num_putback = putback_size;
- std::memmove(buffer + putback_size - num_putback,
- gptr() - num_putback, num_putback);
-
- size_t n = fread(buffer + putback_size, 1, buffer_size - putback_size, fd);
- if (n <= 0)
- return EOF;
-
- setg(buffer + putback_size - num_putback, buffer + putback_size,
- buffer + putback_size + n);
-
- return *gptr();
-}
-
-PStreamInBuf::PStreamInBuf(const std::string & command) :
- _command(command),
- fd(popen(command.c_str(), "r"))
-{
- if (0 == fd)
- throw PStreamError("popen('" + _command + "', 'r') failed: " +
- strerror(errno));
-
- setg(buffer + putback_size, buffer + putback_size, buffer + putback_size);
-}
-
-PStreamInBuf::~PStreamInBuf()
-{
- if (0 != fd)
- pclose(fd);
-}
-
-int
-PStreamInBuf::exit_status()
-{
- if (0 != fd)
- {
- _exit_status = pclose(fd);
- fd = 0;
- }
- return _exit_status;
-}
diff --git a/paludis/pstream_in_buf.hh b/paludis/pstream_in_buf.hh
deleted file mode 100644
index 2f0a923..0000000
--- a/paludis/pstream_in_buf.hh
+++ /dev/null
@@ -1,97 +0,0 @@
-/* vim: set sw=4 sts=4 et foldmethod=syntax : */
-
-/*
- * Copyright (c) 2006 Ciaran McCreesh <ciaranm@gentoo.org>
- *
- * This file is part of the Paludis package manager. Paludis is free software;
- * you can redistribute it and/or modify it under the terms of the GNU General
- * Public License as published by the Free Software Foundation; either version
- * 2 of the License, or (at your option) any later version.
- *
- * Paludis is distributed in the hope that it will be useful, but WITHOUT ANY
- * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
- * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
- * details.
- *
- * You should have received a copy of the GNU General Public License along with
- * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
- * Place, Suite 330, Boston, MA 02111-1307 USA
- */
-
-#ifndef PALUDIS_GUARD_PALUDIS_PSTREAM_IN_BUF_HH
-#define PALUDIS_GUARD_PALUDIS_PSTREAM_IN_BUF_HH 1
-
-#include <paludis/instantiation_policy.hh>
-#include <streambuf>
-#include <limits>
-#include <string>
-#include <cstdio>
-
-/** \file
- * Declarations for the PStreamInBuf class.
- *
- * \ingroup PStream
- */
-
-namespace paludis
-{
- /**
- * Input buffer class for a process, invoked using popen(3).
- *
- * Bidirectional I/O isn't supported since we haven't needed it yet, and
- * because popen on Linux is unidirectional.
- *
- * See Josuttis' "The C++ Standard Library" Ch. 13.13 for what we're doing
- * here. The buffer code is based upon the "io/inbuf1.hpp" example in
- * section 13.13.3.
- */
- class PStreamInBuf :
- public std::streambuf,
- private InstantiationPolicy<PStreamInBuf, instantiation_method::NonCopyableTag>
- {
- private:
- const std::string _command;
-
- int _exit_status;
-
- protected:
- FILE * fd;
-
- static const int putback_size = std::numeric_limits<unsigned>::digits >> 3;
-
- static const int buffer_size = 3 * putback_size;
-
- char buffer[buffer_size];
-
- virtual int_type underflow();
-
- public:
- /**
- * Constructor.
- *
- * \param command The command to run. See PStream for discussion.
- */
- PStreamInBuf(const std::string & command);
-
- /**
- * Destructor.
- */
- ~PStreamInBuf();
-
- /**
- * What was our command?
- */
- const std::string & command() const
- {
- return _command;
- }
-
- /**
- * What is our exit status?
- */
- int exit_status();
- };
-
-}
-
-#endif