summaryrefslogtreecommitdiffstats
path: root/libparc/parc/algol/parc_OutputStream.h
blob: db0ba3431f3c034989af8743315ae88ba9e11b1b (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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/*
 * 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_OutputStream.h
 * @ingroup inputoutput
 * @brief A polymophic interface to specific implementations of modules that implement the
 *        output stream capabilities.
 *
 *
 *
 */
#ifndef libparc_parc_OutputStream_h
#define libparc_parc_OutputStream_h

#include <parc/algol/parc_Buffer.h>

struct parc_output_stream;
typedef struct parc_output_stream PARCOutputStream;

typedef struct parc_output_stream_interface {
    size_t (*Write)(PARCOutputStream *stream, PARCBuffer *buffer);

    PARCOutputStream *(*Acquire)(PARCOutputStream * stream);

    void (*Release)(PARCOutputStream **streamPtr);
} PARCOutputStreamInterface;

/**
 * Create an valid PARCOutputStream instance from the given pointers to a properly
 * initialized `PARCOutputStreamInterface`
 * and specific instance structure that will be supplied to the underlying interface.
 *
 * @param [in] instance A pointer to a `PARCObject` that will be the parameter to the functions specifed by @p interface.
 * @param [in] interface A pointer to a `PARCOutputStreamInterface`.
 *
 * @return non-NULL A pointer to a valid PARCOutputStream instance.
 * @return NULL An error occurred.
 *
 * Example:
 * @code
 * {
 *     PARCFileOutputStream *fileOutput = parcFileOutputStream_Create(1);
 *     PARCOutputStream *output = parcOutputStream_Create(parcFileOutputStream_Acquire(fileOutputStream),
 *                                                        PARCFileOutputStreamAsPARCInputStream);
 * }
 * @endcode
 */
PARCOutputStream *parcOutputStream_Create(void *instance, const PARCOutputStreamInterface *interface);

/**
 * Write the contents of the given `PARCBuffer` to the output stream.
 *
 * The position of the `PARCBuffer` will be set to its limit as a side-effect.
 *
 * @param [in] stream A pointer to a valid `PARCOutputStream` instance.
 * @param [in] buffer A pointer to the `PARCBuffer` whose contents should be written to @p stream.
 *
 * @return The number of bytes written
 *
 * Example:
 * @code
 * {
 *     PARCFileOutputStream *fileOutput = parcFileOutputStream_Create(1);
 *     PARCOutputStream *output = parcOutputStream_Create(parcFileOutputStream_Acquire(fileOutputStream),
 *                                                        PARCFileOutputStreamAsPARCInputStream);
 *     PARCBuffer *buffer = parcBuffer_WrapCString("Hello World");
 *     parcOutputStream_Write(output, buffer);
 *     parcOutputStream_Release(&output);
 * }
 * @endcode
 */
size_t parcOutputStream_Write(PARCOutputStream *stream, PARCBuffer *buffer);

/**
 * Write a nul-terminated C string to the given `PARCOutputStream`.
 *
 * @param [in] stream A pointer to a valid `PARCOutputStream` instance.
 * @param [in] string A nul-terminated C string.
 *
 * @return The number of bytes written.
 *
 * Example:
 * @code
 * {
 *     PARCFileOutputStream *fileOutput = parcFileOutputStream_Create(1);
 *     PARCOutputStream *output = parcOutputStream_Create(parcFileOutputStream_Acquire(fileOutputStream),
 *                                                        PARCFileOutputStreamAsPARCInputStream);
 *
 *     parcOutputStream_WriteCStrings(output, "Hello", " ", "World", NULL);
 *
 *     parcOutputStream_Release(&output);
 * }
 * @endcode
 */
size_t parcOutputStream_WriteCString(PARCOutputStream *stream, const char *string);

/**
 * Write one or more nul-terminated C strings to the given `PARCOutputStream`.
 *
 * @param [in] stream A pointer to a valid `PARCOutputStream` instance.
 * @param [in] ... A NULL terminated variable argument list of nul-terminated C strings.
 *
 * @return The number of bytes written.
 *
 * Example:
 * @code
 * {
 *     PARCFileOutputStream *fileOutput = parcFileOutputStream_Create(1);
 *     PARCOutputStream *output = parcOutputStream_Create(parcFileOutputStream_Acquire(fileOutputStream),
 *                                                        PARCFileOutputStreamAsPARCInputStream);
 *
 *     parcOutputStream_WriteCStrings(output, "Hello", " ", "World", NULL);
 *
 *     parcOutputStream_Release(&output);
 * }
 * @endcode
 */
size_t parcOutputStream_WriteCStrings(PARCOutputStream *stream, ...);

/**
 * Increase the number of references to a `PARCOutputStream`.
 *
 * Note that new `PARCOutputStream` is not created,
 * only that the given `PARCOutputStream` reference count is incremented.
 * Discard the reference by invoking `parcOutputStream_Release`.
 *
 * @param [in] stream A pointer to a `PARCOutputStream` instance.
 *
 * @return The input `PARCOutputStream` pointer.
 *
 * Example:
 * @code
 * {
 *     PARCFileOutputStream *fileOutput = parcFileOutputStream_Create(1);
 *     PARCOutputStream *output = parcOutputStream_Create(parcFileOutputStream_Acquire(fileOutputStream),
 *                                                        PARCFileOutputStreamAsPARCInputStream);
 *     PARCBuffer *buffer = parcBuffer_WrapCString("Hello World");
 *     parcOutputStream_Write(output, buffer);
 *
 *     PARCOutputStream *x = parcOutputStream_Acquire(output);
 *
 *     parcBuffer_Release(&x);
 *     parcOutputStream_Release(&output);
 * }
 * @endcode
 * @see parcOutputStream_Release
 */
PARCOutputStream *parcOutputStream_Acquire(const PARCOutputStream *stream);

/**
 * Release a previously acquired reference to the specified instance,
 * decrementing the reference count for the instance.
 *
 * The pointer to the instance is set to NULL as a side-effect of this function.
 *
 * If the invocation causes the last reference to the instance to be released,
 * the instance is deallocated and the instance's interface will perform
 * additional cleanup and release other privately held references.
 *
 * @param [in] streamPtr A pointer to a pointer to the instance to release.
 *
 * Example:
 * @code
 * {
 *     PARCFileOutputStream *fileOutput = parcFileOutputStream_Create(1);
 *     PARCOutputStream *output = parcOutputStream_Create(parcFileOutputStream_Acquire(fileOutputStream),
 *                                                        PARCFileOutputStreamAsPARCInputStream);
 *     PARCBuffer *buffer = parcBuffer_WrapCString("Hello World");
 *     parcOutputStream_Write(output, buffer);
 *     parcOutputStream_Release(&output);
 * }
 * @endcode
 * @see parcOutputStream_Acquire
 * @see parcOutputStream_Create
 */
void parcOutputStream_Release(PARCOutputStream **streamPtr);
#endif // libparc_parc_OutputStream_h