Commit 74a194f2d0aac3ad6b700826667012f855596819

Authored by perry.werneck@gmail.com
1 parent d851a970

Trabalhando na classe básica

src/classlib/local.cc
... ... @@ -300,6 +300,7 @@
300 300 int (*_get_next_unprotected)(H3270 *hSession, int baddr0);
301 301 void (*_popup_va)(H3270 *session, LIB3270_NOTIFY id , const char *title, const char *message, const char *fmt, va_list);
302 302 void * (*_free)(void *);
  303 + const char * (*_get_charset)(H3270 *hSession);
303 304  
304 305 public:
305 306  
... ... @@ -346,6 +347,8 @@
346 347 { (void **) & _get_next_unprotected, "lib3270_get_next_unprotected" },
347 348 { (void **) & _popup_va, "lib3270_popup_va" },
348 349 { (void **) & _free, "lib3270_free" },
  350 + { (void **) & _get_charset, "lib3270_get_charset" },
  351 +
349 352 };
350 353  
351 354  
... ... @@ -366,6 +369,8 @@
366 369 set_trace_handler(tracehandler);
367 370 this->hSession = lib3270_new("");
368 371  
  372 + set_charset();
  373 +
369 374 }
370 375  
371 376 virtual ~local()
... ... @@ -535,6 +540,10 @@
535 540 return 0;
536 541 }
537 542  
  543 + const char * get_charset(void)
  544 + {
  545 + return _get_charset(hSession);
  546 + }
538 547  
539 548 };
540 549  
... ...
src/classlib/session.cc
... ... @@ -29,6 +29,8 @@
29 29  
30 30 #include <stdarg.h>
31 31 #include <stdio.h>
  32 + #include <string.h>
  33 + #include <malloc.h>
32 34  
33 35 #include <pw3270/class.h>
34 36  
... ... @@ -46,6 +48,11 @@
46 48  
47 49 session::session()
48 50 {
  51 +#ifdef HAVE_ICONV
  52 + this->conv2Local = (iconv_t) (-1);
  53 + this->conv2Host = (iconv_t) (-1);
  54 +#endif
  55 +
49 56 if(first)
50 57 {
51 58 prev = last;
... ... @@ -58,10 +65,21 @@
58 65 prev = next = 0;
59 66 first = last = this;
60 67 }
  68 +
61 69 }
62 70  
63 71 session::~session()
64 72 {
  73 +#ifdef HAVE_ICONV
  74 +
  75 + if(this->conv2Local != (iconv_t) (-1))
  76 + iconv_close(this->conv2Local);
  77 +
  78 + if(this->conv2Host != (iconv_t) (-1))
  79 + iconv_close(this->conv2Host);
  80 +
  81 +#endif
  82 +
65 83 if(prev)
66 84 prev->next = next;
67 85 else
... ... @@ -103,11 +121,38 @@
103 121 }
104 122  
105 123 // Object settings
106   - void session::set_charset(const char *charset)
  124 + void session::set_charset(const char *remote, const char *local)
107 125 {
  126 +#ifdef HAVE_ICONV
  127 +
  128 + if(this->conv2Local != (iconv_t) (-1))
  129 + iconv_close(this->conv2Local);
  130 +
  131 + if(this->conv2Host != (iconv_t) (-1))
  132 + iconv_close(this->conv2Host);
  133 +
  134 + if(!remote)
  135 + remote = this->get_charset();
  136 +
  137 + if(strcmp(local,remote))
  138 + {
  139 + // Local and remote charsets aren't the same, setup conversion
  140 + conv2Local = iconv_open(local, remote);
  141 + conv2Host = iconv_open(remote,local);
  142 + }
  143 + else
  144 + {
  145 + conv2Local = conv2Host = (iconv_t)(-1);
  146 + }
  147 +#endif
108 148  
109 149 }
110 150  
  151 + const char * session::get_charset(void)
  152 + {
  153 + return "ISO-8859-1";
  154 + }
  155 +
