Menu

[r24]: / trunk / uploader / libctb / src / iobase.cpp  Maximize  Restore  History

Download this file

212 lines (193 with data), 4.7 kB

  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
/////////////////////////////////////////////////////////////////////////////
// Name: iobase.cpp
// Purpose:
// Author: Joachim Buermann
// Id: $Id: iobase.cpp,v 1.1.1.1 2004/11/24 10:30:11 jb Exp $
// Copyright: (c) 2001 Joachim Buermann
// Licence: wxWindows licence
/////////////////////////////////////////////////////////////////////////////
#include <stdio.h>
#include <string.h>
#include "ctb-0.16/iobase.h"
#include "ctb-0.16/timer.h"
namespace ctb {
#define DELTA_BUFSIZE 512
int IOBase::Readv(char* buf,size_t len,unsigned int timeout_in_ms)
{
char *cp = buf;
int n = 0;
int timeout = 0;
size_t toread = len;
Timer t(timeout_in_ms,&timeout,NULL);
if(timeout_in_ms != 0xFFFFFFFF) {
t.start();
}
while(!timeout && (toread > 0)) {
if((n = Read(cp,toread)) < 0) {
break;
}
if(!n) {
sleepms(1);
}
toread -= n;
cp += n;
}
// ok, all bytes received
return(len - toread);
};
/*
Readv() calls the member function Read() repeatedly, til all
demand bytes were received. To avoid an endless loop, you
can refer an integer, which was set unequal zero after a
specific time. (See the timer class)
*/
int IOBase::Readv(char* buf,size_t len,int* timeout_flag,bool nice)
{
size_t toread = len;
int n = 0;
char *cp = buf;
while(toread > 0) {
if(timeout_flag && (*timeout_flag > 0)) {
return (len - toread);
}
if((n = Read(cp,toread)) < 0) {
return (len - toread);
}
if(!n && nice) {
sleepms(1);
}
if (n > 0)
{
toread -= n;
cp += n;
}
}
// ok, all bytes received
return(len - toread);
};
int IOBase::ReadUntilEOS(char*& readbuf,
size_t* readedBytes,
char* eosString,
long timeout_in_ms,
char quota)
{
int n = 0;
int timeout = 0;
int bufsize = DELTA_BUFSIZE;
int result = 0;
int quoted = 0;
char* buf = new char[bufsize];
char* des = buf;
char* eos = eosString;
char ch;
Timer t(timeout_in_ms,&timeout,NULL);
t.start();
while(!timeout) {
if(des >= &buf[bufsize]) {
// buffer full, realloc more memory
char* tmp = new char[bufsize + DELTA_BUFSIZE + 1];
memcpy(tmp,buf,bufsize);
delete[] buf;
buf = tmp;
des = &buf[bufsize];
bufsize += DELTA_BUFSIZE;
}
// read next byte
n = Read(&ch,1);
if(n < 0) {
// an error occured
result = -1;
break;
}
else if(n == 0) {
// no data available, give up the processor for some time
// to reduce the cpu last
sleepms(10);
continue;
}
// if eos is composed of more than one char, and the current
// byte doesn't match the next eos character, we handle the
// readed byte as a normal char (and not an eos)
if((eos != eosString) && (ch != *eos)) {
// FIXME!
// write all characters, which was matched the eos string
// until now (with the first wrong character all received
// eos characters are invalid and must handled as normal
// characters).
// This doesn't work right and is only a little workaround
// because the received eos chars are lost
PutBack(ch);
// because we doesn't match the eos string, we must 'reset'
// the eos match
eos = eosString;
continue;
}
else {
if((ch == *eos) && !quoted) {
if(*++eos == 0) {
// the eos string is complete
result = 1;
break;
}
continue;
}
}
if(ch == quota) {
quoted ^= 1;
}
*des++ = ch;
}
*des = 0;
readbuf = buf;
*readedBytes = des - buf;
return result;
};
int IOBase::Writev(char* buf,size_t len,unsigned int timeout_in_ms)
{
char *cp = buf;
int n = 0;
int timeout = 0;
size_t towrite = len;
Timer t(timeout_in_ms,&timeout,NULL);
if(timeout_in_ms != 0xFFFFFFFF) {
t.start();
}
while(!timeout && (towrite > 0)) {
if((n = Write(cp,towrite)) < 0) {
// an error occurs
break;
}
if(!n) {
sleepms(1);
}
towrite -= n;
cp += n;
}
return (len - towrite);
};
/*
Similar to Readv(). Writev() calls Write() repeatedly till
all bytes are written.
*/
int IOBase::Writev(char* buf,size_t len,int* timeout_flag,bool nice)
{
size_t towrite = len;
int n = 0;
char *cp = buf;
while(towrite > 0) {
if(timeout_flag && (*timeout_flag > 0)) {
return (len - towrite);
}
if((n = Write(cp,towrite)) < 0) {
// an error occurs
return (len - towrite);
}
if(!n && nice) {
sleepms(1);
}
towrite -= n;
cp += n;
}
return(len);
};
} // namespace ctb
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.