| 1 | /*************************************************************************** |
|---|
| 2 | * ioBlockChannel.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/ioBlockChannel.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::block; |
|---|
| 42 | |
|---|
| 43 | channel::channel(short protection) : io::channel(protection), |
|---|
| 44 | pos(0), |
|---|
| 45 | append(false) |
|---|
| 46 | { |
|---|
| 47 | } |
|---|
| 48 | |
|---|
| 49 | //------------------------------------------------------------------- |
|---|
| 50 | |
|---|
| 51 | channel::~channel() |
|---|
| 52 | { |
|---|
| 53 | } |
|---|
| 54 | |
|---|
| 55 | //------------------------------------------------------------------- |
|---|
| 56 | |
|---|
| 57 | dodo::string |
|---|
| 58 | channel::read() const |
|---|
| 59 | { |
|---|
| 60 | pc::sync::stack pg(keeper); |
|---|
| 61 | |
|---|
| 62 | dodo::string data; |
|---|
| 63 | unsigned long n; |
|---|
| 64 | |
|---|
| 65 | #ifndef IO_WO_XEXEC |
|---|
| 66 | performPreExec(OPERATION_READ); |
|---|
| 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 | pos += n; |
|---|
| 96 | |
|---|
| 97 | return data; |
|---|
| 98 | } |
|---|
| 99 | |
|---|
| 100 | //------------------------------------------------------------------- |
|---|
| 101 | |
|---|
| 102 | dodo::string |
|---|
| 103 | channel::readString() const |
|---|
| 104 | { |
|---|
| 105 | pc::sync::stack pg(keeper); |
|---|
| 106 | |
|---|
| 107 | dodo::string data; |
|---|
| 108 | unsigned long n; |
|---|
| 109 | |
|---|
| 110 | #ifndef IO_WO_XEXEC |
|---|
| 111 | performPreExec(OPERATION_READSTRING); |
|---|
| 112 | #endif |
|---|
| 113 | |
|---|
| 114 | data = dodo::string('\0', bs); |
|---|
| 115 | |
|---|
| 116 | dodo_try { |
|---|
| 117 | n = _readString((char *)data.data()); |
|---|
| 118 | data.resize(n); |
|---|
| 119 | } dodo_catch (exception::basic *e UNUSED) { |
|---|
| 120 | data.clear(); |
|---|
| 121 | |
|---|
| 122 | dodo_rethrow; |
|---|
| 123 | } |
|---|
| 124 | |
|---|
| 125 | #ifndef IO_WO_XEXEC |
|---|
| 126 | collectedData.buffer = data; |
|---|
| 127 | |
|---|
| 128 | performPostExec(OPERATION_READSTRING); |
|---|
| 129 | |
|---|
| 130 | data = collectedData.buffer; |
|---|
| 131 | |
|---|
| 132 | collectedData.buffer.clear(); |
|---|
| 133 | #endif |
|---|
| 134 | |
|---|
| 135 | pos += n; |
|---|
| 136 | |
|---|
| 137 | return data; |
|---|
| 138 | } |
|---|
| 139 | |
|---|
| 140 | //------------------------------------------------------------------- |
|---|
| 141 | |
|---|
| 142 | unsigned long |
|---|
| 143 | channel::write(const dodo::string &data) const |
|---|
| 144 | { |
|---|
| 145 | pc::sync::stack pg(keeper); |
|---|
| 146 | |
|---|
| 147 | unsigned long n; |
|---|
| 148 | |
|---|
| 149 | #ifndef IO_WO_XEXEC |
|---|
| 150 | collectedData.buffer = data; |
|---|
| 151 | |
|---|
| 152 | performPreExec(OPERATION_WRITE); |
|---|
| 153 | |
|---|
| 154 | dodo_try { |
|---|
| 155 | n = _write(collectedData.buffer.data()); |
|---|
| 156 | } dodo_catch (exception::basic *e UNUSED) { |
|---|
| 157 | collectedData.buffer.clear(); |
|---|
| 158 | |
|---|
| 159 | dodo_rethrow; |
|---|
| 160 | } |
|---|
| 161 | #else |
|---|
| 162 | n = _write(data.data()); |
|---|
| 163 | #endif |
|---|
| 164 | |
|---|
| 165 | #ifndef IO_WO_XEXEC |
|---|
| 166 | performPostExec(OPERATION_WRITE); |
|---|
| 167 | |
|---|
| 168 | collectedData.buffer.clear(); |
|---|
| 169 | #endif |
|---|
| 170 | |
|---|
| 171 | pos += n; |
|---|
| 172 | |
|---|
| 173 | return n; |
|---|
| 174 | } |
|---|
| 175 | |
|---|
| 176 | //------------------------------------------------------------------- |
|---|
| 177 | |
|---|
| 178 | unsigned long |
|---|
| 179 | channel::writeString(const dodo::string &data) const |
|---|
| 180 | { |
|---|
| 181 | pc::sync::stack pg(keeper); |
|---|
| 182 | |
|---|
| 183 | unsigned long n; |
|---|
| 184 | |
|---|
| 185 | #ifndef IO_WO_XEXEC |
|---|
| 186 | collectedData.buffer = data; |
|---|
| 187 | |
|---|
| 188 | performPreExec(OPERATION_WRITESTRING); |
|---|
| 189 | |
|---|
| 190 | dodo_try { |
|---|
| 191 | n = _writeString(collectedData.buffer.data()); |
|---|
| 192 | } dodo_catch (exception::basic *e UNUSED) { |
|---|
| 193 | collectedData.buffer.clear(); |
|---|
| 194 | |
|---|
| 195 | dodo_rethrow; |
|---|
| 196 | } |
|---|
| 197 | |
|---|
| 198 | pos += n; |
|---|
| 199 | #else |
|---|
| 200 | n = _writeString(data.data()); |
|---|
| 201 | |
|---|
| 202 | pos += n; |
|---|
| 203 | #endif |
|---|
| 204 | |
|---|
| 205 | #ifndef IO_WO_XEXEC |
|---|
| 206 | performPostExec(OPERATION_WRITESTRING); |
|---|
| 207 | |
|---|
| 208 | collectedData.buffer.clear(); |
|---|
| 209 | #endif |
|---|
| 210 | |
|---|
| 211 | return n; |
|---|
| 212 | } |
|---|
| 213 | |
|---|
| 214 | //------------------------------------------------------------------- |
|---|
| 215 | |
|---|