Line | Branch | Exec | Source |
---|---|---|---|
1 | /* GATE PROJECT LICENSE: | ||
2 | +----------------------------------------------------------------------------+ | ||
3 | | Copyright(c) 2018-2025, Stefan Meislinger <sm@opengate.at> | | ||
4 | | All rights reserved. | | ||
5 | | | | ||
6 | | Redistribution and use in source and binary forms, with or without | | ||
7 | | modification, are permitted provided that the following conditions are met:| | ||
8 | | | | ||
9 | | 1. Redistributions of source code must retain the above copyright notice, | | ||
10 | | this list of conditions and the following disclaimer. | | ||
11 | | 2. Redistributions in binary form must reproduce the above copyright | | ||
12 | | notice, this list of conditions and the following disclaimer in the | | ||
13 | | documentation and/or other materials provided with the distribution. | | ||
14 | | | | ||
15 | | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"| | ||
16 | | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | | ||
17 | | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | | ||
18 | | ARE DISCLAIMED.IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE | | ||
19 | | LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | | ||
20 | | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | | ||
21 | | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | | ||
22 | | INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | | ||
23 | | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | | ||
24 | | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF | | ||
25 | | THE POSSIBILITY OF SUCH DAMAGE. | | ||
26 | +----------------------------------------------------------------------------+ | ||
27 | */ | ||
28 | |||
29 | #include "gate/io/pipes.hpp" | ||
30 | #include "gate/gatetypes.hpp" | ||
31 | #include "gate/exceptions.hpp" | ||
32 | #include "gate/memalloc.hpp" | ||
33 | |||
34 | namespace gate | ||
35 | { | ||
36 | namespace io | ||
37 | { | ||
38 | |||
39 | gate_pipe_t const Pipe::Invalid = gate_pipe_invalid; | ||
40 | |||
41 | ✗ | Pipe::Pipe() | |
42 | { | ||
43 | ✗ | result_t result = gate_pipe_create(&this->impl_read, &this->impl_write); | |
44 | ✗ | GATEXX_CHECK_ERROR(result); | |
45 | ✗ | } | |
46 | |||
47 | 4 | Pipe::Pipe(String& name, OpenModeEnum mode) | |
48 | 4 | : impl_read(Pipe::Invalid), impl_write(Pipe::Invalid) | |
49 | { | ||
50 | 4 | gate_string_t str = GATE_STRING_INIT_EMPTY; | |
51 | gate_result_t result; | ||
52 |
2/3✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
|
4 | switch (mode) |
53 | { | ||
54 | 2 | case OpenMode_Create: | |
55 | { | ||
56 |
1/2✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
|
2 | result = gate_pipe_open(&str, GATE_PIPE_FLAG_CREATE, &this->impl_read, &this->impl_write); |
57 | 2 | name = String::createFrom(str); | |
58 | 2 | break; | |
59 | } | ||
60 | 2 | case OpenMode_Read: | |
61 | case OpenMode_Write: | ||
62 | { | ||
63 |
1/2✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
|
2 | gate_string_create_copy(&str, name.c_impl()); |
64 |
1/2✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
|
2 | result = gate_pipe_open(&str, (gate_enumint_t)mode, &this->impl_read, &this->impl_write); |
65 |
1/2✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
|
2 | gate_string_release(&str); |
66 | 2 | break; | |
67 | } | ||
68 | ✗ | default: | |
69 | { | ||
70 | ✗ | result = GATE_RESULT_INVALIDARG; | |
71 | ✗ | break; | |
72 | } | ||
73 | } | ||
74 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
4 | GATEXX_CHECK_ERROR(result); |
75 | 4 | } | |
76 | ✗ | Pipe::Pipe(gate_pipe_t import_read_pipe, gate_pipe_t import_write_pipe) | |
77 | : impl_read(import_read_pipe), | ||
78 | ✗ | impl_write(import_write_pipe) | |
79 | { | ||
80 | ✗ | } | |
81 | ✗ | Pipe::Pipe(Pipe const& pipe) | |
82 | : impl_read(gate_pipe_invalid), | ||
83 | ✗ | impl_write(gate_pipe_invalid) | |
84 | { | ||
85 | result_t result; | ||
86 | ✗ | if (pipe.impl_read != gate_pipe_invalid) | |
87 | { | ||
88 | ✗ | result = gate_pipe_duplicate(pipe.impl_read, &this->impl_read); | |
89 | ✗ | GATEXX_CHECK_ERROR(result); | |
90 | } | ||
91 | |||
92 | ✗ | if (pipe.impl_write != gate_pipe_invalid) | |
93 | { | ||
94 | ✗ | result = gate_pipe_duplicate(pipe.impl_write, &this->impl_write); | |
95 | ✗ | if (GATE_FAILED(result)) | |
96 | { | ||
97 | ✗ | if (this->impl_read != gate_pipe_invalid) | |
98 | { | ||
99 | ✗ | gate_pipe_close(this->impl_read); | |
100 | } | ||
101 | ✗ | GATEXX_RAISE_EXCEPTION(result, NULL, 0); | |
102 | } | ||
103 | } | ||
104 | ✗ | } | |
105 | ✗ | Pipe& Pipe::operator=(Pipe const& src) | |
106 | { | ||
107 | ✗ | if (this != &src) | |
108 | { | ||
109 | ✗ | Pipe that(src); | |
110 | ✗ | swapRefsNoExcept(this->impl_read, that.impl_read); | |
111 | ✗ | swapRefsNoExcept(this->impl_write, that.impl_write); | |
112 | } | ||
113 | ✗ | return *this; | |
114 | } | ||
115 | 8 | Pipe::~Pipe() noexcept | |
116 | { | ||
117 |
1/2✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
|
4 | if (this->impl_read != gate_pipe_invalid) |
118 | { | ||
119 | 4 | gate_pipe_close(this->impl_read); | |
120 | } | ||
121 |
3/4✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 2 times.
|
4 | if ((this->impl_write != this->impl_read) && (this->impl_write != gate_pipe_invalid)) |
122 | { | ||
123 | 2 | gate_pipe_close(this->impl_write); | |
124 | } | ||
125 | 4 | } | |
126 | 2 | bool_t Pipe::canRead() const | |
127 | { | ||
128 | 2 | return this->impl_read != gate_pipe_invalid; | |
129 | } | ||
130 | 2 | bool_t Pipe::canWrite() const | |
131 | { | ||
132 | 2 | return this->impl_write != gate_pipe_invalid; | |
133 | } | ||
134 | ✗ | gate_pipe_t const* Pipe::c_impl_read() const noexcept | |
135 | { | ||
136 | ✗ | return &this->impl_read; | |
137 | } | ||
138 | ✗ | gate_pipe_t const* Pipe::c_impl_write() const noexcept | |
139 | { | ||
140 | ✗ | return &this->impl_write; | |
141 | } | ||
142 | |||
143 | ✗ | gate_pipe_t const* Pipe::c_impl() const noexcept | |
144 | { | ||
145 | ✗ | if (this->canRead()) | |
146 | { | ||
147 | ✗ | return this->c_impl_read(); | |
148 | } | ||
149 | ✗ | else if (this->canWrite()) | |
150 | { | ||
151 | ✗ | return this->c_impl_write(); | |
152 | } | ||
153 | ✗ | return NULL; | |
154 | } | ||
155 | |||
156 | 1 | bool_t Pipe::dataAvailable() | |
157 | { | ||
158 | 1 | gate_bool_t value = false; | |
159 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
1 | result_t result = gate_pipe_data_available(this->impl_read, &value); |
160 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
1 | GATEXX_CHECK_EXCEPTION(result); |
161 | 1 | return value; | |
162 | } | ||
163 | 1 | size_t Pipe::read(char* buffer, size_t bufferlen) | |
164 | { | ||
165 | 1 | size_t received = 0; | |
166 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
1 | result_t result = gate_pipe_read(this->impl_read, buffer, bufferlen, &received); |
167 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
1 | GATEXX_CHECK_EXCEPTION(result); |
168 | 1 | return received; | |
169 | } | ||
170 | 1 | size_t Pipe::write(char const* buffer, size_t bufferlen) | |
171 | { | ||
172 | 1 | size_t written = 0; | |
173 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
1 | result_t result = gate_pipe_write(this->impl_write, buffer, bufferlen, &written); |
174 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
1 | GATEXX_CHECK_EXCEPTION(result); |
175 | 1 | return written; | |
176 | } | ||
177 | |||
178 | ✗ | String Pipe::exportRead() const | |
179 | { | ||
180 | ✗ | gate_string_t str = GATE_STRING_INIT_EMPTY; | |
181 | ✗ | result_t result = gate_pipe_export(this->impl_read, &str); | |
182 | ✗ | GATEXX_CHECK_ERROR(result); | |
183 | ✗ | return String::createFrom(str); | |
184 | } | ||
185 | ✗ | String Pipe::exportWrite() const | |
186 | { | ||
187 | ✗ | gate_string_t str = GATE_STRING_INIT_EMPTY; | |
188 | ✗ | result_t result = gate_pipe_export(this->impl_write, &str); | |
189 | ✗ | GATEXX_CHECK_ERROR(result); | |
190 | ✗ | return String::createFrom(str); | |
191 | } | ||
192 | |||
193 | ✗ | Stream Pipe::openStream(String& name, OpenModeEnum mode) | |
194 | { | ||
195 | ✗ | gate_stream_t* ptr_strm = NULL; | |
196 | ✗ | gate_string_t str_name = GATE_STRING_INIT_EMPTY; | |
197 | ✗ | gate_string_duplicate(&str_name, name.c_impl()); | |
198 | ✗ | result_t result = gate_pipe_open_stream(&str_name, mode, &ptr_strm); | |
199 | ✗ | GATEXX_CHECK_EXCEPTION(result); | |
200 | ✗ | String new_name = String::createFrom(str_name); | |
201 | ✗ | name.swap(new_name); | |
202 | ✗ | return Stream(ptr_strm); | |
203 | } | ||
204 | |||
205 | |||
206 | |||
207 | } // end of namespace io | ||
208 | } // end of namespace gate | ||
209 |