CoreFW  Check-in [2cee5ea2d5]

Overview
Comment:Add cfw_stream_read_line().

This also adds a caching infrastructure to cfw_stream.

Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 2cee5ea2d537c745497b002efec0588992b836af737e739d1ed02b53e0bf25be
User & Date: js on 2012-09-30 01:01:37
Other Links: manifest | tags
Context
2012-09-30
01:02
Add cfw_stream_write_line(). check-in: 168ba16eda user: js tags: trunk
01:01
Add cfw_stream_read_line(). check-in: 2cee5ea2d5 user: js tags: trunk
00:59
Fix cfw_strndup() and add cfw_strnlen(). check-in: 3247812f3b user: js tags: trunk
Changes

Modified src/file.c from [6570f4dc53] to [222c0e621d].

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
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







-
-
+
+
















-
+







ctor(void *ptr, va_list args)
{
	CFWFile *file = ptr;
	const char *path = va_arg(args, const char*);
	const char *mode = va_arg(args, const char*);
	int flags;

	/* Make sure we have a valid pointer in case we error out */
	file->stream.ops = NULL;
	/* Make sure we have a valid file in case we error out */
	cfw_stream->ctor(ptr, args);
	file->eof = false;

	if ((flags = parse_mode(mode)) == -1)
		return false;

	if ((file->fd = open(path, flags, DEFAULT_MODE)) == -1)
		return false;

	file->stream.ops = &stream_ops;

	return true;
}

static void
dtor(void *ptr)
{
	cfw_stream_close(ptr);
	cfw_stream->dtor(ptr);
}

