CoreFW  Check-in [6893b9f5ab]

Overview
Comment:Rename eof to at_end.

This is more fitting as end of file is not really true for a socket.

Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 6893b9f5aba838c6b48d8b6b0ffbfe8118627baa1d151dbf5051329c30f8986a
User & Date: js on 2012-09-30 01:34:46
Other Links: manifest | tags
Context
2012-09-30
01:46
Add a few new string functions. check-in: 499faa2771 user: js tags: trunk
01:34
Rename eof to at_end. check-in: 6893b9f5ab user: js tags: trunk
01:10
Make glibc happy. check-in: f1c55cc298 user: js tags: trunk
Changes

Modified src/file.c from [8363b05eea] to [7ba5522f8b].

53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#endif

#define DEFAULT_MODE S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH

struct CFWFile {
	CFWStream stream;
	int fd;
	bool eof;
};

static int
parse_mode(const char *mode)
{
	if (!strcmp(mode, "r"))
		return O_RDONLY;







|







53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#endif

#define DEFAULT_MODE S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH

struct CFWFile {
	CFWStream stream;
	int fd;
	bool at_end;
};

static int
parse_mode(const char *mode)
{
	if (!strcmp(mode, "r"))
		return O_RDONLY;
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
static ssize_t
file_read(void *ptr, void *buf, size_t len)
{
	CFWFile *file = ptr;
	ssize_t ret;

	if ((ret = read(file->fd, buf, len)) == 0)
		file->eof = true;

	return ret;
}

static bool
file_write(void *ptr, const void *buf, size_t len)
{
	CFWFile *file = ptr;
	ssize_t ret;

	if ((ret = write(file->fd, buf, len)) < len)
		return false;

	return true;
}

static bool
file_eof(void *ptr)
{
	CFWFile *file = ptr;

	return file->eof;
}

static void
file_close(void *ptr)
{
	CFWFile *file = ptr;

	close(file->fd);
}

static struct cfw_stream_ops stream_ops = {
	.read = file_read,
	.write = file_write,
	.eof = file_eof,
	.close = file_close
};

static bool
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 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;








|

















|



|













|













|







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
static ssize_t
file_read(void *ptr, void *buf, size_t len)
{
	CFWFile *file = ptr;
	ssize_t ret;

	if ((ret = read(file->fd, buf, len)) == 0)
		file->at_end = true;

	return ret;
}

static bool
file_write(void *ptr, const void *buf, size_t len)
{
	CFWFile *file = ptr;
	ssize_t ret;

	if ((ret = write(file->fd, buf, len)) < len)
		return false;

	return true;
}

static bool
file_at_end(void *ptr)
{
	CFWFile *file = ptr;

	return file->at_end;
}

static void
file_close(void *ptr)
{
	CFWFile *file = ptr;

	close(file->fd);
}

static struct cfw_stream_ops stream_ops = {
	.read = file_read,
	.write = file_write,
	.at_end = file_at_end,
	.close = file_close
};

static bool
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 file in case we error out */
	cfw_stream->ctor(ptr, args);
	file->at_end = false;

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

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

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
		.obj = {
			.cls = &class,
			.ref_cnt = INT_MAX
		},
		.ops = &stream_ops
	},
	.fd = 0,
	.eof = false
};
static CFWFile cfw_stdout_ = {
	.stream = {
		.obj = {
			.cls = &class,
			.ref_cnt = INT_MAX
		},
		.ops = &stream_ops
	},
	.fd = 1,
	.eof = false
};
static CFWFile cfw_stderr_ = {
	.stream = {
		.obj = {
			.cls = &class,
			.ref_cnt = INT_MAX
		},
		.ops = &stream_ops
	},
	.fd = 2,
	.eof = false
};
CFWFile *cfw_stdin = &cfw_stdin_;
CFWFile *cfw_stdout = &cfw_stdout_;
CFWFile *cfw_stderr = &cfw_stderr_;







|










|










