- Add <cstring> for "memset", "strchr" and "strstr"

- Use the C++ wrapper headers consistently
- Fix indentation

svn path=/trunk/; revision=32686
This commit is contained in:
Colin Finck 2008-03-15 00:35:48 +00:00
parent 1e2d76a86c
commit ddef9f1617

View file

@ -11,7 +11,8 @@
#include "namedpipe_reader.h" #include "namedpipe_reader.h"
#include <iostream> #include <iostream>
#include <assert.h> #include <cassert>
#include <cstring>
namespace System_ namespace System_
{ {
@ -22,51 +23,51 @@ namespace System_
#else #else
const char * NamedPipeReader::s_LineBreak = "\x0D\x0A\0"; const char * NamedPipeReader::s_LineBreak = "\x0D\x0A\0";
#endif #endif
using std::vector; using std::vector;
//--------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------
NamedPipeReader::NamedPipeReader() : DataSource(), h_Pipe(NULLVAL), m_Buffer(0) NamedPipeReader::NamedPipeReader() : DataSource(), h_Pipe(NULLVAL), m_Buffer(0)
{ {
} }
//--------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------
NamedPipeReader::~NamedPipeReader() NamedPipeReader::~NamedPipeReader()
{ {
if (m_Buffer) if (m_Buffer)
free(m_Buffer); free(m_Buffer);
} }
bool NamedPipeReader::isSourceOpen() bool NamedPipeReader::isSourceOpen()
{ {
return true; return true;
} }
//--------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------
bool NamedPipeReader::openSource(const string & PipeCmd) bool NamedPipeReader::openSource(const string & PipeCmd)
{ {
if (h_Pipe != NULLVAL) if (h_Pipe != NULLVAL)
{ {
cerr << "NamedPipeReader::openPipe> pipe already open" << endl; cerr << "NamedPipeReader::openPipe> pipe already open" << endl;
return false; return false;
} }
#ifndef __LINUX__ #ifndef __LINUX__
h_Pipe = CreateFile(PipeCmd.c_str(), h_Pipe = CreateFile(PipeCmd.c_str(),
GENERIC_WRITE | GENERIC_READ, GENERIC_WRITE | GENERIC_READ,
0, 0,
NULL, NULL,
OPEN_EXISTING, OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL, FILE_ATTRIBUTE_NORMAL,
(HANDLE) (HANDLE)
NULL); NULL);
if(INVALID_HANDLE_VALUE == h_Pipe) { if(INVALID_HANDLE_VALUE == h_Pipe) {
cerr << "NamedPipeReader::openPipe> failed to open pipe " << PipeCmd << " Error:" << GetLastError() << endl; cerr << "NamedPipeReader::openPipe> failed to open pipe " << PipeCmd << " Error:" << GetLastError() << endl;
h_Pipe = NULLVAL; h_Pipe = NULLVAL;
return false; return false;
} }
else else
{ {
cout << "NamedPipeReader::openPipe> successfully opened pipe" << endl; cout << "NamedPipeReader::openPipe> successfully opened pipe" << endl;
if (!m_Buffer) if (!m_Buffer)
{ {
@ -74,49 +75,49 @@ namespace System_
m_Buffer = (char*)malloc(sizeof(char) * m_BufferLength); m_Buffer = (char*)malloc(sizeof(char) * m_BufferLength);
} }
ConnectNamedPipe(h_Pipe, ConnectNamedPipe(h_Pipe,
0); 0);
return true; return true;
} }
#else #else
h_Pipe = open(PipeCmd.c_str(), O_RDONLY); h_Pipe = open(PipeCmd.c_str(), O_RDONLY);
if(INVALID_HANDLE_VALUE == h_Pipe) { if(INVALID_HANDLE_VALUE == h_Pipe) {
cerr << "NamedPipeReader::openPipe> failed to open pipe " << PipeCmd << endl; cerr << "NamedPipeReader::openPipe> failed to open pipe " << PipeCmd << endl;
h_Pipe = NULLVAL; h_Pipe = NULLVAL;
return false; return false;
} }
else else
{ {
cout << "NamedPipeReader::openPipe> successfully opened pipe handle: "<< h_Pipe << endl << "Src: " << PipeCmd << endl; cout << "NamedPipeReader::openPipe> successfully opened pipe handle: "<< h_Pipe << endl << "Src: " << PipeCmd << endl;
if (!m_Buffer) if (!m_Buffer)
{ {
m_BufferLength = 100; m_BufferLength = 100;
m_Buffer = (char*)malloc(sizeof(char) * m_BufferLength); m_Buffer = (char*)malloc(sizeof(char) * m_BufferLength);
} }
return true; return true;
} }
#endif #endif
} }
//--------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------
bool NamedPipeReader::closeSource() bool NamedPipeReader::closeSource()
{ {
cerr << "NamedPipeReader::closePipe> entered" << endl; cerr << "NamedPipeReader::closePipe> entered" << endl;
if (h_Pipe == NULLVAL) if (h_Pipe == NULLVAL)
{ {
cerr << "NamedPipeReader::closePipe> pipe is not open" << endl; cerr << "NamedPipeReader::closePipe> pipe is not open" << endl;
return false; return false;
} }
#ifdef __LINUX__ #ifdef __LINUX__
close(h_Pipe); close(h_Pipe);
#else #else
DisconnectNamedPipe(h_Pipe); DisconnectNamedPipe(h_Pipe);
CloseHandle(h_Pipe); CloseHandle(h_Pipe);
#endif #endif
h_Pipe = NULLVAL; h_Pipe = NULLVAL;
return true; return true;
} }
//--------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------
void NamedPipeReader::insertLine(std::vector<string> & vect, string line, bool append_line) void NamedPipeReader::insertLine(std::vector<string> & vect, string line, bool append_line)
{ {
@ -133,14 +134,14 @@ namespace System_
} }
//--------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------
void NamedPipeReader::extractLines(char * buffer, std::vector<string> & vect, bool & append_line, unsigned long cbRead) void NamedPipeReader::extractLines(char * buffer, std::vector<string> & vect, bool & append_line, unsigned long cbRead)
{ {
#if 0 #if 0
long offset = 0; long offset = 0;
size_t start_size = vect.size (); size_t start_size = vect.size ();
char * start = buffer; char * start = buffer;
buffer[cbRead] = '\0'; buffer[cbRead] = '\0';
char * end = strstr(buffer, s_LineBreak); char * end = strstr(buffer, s_LineBreak);
//cout << "extractLines entered with append_line: " << append_line << " cbRead: " << cbRead << "buffer: " << buffer << endl; //cout << "extractLines entered with append_line: " << append_line << " cbRead: " << cbRead << "buffer: " << buffer << endl;
@ -189,90 +190,90 @@ namespace System_
#else #else
DWORD buf_offset = 0; DWORD buf_offset = 0;
char * offset = strchr(buffer, '\x0D'); char * offset = strchr(buffer, '\x0D');
while(offset) while(offset)
{ {
/// ///
/// HACKHACK /// HACKHACK
/// due to some mysterious reason, strchr / strstr sometimes returns /// due to some mysterious reason, strchr / strstr sometimes returns
/// not always the offset to the CR character but to the next LF /// not always the offset to the CR character but to the next LF
/// in MSVC 2005 (Debug Modus) /// in MSVC 2005 (Debug Modus)
if (offset[0] == '\x0A') if (offset[0] == '\x0A')
{ {
if (buf_offset) if (buf_offset)
{ {
offset--; offset--;
} }
else else
{ {
//TODO //TODO
// implement me special case // implement me special case
} }
} }
if (offset[0] == '\x0D') if (offset[0] == '\x0D')
{ {
buf_offset += 2; buf_offset += 2;
offset[0] = '\0'; offset[0] = '\0';
offset +=2; offset +=2;
} }
else else
{ {
/// ///
/// BUG detected in parsing code /// BUG detected in parsing code
/// ///
abort(); abort();
} }
string line = buffer; string line = buffer;
if (append_line) if (append_line)
{ {
assert(vect.empty () == false); assert(vect.empty () == false);
string prev_line = vect[vect.size () -1]; string prev_line = vect[vect.size () -1];
prev_line += line; prev_line += line;
vect.pop_back (); vect.pop_back ();
vect.push_back (prev_line); vect.push_back (prev_line);
append_line = false; append_line = false;
} }
else else
{ {
vect.push_back (line); vect.push_back (line);
} }
buf_offset += line.length(); buf_offset += line.length();
if (buf_offset >= cbRead) if (buf_offset >= cbRead)
{ {
break; break;
} }
buffer = offset; buffer = offset;
offset = strstr(buffer, "\n"); offset = strstr(buffer, "\n");
} }
if (buf_offset < cbRead) if (buf_offset < cbRead)
{ {
buffer[cbRead - buf_offset] = '\0'; buffer[cbRead - buf_offset] = '\0';
string line = buffer; string line = buffer;
if (append_line) if (append_line)
{ {
assert(vect.empty () == false); assert(vect.empty () == false);
string prev_line = vect[vect.size () -1]; string prev_line = vect[vect.size () -1];
vect.pop_back (); vect.pop_back ();
prev_line += line; prev_line += line;
vect.push_back (prev_line); vect.push_back (prev_line);
} }
else else
{ {
vect.push_back (line); vect.push_back (line);
append_line = true; append_line = true;
} }
} }
else else
{ {
append_line = false; append_line = false;
} }
#endif #endif
} }
//--------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------
bool NamedPipeReader::readPipe(char * buffer, int bufferlength, long & bytesread) bool NamedPipeReader::readPipe(char * buffer, int bufferlength, long & bytesread)
{ {
@ -284,12 +285,12 @@ namespace System_
#else #else
DWORD cbRead = 0; DWORD cbRead = 0;
BOOL fSuccess = ReadFile(h_Pipe, BOOL fSuccess = ReadFile(h_Pipe,
buffer, buffer,
(bufferlength-1) * sizeof(char), (bufferlength-1) * sizeof(char),
&cbRead, &cbRead,
NULL); NULL);
if (!fSuccess && GetLastError() != ERROR_MORE_DATA) if (!fSuccess && GetLastError() != ERROR_MORE_DATA)
return false; return false;
#endif #endif
@ -298,9 +299,9 @@ namespace System_
} }
//--------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------
bool NamedPipeReader::readSource(vector<string> & vect) bool NamedPipeReader::readSource(vector<string> & vect)
{ {
size_t lines = vect.size (); size_t lines = vect.size ();
if (h_Pipe == NULLVAL) if (h_Pipe == NULLVAL)
{ {
@ -314,9 +315,9 @@ namespace System_
return false; return false;
} }
bool append_line = false; bool append_line = false;
do do
{ {
memset(m_Buffer, 0x0, m_BufferLength * sizeof(char)); memset(m_Buffer, 0x0, m_BufferLength * sizeof(char));
long cbRead = 0; long cbRead = 0;