summaryrefslogtreecommitdiffstats
path: root/lskat/lskatproc/KInputChildProcess.cpp
blob: 24fca96813ec4984f7a77d52202d21caadf0b059 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/***************************************************************************
                          KInputChildProcess.cpp  -  description
                             -------------------
    begin                : Tue May 2 2000
    copyright            : (C) 2000 by Martin Heni
    email                : martin@heni-online.de
 ***************************************************************************/
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <tqstring.h>

#include "KInputChildProcess.h"
#include "KInputChildProcess.moc"


KInputChildProcess::~KInputChildProcess()
{
  delete buffer;
  delete childConnect;
}
KInputChildProcess::KInputChildProcess(int size_buffer)
  : TQObject(0,0)
{
  buffersize=size_buffer;
  if (buffersize<1) buffersize=1024;
  buffer=new char[buffersize];
  inputbuffer="";
  terminateChild=false;
}
bool KInputChildProcess::exec()
{
  int pos;
  TQString s;

  childConnect=new KChildConnect;
  if (!childConnect) return false;
  connect(childConnect,TQ_SIGNAL(signalReceiveMsg(KEMessage *,int)),
          this,TQ_SLOT(slotReceiveMsg(KEMessage *,int)));
  do
  {
    // Wait for input
    if (feof(stdin)) 
    {
      sleep(1);
      continue;
    }

    if (!fgets(buffer,buffersize,stdin) )
    {
     continue;
    }
    s=buffer;
    s=inputbuffer+s;
    // printf("ChildABC '%s'\n",(const char *)s);
    // fflush(stdout);
    pos=s.findRev(KEMESSAGE_CR);
    if (pos<0)
    {
      inputbuffer=s;
    }
    else if (pos+KEMESSAGE_CR.length()==s.length())
    {
      // CR at the end...calling receive
      childConnect->Receive(s);
    }
    else
    {
      inputbuffer=s.right(s.length()-pos-KEMESSAGE_CR.length());
      s=s.left(pos+KEMESSAGE_CR.length());
      // printf("s='%s' in='%s'\n",(const char *)s,(const char *)inputbuffer);
      childConnect->Receive(s);
    }
  }while(!terminateChild);
  return true;
}

void KInputChildProcess::Terminate()
{
  terminateChild=true;
}
bool KInputChildProcess::IsTerminated()
{
  return terminateChild;
}

bool KInputChildProcess::ReceiveMsg(KEMessage *msg,int id)
{
  return false;
}
void KInputChildProcess::slotReceiveMsg(KEMessage *msg,int id)
{
  if (!ReceiveMsg(msg,id))  // made for virtual override
  {
    // otherwise emit signal
    emit signalReceiveMsg(msg,id);
  }
}
bool KInputChildProcess::SendMsg(KEMessage *msg)
{
  if (childConnect) return childConnect->SendMsg(msg);
  return false;
}