summaryrefslogtreecommitdiffstats
path: root/mpeglib/example/yaf/yafcore/multiReader.cpp
blob: 41a62263dc5e64d77facee094974ef1e78b88eda (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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/*
  This class can waits for input on different istreams
  Copyright (C) 1998  Martin Vogt

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU Library General Public License as published by
  the Free Software Foundation.

  For more information look at the file COPYRIGHT in this package

 */



#include "multiReader.h"



MultiReader::MultiReader() {
  int i;

  buffer=new Buffer(201);
  for(i=0;i<_MAX_INPUT;i++) {
    lineInputArray[i]=new LineInput;
    lineInputArray[i]->tmpLineStack=new LineStack();
    lineInputArray[i]->empty=true;
  }
  script=new LineStack();
}



MultiReader::~MultiReader() {
  int i;
  for(i=0;i<_MAX_INPUT;i++) {
    delete lineInputArray[i]->tmpLineStack;
    delete lineInputArray[i];
  }
  delete script;
}



int MultiReader::add(int fd) {
  int nPos;
  nPos=getEmptySlot();
  
  if (nPos == -1) {
    return -1;
  }
  lineInputArray[nPos]->fd=fd;
  lineInputArray[nPos]->empty=false;
  return nPos;
}

void MultiReader::add(LineStack* aScript) {
  script->appendBottom(aScript);
}


void MultiReader::add(Buffer* aScript) {
  script->appendBottom(aScript->getData(),aScript->len());
}




void MultiReader::remove (int fd) {
  int nPos;
  nPos=getSlot(fd);
  
  if (nPos == -1) {
    return ;
  }
  lineInputArray[nPos]->empty=true;
}



void MultiReader::waitForLine() {
  while(hasLine() == false) {
    doSelect(NULL);
  }
}


void MultiReader::poll(struct timeval* timeout) {
  doSelect(timeout);
}



void MultiReader::doSelect(struct timeval* timeout) {
  int i;
  int ret;
  fd_set readfds;
  int nBytes;
  int maxFd=0;

  FD_ZERO(&readfds);
  for(i=0;i<_MAX_INPUT;i++) {
    if (lineInputArray[i]->empty == false) {
      FD_SET(lineInputArray[i]->fd,&readfds);
      if (lineInputArray[i]->fd > maxFd) {
	maxFd=lineInputArray[i]->fd;
      }
    }
  } 
  ret=select(maxFd+1,&readfds,NULL,NULL,timeout);
  if (ret < 0) {
    if (errno < 0) {
      perror("nach select multireader:");
      exit(0);
    }
  }
  if (ret == 0) return;
  
  for(i=0;i<_MAX_INPUT;i++) {
    if (lineInputArray[i]->empty == false) {
      if (FD_ISSET(lineInputArray[i]->fd,&readfds)) {
	nBytes=read(lineInputArray[i]->fd,buffer->getData(),200);
	if (nBytes == 0) {
	  perror("MultiReader:read error!");
	  exit(-1);
	}
	(buffer->getData())[nBytes]='\0';
	
	lineInputArray[i]->tmpLineStack->appendBottom(buffer->getData(),
						      nBytes);
	FD_CLR(lineInputArray[i]->fd,&readfds);
      }
    }
  } 
} 



int MultiReader::hasLine() {
  int i;
  LineStack* lineStack;        // owned by class
  
  if (script->hasLine() == true) {
    return true;
  }

  for(i=0;i<_MAX_INPUT;i++) {
    if (lineInputArray[i]->empty == false) {
      lineStack=lineInputArray[i]->tmpLineStack;
      if (lineStack->hasLine()) {
	return true;
      }
    }
  } 
  return false;
}


void MultiReader::getLine(Buffer* buffer) {
  int i;
  LineStack* lineStack;        // owned by class
  buffer->clear();

  if (script->hasLine()==true) {
    script->nextLine(buffer);
  } else {
    for(i=0;i<_MAX_INPUT;i++) {
      if (lineInputArray[i]->empty == false) {
	lineStack=lineInputArray[i]->tmpLineStack;
	
	if (lineStack->hasLine()) {
	  lineStack->nextLine(buffer);
	  return;
	}
      }
    } 
  }
  buffer->append("\n");
}




int MultiReader::getEmptySlot() {
  int i;

  for(i=0;i<_MAX_INPUT;i++) {
    if (lineInputArray[i]->empty == true) {
      return i;
    }
  }
  return -1;  
}


int MultiReader::getSlot(int fd) {
  int i;

  for(i=0;i<_MAX_INPUT;i++) {
    if (lineInputArray[i]->empty == false) {
      if (lineInputArray[i]->fd == fd) {
	return i;
      }
    }
  }
  return -1;
}