|




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
		.obj = {
			.cls = &class,
			.ref_cnt = INT_MAX
		},
		.ops = &stream_ops
	},
	.fd = 0,
	.at_end = false
};
static CFWFile cfw_stdout_ = {
	.stream = {
		.obj = {
			.cls = &class,
			.ref_cnt = INT_MAX
		},
		.ops = &stream_ops
	},
	.fd = 1,
	.at_end = false
};
static CFWFile cfw_stderr_ = {
	.stream = {
		.obj = {
			.cls = &class,
			.ref_cnt = INT_MAX
		},
		.ops = &stream_ops
	},
	.fd = 2,
	.at_end = false
};
CFWFile *cfw_stdin = &cfw_stdin_;
CFWFile *cfw_stdout = &cfw_stdout_;
CFWFile *cfw_stderr = &cfw_stderr_;

Modified src/stream.c from [c9268ffce9] to [316847ca63].

137
138
139
140
141
142
143
144
145
146
147
148
149
150
151

	/* 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;








|







137
138
139
140
141
142
143
144
145
146
147
148
149
150
151

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

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

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

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

			ret_len = stream->cache_len;

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
	}

	free(tmp);
	return true;
}

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)
{
	CFWStream *stream = ptr;








|









|







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
	}

	free(tmp);
	return true;
}

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

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

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

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

void
cfw_stream_close(void *ptr)
{
	CFWStream *stream = ptr;

Modified src/stream.h from [677d5c6a56] to [bcb155f507].

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
#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_write_line(void*, const char*);
extern bool cfw_stream_eof(void*);
extern void cfw_stream_close(void*);
#endif







|
















|


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
#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 (*at_end)(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_write_line(void*, const char*);
extern bool cfw_stream_at_end(void*);
extern void cfw_stream_close(void*);
#endif

Modified src/tcpsocket.c from [cad757af38] to [ba08fae5df].

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

#include "stream.h"
#include "tcpsocket.h"

struct CFWTCPSocket {
	CFWStream stream;
	int fd;
	bool eof;
};

static ssize_t
sock_read(void *ptr, void *buf, size_t len)
{
	CFWTCPSocket *sock = ptr;
	ssize_t ret;

	if ((ret = recv(sock->fd, buf, len, 0)) == 0)
		sock->eof = true;

	return ret;
}

static bool
sock_write(void *ptr, const void *buf, size_t len)
{
	CFWTCPSocket *sock = ptr;
	ssize_t ret;

	if ((ret = send(sock->fd, buf, len, 0)) < len)
		return false;

	return true;
}

static bool
sock_eof(void *ptr)
{
	CFWTCPSocket *sock = ptr;

	return sock->eof;
}

static void
sock_close(void *ptr)
{
	CFWTCPSocket *sock = ptr;

	if (sock->fd != -1)
		close(sock->fd);
}

static struct cfw_stream_ops stream_ops = {
	.read = sock_read,
	.write = sock_write,
	.eof = sock_eof,
	.close = sock_close
};

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







|









|

















|



|














|












|







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

#include "stream.h"
#include "tcpsocket.h"

struct CFWTCPSocket {
	CFWStream stream;
	int fd;
	bool at_end;
};

static ssize_t
sock_read(void *ptr, void *buf, size_t len)
{
	CFWTCPSocket *sock = ptr;
	ssize_t ret;

	if ((ret = recv(sock->fd, buf, len, 0)) == 0)
		sock->at_end = true;

	return ret;
}

static bool
sock_write(void *ptr, const void *buf, size_t len)
{
	CFWTCPSocket *sock = ptr;
	ssize_t ret;

	if ((ret = send(sock->fd, buf, len, 0)) < len)
		return false;

	return true;
}

static bool
sock_at_end(void *ptr)
{
	CFWTCPSocket *sock = ptr;

	return sock->at_end;
}

static void
sock_close(void *ptr)
{
	CFWTCPSocket *sock = ptr;

	if (sock->fd != -1)
		close(sock->fd);
}

static struct cfw_stream_ops stream_ops = {
	.read = sock_read,
	.write = sock_write,
	.at_end = sock_at_end,
	.close = sock_close
};

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->at_end = false;

	return true;
}

static void
dtor(void *ptr)
{