Commit 14b4a07654af63a2ae79f8a4ef15d61acc2862ed
1 parent
83a5155f
Exists in
master
and in
3 other branches
Fixing memory leaks, working on C++ API.
Showing
9 changed files
with
109 additions
and
31 deletions
Show diff stats
.gitignore
src/include/lib3270++.h
| ... | ... | @@ -203,9 +203,8 @@ |
| 203 | 203 | virtual void waitForReady(time_t timeout = 5) throw() = 0; |
| 204 | 204 | |
| 205 | 205 | // Gets |
| 206 | - virtual std::string toString() const = 0; | |
| 207 | - virtual std::string toString(int baddr = 0, size_t len = -1, bool lf = false) = 0; | |
| 208 | - virtual std::string toString(int row, int col, size_t sz, bool lf = false) = 0; | |
| 206 | + virtual std::string toString(int baddr = 0, size_t len = -1, char lf = '\n') const = 0; | |
| 207 | + virtual std::string toString(int row, int col, size_t sz, char lf = '\n') const = 0; | |
| 209 | 208 | |
| 210 | 209 | inline operator std::string() const { |
| 211 | 210 | return toString(); |
| ... | ... | @@ -281,9 +280,7 @@ |
| 281 | 280 | return session->getConnectionState() == state; |
| 282 | 281 | } |
| 283 | 282 | |
| 284 | - inline void connect(const char *url) { | |
| 285 | - this->session->connect(url); | |
| 286 | - } | |
| 283 | + void connect(const char *url); | |
| 287 | 284 | |
| 288 | 285 | inline ProgramMessage getProgramMessage() const { |
| 289 | 286 | return session->getProgramMessage(); |
| ... | ... | @@ -335,10 +332,7 @@ |
| 335 | 332 | return *this; |
| 336 | 333 | } |
| 337 | 334 | |
| 338 | - inline Host & push(const Action action) { | |
| 339 | - session->push(action); | |
| 340 | - return *this; | |
| 341 | - } | |
| 335 | + Host & push(const Action action); | |
| 342 | 336 | |
| 343 | 337 | // Get contents. |
| 344 | 338 | |
| ... | ... | @@ -357,6 +351,8 @@ |
| 357 | 351 | return *this; |
| 358 | 352 | } |
| 359 | 353 | |
| 354 | + std::string toString() const; | |
| 355 | + | |
| 360 | 356 | // Event listeners |
| 361 | 357 | inline Host & insert(Event::Type type, std::function <void(const Event &event)> listener) noexcept { |
| 362 | 358 | session->insert(type, listener); |
| ... | ... | @@ -381,16 +377,17 @@ |
| 381 | 377 | return session.pop(value); |
| 382 | 378 | } |
| 383 | 379 | |
| 384 | - template <typename T> | |
| 380 | + template <typename T> | |
| 385 | 381 | inline TN3270_PUBLIC TN3270::Host & operator<<(TN3270::Host& host, const T value) { |
| 386 | 382 | return host.push(value); |
| 387 | 383 | } |
| 388 | 384 | |
| 389 | - template <typename T> | |
| 390 | - inline TN3270_PUBLIC TN3270::Host & operator>>(TN3270::Host& host, const T value) { | |
| 391 | - return host.pop(value); | |
| 385 | + inline std::ostream & operator<<(std::ostream &stream, const TN3270::Host& host) { | |
| 386 | + stream << host.toString(); | |
| 387 | + return stream; | |
| 392 | 388 | } |
| 393 | 389 | |
| 390 | + | |
| 394 | 391 | #endif |
| 395 | 392 | |
| 396 | 393 | #endif // LIB3270_H_INCLUDED | ... | ... |
src/lib3270++/host.cc
| ... | ... | @@ -55,8 +55,15 @@ |
| 55 | 55 | this->session = nullptr; |
| 56 | 56 | } |
| 57 | 57 | |
| 58 | + void Host::connect(const char *url) { | |
| 59 | + this->session->connect(url); | |
| 60 | + sync(); | |
| 61 | + } | |
| 62 | + | |
| 63 | + | |
| 58 | 64 | /// @brief Writes characters to the associated file from the put area |
| 59 | 65 | int Host::sync() { |
| 66 | + this->session->waitForReady(); | |
| 60 | 67 | return 0; |
| 61 | 68 | } |
| 62 | 69 | |
| ... | ... | @@ -74,5 +81,20 @@ |
| 74 | 81 | |
| 75 | 82 | } |
| 76 | 83 | |
| 84 | + Host & Host::push(const Action action) { | |
| 85 | + session->push(action); | |
| 86 | + sync(); | |
| 87 | + return *this; | |
| 88 | + } | |
| 89 | + | |
| 90 | + std::string Host::toString() const { | |
| 91 | + | |
| 92 | + if(this->session->getConnectionState() == TN3270::DISCONNECTED) { | |
| 93 | + throw std::system_error(ENOTCONN, std::system_category()); | |
| 94 | + } | |
| 95 | + | |
| 96 | + return this->session->toString(); | |
| 97 | + } | |
| 98 | + | |
| 77 | 99 | } |
| 78 | 100 | ... | ... |
src/lib3270++/lib3270++.cbp
| ... | ... | @@ -8,7 +8,7 @@ |
| 8 | 8 | <Option compiler="gcc" /> |
| 9 | 9 | <Build> |
| 10 | 10 | <Target title="Debug"> |
| 11 | - <Option output=".bin/Debug/C++ Bindings for lib3270" prefix_auto="1" extension_auto="1" /> | |
| 11 | + <Option output="../../.bin/Debug/lib3270++" prefix_auto="1" extension_auto="1" /> | |
| 12 | 12 | <Option object_output=".obj/Debug/" /> |
| 13 | 13 | <Option type="1" /> |
| 14 | 14 | <Option compiler="gcc" /> |
| ... | ... | @@ -42,6 +42,8 @@ |
| 42 | 42 | </Linker> |
| 43 | 43 | <Unit filename="../include/lib3270++.h" /> |
| 44 | 44 | <Unit filename="../include/lib3270.h" /> |
| 45 | + <Unit filename="../include/lib3270/action_table.h" /> | |
| 46 | + <Unit filename="../include/lib3270/actions.h" /> | |
| 45 | 47 | <Unit filename="../include/lib3270/popup.h" /> |
| 46 | 48 | <Unit filename="../include/lib3270/session.h" /> |
| 47 | 49 | <Unit filename="abstract.cc" /> | ... | ... |
src/lib3270++/local/session.cc
| ... | ... | @@ -37,6 +37,7 @@ |
| 37 | 37 | */ |
| 38 | 38 | |
| 39 | 39 | #include "../private.h" |
| 40 | + #include <lib3270/actions.h> | |
| 40 | 41 | |
| 41 | 42 | extern "C" { |
| 42 | 43 | #include <lib3270/actions.h> |
| ... | ... | @@ -82,6 +83,8 @@ |
| 82 | 83 | |
| 83 | 84 | void Local::Session::wait(time_t timeout) { |
| 84 | 85 | |
| 86 | + std::lock_guard<std::mutex> lock(sync); | |
| 87 | + | |
| 85 | 88 | int rc = lib3270_wait_for_ready(this->hSession, timeout); |
| 86 | 89 | |
| 87 | 90 | if(rc) { |
| ... | ... | @@ -98,9 +101,7 @@ |
| 98 | 101 | throw std::system_error(rc, std::system_category()); |
| 99 | 102 | } |
| 100 | 103 | |
| 101 | - wait(); | |
| 102 | - | |
| 103 | - } | |
| 104 | + } | |
| 104 | 105 | |
| 105 | 106 | void Local::Session::disconnect() { |
| 106 | 107 | std::lock_guard<std::mutex> lock(sync); |
| ... | ... | @@ -109,23 +110,42 @@ |
| 109 | 110 | |
| 110 | 111 | // Wait for session state. |
| 111 | 112 | void Local::Session::waitForReady(time_t timeout) throw() { |
| 113 | + this->wait(timeout); | |
| 114 | + } | |
| 112 | 115 | |
| 113 | - std::lock_guard<std::mutex> lock(sync); | |
| 114 | - wait(timeout); | |
| 116 | + std::string Local::Session::toString(int baddr, size_t len, char lf) const { | |
| 117 | + | |
| 118 | + std::lock_guard<std::mutex> lock(const_cast<Local::Session *>(this)->sync); | |
| 119 | + | |
| 120 | + char * text = lib3270_get_text(hSession, baddr, len, lf); | |
| 121 | + | |
| 122 | + if(!text) { | |
| 123 | + throw std::runtime_error("Can't get screen contents"); | |
| 124 | + } | |
| 125 | + | |
| 126 | + string rc = convertFromHost(text); | |
| 127 | + | |
| 128 | + lib3270_free(text); | |
| 129 | + | |
| 130 | + return rc; | |
| 115 | 131 | |
| 116 | 132 | } |
| 117 | 133 | |
| 118 | - // Gets | |
| 119 | - std::string Local::Session::toString() const { | |
| 134 | + std::string Local::Session::toString(int row, int col, size_t sz, char lf) const { | |
| 135 | + | |
| 120 | 136 | std::lock_guard<std::mutex> lock(const_cast<Local::Session *>(this)->sync); |
| 121 | - } | |
| 122 | 137 | |
| 123 | - std::string Local::Session::toString(int baddr, size_t len, bool lf) { | |
| 124 | - std::lock_guard<std::mutex> lock(sync); | |
| 125 | - } | |
| 138 | + char * text = lib3270_get_text_at(hSession, row, col, sz, lf); | |
| 126 | 139 | |
| 127 | - std::string Local::Session::toString(int row, int col, size_t sz, bool lf) { | |
| 128 | - std::lock_guard<std::mutex> lock(sync); | |
| 140 | + if(!text) { | |
| 141 | + throw std::runtime_error("Can't get screen contents"); | |
| 142 | + } | |
| 143 | + | |
| 144 | + string rc = convertFromHost(text); | |
| 145 | + | |
| 146 | + lib3270_free(text); | |
| 147 | + | |
| 148 | + return rc; | |
| 129 | 149 | } |
| 130 | 150 | |
| 131 | 151 | ProgramMessage Local::Session::getProgramMessage() const { |
| ... | ... | @@ -168,6 +188,30 @@ |
| 168 | 188 | |
| 169 | 189 | TN3270::Session & Local::Session::push(const Action action) { |
| 170 | 190 | std::lock_guard<std::mutex> lock(sync); |
| 191 | + | |
| 192 | + switch(action) { | |
| 193 | + case ENTER: | |
| 194 | + lib3270_enter(hSession); | |
| 195 | + break; | |
| 196 | + | |
| 197 | + case ERASE: | |
| 198 | + lib3270_erase(hSession); | |
| 199 | + break; | |
| 200 | + | |
| 201 | + case ERASE_EOF: | |
| 202 | + lib3270_eraseeof(hSession); | |
| 203 | + break; | |
| 204 | + | |
| 205 | + case ERASE_EOL: | |
| 206 | + lib3270_eraseeol(hSession); | |
| 207 | + break; | |
| 208 | + | |
| 209 | + case ERASE_INPUT: | |
| 210 | + lib3270_eraseinput(hSession); | |
| 211 | + break; | |
| 212 | + | |
| 213 | + } | |
| 214 | + | |
| 171 | 215 | return *this; |
| 172 | 216 | } |
| 173 | 217 | ... | ... |
src/lib3270++/private.h
| ... | ... | @@ -45,6 +45,7 @@ |
| 45 | 45 | #include <lib3270++.h> |
| 46 | 46 | #include <lib3270/popup.h> |
| 47 | 47 | #include <system_error> |
| 48 | + #include <stdexcept> | |
| 48 | 49 | |
| 49 | 50 | |
| 50 | 51 | #ifdef HAVE_ICONV |
| ... | ... | @@ -133,9 +134,8 @@ |
| 133 | 134 | void waitForReady(time_t timeout = 5) throw() override; |
| 134 | 135 | |
| 135 | 136 | // Gets |
| 136 | - std::string toString() const override; | |
| 137 | - std::string toString(int baddr = 0, size_t len = -1, bool lf = false) override; | |
| 138 | - std::string toString(int row, int col, size_t sz, bool lf = false) override; | |
| 137 | + std::string toString(int baddr, size_t len, char lf) const override; | |
| 138 | + std::string toString(int row, int col, size_t sz, char lf) const override; | |
| 139 | 139 | |
| 140 | 140 | ProgramMessage getProgramMessage() const override; |
| 141 | 141 | ... | ... |
src/lib3270++/testprogram/testprogram.cc
| ... | ... | @@ -39,6 +39,8 @@ |
| 39 | 39 | #include <cstdlib> |
| 40 | 40 | #include <lib3270++.h> |
| 41 | 41 | |
| 42 | + using namespace std; | |
| 43 | + | |
| 42 | 44 | /*---[ Implement ]----------------------------------------------------------------------------------*/ |
| 43 | 45 | |
| 44 | 46 | int main(int argc, const char *argv[]) { |
| ... | ... | @@ -46,6 +48,11 @@ |
| 46 | 48 | TN3270::Host host; |
| 47 | 49 | |
| 48 | 50 | host.connect(getenv("TN3270URL")); |
| 51 | + cout << host << endl; | |
| 52 | + | |
| 53 | + host << TN3270::ENTER; | |
| 54 | + | |
| 55 | + | |
| 49 | 56 | |
| 50 | 57 | return 0; |
| 51 | 58 | } | ... | ... |
src/lib3270/session.c