Coverage Report

Created: 2025-07-11 06:13

/src/readstat/src/test/test_buffer_io.c
Line
Count
Source (jump to first uncovered line)
1
#include <stdlib.h>
2
3
#include "../readstat.h"
4
5
#include "test_buffer.h"
6
#include "test_buffer_io.h"
7
8
20.2k
int rt_open_handler(const char *path, void *io_ctx) {
9
20.2k
    return 0;
10
20.2k
}
11
12
20.2k
int rt_close_handler(void *io_ctx) {
13
20.2k
    return 0;
14
20.2k
}
15
16
readstat_off_t rt_seek_handler(readstat_off_t offset,
17
500k
        readstat_io_flags_t whence, void *io_ctx) {
18
500k
    rt_buffer_ctx_t *buffer_ctx = (rt_buffer_ctx_t *)io_ctx;
19
500k
    readstat_off_t newpos = -1;
20
500k
    if (whence == READSTAT_SEEK_SET) {
21
142k
        newpos = offset;
22
358k
    } else if (whence == READSTAT_SEEK_CUR) {
23
339k
        newpos = buffer_ctx->pos + offset;
24
339k
    } else if (whence == READSTAT_SEEK_END) {
25
18.6k
        newpos = buffer_ctx->buffer->used + offset;
26
18.6k
    }
27
28
500k
    if (newpos < 0)
29
330
        return -1;
30
31
500k
    if (newpos > buffer_ctx->buffer->used)
32
937
        return -1;
33
34
499k
    buffer_ctx->pos = newpos;
35
499k
    return newpos;
36
500k
}
37
38
12.6M
ssize_t rt_read_handler(void *buf, size_t nbytes, void *io_ctx) {
39
12.6M
    rt_buffer_ctx_t *buffer_ctx = (rt_buffer_ctx_t *)io_ctx;
40
12.6M
    ssize_t bytes_copied = 0;
41
12.6M
    ssize_t bytes_left = buffer_ctx->buffer->used - buffer_ctx->pos;
42
12.6M
    if (nbytes <= bytes_left) {
43
12.6M
        memcpy(buf, buffer_ctx->buffer->bytes + buffer_ctx->pos, nbytes);
44
12.6M
        bytes_copied = nbytes;
45
12.6M
    } else if (bytes_left > 0) {
46
2.54k
        memcpy(buf, buffer_ctx->buffer->bytes + buffer_ctx->pos, bytes_left);
47
2.54k
        bytes_copied = bytes_left;
48
2.54k
    }
49
12.6M
    buffer_ctx->pos += bytes_copied;
50
12.6M
    return bytes_copied;
51
12.6M
}
52
53
readstat_error_t rt_update_handler(long file_size, readstat_progress_handler progress_handler,
54
21.0k
        void *user_ctx, void *io_ctx) {
55
21.0k
    if (!progress_handler)
56
21.0k
        return READSTAT_OK;
57
58
0
    rt_buffer_ctx_t *buffer_ctx = (rt_buffer_ctx_t *)io_ctx;
59
60
0
    if (progress_handler(1.0 * buffer_ctx->pos / buffer_ctx->buffer->used, user_ctx))
61
0
        return READSTAT_ERROR_USER_ABORT;
62
63
0
    return READSTAT_OK;
64
0
}