| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* GATE PROJECT LICENSE: | ||
| 2 | +----------------------------------------------------------------------------+ | ||
| 3 | | Copyright (c) 2018-2026, 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 | 1 | Pipe::Pipe() | |
| 42 | { | ||
| 43 | 1 | result_t result = gate_pipe_create(&this->impl_read, &this->impl_write); | |
| 44 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | GATEXX_CHECK_ERROR(result); |
| 45 | 1 | } | |
| 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 | |||
| 82 | 1 | Pipe::Pipe(String const* ptrImportReadId, String const* ptrImportWriteId) | |
| 83 | : impl_read(Pipe::Invalid), | ||
| 84 | 1 | impl_write(Pipe::Invalid) | |
| 85 | { | ||
| 86 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
1 | if (!ptrImportReadId && !ptrImportWriteId) |
| 87 | { | ||
| 88 | ✗ | GATEXX_CHECK_ERROR(results::InvalidArg); | |
| 89 | } | ||
| 90 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | if (ptrImportReadId) |
| 91 | { | ||
| 92 | 1 | result_t result = gate_pipe_import(ptrImportReadId->c_impl(), &this->impl_read); | |
| 93 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | GATEXX_CHECK_EXCEPTION(result); |
| 94 | } | ||
| 95 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | if (ptrImportWriteId) |
| 96 | { | ||
| 97 | 1 | result_t result = gate_pipe_import(ptrImportWriteId->c_impl(), &this->impl_write); | |
| 98 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | GATEXX_CHECK_EXCEPTION(result); |
| 99 | } | ||
| 100 | 1 | } | |
| 101 | |||
| 102 | |||
| 103 | ✗ | Pipe::Pipe(Pipe const& pipe) | |
| 104 | : impl_read(gate_pipe_invalid), | ||
| 105 | ✗ | impl_write(gate_pipe_invalid) | |
| 106 | { | ||
| 107 | result_t result; | ||
| 108 | ✗ | if (pipe.impl_read != gate_pipe_invalid) | |
| 109 | { | ||
| 110 | ✗ | result = gate_pipe_duplicate(pipe.impl_read, &this->impl_read); | |
| 111 | ✗ | GATEXX_CHECK_ERROR(result); | |
| 112 | } | ||
| 113 | |||
| 114 | ✗ | if (pipe.impl_write != gate_pipe_invalid) | |
| 115 | { | ||
| 116 | ✗ | result = gate_pipe_duplicate(pipe.impl_write, &this->impl_write); | |
| 117 | ✗ | if (GATE_FAILED(result)) | |
| 118 | { | ||
| 119 | ✗ | if (this->impl_read != gate_pipe_invalid) | |
| 120 | { | ||
| 121 | ✗ | gate_pipe_close(this->impl_read); | |
| 122 | } | ||
| 123 | ✗ | GATEXX_RAISE_EXCEPTION(result, NULL, 0); | |
| 124 | } | ||
| 125 | } | ||
| 126 | ✗ | } | |
| 127 | ✗ | Pipe& Pipe::operator=(Pipe const& src) | |
| 128 | { | ||
| 129 | ✗ | if (this != &src) | |
| 130 | { | ||
| 131 | ✗ | Pipe that(src); | |
| 132 | ✗ | swapRefsNoExcept(this->impl_read, that.impl_read); | |
| 133 | ✗ | swapRefsNoExcept(this->impl_write, that.impl_write); | |
| 134 | } | ||
| 135 | ✗ | return *this; | |
| 136 | } | ||
| 137 | 12 | Pipe::~Pipe() noexcept | |
| 138 | { | ||
| 139 |
1/2✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
|
6 | if (this->impl_read != gate_pipe_invalid) |
| 140 | { | ||
| 141 | 6 | gate_pipe_close(this->impl_read); | |
| 142 | } | ||
| 143 |
3/4✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✓ Branch 3 taken 2 times.
|
6 | if ((this->impl_write != this->impl_read) && (this->impl_write != gate_pipe_invalid)) |
| 144 | { | ||
| 145 | 4 | gate_pipe_close(this->impl_write); | |
| 146 | } | ||
| 147 | 6 | } | |
| 148 | 3 | bool_t Pipe::canRead() const | |
| 149 | { | ||
| 150 | 3 | return this->impl_read != gate_pipe_invalid; | |
| 151 | } | ||
| 152 | 2 | bool_t Pipe::canWrite() const | |
| 153 | { | ||
| 154 | 2 | return this->impl_write != gate_pipe_invalid; | |
| 155 | } | ||
| 156 | 1 | gate_pipe_t const* Pipe::c_impl_read() const noexcept | |
| 157 | { | ||
| 158 | 1 | return &this->impl_read; | |
| 159 | } | ||
| 160 | ✗ | gate_pipe_t const* Pipe::c_impl_write() const noexcept | |
| 161 | { | ||
| 162 | ✗ | return &this->impl_write; | |
| 163 | } | ||
| 164 | |||
| 165 | 1 | gate_pipe_t const* Pipe::c_impl() const noexcept | |
| 166 | { | ||
| 167 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
1 | if (this->canRead()) |
| 168 | { | ||
| 169 | 1 | return this->c_impl_read(); | |
| 170 | } | ||
| 171 | ✗ | else if (this->canWrite()) | |
| 172 | { | ||
| 173 | ✗ | return this->c_impl_write(); | |
| 174 | } | ||
| 175 | ✗ | return NULL; | |
| 176 | } | ||
| 177 | |||
| 178 | 1 | bool_t Pipe::dataAvailable() | |
| 179 | { | ||
| 180 | 1 | gate_bool_t value = false; | |
| 181 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
1 | result_t result = gate_pipe_data_available(this->impl_read, &value); |
| 182 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
1 | GATEXX_CHECK_EXCEPTION(result); |
| 183 | 1 | return value; | |
| 184 | } | ||
| 185 | 1 | size_t Pipe::read(char* buffer, size_t bufferlen) | |
| 186 | { | ||
| 187 | 1 | size_t received = 0; | |
| 188 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
1 | result_t result = gate_pipe_read(this->impl_read, buffer, bufferlen, &received); |
| 189 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
1 | GATEXX_CHECK_EXCEPTION(result); |
| 190 | 1 | return received; | |
| 191 | } | ||
| 192 | 1 | size_t Pipe::write(char const* buffer, size_t bufferlen) | |
| 193 | { | ||
| 194 | 1 | size_t written = 0; | |
| 195 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
1 | result_t result = gate_pipe_write(this->impl_write, buffer, bufferlen, &written); |
| 196 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
1 | GATEXX_CHECK_EXCEPTION(result); |
| 197 | 1 | return written; | |
| 198 | } | ||
| 199 | |||
| 200 | 1 | String Pipe::exportRead() const | |
| 201 | { | ||
| 202 | 1 | gate_string_t str = GATE_STRING_INIT_EMPTY; | |
| 203 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
1 | result_t result = gate_pipe_export(this->impl_read, &str); |
| 204 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
1 | GATEXX_CHECK_ERROR(result); |
| 205 | 2 | return String::createFrom(str); | |
| 206 | } | ||
| 207 | 1 | String Pipe::exportWrite() const | |
| 208 | { | ||
| 209 | 1 | gate_string_t str = GATE_STRING_INIT_EMPTY; | |
| 210 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
1 | result_t result = gate_pipe_export(this->impl_write, &str); |
| 211 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
1 | GATEXX_CHECK_ERROR(result); |
| 212 | 2 | return String::createFrom(str); | |
| 213 | } | ||
| 214 | |||
| 215 | ✗ | Stream Pipe::openStream(String& name, OpenModeEnum mode) | |
| 216 | { | ||
| 217 | ✗ | gate_stream_t* ptr_strm = NULL; | |
| 218 | ✗ | gate_string_t str_name = GATE_STRING_INIT_EMPTY; | |
| 219 | ✗ | gate_string_duplicate(&str_name, name.c_impl()); | |
| 220 | ✗ | result_t result = gate_pipe_open_stream(&str_name, mode, &ptr_strm); | |
| 221 | ✗ | GATEXX_CHECK_EXCEPTION(result); | |
| 222 | ✗ | String new_name = String::createFrom(str_name); | |
| 223 | ✗ | name.swap(new_name); | |
| 224 | ✗ | return Stream(ptr_strm); | |
| 225 | } | ||
| 226 | |||
| 227 | |||
| 228 | |||
| 229 | } // end of namespace io | ||
| 230 | } // end of namespace gate | ||
| 231 |