Ion C
C library for Ion
ion_stream.h
Go to the documentation of this file.
1 /*
2  * Copyright 2012-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License").
5  * You may not use this file except in compliance with the License.
6  * A copy of the License is located at:
7  *
8  * http://aws.amazon.com/apache2.0/
9  *
10  * or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific
12  * language governing permissions and limitations under the License.
13  */
16 /*
17  * updated versions of the file stream handling support for
18  * the IonC readers and writers.
19  *
20  * fn_fill - updates the streams buffer, curr, limit and max
21  * fields. Should return -1 on eof or bytes read
22  * or error. For a writable stream this may need to
23  * append an empty buffer.
24  *
25  * fn_unread- unreads a byte when there is no room in the current
26  * buffer to hold the unread byte. This will not get
27  * called with an EOF value. Normal behavior is to create
28  * an empty buffer that "preceeds" the current buffer and
29  * write the byte there. It may use the the actual previous
30  * buffer. Or it may fail.
31  *
32  * fn_flush - writes any dirty data out to the output stream.
33  *
34  * fn_close - releases any resources as needed.
35  *
36 
37 
38  MISSING: skip, fixes for block read/unread, collapse some functions
39 
40 
41  use cases:
42 
43  read only stream - (aka (can write == false)
44  --next char
45  if not end of buffer - done
46  if user buffer - fail
47  find next page
48  if not found
49  if mark allocate new page
50  fill page from last read position
51  --unread
52  if beginning of page
53  find previous page
54  else (not found)
55  create temp page
56  mark as fake
57  save current page
58  make new (found or temp) page current @ end of page
59  back up in current page
60  --write - fail
61  --seek - fail
62  --skip N
63  next char N times
64 
65  write only stream - (aka (can read == false)
66  --next char - fail
67  --unread - fail
68  --write
69  if no room
70  write current page to disk
71  release current page
72  get new page
73  fill page
74  make page current @ beginning of buf
75  update dirty pointers
76  --seek - fail
77  --skip - fail
78 
79 ----- read only and write only streams are not seekable
80 
81  user buffer
82  --to initialize allocate stream and 1 page set up page to point to user buffer
83  --next char
84  if end of buffer fail with EOF
85  --unread
86  if beginning of buffer - fail
87  back up in current buffer
88  --write
89  if write past buffer length - fail
90  write at current position
91  update dirty pointers
92  --seek
93  if position outside buffer - fail
94  set position
95  --skip
96  compute new position
97  seek to new position
98 
99  seekable (read/write)
100  --next char - fail
101  --unread - fail
102  --write
103  if no room in current
104  find next page
105  if not found
106  make page
107  seek to page
108  fill page
109  write data at page
110  update dirty pointers
111  --seek
112  find page
113  if not found
114  make page
115  seek in file to page start
116  fill page
117  make page current
118  set position @ position in page
119  --skip
120  compute new position
121  seek to new position
122 
123  ----make page
124  when not buffering
125  flush current page
126  release current page
127  allocate page
128 
129  ----make page current
130  if page != current page
131  flush current page
132  if not buffering release current page
133  set up stream members to new page
134  clear dirty pointers
135  *
136  */
137 
138 #ifndef ION_STREAM_H_
139 #define ION_STREAM_H_
140 
141 #include "ion_types.h"
142 #include "ion_platform_config.h"
143 #include "ion_errors.h"
144 
145 #ifdef __cplusplus
146 extern "C" {
147 #endif
148 
149 #ifndef ION_STREAM_DECL
150 #define ION_STREAM_DECL
151 
152 // needed when the stream is used outside the context of the
153 // general Ion library. Otherwise this must be defined in the
154 // Ion type header (ion_types.h).
155 typedef struct _ion_stream ION_STREAM;
156 
157 // decl's for user managed stream
158 typedef iERR (*ION_STREAM_HANDLER)(struct _ion_user_stream *pstream);
159 struct _ion_user_stream
160 {
161  BYTE *curr;
162  BYTE *limit;
163  void *handler_state;
164  ION_STREAM_HANDLER handler;
165 };
166 
167 #endif
168 
169 typedef struct _ion_stream_user_paged ION_STREAM_USER_PAGED;
170 typedef struct _ion_stream_paged ION_STREAM_PAGED;
171 typedef struct _ion_page ION_PAGE;
172 typedef int32_t PAGE_ID;
173 typedef int64_t POSITION;
174 
176 
177 // public constructors
178 
180 
181 
182 ION_API_EXPORT iERR ion_stream_open_buffer(BYTE *buffer // pointer to memory to stream over
183  , SIZE buf_length // length of user buffer, filled or not
184  , SIZE buf_filled // length of user filled data (0 or more bytes)
185  , BOOL read_only // if read_only is true write is disallowed (read is always valid)
186  , ION_STREAM **pp_stream
187 );
188 ION_API_EXPORT iERR ion_stream_open_memory_only(ION_STREAM **pp_stream);
189 
190 ION_API_EXPORT iERR ion_stream_open_stdin(ION_STREAM **pp_stream);
191 ION_API_EXPORT iERR ion_stream_open_stdout(ION_STREAM **pp_stream);
192 ION_API_EXPORT iERR ion_stream_open_stderr(ION_STREAM **pp_stream);
193 
194 ION_API_EXPORT iERR ion_stream_open_file_in(FILE *in, ION_STREAM **pp_stream);
195 ION_API_EXPORT iERR ion_stream_open_file_out(FILE *out, ION_STREAM **pp_stream);
196 ION_API_EXPORT iERR ion_stream_open_file_rw(FILE *fp, BOOL cache_all, ION_STREAM **pp_stream);
197 
198 ION_API_EXPORT iERR ion_stream_open_handler_in(ION_STREAM_HANDLER fn_input_handler, void *handler_state, ION_STREAM **pp_stream);
199 ION_API_EXPORT iERR ion_stream_open_handler_out(ION_STREAM_HANDLER fn_output_handler, void *handler_state, ION_STREAM **pp_stream);
200 
201 ION_API_EXPORT iERR ion_stream_open_fd_in(int fd_in, ION_STREAM **pp_stream);
202 ION_API_EXPORT iERR ion_stream_open_fd_out(int fd_out, ION_STREAM **pp_stream);
203 ION_API_EXPORT iERR ion_stream_open_fd_rw(int fd, BOOL cache_all, ION_STREAM **pp_stream);
204 
205 ION_API_EXPORT iERR ion_stream_flush(ION_STREAM *stream);
206 ION_API_EXPORT iERR ion_stream_close(ION_STREAM *stream);
207 
209 
210 // informational routines (aka getters)
211 
213 
214 ION_API_EXPORT BOOL ion_stream_can_read (ION_STREAM *stream);
215 ION_API_EXPORT BOOL ion_stream_can_write (ION_STREAM *stream);
216 ION_API_EXPORT BOOL ion_stream_can_seek (ION_STREAM *stream);
217 ION_API_EXPORT BOOL ion_stream_can_mark (ION_STREAM *stream);
218 ION_API_EXPORT BOOL ion_stream_is_dirty (ION_STREAM *stream);
219 ION_API_EXPORT BOOL ion_stream_is_mark_open (ION_STREAM *stream);
220 ION_API_EXPORT POSITION ion_stream_get_position (ION_STREAM *stream);
221 ION_API_EXPORT FILE *ion_stream_get_file_stream (ION_STREAM *stream);
222 ION_API_EXPORT POSITION ion_stream_get_mark_start (ION_STREAM *stream);
223 ION_API_EXPORT POSITION ion_stream_get_marked_length (ION_STREAM *stream);
224 
226 
227 // public instance methods
228 
230 
231 ION_API_EXPORT iERR ion_stream_read_byte (ION_STREAM *stream, int *p_c);
232 ION_API_EXPORT iERR ion_stream_read (ION_STREAM *stream, BYTE *buf, SIZE len, SIZE *p_bytes_read);
233 ION_API_EXPORT iERR ion_stream_unread_byte (ION_STREAM *stream, int c);
234 ION_API_EXPORT iERR ion_stream_write (ION_STREAM *stream, BYTE *buf, SIZE len, SIZE *p_bytes_written);
235 ION_API_EXPORT iERR ion_stream_write_byte (ION_STREAM *stream, int byte);
236 ION_API_EXPORT iERR ion_stream_write_byte_no_checks(ION_STREAM *stream, int byte);
237 ION_API_EXPORT iERR ion_stream_write_stream (ION_STREAM *stream, ION_STREAM *stream_input, SIZE len, SIZE *p_written);
238 ION_API_EXPORT iERR ion_stream_seek (ION_STREAM *stream, POSITION position);
239 ION_API_EXPORT iERR ion_stream_truncate (ION_STREAM *stream);
240 ION_API_EXPORT iERR ion_stream_skip (ION_STREAM *stream, SIZE distance, SIZE *p_skipped);
241 ION_API_EXPORT iERR ion_stream_mark (ION_STREAM *stream);
242 ION_API_EXPORT iERR ion_stream_mark_remark (ION_STREAM *stream, POSITION position);
243 ION_API_EXPORT iERR ion_stream_mark_rewind (ION_STREAM *stream);
244 ION_API_EXPORT iERR ion_stream_mark_clear (ION_STREAM *stream);
245 
246 #ifdef __cplusplus
247 }
248 #endif
249 
250 #endif /* ION_STREAM_H_ */
Definition: ion_types.h:159