052b4c383a8983822bc8b2589ddad177d73a0bdd
1 /* $Id: pipe_reader.cpp 24643 2006-10-24 11:45:21Z janderwald $
3 * PROJECT: System regression tool for ReactOS
4 * LICENSE: GPL - See COPYING in the top level directory
5 * FILE: tools/sysreg/namedpipe_reader.cpp
6 * PURPOSE: pipe reader support
7 * PROGRAMMERS: Johannes Anderwald (johannes.anderwald at sbox tugraz at)
8 * Christoph von Wittich (Christoph_vW@ReactOS.org)
11 #include "namedpipe_reader.h"
19 #define MIN(a, b) (((a) < (b)) ? (a) : (b))
22 const char * NamedPipeReader::s_LineBreak
= "\x0A\0";
24 const char * NamedPipeReader::s_LineBreak
= "\x0D\x0A\0";
27 //---------------------------------------------------------------------------------------
28 NamedPipeReader::NamedPipeReader() : DataSource(), h_Pipe(NULLVAL
), m_Buffer(0)
32 //---------------------------------------------------------------------------------------
33 NamedPipeReader::~NamedPipeReader()
39 bool NamedPipeReader::isSourceOpen()
44 //---------------------------------------------------------------------------------------
46 bool NamedPipeReader::openSource(const string
& PipeCmd
)
48 if (h_Pipe
!= NULLVAL
)
50 cerr
<< "NamedPipeReader::openPipe> pipe already open" << endl
;
54 h_Pipe
= CreateFile(PipeCmd
.c_str(),
55 GENERIC_WRITE
| GENERIC_READ
,
59 FILE_ATTRIBUTE_NORMAL
,
63 if(INVALID_HANDLE_VALUE
== h_Pipe
) {
64 cerr
<< "NamedPipeReader::openPipe> failed to open pipe " << PipeCmd
<< " Error:" << GetLastError() << endl
;
70 cout
<< "NamedPipeReader::openPipe> successfully opened pipe" << endl
;
75 m_Buffer
= (char*)malloc(sizeof(char) * m_BufferLength
);
77 ConnectNamedPipe(h_Pipe
,
82 h_Pipe
= open(PipeCmd
.c_str(), O_RDONLY
);
84 if(INVALID_HANDLE_VALUE
== h_Pipe
) {
85 cerr
<< "NamedPipeReader::openPipe> failed to open pipe " << PipeCmd
<< endl
;
91 cout
<< "NamedPipeReader::openPipe> successfully opened pipe handle: "<< h_Pipe
<< endl
<< "Src: " << PipeCmd
<< endl
;
95 m_Buffer
= (char*)malloc(sizeof(char) * m_BufferLength
);
102 //---------------------------------------------------------------------------------------
104 bool NamedPipeReader::closeSource()
106 cerr
<< "NamedPipeReader::closePipe> entered" << endl
;
107 if (h_Pipe
== NULLVAL
)
109 cerr
<< "NamedPipeReader::closePipe> pipe is not open" << endl
;
115 DisconnectNamedPipe(h_Pipe
);
121 //---------------------------------------------------------------------------------------
122 void NamedPipeReader::insertLine(std::vector
<string
> & vect
, string line
, bool append_line
)
124 if (append_line
&& vect
.size ())
126 string prev
= vect
[vect
.size () - 1];
128 vect
[vect
.size () - 1] = prev
;
132 vect
.push_back (line
);
136 //---------------------------------------------------------------------------------------
137 void NamedPipeReader::extractLines(char * buffer
, std::vector
<string
> & vect
, bool & append_line
, unsigned long cbRead
)
141 size_t start_size
= vect
.size ();
142 char * start
= buffer
;
143 buffer
[cbRead
] = '\0';
144 char * end
= strstr(buffer
, s_LineBreak
);
146 //cout << "extractLines entered with append_line: " << append_line << " cbRead: " << cbRead << "buffer: " << buffer << endl;
154 end
+= (sizeof(s_LineBreak
) / sizeof(char));
156 offset
+= line
.length() + (sizeof(s_LineBreak
) / sizeof(char));
158 // cout << "Offset: "<< offset << "cbRead: " << cbRead << "line: " << line << endl;
159 insertLine(vect
, line
, append_line
);
168 // cout << "inserting line start_size: " << start_size << "current: " << vect.size () << "line length: "<< line.length () << endl;
171 if (start_size
== vect
.size ())
175 if (start_size
== vect
.size ())
177 insertLine(vect
, line
, true);
181 insertLine(vect
, line
, false);
187 end
= strstr(end
, s_LineBreak
);
192 DWORD buf_offset
= 0;
193 char * offset
= strchr(buffer
, '\x0D');
198 /// due to some mysterious reason, strchr / strstr sometimes returns
199 /// not always the offset to the CR character but to the next LF
200 /// in MSVC 2005 (Debug Modus)
202 if (offset
[0] == '\x0A')
211 // implement me special case
215 if (offset
[0] == '\x0D')
224 /// BUG detected in parsing code
229 string line
= buffer
;
232 assert(vect
.empty () == false);
233 string prev_line
= vect
[vect
.size () -1];
236 vect
.push_back (prev_line
);
241 vect
.push_back (line
);
244 buf_offset
+= line
.length();
245 if (buf_offset
>= cbRead
)
251 offset
= strstr(buffer
, "\n");
253 if (buf_offset
< cbRead
)
255 buffer
[cbRead
- buf_offset
] = '\0';
256 string line
= buffer
;
259 assert(vect
.empty () == false);
260 string prev_line
= vect
[vect
.size () -1];
263 vect
.push_back (prev_line
);
267 vect
.push_back (line
);
277 //---------------------------------------------------------------------------------------
278 bool NamedPipeReader::readPipe(char * buffer
, int bufferlength
, long & bytesread
)
282 long cbRead
= read(h_Pipe
,
284 (bufferlength
-1) * sizeof(char));
287 BOOL fSuccess
= ReadFile(h_Pipe
,
289 (bufferlength
-1) * sizeof(char),
293 if (!fSuccess
&& GetLastError() != ERROR_MORE_DATA
)
300 //---------------------------------------------------------------------------------------
302 bool NamedPipeReader::readSource(vector
<string
> & vect
)
304 size_t lines
= vect
.size ();
306 if (h_Pipe
== NULLVAL
)
308 cerr
<< "Error: pipe is not open" << endl
;
314 cerr
<< "Error: no memory" << endl
;
318 bool append_line
= false;
321 memset(m_Buffer
, 0x0, m_BufferLength
* sizeof(char));
324 if (!readPipe(m_Buffer
, m_BufferLength
-1, cbRead
))
327 extractLines(m_Buffer
, vect
, append_line
, cbRead
);
328 }while (append_line
);
330 return (vect
.size () - lines
);
334 } // end of namespace System_