dlvhex  2.5.0
include/dlvhex2/ProcessBuf.h
Go to the documentation of this file.
00001 
00002 /* dlvhex -- Answer-Set Programming with external interfaces.
00003  * Copyright (C) 2005-2007 Roman Schindlauer
00004  * Copyright (C) 2006-2015 Thomas Krennwallner
00005  * Copyright (C) 2009-2016 Peter Schüller
00006  * Copyright (C) 2011-2016 Christoph Redl
00007  * Copyright (C) 2015-2016 Tobias Kaminski
00008  * Copyright (C) 2015-2016 Antonius Weinzierl
00009  *
00010  * This file is part of dlvhex.
00011  *
00012  * dlvhex is free software; you can redistribute it and/or modify it
00013  * under the terms of the GNU Lesser General Public License as
00014  * published by the Free Software Foundation; either version 2.1 of
00015  * the License, or (at your option) any later version.
00016  *
00017  * dlvhex is distributed in the hope that it will be useful, but
00018  * WITHOUT ANY WARRANTY; without even the implied warranty of
00019  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00020  * Lesser General Public License for more details.
00021  *
00022  * You should have received a copy of the GNU Lesser General Public
00023  * License along with dlvhex; if not, write to the Free Software
00024  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
00025  * 02110-1301 USA.
00026  */
00027 
00038 #if !defined(_DLVHEX_PROCESSBUF_H)
00039 #define _DLVHEX_PROCESSBUF_H
00040 
00041 #ifdef HAVE_CONFIG_H
00042 #include "config.h"
00043 #endif                           // HAVE_CONFIG_H
00044 
00045 #include "dlvhex2/PlatformDefinitions.h"
00046 
00047 #include <iostream>
00048 #include <streambuf>
00049 #include <vector>
00050 #include <string>
00051 
00052 #ifdef WIN32
00053 #include <windows.h>
00054 #undef ERROR
00055 #endif
00056 
00057 DLVHEX_NAMESPACE_BEGIN
00058 
00063 class DLVHEX_EXPORT ProcessBuf : public std::streambuf
00064 {
00065     public:
00067         ProcessBuf();
00068 
00071         ProcessBuf(const ProcessBuf& b);
00072 
00074         virtual
00075             ~ProcessBuf();
00076 
00078         virtual void
00079             endoffile();
00080 
00084         virtual pid_t
00085             open(const std::vector<std::string>& c);
00086 
00090         virtual int
00091             close(bool kill=false);
00092 
00093     private:
00094 
00095     #ifdef WIN32
00096 
00097         PROCESS_INFORMATION processInformation;
00099         HANDLE g_hChildStd_IN_Rd;
00101         HANDLE g_hChildStd_IN_Wr;
00103         HANDLE g_hChildStd_OUT_Rd;
00105         HANDLE g_hChildStd_OUT_Wr;
00106     #elif defined(POSIX)
00107         /* \brief Child process ID (POSIX). */
00108         pid_t process;
00110         int outpipes[2];
00112         int inpipes[2];
00113     #else
00114     #error Either POSIX or WIN32 must be defined
00115     #endif
00116 
00117         int status;
00119         unsigned bufsize;
00120 
00122         std::streambuf::char_type* obuf;
00124         std::streambuf::char_type* ibuf;
00125 
00127         void
00128             initBuffers();
00129 
00130     protected:
00134         virtual std::streambuf::int_type
00135             overflow(std::streambuf::int_type c);
00136 
00140         virtual std::streambuf::int_type
00141             underflow();
00142 
00146         virtual std::streambuf::int_type
00147             sync();
00148 };
00149 
00150 DLVHEX_NAMESPACE_END
00151 #endif                           // _DLVHEX_PROCESSBUF_H
00152 
00153 
00154 // vim:expandtab:ts=4:sw=4:
00155 // mode: C++
00156 // End: