Commit 14b4a07654af63a2ae79f8a4ef15d61acc2862ed

Authored by Perry Werneck
1 parent 83a5155f

Fixing memory leaks, working on C++ API.

@@ -47,3 +47,4 @@ SRPMS @@ -47,3 +47,4 @@ SRPMS
47 resources.rc 47 resources.rc
48 makeruntime.sh 48 makeruntime.sh
49 scripts 49 scripts
  50 +vgcore.*
src/include/lib3270++.h
@@ -203,9 +203,8 @@ @@ -203,9 +203,8 @@
203 virtual void waitForReady(time_t timeout = 5) throw() = 0; 203 virtual void waitForReady(time_t timeout = 5) throw() = 0;
204 204
205 // Gets 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 inline operator std::string() const { 209 inline operator std::string() const {
211 return toString(); 210 return toString();
@@ -281,9 +280,7 @@ @@ -281,9 +280,7 @@
281 return session->getConnectionState() == state; 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 inline ProgramMessage getProgramMessage() const { 285 inline ProgramMessage getProgramMessage() const {
289 return session->getProgramMessage(); 286 return session->getProgramMessage();
@@ -335,10 +332,7 @@ @@ -335,10 +332,7 @@
335 return *this; 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 // Get contents. 337 // Get contents.
344 338
@@ -357,6 +351,8 @@ @@ -357,6 +351,8 @@
357 return *this; 351 return *this;
358 } 352 }
359 353
  354 + std::string toString() const;
  355 +
