862a39470db7e0b6ccd7de0a0c2559c49b4fa7c4
[reactos.git] / reactos / tools / sysreg / pipe_reader.cpp
1 /* $Id$
2 *
3 * PROJECT: System regression tool for ReactOS
4 * LICENSE: GPL - See COPYING in the top level directory
5 * FILE: tools/sysreg/conf_parser.h
6 * PURPOSE: pipe reader support
7 * PROGRAMMERS: Johannes Anderwald (johannes.anderwald at sbox tugraz at)
8 */
9
10 #include "pipe_reader.h"
11
12 #include <iostream>
13 #include <assert.h>
14
15 namespace System_
16 {
17 using std::vector;
18 //---------------------------------------------------------------------------------------
19 PipeReader::PipeReader() : m_File(NULL)
20 {
21
22 }
23
24 //---------------------------------------------------------------------------------------
25 PipeReader::~PipeReader()
26 {
27
28 }
29
30 //---------------------------------------------------------------------------------------
31
32 bool PipeReader::openSource(string const & PipeCmd)
33 {
34 if (m_File != NULL)
35 {
36 cerr << "PipeReader::openPipe> pipe already open" << endl;
37 return false;
38 }
39
40 cerr << "cmd>" << PipeCmd << endl;
41
42 m_File = popen(PipeCmd.c_str(), "r"); //AccessMode.c_str());
43 if (m_File)
44 {
45 cerr << "PipeReader::openPipe> successfully opened pipe" << endl;
46 return true;
47 }
48
49 cerr << "PipeReader::openPipe> failed to open pipe " << PipeCmd << endl;
50 return false;
51 }
52
53 //---------------------------------------------------------------------------------------
54
55 bool PipeReader::closeSource()
56 {
57 if (!m_File)
58 {
59 cerr << "PipeReader::closePipe> pipe is not open" << endl;
60 return false;
61 }
62
63 int res = pclose(m_File);
64
65 if (res == INT_MAX)
66 {
67 cerr << "Error: _pclose failed " <<endl;
68 return false;
69 }
70
71 m_File = NULL;
72 return true;
73 }
74
75 //---------------------------------------------------------------------------------------
76
77 bool PipeReader::isSourceOpen()
78 {
79 return feof(m_File);
80 }
81
82 //---------------------------------------------------------------------------------------
83
84 bool PipeReader::readSource(vector<string> & lines)
85 {
86 char * buf = (char*)malloc(100 * sizeof(char));
87 //#ifdef NDEBUG
88 memset(buf, 0x0, sizeof(char) * 100);
89 //#endif
90
91 char * res = fgets(buf, 100, m_File);
92 if (!res)
93 {
94 //cerr << "Error: PipeReader::readPipe failed" << endl;
95 free(buf);
96 return false;
97 }
98 fflush(m_File);
99 string line(buf);
100 lines.push_back(line);
101 return true;
102 }
103
104 } // end of namespace System_