2 * Copyright (c) 2013 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 * @file process_pipe.cpp
18 * @author Tomasz Iwanek (t.iwanek@samsung.com)
20 * @brief This file is the implementation pipe from process
23 #include <dpl/test/process_pipe.h>
27 ProcessPipe::ProcessPipe(PipeErrorPolicy err) : m_file(NULL), m_errPolicy(err)
31 ProcessPipe::~ProcessPipe()
35 void ProcessPipe::Open(const std::string & command)
39 ThrowMsg(Exception::DoubleOpen, "Trying to open pipe second time. Close it first");
42 std::string stdErrRedirection;
45 case PipeErrorPolicy::NONE: break;
46 case PipeErrorPolicy::OFF: stdErrRedirection = " 2>/dev/null"; break;
47 case PipeErrorPolicy::PIPE: stdErrRedirection = " 2>&1"; break;
51 std::string fcommand = command + stdErrRedirection;
52 FILE * file = popen(fcommand.c_str(), "r");
54 // Throw an exception if an error occurred
56 ThrowMsg(FileInput::Exception::OpenFailed, fcommand);
59 // Save new descriptor
61 m_fd = fileno(m_file);
64 void ProcessPipe::Close()
70 if (pclose(m_file) == -1) {
71 Throw(FileInput::Exception::CloseFailed);