//------------------------------------------------------------------------------
// <copyright file="LqrSMACPOpen2.h" company="LiquidRobotics">
//
// Copyright (c) 2014 Liquid Robotics, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
// This software is associated with a release.  Liquid Robotics makes no
// representation that the design, interfaces or component distribution will
// remain unchanged in future releases.
//
// </copyright>
//
// <summary>
//   Declaration of POpen2 and the related POpen2ProcessInfo structure, which
//   opens a process and provides access to the input and output pipes and the
//   process ID.
// </summary>
//
// <owner>Mike Cookson</owner>
//------------------------------------------------------------------------------

#ifndef __LQRSMACPOPEN2_H__
#define __LQRSMACPOPEN2_H__

#include "LqrSMACSharedLib.h"
#include <string>
#include <unistd.h>

namespace LiquidRobotics {
namespace SMACFramework {

struct SHARED_LIB_PUBLIC POpen2ProcessInfo
{
	POpen2ProcessInfo()
	: success(false)
	, infd(-1)
	, outfd(-1)
	, errfd(-1)
	, pid(-1)
	{
	}

	operator bool()
	{
		return success;
	}

	void Close()
	{
		if (infd != -1) 	close(infd);
		if (outfd != -1) 	close(outfd);
		if (errfd != -1) 	close(errfd);
	}

	void Reset(bool closeFirst = true)
	{
		if (closeFirst) Close();
		*this = POpen2ProcessInfo();
	}

	bool success;
	int infd;
	int outfd;
	int errfd;
	pid_t pid;
};

SHARED_LIB_PUBLIC POpen2ProcessInfo POpen2(const std::string& command, bool outputEqualsError = false);

}
}

#endif
