summaryrefslogtreecommitdiff
path: root/tool_source.c
blob: cbba76ca34cbcf87587ac78430ce1193e016e5e5 (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
#include "tool_source.h"
#include "tool_select.h"

#include <vdr/tools.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>

cTBSource::cTBSource(void) {
	m_BytesRead = 0;
	m_BytesWritten = 0;
	m_Filed = -1;
}

bool cTBSource::Open(int Filed, bool IsUnixFd) {
	if (IsOpen())
		Close();

	m_Filed = Filed;
	if (IsUnixFd && ::fcntl(m_Filed, F_SETFL, O_NONBLOCK) == -1)
		return false;

	return true;
}

cTBSource::~cTBSource() {
}

bool cTBSource::Close(void) {
	if (!IsOpen()) {
		errno = EBADF;
		return false;
	}

	m_Filed = -1;
	return true;
}

ssize_t cTBSource::Read(void *Buffer, size_t Length) {
	ssize_t res;
	while ((res = SysRead(Buffer, Length)) < 0 && errno == EINTR)
		errno = 0;
	if (res > 0) m_BytesRead += res;
	return res;
}

ssize_t cTBSource::Write(const void *Buffer, size_t Length) {
	ssize_t res;
	while ((res = SysWrite(Buffer, Length)) < 0 && errno == EINTR)
		errno = 0;
	if (res > 0) m_BytesWritten += res;
	return res;
}

bool cTBSource::TimedWrite(const void *Buffer, size_t Length, uint TimeoutMs) {
	cTBSelect sel;
	int ms, offs;

	cTimeMs starttime;
	ms = TimeoutMs;
	offs = 0;
	while (Length > 0) {
		int b;

		sel.Clear();
		sel.Add(m_Filed, true);
		if (sel.Select(ms) == -1)
			return false;

		if (sel.CanWrite(m_Filed)) {
			if ((b = Write((char*)Buffer + offs, Length)) == -1)
				return false;
			offs += b;
			Length -= b;
		}

		ms = TimeoutMs - starttime.Elapsed();
		if (ms <= 0) {
			errno = ETIMEDOUT;
			return false;
		}
	}
	return true;
}

bool cTBSource::SafeWrite(const void *Buffer, size_t Length) {
	cTBSelect sel;
	int offs;

	offs = 0;
	while (Length > 0) {
		int b;

		sel.Clear();
		sel.Add(m_Filed, true);
		if (sel.Select() == -1)
			return false;

		if (sel.CanWrite(m_Filed)) {
			if ((b = Write((char*)Buffer + offs, Length)) == -1)
				return false;
			offs += b;
			Length -= b;
		}
		
	}
	return true;
}

ssize_t cTBSource::ReadUntil(void *Buffer, size_t Length, const char *Seq,
		uint TimeoutMs) {
	int ms;
	size_t len;
	cTBSelect sel;

	if ((len = m_LineBuffer.find(Seq)) != (size_t)-1) {
		if (len > Length) {
			errno = ENOBUFS;
			return -1;
		}
		memcpy(Buffer, m_LineBuffer.data(), len);
		m_LineBuffer.erase(0, len + strlen(Seq));
		//Dprintf("ReadUntil: Served from Linebuffer: %d, |%.*s|\n", len, len - 1,
		//		(char*)Buffer);
		return len;
	}

	cTimeMs starttime;
	ms = TimeoutMs;
	while (m_LineBuffer.size() < BUFSIZ) {
		sel.Clear();
		sel.Add(m_Filed, false);

		if (sel.Select(ms) == -1)
			return -1;
		
		if (sel.CanRead(m_Filed)) {
			int b;

			len = m_LineBuffer.size();
			m_LineBuffer.resize(BUFSIZ);
			if ((b = Read((char*)m_LineBuffer.data() + len, BUFSIZ - len)) == -1)
				return -1;
			m_LineBuffer.resize(len + b);

			if ((len = m_LineBuffer.find(Seq)) != (size_t)-1) {
				if (len > Length) {
					errno = ENOBUFS;
					return -1;
				}
				memcpy(Buffer, m_LineBuffer.data(), len);
				m_LineBuffer.erase(0, len + strlen(Seq));
				//Dprintf("ReadUntil: Served from Linebuffer: %d, |%.*s|\n", len, len - 1,
				//		(char*)Buffer);
				return len;
			}
		}

		ms = TimeoutMs - starttime.Elapsed();
		if (ms <= 0) {
			errno = ETIMEDOUT;
			return -1;
		}
	}
	errno = ENOBUFS;
	return -1;
}