Move BIBLIOGRAPHY and FILEFORMAT to docs/
[gazelle.git] / runtime / bc_read_stream.h
blobab67fcc1a709fd1695adc5092dbb15fd28ad25da
1 /*********************************************************************
3 Gazelle: a system for building fast, reusable parsers
5 bc_read_stream.h
7 This file presents a public stream-based interface for reading files
8 in Bitcode format. It allows skipping blocks and rewinding to the
9 beginning of a block.
11 Copyright (c) 2007 Joshua Haberman. See LICENSE for details.
13 *********************************************************************/
15 #ifndef BITCODE_READ_STREAM
16 #define BITCODE_READ_STREAM
18 #include <stdint.h>
20 struct bc_read_stream;
22 /**********************************************************
24 Open/Close Stream
26 ***********************************************************/
28 struct bc_read_stream *bc_rs_open_file(const char *filename);
29 struct bc_read_stream *bc_rs_open_mem(const char *data);
30 void bc_rs_close_stream(struct bc_read_stream *stream);
32 /**********************************************************
34 Moving around the stream
36 ***********************************************************/
38 enum RecordType {
39 DataRecord, /* This is a normal data record that contains a series of integers */
40 StartBlock, /* This is the start of a block: you can descend into it or skip over it */
41 EndBlock, /* This indicates the end of a block. */
42 DefineAbbrev, /* This record defines an abbreviation. */
43 Eof, /* This indicates end-of-file. */
44 Err /* This indicates an error. */
47 struct record_info {
48 enum RecordType record_type;
49 uint32_t id; /* record id for data records, block id for StartBlock */
52 /* Advance to the next record in the file, returning a tag indicating what
53 * kind of record it is.
55 * Note that not every literal record in the stream will be passed to you,
56 * the client. Some, like records that define abbreviations, are handled
57 * internally because they do not contain stream-level data. */
58 struct record_info bc_rs_next_data_record(struct bc_read_stream *stream);
60 /* Get the total number of integers in this data record, or the remaining number
61 * of integers in this data record, respectively. */
62 int bc_rs_get_record_size(struct bc_read_stream *stream);
63 int bc_rs_get_remaining_record_size(struct bc_read_stream *stream);
65 /* Skip a block by calling this function before reading the first record
66 * of a block. Calling it in other circumstances is an error and the
67 * results are undefined. */
68 void bc_rs_skip_block(struct bc_read_stream *stream);
70 void bc_rs_rewind_block(struct bc_read_stream *stream);
72 /**********************************************************
74 Reading Data
76 ***********************************************************/
78 /* Get the next integer from the current data record and advance the stream.
79 * If there an error of any kind occurs, the corresponding error bits
80 * are set on the stream (check them with bc_rs_get_error()), and these
81 * functions themselves return an undefined value. */
82 uint8_t bc_rs_read_next_8(struct bc_read_stream *stream);
83 uint16_t bc_rs_read_next_16(struct bc_read_stream *stream);
84 uint32_t bc_rs_read_next_32(struct bc_read_stream *stream);
85 uint64_t bc_rs_read_next_64(struct bc_read_stream *stream);
87 uint8_t bc_rs_read_8(struct bc_read_stream *stream, int i);
88 uint16_t bc_rs_read_16(struct bc_read_stream *stream, int i);
89 uint32_t bc_rs_read_32(struct bc_read_stream *stream, int i);
90 uint64_t bc_rs_read_64(struct bc_read_stream *stream, int i);
92 /**********************************************************
94 Error Reporting
96 ***********************************************************/
98 /* These are the error flags for the data stream, and a means for reading them. */
100 /* The value in the stream was too large for the bc_rs_get_* function you called.
101 * For example, the stream's value was 257 but you called bc_rs_get_8(). */
102 #define BITCODE_ERR_VALUE_TOO_LARGE 0x1
104 /* There were no more values in a record when you called bc_rs_get_*. */
105 #define BITCODE_ERR_NO_SUCH_VALUE 0x2
107 /* I/O error reading the input file */
108 #define BITCODE_ERR_IO 0x4
110 /* Bitcode data is corrupt */
111 #define BITCODE_ERR_CORRUPT_INPUT 0x8
113 #define BITCODE_ERR_INTERNAL 0x10
115 int bc_rs_get_error(struct bc_read_stream *stream);
117 #endif
120 * Local Variables:
121 * c-file-style: "bsd"
122 * c-basic-offset: 4
123 * indent-tabs-mode: nil
124 * End:
125 * vim:et:sts=4:sw=4