diff --git a/io/include/blue/io/file.h b/io/include/blue/io/file.h index b4d5dca..bd55cda 100644 --- a/io/include/blue/io/file.h +++ b/io/include/blue/io/file.h @@ -60,7 +60,6 @@ BLUE_API b_result b_file_open_temp(b_file_mode mode, b_file **out); BLUE_API b_result b_file_open_shadow( b_file *original, b_file_mode mode, b_file **out); -BLUE_API b_status b_file_open_stream(b_file *file, b_stream **out); BLUE_API b_status b_file_stat(b_file *file, b_file_info *out); BLUE_API b_status b_file_size(b_file *file, size_t *out_len); BLUE_API b_status b_file_cursor(b_file *file, size_t *out_pos); diff --git a/io/sys/darwin/file.c b/io/sys/darwin/file.c index bade5bf..3cb757f 100644 --- a/io/sys/darwin/file.c +++ b/io/sys/darwin/file.c @@ -16,14 +16,13 @@ #define CHECK_FLAG(v, f) (((v) & (f)) == (f)) -static enum b_status stream_close(struct b_stream *); -static enum b_status stream_getc(struct b_stream *, int *); -static enum b_status stream_read( - struct b_stream *, unsigned char *, size_t, size_t *); +static enum b_status stream_close(b_stream *); +static enum b_status stream_getc(b_stream *, int *); +static enum b_status stream_read(b_stream *, unsigned char *, size_t, size_t *); static enum b_status stream_write( - struct b_stream *, const unsigned char *, size_t, size_t *); -static enum b_status stream_seek(struct b_stream *, long long, b_stream_seek_origin); -static enum b_status stream_tell(const struct b_stream *, size_t *); + b_stream *, const unsigned char *, size_t, size_t *); +static enum b_status stream_seek(b_stream *, long long, b_stream_seek_origin); +static enum b_status stream_tell(const b_stream *, size_t *); /*** PRIVATE DATA *************************************************************/ @@ -112,42 +111,6 @@ static const b_path *file_path(const struct b_file_p *file) return file->path; } -static enum b_status file_open_stream( - struct b_file_p *file, b_file *obj, struct b_stream **out) -{ - struct b_stream *stream = malloc(sizeof *stream); - if (!stream) { - return B_ERR_NO_MEMORY; - } - - memset(stream, 0x0, sizeof *stream); - - if (file->mode & B_FILE_READ_ONLY) { - stream->s_mode |= B_STREAM_READ; - } - - if (file->mode & B_FILE_WRITE_ONLY) { - stream->s_mode |= B_STREAM_WRITE; - } - - if (file->mode & B_FILE_BINARY) { - stream->s_mode |= B_STREAM_BINARY; - } - - stream->s_ptr0 = b_file_ref(obj); - stream->s_ptr1 = file; - stream->s_close = stream_close; - stream->s_getc = stream_getc; - stream->s_read = stream_read; - stream->s_write = stream_write; - stream->s_seek = stream_seek; - stream->s_tell = stream_tell; - - *out = stream; - - return B_SUCCESS; -} - static enum b_status file_stat(struct b_file_p *file, struct b_file_info *out) { struct stat st; @@ -326,22 +289,19 @@ static enum b_status file_write( /*** STREAM FUNCTIONS *********************************************************/ -static enum b_status stream_close(struct b_stream *stream) +static enum b_status stream_close(b_stream *stream) { - b_file *file = stream->s_ptr0; - b_file_unref(file); - return B_SUCCESS; } -static enum b_status stream_getc(struct b_stream *stream, int *out) +static enum b_status stream_getc(b_stream *stream, int *out) { - struct b_file_p *file = stream->s_ptr1; + struct b_file_p *file = b_object_get_private(stream, B_TYPE_FILE); char c; size_t nr_read = 0; enum b_status status - = file_read(file, stream->s_cursor, sizeof c, &c, &nr_read); + = file_read(file, B_OFFSET_CURRENT, sizeof c, &c, &nr_read); if (status != B_SUCCESS) { return status; } @@ -350,40 +310,34 @@ static enum b_status stream_getc(struct b_stream *stream, int *out) return B_ERR_NO_DATA; } - stream->s_cursor += nr_read; *out = c; return B_SUCCESS; } static enum b_status stream_read( - struct b_stream *stream, unsigned char *buf, size_t max, size_t *nr_read) + b_stream *stream, unsigned char *buf, size_t max, size_t *nr_read) { - struct b_file_p *file = stream->s_ptr1; + struct b_file_p *file = b_object_get_private(stream, B_TYPE_FILE); enum b_status status - = file_read(file, stream->s_cursor, max, buf, nr_read); - - stream->s_cursor += *nr_read; + = file_read(file, B_OFFSET_CURRENT, max, buf, nr_read); return status; } static enum b_status stream_write( - struct b_stream *stream, const unsigned char *buf, size_t count, - size_t *nr_written) + b_stream *stream, const unsigned char *buf, size_t count, size_t *nr_written) { - struct b_file_p *file = stream->s_ptr1; + struct b_file_p *file = b_object_get_private(stream, B_TYPE_FILE); enum b_status status - = file_write(file, stream->s_cursor, count, buf, nr_written); - - stream->s_cursor += *nr_written; + = file_write(file, B_OFFSET_CURRENT, count, buf, nr_written); return status; } static enum b_status stream_seek( - struct b_stream *stream, long long offset, b_stream_seek_origin origin) + b_stream *stream, long long offset, b_stream_seek_origin origin) { b_seek_basis basis; switch (origin) { @@ -400,19 +354,14 @@ static enum b_status stream_seek( return B_ERR_INVALID_ARGUMENT; } - struct b_file_p *file = stream->s_ptr1; + struct b_file_p *file = b_object_get_private(stream, B_TYPE_FILE); - enum b_status status = file_seek(file, offset, basis); - if (!B_OK(status)) { - return status; - } - - return file_cursor(file, &stream->s_cursor); + return file_seek(file, offset, basis); } -static enum b_status stream_tell(const struct b_stream *stream, size_t *pos) +static enum b_status stream_tell(const b_stream *stream, size_t *pos) { - const struct b_file_p *file = stream->s_ptr1; + const struct b_file_p *file = b_object_get_private(stream, B_TYPE_FILE); off_t v = lseek(file->fd, 0, SEEK_CUR); if (v == (off_t)-1) { return b_status_from_errno(errno, B_ERR_IO_FAILURE); @@ -473,6 +422,19 @@ b_result b_file_open( } struct b_file_p *p = b_object_get_private(file, B_TYPE_FILE); + b_stream_cfg *cfg = b_object_get_protected(file, B_TYPE_STREAM); + + if (mode & B_FILE_READ_ONLY) { + cfg->s_mode |= B_STREAM_READ; + } + + if (mode & B_FILE_WRITE_ONLY) { + cfg->s_mode |= B_STREAM_WRITE; + } + + if (mode & B_FILE_BINARY) { + cfg->s_mode |= B_STREAM_BINARY; + } p->fd = fd; p->path = abs_path; @@ -519,11 +481,6 @@ const b_path *b_file_path(const b_file *file) B_CLASS_DISPATCH_STATIC_0(B_TYPE_FILE, file_path, file); } -enum b_status b_file_open_stream(b_file *file, struct b_stream **out) -{ - B_CLASS_DISPATCH_STATIC(B_TYPE_FILE, file_open_stream, file, file, out); -} - enum b_status b_file_stat(b_file *file, struct b_file_info *out) { B_CLASS_DISPATCH_STATIC(B_TYPE_FILE, file_stat, file, out); @@ -595,10 +552,20 @@ B_TYPE_CLASS_DEFINITION_BEGIN(b_file) B_TYPE_CLASS_INTERFACE_BEGIN(b_object, B_TYPE_OBJECT) B_INTERFACE_ENTRY(to_string) = NULL; B_TYPE_CLASS_INTERFACE_END(b_object, B_TYPE_OBJECT) + + B_TYPE_CLASS_INTERFACE_BEGIN(b_stream, B_TYPE_STREAM) + B_INTERFACE_ENTRY(s_close) = stream_close; + B_INTERFACE_ENTRY(s_getc) = stream_getc; + B_INTERFACE_ENTRY(s_read) = stream_read; + B_INTERFACE_ENTRY(s_write) = stream_write; + B_INTERFACE_ENTRY(s_seek) = stream_seek; + B_INTERFACE_ENTRY(s_tell) = stream_tell; + B_TYPE_CLASS_INTERFACE_END(b_stream, B_TYPE_STREAM) B_TYPE_CLASS_DEFINITION_END(b_file) B_TYPE_DEFINITION_BEGIN(b_file) B_TYPE_ID(0x495a73f6, 0xb8c3, 0x4e17, 0xb5f4, 0x6fc321f67c7b); + B_TYPE_EXTENDS(B_TYPE_STREAM); B_TYPE_CLASS(b_file_class); B_TYPE_INSTANCE_PRIVATE(struct b_file_p); B_TYPE_INSTANCE_INIT(file_init);