2
This class can wait for an input by the user
3
Copyright (C) 1998 Martin Vogt
5
This program is free software; you can redistribute it and/or modify
6
it under the terms of the GNU Library General Public License as published by
7
the Free Software Foundation.
9
For more information look at the file COPYRIGHT in this package
14
#include "inputInterface.h"
15
#include "multiReader.h"
19
InputInterface::InputInterface(){
21
currentLine= new Buffer(300);
22
rawLine=new Buffer(300);
23
loopback=new Buffer(300);
25
currentCommandNumber=42;
26
multiReader=new MultiReader();
27
yafInput=new ifstream("yaf.script");
28
if (yafInput->fail() == false) {
29
cout << "Command:0 Msg:comment found yaf.script. Parsing first"<<endl;
30
insertYafScript(yafInput);
36
InputInterface::~InputInterface(){
45
int InputInterface::addFileDescriptor(int fd) {
47
back=multiReader->add(fd);
52
void InputInterface::removeFileDescriptor(int fd) {
53
multiReader->remove(fd);
57
int InputInterface::getCurrentCommandNumber() {
59
back=currentCommandNumber;
63
void InputInterface::increaseCurrentCommandNumber(){
64
currentCommandNumber++;
67
int InputInterface::write(int fd,const char* txt) {
72
if (protocolSyntax == true) {
73
snprintf(loopback->getData(),300,
74
"Command:41 Msg:%s",txt);
76
strcpy(loopback->getData(),txt);
79
back=::write(fd,loopback->getData(),len);
86
void InputInterface::waitForLine() {
88
while(multiReader->hasLine() == false) {
89
multiReader->waitForLine();
91
multiReader->getLine(rawLine);
92
makeValidLine(rawLine->getData());
96
void InputInterface::setProtocolSyntax (int proto) {
102
void InputInterface::makeValidLine(char* line) {
107
if (line[len-1] == '\n') {
111
if (strncmp("noprotocol",line,10) == 0){
112
setProtocolSyntax(false);
114
increaseCurrentCommandNumber();
115
snprintf(currentLine->getData(),300,
116
"Command:%d Msg:%s",currentCommandNumber,line);
119
if (strncmp("protocol",line,8) == 0 ){
120
setProtocolSyntax(true);
122
increaseCurrentCommandNumber();
123
snprintf(currentLine->getData(),300,
124
"Command:%d Msg:%s",currentCommandNumber,line);
128
// Now the part if we do _not_ bypass the protocol-state
130
if (protocolSyntax == false) {
132
increaseCurrentCommandNumber();
133
snprintf(currentLine->getData(),300,
134
"Command:%d Msg:%s",currentCommandNumber,line);
136
increaseCurrentCommandNumber();
137
strcpy(currentLine->getData(),line);
144
void InputInterface::addInputLine(struct Buffer* buffer) {
145
multiReader->add(buffer);
149
void InputInterface::insertYafScript(ifstream* stream) {
152
Buffer yafScriptBuffer(300);
154
if (stream->fail()) return;
155
while (stream->eof()==false) {
157
if (stream->eof()) break;
159
yafScriptBuffer.append(&bst,1);
161
nBytes=(yafScriptBuffer.len()-1); // EOF is a character we dont want
163
addInputLine(&yafScriptBuffer);
168
int InputInterface::hasLine() {
169
if (currentLine->len() == 0) return 0;
173
void InputInterface::clearLine() {
174
currentLine->clear();
178
char* InputInterface::getLine() {
181
back=currentLine->getData();
183
ofstream infile("instream.dbg",ios::app);
184
infile << back <<endl;