File: SIOStream.cpp

package info (click to toggle)
yudit 3.1.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 18,472 kB
  • sloc: cpp: 76,344; perl: 5,630; makefile: 989; ansic: 823; sh: 441
file content (340 lines) | stat: -rw-r--r-- 6,239 bytes parent folder | download
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
/** 
 *  Yudit Unicode Editor Source File
 *
 *  GNU Copyright (C) 1997-2023  Gaspar Sinai <gaspar@yudit.org>  
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License, version 2,
 *  dated June 1991. See file COPYYING for details.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */
 
#include "SIOStream.h"
#include <stdio.h>
#ifdef USE_WINAPI
#include <io.h>
#include <windows.h>
#endif

SReader::SReader (const SInputStream& i, SEventTarget* t) : in (i)
{
  errFlag = false;
  finishFlag = false;
  more = true;
  SEventHandler::addInput (&in, this);
  trg = t;
  if (trg) SEventHandler::addJob (this, trg);
}

SReader::SReader (const SInputStream& i, const SStringVector& sep, SEventTarget* t) : sepVector (sep), in(i)
{
  errFlag = false;
  finishFlag = false;
  more = true;
  trg = t;
  SEventHandler::addInput (&in, this);
  if (trg) SEventHandler::addJob (this, trg);
}

bool
SReader::isOK()
{
  return !errFlag;
}

bool
SReader::close()
{
  return in.close();
}

SReader::~SReader ()
{
}

bool
SReader::read (SString* s)
{
 // This is how we read things unblocked.
#ifdef USE_WINAPI
  if (in.getType()==SInputStream::PIPE)
  {
    DWORD n;
    char* buff = new char[4096];
    CHECK_NEW (buff);
    do
    {
      if (!ReadFile ((char*)in.getId(), buff, 4096, &n, 0) ||  n<=0)
      {
        break;
        errFlag=true;
      }
      SString ns(buff, n);
      read (&in, ns);
    } while (n>0);
    delete buff;
    read (&in, SStringNull);
  }
  if (in.getType()==SInputStream::FILE)
  {
    int n;
    char* buff = new char[4096];
    CHECK_NEW (buff);
    
    do
    {
      n  = ::read (in.getId(), buff, 4096); 
      SString ns(buff, n);
      read (&in, ns);
    } while (n>0);
    delete buff;
    read (&in, SStringNull);
  }
#endif
 if (trg==0)
 {
   bool n = true;
   while (n==true && finishFlag==false)
   {
     n = SEventHandler::next();
   }
 }
 if (errFlag || !finishFlag) return false;

 if (sepVector.size() != 0)
 {
   if (line.size() ==0)
   {
     if (!more)
     {
        *s = buffer;
        buffer.clear();
     }
     else
     {
       *s = SStringNull;
     }
   }
   else
   {
     *s = line[0];
     line.remove(0);
   }
   if (more && line.size() == 0)
   {
      if (in.getId() <0)
      {
        finishFlag=true; 
        errFlag=true; 
        more = false;
        return false;
      }
      SEventHandler::addInput (&in, this);
      if (trg) SEventHandler::addJob (this, trg);
      finishFlag = 0;
   }
   return true;
 }
 *s = buffer;
 buffer.clear();
 return true;
}

int
SReader::run ()
{
  if (finishFlag) return -1;
  return 0;
}

/*a*
 * This is for event target if t == 0
 */
bool
SReader::read (const SEventSource* s, const SString& m)
{
  buffer.append (m);
  // End of stream...
  if (m.size() == 0)
  {
    finishFlag = true;
    more = false;
    return false;
  }
  if (sepVector.size() != 0) 
  {
     while (true)
     {
        int found =-1;
        unsigned int i;
        for (i=0; i<sepVector.size(); i++) 
        {
           found = buffer.find (sepVector[i]);
           if (found>=0) break;
        }
        if (found < 0) break;
        unsigned int ind = found+sepVector[i].size();
        SString a(buffer.array(), ind);
        line.append (a);
        buffer.remove (0, ind);
     }
     // Job is done!
     if (line.size())
     {
        finishFlag = true;
        return false;
     }
  }
  return true;
}

/*a*
 * This is for event target fi t == 0
 */
void
SReader::error (const SEventSource* s)
{
  errFlag = true;
}

SWriter::SWriter (const SOutputStream& i, SEventTarget* t) : out(i)
{
  errFlag = false;
  finishFlag = true;
  writeCount = 0;
  trg = t;
  if (t)
  {
    SEventHandler::addJob (this, t);
  }
}

SWriter::~SWriter ()
{
}

bool
SWriter::write (const SString& s)
{
  if (errFlag) return false;
#ifdef USE_WINAPI
  if (out.getType()==SOutputStream::PIPE)
  {
    DWORD n;
    SString rem=s;
    while (rem.size()) {
      if (rem.size()==0) break;
      if (!WriteFile ((char*)out.getId(), rem.array(), rem.size(), &n, 0) 
          || n <= 0)
      {
        errFlag=true;
        break;
      }
      SString ns(rem.array(), n, rem.size()-n);
      rem = ns;
    }
    finishFlag = true;
    if (n<0) errFlag = true;
    return !errFlag;
  }
  else if (out.getType()==SOutputStream::FILE)
  {
    int n;
    SString rem=s;
    while (rem.size()) {
      if (rem.size()==0) break;
      n  = ::write ((int)out.getId(), rem.array(), rem.size()); 
      if (n<0) break;
      SString ns(rem.array(), n, rem.size()-n);
      rem = ns;
    }
    finishFlag = true;
    if (n<0) errFlag = true;
    return !errFlag;
  }
#endif
  if (finishFlag)
  {
    finishFlag = false;
    if (trg) SEventHandler::addJob (this, trg);
  }
  if (out.getId() <0)
  {
     finishFlag=true; 
     errFlag=true; 
     return false;
  }
  SEventHandler::addOutput (&out, this, s);
  writeCount++;
  finishFlag = false;
  // SGC finish
  if (trg) {
    return true;
  }
  if (trg==0)
  {
    bool n = true;
    do
    {
     n = SEventHandler::next();
     if (out.getId() <0)
     {
         finishFlag=true; 
         errFlag=true; 
         return false;
     }
    } while (n==true && finishFlag==false);
  }
  return !errFlag;
}

/**
 * This is as a job. Eent manager use 
 */
int
SWriter::run ()
{
  if (finishFlag) return -1;
  return 0;
}

/**
 * This is for event target fi t == 0
 */
bool
SWriter::write (const SEventSource* s)
{
  writeCount--;
  if (writeCount == 0) finishFlag = true;
  if (finishFlag) return false;
  return true;
}

/*a*
 * This is for event target fi t == 0
 */
void
SWriter::error (const SEventSource* s)
{
  errFlag = true; finishFlag = true;
}

bool
SWriter::isOK()
{
  return !errFlag;
}

bool
SWriter::close()
{
  return out.close();
}