111 156 // 3270 methods
112 157 string session::get_version(void)
113 158 {
... ... @@ -189,6 +234,57 @@
189 234 return NULL;
190 235 }
191 236  
  237 + string * session::get_3270_text(string *str)
  238 + {
  239 +#ifdef HAVE_ICONV
  240 + if(str && conv2Host != (iconv_t)(-1))
  241 + {
  242 + size_t in = str->length();
  243 + size_t out = (in << 1);
  244 + char * ptr;
  245 + char * outBuffer = (char *) malloc(out);
  246 + ICONV_CONST char * inBuffer = (ICONV_CONST char *) str->c_str();
  247 +
  248 + memset(ptr=outBuffer,0,out);
  249 +
  250 + iconv(conv2Host,NULL,NULL,NULL,NULL); // Reset state
  251 +
  252 + if(iconv(conv2Host,&inBuffer,&in,&ptr,&out) != ((size_t) -1))
  253 + str->assign(outBuffer);
  254 +
  255 + free(outBuffer);
  256 + }
  257 +#endif // HAVE_ICONV
  258 +
  259 + return str;
  260 + }
  261 +
  262 + string * session::get_local_text(string *str)
  263 + {
  264 +#ifdef HAVE_ICONV
  265 + if(str && conv2Local != (iconv_t)(-1))
  266 + {
  267 + size_t in = str->length();
  268 + size_t out = (in << 1);
  269 + char * ptr;
  270 + char * outBuffer = (char *) malloc(out);
  271 + ICONV_CONST char * inBuffer = (ICONV_CONST char *) str->c_str();
  272 +
  273 + memset(ptr=outBuffer,0,out);
  274 +
  275 + iconv(conv2Local,NULL,NULL,NULL,NULL); // Reset state
  276 +
  277 + if(iconv(conv2Local,&inBuffer,&in,&ptr,&out) != ((size_t) -1))
  278 + str->assign(outBuffer);
  279 +
  280 + free(outBuffer);
  281 + }
  282 +#endif // HAVE_ICONV
  283 +
  284 + return str;
  285 + }
  286 +
  287 +
192 288 }
193 289  
194 290  
... ...
src/classlib/testprogram.cc
... ... @@ -48,6 +48,7 @@
48 48 cout << "\tDisconnected" << endl;
49 49  
50 50 cout << "\tSession state: " << session->get_cstate() << endl;
  51 + cout << "\tCharset: " << session->get_charset() << endl;
51 52  
52 53 delete session;
53 54 return 0;
... ...
src/include/pw3270/class.h
... ... @@ -69,7 +69,13 @@
69 69  
70 70 };
71 71  
  72 +#if defined (HAVE_GNUC_VISIBILITY)
  73 + class __attribute__((visibility("default"))) session
  74 +#elif defined(WIN32)
  75 + class __declspec (dllexport) session
  76 +#else
72 77 class session
  78 +#endif
73 79 {
74 80 public:
75 81  
... ... @@ -82,9 +88,6 @@
82 88 static session * get_default(void);
83 89 static void set_plugin(session * (*factory)(const char *name));
84 90  
85   - // Object settings
86   - void set_charset(const char *charset);
87   -
88 91 // Log management
89 92 void log(const char *fmt, ...);
90 93 void logva(const char *fmt, va_list args);
... ... @@ -93,6 +96,8 @@
93 96 virtual string get_version(void);
94 97 virtual string get_revision(void);
95 98  
  99 + virtual const char * get_charset(void);
  100 +
96 101 virtual bool is_connected(void) = 0;
97 102 virtual bool is_ready(void) = 0;
98 103  
... ... @@ -139,6 +144,16 @@
139 144 virtual int popup_dialog(LIB3270_NOTIFY id , const char *title, const char *message, const char *fmt, ...);
140 145 virtual string * file_chooser_dialog(GtkFileChooserAction action, const char *title, const char *extension, const char *filename);
141 146  
  147 + string * get_3270_text(string *str);
  148 + string * get_local_text(string *str);
  149 +
  150 + protected:
  151 +#ifdef WIN32
  152 + void set_charset(const char *remote = 0, const char *local = "CP1252");
  153 +#else
  154 + void set_charset(const char *remote = 0, const char *local = "UTF-8");
  155 +#endif // WIN32
  156 +
142 157 private:
143 158  
144 159 session * prev;
... ...
src/plugins/rx3270/rx3270.cc
... ... @@ -46,7 +46,7 @@
46 46 #include <string.h>
47 47 #include <stdarg.h>
48 48  
49   - static rx3270 * factory_default(const char *type);
  49 + static rx3270 * factory_default(const char *type);
50 50  
51 51 /*--[ Globals ]--------------------------------------------------------------------------------------*/
52 52  
... ...