static CFWClass class = {
	.name = "CFWFile",
	.size = sizeof(CFWFile),
	.ctor = ctor,
	.dtor = dtor

Modified src/stream.c from [5dca9bcc2f] to [7f07dd8ac4].

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
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







+



+
+







+
+



















+
-
-
+
+

-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+







 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include <stdlib.h>
#include <string.h>

#include "stream.h"

#define BUFFER_SIZE 4096

static bool
ctor(void *ptr, va_list args)
{
	CFWStream *stream = ptr;

	stream->ops = NULL;
	stream->cache = NULL;
	stream->cache_len = 0;

	return true;
}

static void
dtor(void *ptr)
{
	cfw_stream_close(ptr);
}

ssize_t
cfw_stream_read(void *ptr, void *buf, size_t len)
{
	CFWStream *stream = ptr;
	ssize_t ret;

	if (stream == NULL || stream->ops == NULL)
		return -1;

	if (stream->cache == NULL) {
	if ((ret = stream->ops->read(stream, buf, len)) < -1)
		ret = -1;
		if ((ret = stream->ops->read(stream, buf, len)) < -1)
			ret = -1;

	return ret;
		return ret;
	}

	if (len >= stream->cache_len) {
		ret = stream->cache_len;

		memcpy(buf, stream->cache, stream->cache_len);

		free(stream->cache);
		stream->cache = NULL;
		stream->cache_len = 0;

		return ret;
	} else {
		char *tmp;

		if ((tmp = malloc(stream->cache_len - len)) == NULL)
			return -1;
		memcpy(tmp, stream->cache + len, stream->cache_len - len);
		memcpy(buf, stream->cache, len);

		free(stream->cache);
		stream->cache = tmp;
		stream->cache_len -= len;

		return len;
	}
}

CFWString*
cfw_stream_read_line(void *ptr)
{
	CFWStream *stream = ptr;
	CFWString *ret;
	char *buf, *ret_str, *new_cache;
	ssize_t buf_len;
	size_t i, ret_len;

	/* Look if there is a line or \0 in our cache */
	if (stream->cache != NULL) {
		for (i = 0; i < stream->cache_len; i++) {
			if (stream->cache[i] == '\n' ||
			    stream->cache[i] == '\0') {
				ret_len = i;
				if (i > 0 && stream->cache[i - 1] == '\r')
					ret_len--;

				ret_str = cfw_strndup(stream->cache, ret_len);
				if (ret_str == NULL)
					return NULL;

				ret = cfw_create(cfw_string, NULL);
				if (ret == NULL) {
					free(ret_str);
					return NULL;
				}
				cfw_string_set_nocopy(ret, ret_str, ret_len);

				new_cache = malloc(stream->cache_len - i - 1);
				if (new_cache == NULL)
					return NULL;
				memcpy(new_cache, stream->cache + i + 1,
				    stream->cache_len - i - 1);

				free(stream->cache);
				stream->cache = new_cache;
				stream->cache_len -= i + 1;

				return ret;
			}
		}
	}

	/* Read and see if we get a newline or \0 */

	if ((buf = malloc(BUFFER_SIZE)) == NULL)
		return NULL;

	for (;;) {
		if (stream->ops->eof(stream)) {
			free(buf);

			if (stream->cache == NULL)
				return NULL;

			ret_len = stream->cache_len;

			if (ret_len > 0 && stream->cache[ret_len - 1] == '\r')
				ret_len--;

			ret_str = cfw_strndup(stream->cache, ret_len);
			if (ret_str == NULL)
				return NULL;

			ret = cfw_create(cfw_string, NULL);
			if (ret == NULL) {
				free(ret_str);
				return NULL;
			}
			cfw_string_set_nocopy(ret, ret_str, ret_len);

			free(stream->cache);
			stream->cache = NULL;
			stream->cache_len = 0;

			return ret;
		}

		buf_len = stream->ops->read(stream, buf, BUFFER_SIZE);
		if (buf_len == -1) {
			free(buf);
			return NULL;
		}

		/* Look if there's a newline or \0 */
		for (i = 0; i < buf_len; i++) {
			if (buf[i] == '\n' || buf[i] == '\0') {
				ret_len = stream->cache_len + i;

				if ((ret_str = malloc(ret_len + 1)) == NULL) {
					/*
					 * FIXME: We lost the current buffer.
					 *	  Mark the stream as broken?
					 */
					free(buf);
					return NULL;
				}
				memcpy(ret_str, stream->cache,
				    stream->cache_len);
				memcpy(ret_str + stream->cache_len, buf, i);
				if (ret_len > 0 && ret_str[ret_len - 1] == '\r')
					ret_len--;
				ret_str[ret_len] = '\0';

				ret = cfw_create(cfw_string, NULL);
				if (ret == NULL) {
					free(buf);
					free(ret_str);
					return NULL;
				}
				cfw_string_set_nocopy(ret, ret_str, ret_len);

				new_cache = malloc(buf_len - i - 1);
				if (new_cache == NULL) {
					free(buf);
					return NULL;
				}
				memcpy(new_cache, buf + i + 1, buf_len - i - 1);

				free(stream->cache);
				stream->cache = new_cache;
				stream->cache_len = buf_len - i - 1;

				free(buf);
				return ret;
			}
		}

		/* There was no newline or \0 */
		new_cache = realloc(stream->cache, stream->cache_len + buf_len);
		if (new_cache == NULL) {
			free(buf);
			return NULL;
		}
		memcpy(new_cache + stream->cache_len, buf, buf_len);
		stream->cache = new_cache;
		stream->cache_len += buf_len;
	}
}

bool
cfw_stream_write(void *ptr, const void *buf, size_t len)
{
	CFWStream *stream = ptr;

79
80
81
82
83
84
85



86
87
88
89
90
91
92
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268







+
+
+







bool
cfw_stream_eof(void *ptr)
{
	CFWStream *stream = ptr;

	if (stream == NULL || stream->ops == NULL)
		return true;

	if (stream->cache != NULL)
		return false;

	return stream->ops->eof(stream);
}

void
cfw_stream_close(void *ptr)
{

Modified src/stream.h from [80dfac9647] to [768e2d2979].

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
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







+











+
+




+





#ifndef __COREFW_STREAM_H__
#define __COREFW_STREAM_H__

#include <unistd.h>

#include "class.h"
#include "object.h"
#include "string.h"

struct cfw_stream_ops {
	ssize_t (*read)(void*, void*, size_t);
	bool (*write)(void*, const void*, size_t);
	bool (*eof)(void*);
	void (*close)(void*);
};

typedef struct CFWStream {
	CFWObject obj;
	struct cfw_stream_ops *ops;
	char *cache;
	size_t cache_len;
} CFWStream;

extern CFWClass *cfw_stream;
extern ssize_t cfw_stream_read(void*, void*, size_t);
extern CFWString* cfw_stream_read_line(void*);
extern bool cfw_stream_write(void*, const void*, size_t);
extern bool cfw_stream_write_string(void*, const char*);
extern bool cfw_stream_eof(void*);
extern void cfw_stream_close(void*);
#endif

Modified src/tcpsocket.c from [8af7b73d7d] to [f4d3eaafb5].

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
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







+
+










-
+







};

static bool
ctor(void *ptr, va_list args)
{
	CFWTCPSocket *sock = ptr;

	cfw_stream->ctor(ptr, args);

	sock->fd = -1;
	sock->stream.ops = &stream_ops;
	sock->eof = false;

	return true;
}

static void
dtor(void *ptr)
{
	cfw_stream_close(ptr);
	cfw_stream->dtor(ptr);
}

bool
cfw_tcpsocket_connect(CFWTCPSocket *sock, const char *host, uint16_t port)
{
	struct addrinfo hints, *res, *res0;
	char portstr[7];