FlushConsoleInputBuffer(hInput);
}
nReadEnd = 0;
+ lpFeedEnd = NULL;
InitializeCriticalSection(&CriticalSection);
hEvent=CreateEvent(NULL, // default security
FALSE, // auto reset
LeaveCriticalSection(&CriticalSection);
}
-
-
-int PipeStruct::ReadLine(void){
+int PipeStruct::ReadData(void){
DWORD dwBytes;
int ret;
- int start=0;
- int start1;
if(!bPipe){
fgets(lpReadBuffer,LINE_INPUT_MAX_CHAR,stdin);
- start=strlen(lpReadBuffer);
- if(!start){
+ dwBytes=strlen(lpReadBuffer);
+ if(!dwBytes){
set_EOF_();
lpReadBuffer[0]='\0';
+ return 0;
}
- return start;
}else{
+ // Unfortunately we need to use polling here.
+ // Otherwise Windows returns single bytes if
+ // the engine runs at low priority.
+ // This kills performance.
while(TRUE){
- // Unfortunately we need to use polling here.
- // Otherwise Windows returns single bytes if
- // the engine runs at low priority.
- // This kills performance.
- while(TRUE){
- ret=PeekNamedPipe(hInput,
- NULL, // don't read anything yet
- 0, // no buffer
- NULL, // no we don't read anything!
- &dwBytes,// now we're talking
- NULL); // nono we don't read anything
- if(!ret){
- set_EOF_();
- lpReadBuffer[0]='\0';
- return 0;
- }
- if(dwBytes>0){
- break;
- }else{
- Idle();
- }
-
- }
- ret=ReadFile(hInput,
- lpReadBuffer+start,
- LINE_INPUT_MAX_CHAR-start,
- &dwBytes,
- NULL);
+ ret=PeekNamedPipe(hInput,
+ NULL, // don't read anything yet
+ 0, // no buffer
+ NULL, // no we don't read anything!
+ &dwBytes,// now we're talking
+ NULL); // nono we don't read anything
if(!ret){
set_EOF_();
lpReadBuffer[0]='\0';
return 0;
+ }
+ if(dwBytes>0){
+ break;
}else{
- start1=start;
- start+=dwBytes;
- if (memchr(lpReadBuffer+start1, '\n', dwBytes)){
- lpReadBuffer[start]='\0';
- return start;
- }
+ Idle();
}
+
+ }
+ ret=ReadFile(hInput,
+ lpReadBuffer,
+ LINE_INPUT_MAX_CHAR,
+ &dwBytes,
+ NULL);
+ if(!ret){
+ set_EOF_();
+ lpReadBuffer[0]='\0';
+ return 0;
}
}
+ lpReadBuffer[dwBytes]='\0';
+ return dwBytes;
}
void PipeStruct::ReadInput(void) {
DWORD dwBytes;
int ret;
- ret=ReadLine();
+ BOOL bSetEvent=FALSE;
+ ret=ReadData();
EnterCriticalSection(&CriticalSection);
if(!EOF_()){
if(ret+nReadEnd>=LINE_INPUT_MAX_CHAR){
}
memcpy(lpBuffer+nReadEnd,lpReadBuffer,ret);
nReadEnd += ret;
+ if(!lpFeedEnd){
+ lpFeedEnd = (char *) memchr(lpBuffer, '\n', nReadEnd);
+ }
+ if(lpFeedEnd){
+ bSetEvent=TRUE;
+ }
}
LeaveCriticalSection(&CriticalSection);
- SetEvent(hEvent);
-}
-
-void PipeStruct::LineOutput(const char *szLineStr) const {
- DWORD dwBytes;
- int nStrLen;
- char szWriteBuffer[LINE_INPUT_MAX_CHAR];
- if(bPipe){
- nStrLen = strlen(szLineStr);
- memcpy(szWriteBuffer, szLineStr, nStrLen);
- szWriteBuffer[nStrLen] = '\r';
- szWriteBuffer[nStrLen + 1] = '\n';
- WriteFile(hOutput, szWriteBuffer, nStrLen + 2, &dwBytes, NULL);
- }else{
- printf("%s\n",szLineStr);
- fflush(stdout);
+ if(EOF_() || bSetEvent){
+ SetEvent(hEvent);
}
}
bool PipeStruct::GetBuffer(char *szLineStr) {
- char *lpFeedEnd;
int nFeedEnd;
int ret;
EnterCriticalSection(&CriticalSection);
- lpFeedEnd = (char *) memchr(lpBuffer, '\n', nReadEnd);
if (lpFeedEnd == NULL) {
ret=FALSE;
} else {
nFeedEnd ++;
nReadEnd -= nFeedEnd;
memcpy(lpBuffer, lpBuffer + nFeedEnd, nReadEnd);
+ lpFeedEnd = (char *) memchr(lpBuffer, '\n', nReadEnd);
ret=TRUE;
}
LeaveCriticalSection(&CriticalSection);
}
}
}
+
+void PipeStruct::LineOutput(const char *szLineStr) const {
+ DWORD dwBytes;
+ int nStrLen;
+ char szWriteBuffer[LINE_INPUT_MAX_CHAR];
+ if(bPipe){
+ nStrLen = strlen(szLineStr);
+ memcpy(szWriteBuffer, szLineStr, nStrLen);
+ szWriteBuffer[nStrLen] = '\r';
+ szWriteBuffer[nStrLen + 1] = '\n';
+ WriteFile(hOutput, szWriteBuffer, nStrLen + 2, &dwBytes, NULL);
+ }else{
+ printf("%s\n",szLineStr);
+ fflush(stdout);
+ }
+}
#endif