360 // Event listeners 356 // Event listeners
361 inline Host & insert(Event::Type type, std::function <void(const Event &event)> listener) noexcept { 357 inline Host & insert(Event::Type type, std::function <void(const Event &event)> listener) noexcept {
362 session->insert(type, listener); 358 session->insert(type, listener);
@@ -381,16 +377,17 @@ @@ -381,16 +377,17 @@
381 return session.pop(value); 377 return session.pop(value);
382 } 378 }
383 379
384 - template <typename T> 380 + template <typename T>
385 inline TN3270_PUBLIC TN3270::Host & operator<<(TN3270::Host& host, const T value) { 381 inline TN3270_PUBLIC TN3270::Host & operator<<(TN3270::Host& host, const T value) {
386 return host.push(value); 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 #endif 391 #endif
395 392
396 #endif // LIB3270_H_INCLUDED 393 #endif // LIB3270_H_INCLUDED
src/lib3270++/host.cc
@@ -55,8 +55,15 @@ @@ -55,8 +55,15 @@
55 this->session = nullptr; 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 /// @brief Writes characters to the associated file from the put area 64 /// @brief Writes characters to the associated file from the put area
59 int Host::sync() { 65 int Host::sync() {
  66 + this->session->waitForReady();
60 return 0; 67 return 0;
61 } 68 }
62 69
@@ -74,5 +81,20 @@ @@ -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,7 +8,7 @@
8 <Option compiler="gcc" /> 8 <Option compiler="gcc" />
9 <Build> 9 <Build>
10 <Target title="Debug"> 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 <Option object_output=".obj/Debug/" /> 12 <Option object_output=".obj/Debug/" />
13 <Option type="1" /> 13 <Option type="1" />
14 <Option compiler="gcc" /> 14 <Option compiler="gcc" />
@@ -42,6 +42,8 @@ @@ -42,6 +42,8 @@
42 </Linker> 42 </Linker>
43 <Unit filename="../include/lib3270++.h" /> 43 <Unit filename="../include/lib3270++.h" />
44 <Unit filename="../include/lib3270.h" /> 44 <Unit filename="../include/lib3270.h" />
  45 + <Unit filename="../include/lib3270/action_table.h" />
  46 + <Unit filename="../include/lib3270/actions.h" />
45 <Unit filename="../include/lib3270/popup.h" /> 47 <Unit filename="../include/lib3270/popup.h" />
46 <Unit filename="../include/lib3270/session.h" /> 48 <Unit filename="../include/lib3270/session.h" />
47 <Unit filename="abstract.cc" /> 49 <Unit filename="abstract.cc" />
src/lib3270++/local/session.cc
@@ -37,6 +37,7 @@ @@ -37,6 +37,7 @@
37 */ 37 */
38 38
39 #include "../private.h" 39 #include "../private.h"
  40 + #include <lib3270/actions.h>
40 41
41 extern "C" { 42 extern "C" {
42 #include <lib3270/actions.h> 43 #include <lib3270/actions.h>
@@ -82,6 +83,8 @@ @@ -82,6 +83,8 @@
82 83
83 void Local::Session::wait(time_t timeout) { 84 void Local::Session::wait(time_t timeout) {
84 85
  86 + std::lock_guard<std::mutex> lock(sync);
  87 +
85 int rc = lib3270_wait_for_ready(this->hSession, timeout); 88 int rc = lib3270_wait_for_ready(this->hSession, timeout);
86 89
87 if(rc) { 90 if(rc) {
@@ -98,9 +101,7 @@ @@ -98,9 +101,7 @@
98 throw std::system_error(rc, std::system_category()); 101 throw std::system_error(rc, std::system_category());
99 } 102 }
100 103
101 - wait();  
102 -  
103 - } 104 + }
104 105
105 void Local::Session::disconnect() { 106 void Local::Session::disconnect() {
106 std::lock_guard<std::mutex> lock(sync); 107 std::lock_guard<std::mutex> lock(sync);
@@ -109,23 +110,42 @@ @@ -109,23 +110,42 @@
109 110
110 // Wait for session state. 111 // Wait for session state.
111 void Local::Session::waitForReady(time_t timeout) throw() { 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 std::lock_guard<std::mutex> lock(const_cast<Local::Session *>(this)->sync); 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 ProgramMessage Local::Session::getProgramMessage() const { 151 ProgramMessage Local::Session::getProgramMessage() const {
@@ -168,6 +188,30 @@ @@ -168,6 +188,30 @@
168 188
169 TN3270::Session & Local::Session::push(const Action action) { 189 TN3270::Session & Local::Session::push(const Action action) {
170 std::lock_guard<std::mutex> lock(sync); 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 return *this; 215 return *this;
172 } 216 }
173 217
src/lib3270++/private.h
@@ -45,6 +45,7 @@ @@ -45,6 +45,7 @@
45 #include <lib3270++.h> 45 #include <lib3270++.h>
46 #include <lib3270/popup.h> 46 #include <lib3270/popup.h>
47 #include <system_error> 47 #include <system_error>
  48 + #include <stdexcept>
48 49
49 50
50 #ifdef HAVE_ICONV 51 #ifdef HAVE_ICONV
@@ -133,9 +134,8 @@ @@ -133,9 +134,8 @@
133 void waitForReady(time_t timeout = 5) throw() override; 134 void waitForReady(time_t timeout = 5) throw() override;
134 135
135 // Gets 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 ProgramMessage getProgramMessage() const override; 140 ProgramMessage getProgramMessage() const override;
141 141
src/lib3270++/testprogram/testprogram.cc
@@ -39,6 +39,8 @@ @@ -39,6 +39,8 @@
39 #include <cstdlib> 39 #include <cstdlib>
40 #include <lib3270++.h> 40 #include <lib3270++.h>
41 41
  42 + using namespace std;
  43 +
42 /*---[ Implement ]----------------------------------------------------------------------------------*/ 44 /*---[ Implement ]----------------------------------------------------------------------------------*/
43 45
44 int main(int argc, const char *argv[]) { 46 int main(int argc, const char *argv[]) {
@@ -46,6 +48,11 @@ @@ -46,6 +48,11 @@
46 TN3270::Host host; 48 TN3270::Host host;
47 49
48 host.connect(getenv("TN3270URL")); 50 host.connect(getenv("TN3270URL"));
  51 + cout << host << endl;
  52 +
  53 + host << TN3270::ENTER;
  54 +
  55 +
49 56
50 return 0; 57 return 0;
51 } 58 }
src/lib3270/session.c
@@ -108,6 +108,8 @@ void lib3270_session_free(H3270 *h) @@ -108,6 +108,8 @@ void lib3270_session_free(H3270 *h)
108 release_pointer(h->text); 108 release_pointer(h->text);
109 release_pointer(h->zero_buf); 109 release_pointer(h->zero_buf);
110 110
  111 + release_pointer(h->obuf_base);
  112 +
111 release_pointer(h->sbbuf); 113 release_pointer(h->sbbuf);
112 release_pointer(h->tabs); 114 release_pointer(h->tabs);
113 115
src/lib3270/testprogram/testprogram.c
@@ -26,6 +26,9 @@ int main(int numpar, char *param[]) @@ -26,6 +26,9 @@ int main(int numpar, char *param[])
26 26
27 lib3270_wait_for_ready(h,10); 27 lib3270_wait_for_ready(h,10);
28 28
  29 + lib3270_enter(h);
  30 +
  31 + lib3270_wait_for_ready(h,10);
29 32
30 lib3270_session_free(h); 33 lib3270_session_free(h);
31 34