LMMS
Loading...
Searching...
No Matches
byte_source.c
Go to the documentation of this file.
1/*
2 Copyright 2011-2020 David Robillard <d@drobilla.net>
3
4 Permission to use, copy, modify, and/or distribute this software for any
5 purpose with or without fee is hereby granted, provided that the above
6 copyright notice and this permission notice appear in all copies.
7
8 THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15*/
16
17#include "byte_source.h"
18
19#include "system.h"
20
21#include "serd/serd.h"
22
23#include <stdbool.h>
24#include <stdint.h>
25#include <string.h>
26
29{
30 source->read_head = 0;
31 const size_t n_read =
32 source->read_func(source->file_buf, 1, source->page_size, source->stream);
33
34 if (n_read == 0) {
35 source->file_buf[0] = '\0';
36 source->eof = true;
37 return (source->error_func(source->stream) ? SERD_ERR_UNKNOWN
38 : SERD_FAILURE);
39 }
40
41 if (n_read < source->page_size) {
42 source->file_buf[n_read] = '\0';
43 source->buf_size = n_read;
44 }
45
46 return SERD_SUCCESS;
47}
48
51 SerdSource read_func,
52 SerdStreamErrorFunc error_func,
53 void* stream,
54 const uint8_t* name,
55 size_t page_size)
56{
57 const Cursor cur = {name, 1, 1};
58
59 memset(source, '\0', sizeof(*source));
60 source->stream = stream;
61 source->from_stream = true;
62 source->page_size = page_size;
63 source->buf_size = page_size;
64 source->cur = cur;
65 source->error_func = error_func;
66 source->read_func = read_func;
67
68 if (page_size > 1) {
69 source->file_buf = (uint8_t*)serd_allocate_buffer(page_size);
70 source->read_buf = source->file_buf;
71 memset(source->file_buf, '\0', page_size);
72 } else {
73 source->read_buf = &source->read_byte;
74 }
75
76 return SERD_SUCCESS;
77}
78
81{
82 source->prepared = true;
83
84 if (source->from_stream) {
85 return (source->page_size > 1 ? serd_byte_source_page(source)
86 : serd_byte_source_advance(source));
87 }
88
89 return SERD_SUCCESS;
90}
91
94{
95 const Cursor cur = {(const uint8_t*)"(string)", 1, 1};
96
97 memset(source, '\0', sizeof(*source));
98 source->cur = cur;
99 source->read_buf = utf8;
100 return SERD_SUCCESS;
101}
102
105{
106 if (source->page_size > 1) {
108 }
109
110 memset(source, '\0', sizeof(*source));
111 return SERD_SUCCESS;
112}
SerdStatus serd_byte_source_open_source(SerdByteSource *source, SerdSource read_func, SerdStreamErrorFunc error_func, void *stream, const uint8_t *name, size_t page_size)
Definition byte_source.c:50
SerdStatus serd_byte_source_close(SerdByteSource *source)
Definition byte_source.c:104
SerdStatus serd_byte_source_open_string(SerdByteSource *source, const uint8_t *utf8)
Definition byte_source.c:93
SerdStatus serd_byte_source_prepare(SerdByteSource *source)
Definition byte_source.c:80
SerdStatus serd_byte_source_page(SerdByteSource *source)
Definition byte_source.c:28
static SerdStatus serd_byte_source_advance(SerdByteSource *source)
Definition byte_source.h:81
static const char * name
Definition pugl.h:1582
int(* SerdStreamErrorFunc)(void *SERD_NONNULL stream)
Definition serd.h:331
size_t(* SerdSource)(void *SERD_NONNULL buf, size_t size, size_t nmemb, void *SERD_NONNULL stream)
Definition serd.h:345
SerdStatus
Return status code.
Definition serd.h:100
@ SERD_FAILURE
Non-fatal failure.
Definition serd.h:102
@ SERD_ERR_UNKNOWN
Unknown error.
Definition serd.h:103
@ SERD_SUCCESS
No error.
Definition serd.h:101
unsigned char uint8_t
Definition mid.cpp:98
Definition byte_source.h:28
Definition byte_source.h:34
uint8_t * file_buf
Buffer iff reading pages from a file.
Definition byte_source.h:41
size_t read_head
Offset into read_buf.
Definition byte_source.h:43
size_t page_size
Number of bytes to read at a time.
Definition byte_source.h:38
bool prepared
True iff prepared for reading.
Definition byte_source.h:46
const uint8_t * read_buf
Pointer to file_buf or read_byte.
Definition byte_source.h:42
Cursor cur
Cursor for error reporting.
Definition byte_source.h:40
SerdStreamErrorFunc error_func
Error function (e.g. ferror).
Definition byte_source.h:36
void * stream
Stream (e.g. FILE).
Definition byte_source.h:37
bool from_stream
True iff reading from stream.
Definition byte_source.h:45
bool eof
True iff end of file reached.
Definition byte_source.h:47
SerdSource read_func
Read function (e.g. fread).
Definition byte_source.h:35
uint8_t read_byte
1-byte 'buffer' used when not paging
Definition byte_source.h:44
size_t buf_size
Number of bytes in file_buf.
Definition byte_source.h:39
void * serd_allocate_buffer(const size_t size)
Allocate an aligned buffer for I/O.
Definition system.c:69
void serd_free_aligned(void *const ptr)
Free a buffer allocated with an aligned allocation function.
Definition system.c:75