summaryrefslogtreecommitdiffstats
path: root/libparc/parc/algol/parc_FileInputStream.h
blob: 3641e08a5cc3c10df416402b1c304b7021294aea (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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/*
 * Copyright (c) 2017 Cisco and/or its affiliates.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
 * @file parc_FileInputStream.h
 * @ingroup inputoutput
 * @brief  A FileInputStream obtains input bytes from a file in a file system.
 *
 * What files are available depends on the host environment.
 * FileInputStream is meant for reading streams of raw bytes such as image data.
 *
 */

#ifndef libparc_parc_FileInputStream_h
#define libparc_parc_FileInputStream_h

#include <parc/algol/parc_File.h>
#include <parc/algol/parc_Buffer.h>
#include <parc/algol/parc_InputStream.h>

struct parc_file_input_stream;

/**
 * @typedef PARCFileInputStream
 * @brief Read streams of input
 */

typedef struct parc_file_input_stream PARCFileInputStream;

/**
 * The mapping of a `PARCFileInputStream` to the generic `PARCInputStream`.
 *
 * Example:
 * @code
 * <#example#>
 * @endcode
 */
PARCInputStreamInterface *PARCFileInputStreamAsPARCInputStream;

/**
 * Create a `PARCFileInputStream` instance.
 *
 * @param [in] fileDescriptor An abstract indicator for accessing a specific file
 *
 * @return non-NULL A pointer to an instance of `PARCFileInputStream`
 *
 * Example:
 * @code
 * {
 *     <#example#>
 * }
 * @endcode
 */
PARCFileInputStream *parcFileInputStream_Create(int fileDescriptor);

/**
 * Create a `PARCFileInputStream` instance by opening an existing {@link PARCFile} instance.
 *
 * The file specified by `PARCFile` must exist and readable.
 *
 * @param [in] file A pointer to a `PARCFile` instance representing the existing file.
 *
 * @return non-NULL A pointer to a `PARCFileInputStream` instance.
 * @return NULL Memory could not be allocated.
 *
 * Example:
 * @code
 * {
 *     <#example#>
 * }
 * @endcode
 */
PARCFileInputStream *parcFileInputStream_Open(const PARCFile *file);

/**
 * Acquire a new reference to an instance of `PARCFileInputStream`.
 *
 * The reference count to the instance is incremented.
 *
 * @param [in] inputStream The instance of `PARCFileInputStream` to which to refer.
 *
 * @return The same value as the input parameter @p inputStream
 *
 * Example:
 * @code
 * {
 *     <#example#>
 * }
 * @endcode
 */
PARCFileInputStream *parcFileInputStream_Acquire(const PARCFileInputStream *inputStream);

/**
 * Release a `PARCFileInputStream` reference.
 *
 * Only the last invocation where the reference count is decremented to zero,
 * will actually destroy the `PARCFileInputStream`.
 *
 * @param [in,out] inputStreamPtr is a pointer to the `PARCFileInputStream` reference.
 *
 * Example:
 * @code
 * <#example#>
 * @endcode
 */

void parcFileInputStream_Release(PARCFileInputStream **inputStreamPtr);

/**
 * Read a `PARCFileInputStream` into a {@link PARCBuffer}.
 *
 * The contents of the `PARCBuffer` are filled from the current position to the limit.
 * When this function returns, the position is set to the end of the last successfully read byte of data.
 *
 * @param [in] inputStream The `PARCInputStream` to read.
 * @param [in] buffer The `PARCBuffer` to read, from the current position of the buffer to its limit.
 *
 * @return true The entire contents of the `PARCBuffer`, from the current position to the limit, were filled.
 * @return false The entire contents of the `PARCBuffer` were not filled.
 *
 * Example:
 * @code
 * {
 *     PARCFileOutputStream *stream =
 *         parcFileOutputStream_Create(open("/tmp/test_parc_FileOutputStream", O_CREAT|O_WRONLY|O_TRUNC, 0600));
 *
 *     PARCBuffer *buffer = parcBuffer_Allocate(16 * 1024*1024);
 *
 *     parcFileOutputStream_Write(stream, buffer);
 *
 *     assertFalse(parcBuffer_HasRemaining(buffer), "Expected the buffer to be emtpy");
 *
 *     parcBuffer_Release(&buffer);
 *
 *     parcFileOutputStream_Release(&stream);
 * }
 * @endcode
 *
 */
bool parcFileInputStream_Read(PARCFileInputStream *inputStream, PARCBuffer *buffer);

/**
 * Read the content of a `PARCFileInputStream` into a {@link PARCBuffer}.
 *
 * @param [in] inputStream The `PARCFileInputStream` to read.
 *
 * @return non-NULL A pointer to a `PARCBuffer` instance containing the content of the `PARCFileInputStream`.
 * @return NULl Memory could not be allocated.
 *
 * Example:
 * @code
 * {
 *     <#example#>
 * }
 * @endcode
 */
PARCBuffer *parcFileInputStream_ReadFile(PARCFileInputStream *inputStream);
#endif // libparc_parc_FileInputStream_h