summaryrefslogtreecommitdiff
path: root/prism/internal/source.h
blob: b3c2b55be320ec45230cdb5c3f948372604cc7ba (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
#ifndef PRISM_INTERNAL_SOURCE_H
#define PRISM_INTERNAL_SOURCE_H

#include "prism/source.h"
#include "prism/buffer.h"

#include <stdbool.h>

/*
 * The type of source, which determines cleanup behavior.
 */
typedef enum {
    /* Wraps existing constant memory, no cleanup. */
    PM_SOURCE_CONSTANT,

    /* Wraps existing shared memory (non-owning slice), no cleanup. */
    PM_SOURCE_SHARED,

    /* Owns a heap-allocated buffer, freed on cleanup. */
    PM_SOURCE_OWNED,

    /* Memory-mapped file, unmapped on cleanup. */
    PM_SOURCE_MAPPED,

    /* Stream source backed by a pm_buffer_t. */
    PM_SOURCE_STREAM
} pm_source_type_t;

/*
 * The internal representation of a source.
 */
struct pm_source_t {
    /* A pointer to the start of the source data. */
    const uint8_t *source;

    /* The length of the source data in bytes. */
    size_t length;

    /* The type of the source. */
    pm_source_type_t type;

    /* Stream-specific data, only used for PM_SOURCE_STREAM sources. */
    struct {
        /* The buffer that holds the accumulated stream data. */
        pm_buffer_t *buffer;

        /* The stream object to read from. */
        void *stream;

        /* The function to use to read from the stream. */
        pm_source_stream_fgets_t *fgets;

        /* The function to use to check if the stream is at EOF. */
        pm_source_stream_feof_t *feof;

        /* Whether the stream has reached EOF. */
        bool eof;
    } stream;
};

/*
 * Read from a stream into the source's internal buffer. This is used by
 * pm_parse_stream to incrementally read the source.
 */
bool pm_source_stream_read(pm_source_t *source);

/*
 * Returns whether the stream source has reached EOF.
 */
bool pm_source_stream_eof(const pm_source_t *source);

#endif