Debian Codegraph @

crt/aws-c-io/include/aws/io/stream.h

raw symbols SCIP
1 #ifndef AWS_IO_STREAM_H
2 #define AWS_IO_STREAM_H
3
4 /**
5 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
6 * SPDX-License-Identifier: Apache-2.0.
7 */
8
9 #include <aws/common/ref_count.h>
10 #include <aws/io/io.h>
11
12 AWS_PUSH_SANE_WARNING_LEVEL
13
14 struct aws_input_stream;
15 struct aws_byte_buf;
16
17 /*
18 * For seek calls, where in the stream to seek from.
19 * CUR support can come later
20 * Intentionally mirror libc constants
21 */
22 enum aws_stream_seek_basis { AWS_SSB_BEGIN = 0, AWS_SSB_END = 2 };
23
24 struct aws_stream_status {
25 bool is_end_of_stream;
26 bool is_valid;
27 };
28
29 struct aws_input_stream_vtable {
30 int (*seek)(struct aws_input_stream *stream, int64_t offset, enum aws_stream_seek_basis basis);
31 /**
32 * Stream as much data as will fit into the destination buffer and update its length.
33 * The destination buffer's capacity MUST NOT be changed.
34 *
35 * Return AWS_OP_SUCCESS if the read is successful.
36 * If AWS_OP_ERR is returned, the stream is assumed to be invalid and any data written to the buffer is ignored.
37 *
38 * If no more data is currently available, or the end of the stream has been reached, simply return AWS_OP_SUCCESS
39 * without touching the destination buffer.
40 */
41 int (*read)(struct aws_input_stream *stream, struct aws_byte_buf *dest);
42 int (*get_status)(struct aws_input_stream *stream, struct aws_stream_status *status);
43 int (*get_length)(struct aws_input_stream *stream, int64_t *out_length);
44
45 /**
46 * Optional.
47 * If not set, the default aws_ref_count_acquire/release will be used.
48 * Set for high level language binding that has its own refcounting implementation and needs to be kept alive from
49 * C.
50 * If set, ref_count member will not be used.
51 */
52 void (*acquire)(struct aws_input_stream *stream);
53 void (*release)(struct aws_input_stream *stream);
54 };
55
56 /**
57 * Base class for input streams.
58 * Note: when you implement one input stream, the ref_count needs to be initialized to clean up the resource when
59 * reaches to zero.
60 */
61 struct aws_input_stream {
62 /* point to the impl only set if needed. */
63 void *impl;
64 const struct aws_input_stream_vtable *vtable;
65 struct aws_ref_count ref_count;
66 };
67
68 AWS_EXTERN_C_BEGIN
69
70 /**
71 * Increments the reference count on the input stream, allowing the caller to take a reference to it.
72 *
73 * Returns the same input stream passed in.
74 */
75 AWS_IO_API struct aws_input_stream *aws_input_stream_acquire(struct aws_input_stream *stream);
76
77 /**
78 * Decrements a input stream's ref count. When the ref count drops to zero, the input stream will be destroyed.
79 *
80 * Returns NULL always.
81 */
82 AWS_IO_API struct aws_input_stream *aws_input_stream_release(struct aws_input_stream *stream);
83
84 /*
85 * Seek to a position within a stream; analagous to fseek() and its relatives
86 */
87 AWS_IO_API int aws_input_stream_seek(struct aws_input_stream *stream, int64_t offset, enum aws_stream_seek_basis basis);
88
89 /*
90 * Read data from a stream. If data is available, will read up to the (capacity - len) open bytes
91 * in the destination buffer. If AWS_OP_ERR is returned, the destination buffer will be unchanged.
92 */
93 AWS_IO_API int aws_input_stream_read(struct aws_input_stream *stream, struct aws_byte_buf *dest);
94
95 /*
96 * Queries miscellaneous properties of the stream
97 */
98 AWS_IO_API int aws_input_stream_get_status(struct aws_input_stream *stream, struct aws_stream_status *status);
99
100 /*
101 * Returns the total stream length, if able, regardless of current stream position. Under certain conditions,
102 * a valid stream may return an error instead when there is not a good answer (socket stream, for example).
103 *
104 */
105 AWS_IO_API int aws_input_stream_get_length(struct aws_input_stream *stream, int64_t *out_length);
106
107 /* DEPRECATED
108 * Tears down the stream. Equivalent to aws_input_stream_release()
109 */
110 AWS_IO_API void aws_input_stream_destroy(struct aws_input_stream *stream);
111
112 /*
113 * Creates a stream that operates on a range of bytes
114 */
115 AWS_IO_API struct aws_input_stream *aws_input_stream_new_from_cursor(
116 struct aws_allocator *allocator,
117 const struct aws_byte_cursor *cursor);
118
119 /*
120 * Creates a stream that operates on a (not-yet-opened) file.
121 * Destruction closes the file.
122 */
123 AWS_IO_API struct aws_input_stream *aws_input_stream_new_from_file(
124 struct aws_allocator *allocator,
125 const char *file_name);
126
127 /*
128 * Creates an input stream that reads from an already opened file.
129 * Destruction does not close the file.
130 */
131 AWS_IO_API struct aws_input_stream *aws_input_stream_new_from_open_file(struct aws_allocator *allocator, FILE *file);
132
133 AWS_EXTERN_C_END
134 AWS_POP_SANE_WARNING_LEVEL
135
136 #endif /* AWS_IO_STREAM_H */