~ubuntu-branches/ubuntu/hoary/kdemultimedia/hoary

« back to all changes in this revision

Viewing changes to mpeglib/example/yaf/yafcore/inputInterface.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Martin Schulze
  • Date: 2003-01-22 15:00:51 UTC
  • Revision ID: james.westby@ubuntu.com-20030122150051-uihwkdoxf15mi1tn
Tags: upstream-2.2.2
ImportĀ upstreamĀ versionĀ 2.2.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
  This class can wait for an input by the user
 
3
  Copyright (C) 1998  Martin Vogt
 
4
 
 
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.
 
8
 
 
9
  For more information look at the file COPYRIGHT in this package
 
10
 
 
11
 */
 
12
 
 
13
 
 
14
#include "inputInterface.h"
 
15
#include "multiReader.h"
 
16
 
 
17
 
 
18
 
 
19
InputInterface::InputInterface(){
 
20
 
 
21
  currentLine= new Buffer(300);
 
22
  rawLine=new Buffer(300);
 
23
  loopback=new Buffer(300);
 
24
  protocolSyntax=false;
 
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);
 
31
  }
 
32
  yafInput->close();
 
33
 
 
34
}
 
35
 
 
36
InputInterface::~InputInterface(){
 
37
  delete yafInput;
 
38
  delete multiReader;
 
39
  delete currentLine;
 
40
  delete rawLine;
 
41
  delete loopback;
 
42
}
 
43
 
 
44
 
 
45
int InputInterface::addFileDescriptor(int fd) {
 
46
  int back;
 
47
  back=multiReader->add(fd);
 
48
  return back;
 
49
}
 
50
 
 
51
 
 
52
void InputInterface::removeFileDescriptor(int fd) {
 
53
  multiReader->remove(fd);
 
54
}
 
55
 
 
56
 
 
57
int InputInterface::getCurrentCommandNumber() {
 
58
  int back=0;
 
59
  back=currentCommandNumber;
 
60
  return back;
 
61
}
 
62
 
 
63
void InputInterface::increaseCurrentCommandNumber(){
 
64
  currentCommandNumber++;
 
65
}
 
66
 
 
67
int InputInterface::write(int fd,const char* txt) {
 
68
  int back=0;
 
69
 
 
70
  int len;
 
71
  loopback->clear();
 
72
  if (protocolSyntax == true) {
 
73
    snprintf(loopback->getData(),300,
 
74
             "Command:41 Msg:%s",txt);
 
75
  } else {
 
76
    strcpy(loopback->getData(),txt);
 
77
  }
 
78
  len =loopback->len();
 
79
  back=::write(fd,loopback->getData(),len);
 
80
 
 
81
  return back;
 
82
}
 
83
 
 
84
 
 
85
 
 
86
void InputInterface::waitForLine() {
 
87
 
 
88
  while(multiReader->hasLine() == false) {
 
89
    multiReader->waitForLine();
 
90
  }
 
91
  multiReader->getLine(rawLine);
 
92
  makeValidLine(rawLine->getData());
 
93
 
 
94
}
 
95
 
 
96
void InputInterface::setProtocolSyntax (int proto) {
 
97
  protocolSyntax=proto;
 
98
}
 
99
 
 
100
 
 
101
 
 
102
void InputInterface::makeValidLine(char* line) {
 
103
 
 
104
  int len;
 
105
  len=strlen(line);
 
106
  if (len >= 1) {
 
107
    if (line[len-1] == '\n') {
 
108
      line[len-1]='\0';
 
109
    }
 
110
  }
 
111
  if (strncmp("noprotocol",line,10) == 0){
 
112
    setProtocolSyntax(false);
 
113
    clearLine();
 
114
    increaseCurrentCommandNumber();
 
115
    snprintf(currentLine->getData(),300,
 
116
             "Command:%d Msg:%s",currentCommandNumber,line);
 
117
    return;
 
118
  }
 
119
  if (strncmp("protocol",line,8) ==  0 ){
 
120
    setProtocolSyntax(true);
 
121
    clearLine();
 
122
    increaseCurrentCommandNumber();
 
123
    snprintf(currentLine->getData(),300,
 
124
             "Command:%d Msg:%s",currentCommandNumber,line);
 
125
    return;
 
126
  }
 
127
 
 
128
  // Now the part if we do _not_ bypass the protocol-state
 
129
 
 
130
  if (protocolSyntax == false) {
 
131
    clearLine();
 
132
    increaseCurrentCommandNumber();
 
133
    snprintf(currentLine->getData(),300,
 
134
             "Command:%d Msg:%s",currentCommandNumber,line);
 
135
  } else {
 
136
    increaseCurrentCommandNumber();
 
137
    strcpy(currentLine->getData(),line);
 
138
  }
 
139
 
 
140
  return;
 
141
}
 
142
 
 
143
 
 
144
void InputInterface::addInputLine(struct Buffer* buffer) {
 
145
  multiReader->add(buffer);
 
146
}
 
147
 
 
148
 
 
149
void InputInterface::insertYafScript(ifstream* stream) {
 
150
  char bst;
 
151
  int nBytes=0;
 
152
  Buffer yafScriptBuffer(300);
 
153
 
 
154
  if (stream->fail()) return;
 
155
  while (stream->eof()==false) {
 
156
    stream->get(bst);
 
157
    if (stream->eof()) break;
 
158
 
 
159
    yafScriptBuffer.append(&bst,1);
 
160
  }
 
161
  nBytes=(yafScriptBuffer.len()-1);  // EOF is a character we dont want
 
162
 
 
163
  addInputLine(&yafScriptBuffer);
 
164
}
 
165
 
 
166
 
 
167
 
 
168
int  InputInterface::hasLine() {
 
169
  if (currentLine->len() == 0) return 0;
 
170
  return 1;
 
171
}
 
172
 
 
173
void InputInterface::clearLine() {
 
174
  currentLine->clear();
 
175
}
 
176
 
 
177
 
 
178
char* InputInterface::getLine() {
 
179
  char* back=NULL;
 
180
 
 
181
  back=currentLine->getData();
 
182
#ifdef _DEBUG_INPUT
 
183
  ofstream infile("instream.dbg",ios::app);
 
184
  infile << back <<endl;
 
185
  infile.close();
 
186
#endif
 
187
 
 
188
 
 
189
  return back;
 
190
}
 
191
 
 
192
 
 
193