| 1 | /*************************************************************************** |
|---|
| 2 | * ioStreamChannel.cc |
|---|
| 3 | * |
|---|
| 4 | * Sat Jun 13 2009 |
|---|
| 5 | * Copyright 2009 Dmytro Milinevskyy |
|---|
| 6 | * milinevskyy@gmail.com |
|---|
| 7 | ****************************************************************************/ |
|---|
| 8 | |
|---|
| 9 | /* |
|---|
| 10 | * This program is free software; you can redistribute it and/or modify |
|---|
| 11 | * it under the terms of the GNU Lesser General Public License version 2.1 as published by |
|---|
| 12 | * the Free Software Foundation; |
|---|
| 13 | * |
|---|
| 14 | * This program is distributed in the hope that it will be useful, |
|---|
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 17 | * GNU Library General Public License for more details. |
|---|
| 18 | * |
|---|
| 19 | * You should have received a copy of the GNU Lesser General Public License |
|---|
| 20 | * along with this program; if not, write to the Free Software |
|---|
| 21 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|---|
| 22 | */ |
|---|
| 23 | |
|---|
| 24 | /** |
|---|
| 25 | * vim indentation settings |
|---|
| 26 | * set tabstop=4 |
|---|
| 27 | * set shiftwidth=4 |
|---|
| 28 | */ |
|---|
| 29 | |
|---|
| 30 | #include <libdodo/directives.h> |
|---|
| 31 | |
|---|
| 32 | #include <string.h> |
|---|
| 33 | |
|---|
| 34 | #include <libdodo/ioStreamChannel.h> |
|---|
| 35 | #include <libdodo/exceptionBasic.h> |
|---|
| 36 | #include <libdodo/ioChannel.h> |
|---|
| 37 | #include <libdodo/xexec.h> |
|---|
| 38 | #include <libdodo/types.h> |
|---|
| 39 | #include <libdodo/pcSyncStack.h> |
|---|
| 40 | |
|---|
| 41 | using namespace dodo::io::stream; |
|---|
| 42 | |
|---|
| 43 | channel::channel(short protection) : io::channel(protection) |
|---|
| 44 | { |
|---|
| 45 | } |
|---|
| 46 | |
|---|
| 47 | //------------------------------------------------------------------- |
|---|
| 48 | |
|---|
| 49 | channel::~channel() |
|---|
| 50 | { |
|---|
| 51 | } |
|---|
| 52 | |
|---|
| 53 | //------------------------------------------------------------------- |
|---|
| 54 | |
|---|
| 55 | dodo::string |
|---|
| 56 | channel::read() const |
|---|
| 57 | { |
|---|
| 58 | pc::sync::stack pg(keeper); |
|---|
| 59 | |
|---|
| 60 | dodo::string data; |
|---|
| 61 | unsigned long n; |
|---|
| 62 | |
|---|
| 63 | #ifndef IO_WO_XEXEC |
|---|
| 64 | performPreExec(OPERATION_READ); |
|---|
| 65 | |
|---|
| 66 | collectedData.buffer.reserve(bs); |
|---|
| 67 | #endif |
|---|
| 68 | |
|---|
| 69 | data = dodo::string('\0', bs); |
|---|
| 70 | |
|---|
| 71 | dodo_try { |
|---|
| 72 | n = _read((char *)data.data()); |
|---|
| 73 | data.resize(n); |
|---|
| 74 | } dodo_catch (exception::basic *e UNUSED) { |
|---|
| 75 | data.clear(); |
|---|
| 76 | |
|---|
| 77 | #ifndef IO_WO_XEXEC |
|---|
| 78 | collectedData.buffer.clear(); |
|---|
| 79 | #endif |
|---|
| 80 | |
|---|
| 81 | dodo_rethrow; |
|---|
| 82 | } |
|---|
| 83 | |
|---|
| 84 | #ifndef IO_WO_XEXEC |
|---|
| 85 | collectedData.buffer = data; |
|---|
| 86 | |
|---|
| 87 | performPostExec(OPERATION_READ); |
|---|
| 88 | |
|---|
| 89 | data = collectedData.buffer; |
|---|
| 90 | |
|---|
| 91 | collectedData.buffer.clear(); |
|---|
| 92 | #else |
|---|
| 93 | #endif |
|---|
| 94 | |
|---|
| 95 | return data; |
|---|
| 96 | } |
|---|
| 97 | |
|---|
| 98 | //------------------------------------------------------------------- |
|---|
| 99 | |
|---|
| 100 | dodo::string |
|---|
| 101 | channel::readString() const |
|---|
| 102 | { |
|---|
| 103 | pc::sync::stack pg(keeper); |
|---|
| 104 | |
|---|
| 105 | dodo::string data; |
|---|
| 106 | |
|---|
| 107 | #ifndef IO_WO_XEXEC |
|---|
| 108 | performPreExec(OPERATION_READSTRING); |
|---|
| 109 | #endif |
|---|
| 110 | |
|---|
| 111 | data = dodo::string('\0', bs); |
|---|
| 112 | unsigned long n = 0; |
|---|
| 113 | |
|---|
| 114 | dodo_try { |
|---|
| 115 | n = _readString((char *)data.data()); |
|---|
| 116 | data.resize(n); |
|---|
| 117 | } dodo_catch (exception::basic *e UNUSED) { |
|---|
| 118 | data.clear(); |
|---|
| 119 | |
|---|
| 120 | dodo_rethrow; |
|---|
| 121 | } |
|---|
| 122 | |
|---|
| 123 | #ifndef IO_WO_XEXEC |
|---|
| 124 | collectedData.buffer = data; |
|---|
| 125 | |
|---|
| 126 | performPostExec(OPERATION_READSTRING); |
|---|
| 127 | |
|---|
| 128 | data = collectedData.buffer; |
|---|
| 129 | |
|---|
| 130 | collectedData.buffer.clear(); |
|---|
| 131 | #endif |
|---|
| 132 | |
|---|
| 133 | return data; |
|---|
| 134 | } |
|---|
| 135 | |
|---|
| 136 | //------------------------------------------------------------------- |
|---|
| 137 | |
|---|
| 138 | unsigned long |
|---|
| 139 | channel::write(const dodo::string &data) const |
|---|
| 140 | { |
|---|
| 141 | pc::sync::stack pg(keeper); |
|---|
| 142 | |
|---|
| 143 | unsigned long n; |
|---|
| 144 | |
|---|
| 145 | #ifndef IO_WO_XEXEC |
|---|
| 146 | collectedData.buffer = data; |
|---|
| 147 | |
|---|
| 148 | performPreExec(OPERATION_WRITE); |
|---|
| 149 | |
|---|
| 150 | dodo_try { |
|---|
| 151 | n = _write(collectedData.buffer.data()); |
|---|
| 152 | } dodo_catch (exception::basic *e UNUSED) { |
|---|
| 153 | collectedData.buffer.clear(); |
|---|
| 154 | |
|---|
| 155 | dodo_rethrow; |
|---|
| 156 | } |
|---|
| 157 | #else |
|---|
| 158 | n = _write(data.data()); |
|---|
| 159 | #endif |
|---|
| 160 | |
|---|
| 161 | #ifndef IO_WO_XEXEC |
|---|
| 162 | performPostExec(OPERATION_WRITE); |
|---|
| 163 | |
|---|
| 164 | collectedData.buffer.clear(); |
|---|
| 165 | #endif |
|---|
| 166 | |
|---|
| 167 | return n; |
|---|
| 168 | } |
|---|
| 169 | |
|---|
| 170 | //------------------------------------------------------------------- |
|---|
| 171 | |
|---|
| 172 | unsigned long |
|---|
| 173 | channel::writeString(const dodo::string &data) const |
|---|
| 174 | { |
|---|
| 175 | pc::sync::stack pg(keeper); |
|---|
| 176 | |
|---|
| 177 | unsigned long n; |
|---|
| 178 | |
|---|
| 179 | #ifndef IO_WO_XEXEC |
|---|
| 180 | collectedData.buffer = data; |
|---|
| 181 | |
|---|
| 182 | performPreExec(OPERATION_WRITESTRING); |
|---|
| 183 | |
|---|
| 184 | dodo_try { |
|---|
| 185 | n = _writeString(collectedData.buffer.data()); |
|---|
| 186 | } dodo_catch (exception::basic *e UNUSED) { |
|---|
| 187 | collectedData.buffer.clear(); |
|---|
| 188 | |
|---|
| 189 | dodo_rethrow; |
|---|
| 190 | } |
|---|
| 191 | #else |
|---|
| 192 | n = _writeString(data.data()); |
|---|
| 193 | #endif |
|---|
| 194 | |
|---|
| 195 | #ifndef IO_WO_XEXEC |
|---|
| 196 | performPostExec(OPERATION_WRITESTRING); |
|---|
| 197 | |
|---|
| 198 | collectedData.buffer.clear(); |
|---|
| 199 | #endif |
|---|
| 200 | |
|---|
| 201 | return n; |
|---|
| 202 | } |
|---|
| 203 | |
|---|
| 204 | //------------------------------------------------------------------- |
|---|
| 205 | |
|---|