Commit 195ab6016cf29c78606ffb0f9500213d8d7b2b2f

Authored by Perry Werneck
1 parent c4adf69c

Adding convenience iconv wrapper to support the MSVC isolation layer.

lib3270.cbp
... ... @@ -65,6 +65,9 @@
65 65 <Unit filename="src/core/charset/getset.c">
66 66 <Option compilerVar="CC" />
67 67 </Unit>
  68 + <Unit filename="src/core/charset/iconv.c">
  69 + <Option compilerVar="CC" />
  70 + </Unit>
68 71 <Unit filename="src/core/charset/remap.c">
69 72 <Option compilerVar="CC" />
70 73 </Unit>
... ...
src/core/charset/iconv.c 0 → 100644
... ... @@ -0,0 +1,121 @@
  1 +/*
  2 + * "Software pw3270, desenvolvido com base nos códigos fontes do WC3270 e X3270
  3 + * (Paul Mattes Paul.Mattes@usa.net), de emulação de terminal 3270 para acesso a
  4 + * aplicativos mainframe. Registro no INPI sob o nome G3270. Registro no INPI sob o nome G3270.
  5 + *
  6 + * Copyright (C) <2008> <Banco do Brasil S.A.>
  7 + *
  8 + * Este programa é software livre. Você pode redistribuí-lo e/ou modificá-lo sob
  9 + * os termos da GPL v.2 - Licença Pública Geral GNU, conforme publicado pela
  10 + * Free Software Foundation.
  11 + *
  12 + * Este programa é distribuído na expectativa de ser útil, mas SEM QUALQUER
  13 + * GARANTIA; sem mesmo a garantia implícita de COMERCIALIZAÇÃO ou de ADEQUAÇÃO
  14 + * A QUALQUER PROPÓSITO EM PARTICULAR. Consulte a Licença Pública Geral GNU para
  15 + * obter mais detalhes.
  16 + *
  17 + * Você deve ter recebido uma cópia da Licença Pública Geral GNU junto com este
  18 + * programa; se não, escreva para a Free Software Foundation, Inc., 51 Franklin
  19 + * St, Fifth Floor, Boston, MA 02110-1301 USA
  20 + *
  21 + * Este programa está nomeado como charset.c e possui - linhas de código.
  22 + *
  23 + * Contatos:
  24 + *
  25 + * perry.werneck@gmail.com (Alexandre Perry de Souza Werneck)
  26 + * erico.mendonca@gmail.com (Erico Mascarenhas Mendonça)
  27 + *
  28 + */
  29 +
  30 +/**
  31 + * @file charset/iconv.c
  32 + *
  33 + * @brief This module implements the ICONV wrapper methods.
  34 + */
  35 +
  36 +#include <config.h>
  37 +#include <lib3270.h>
  38 +#include <lib3270/charset.h>
  39 +#include <iconv.h>
  40 +#include <string.h>
  41 +
  42 +struct _lib3270_iconv
  43 +{
  44 + /// @brief Convert strings from host codepage to local codepage.
  45 + iconv_t local;
  46 +
  47 + /// @brief Convert string from local codepage to host codepage.
  48 + iconv_t host;
  49 +};
  50 +
  51 +/*---[ Implement ]------------------------------------------------------------------------------------------------------------*/
  52 +
  53 + LIB3270_ICONV * lib3270_iconv_new(const char *remote, const char *local)
  54 + {
  55 + LIB3270_ICONV * converter = lib3270_malloc(sizeof(LIB3270_ICONV));
  56 + memset(converter,0,sizeof(LIB3270_ICONV));
  57 +
  58 + if(strcmp(local,remote)) {
  59 +
  60 + // Local and remote charsets aren't the same, setup conversion
  61 + converter->local = iconv_open(local, remote);
  62 + converter->host = iconv_open(remote,local);
  63 +
  64 + } else {
  65 +
  66 + // Same charset, doesn't convert
  67 + converter->local = converter->host = (iconv_t)(-1);
  68 +
  69 + }
  70 +
  71 + return converter;
  72 + }
  73 +
  74 + void lib3270_iconv_free(LIB3270_ICONV *conv)
  75 + {
  76 + if(conv->local != (iconv_t) (-1))
  77 + iconv_close(conv->local);
  78 +
  79 + if(conv->host != (iconv_t) (-1))
  80 + iconv_close(conv->host);
  81 +
  82 + conv->local = conv->host = (iconv_t) (-1);
  83 +
  84 + lib3270_free(conv);
  85 + }
  86 +
  87 + static char *convert(iconv_t *converter, const char * str, int length)
  88 + {
  89 + if(length < 0)
  90 + length = (int) strlen(str);
  91 +
  92 + if(length && converter != (iconv_t)(-1))
  93 + {
  94 +
  95 + size_t in = length;
  96 + size_t out = (length << 1);
  97 + char * ptr;
  98 + char * outBuffer = (char *) lib3270_malloc(out);
  99 + ICONV_CONST char * inBuffer = (ICONV_CONST char *) str;
  100 +
  101 + memset(ptr=outBuffer,0,out);
  102 +
  103 + iconv(converter,NULL,NULL,NULL,NULL); // Reset state
  104 +
  105 + if(iconv(converter,&inBuffer,&in,&ptr,&out) != ((size_t) -1))
  106 + return (char *) outBuffer;
  107 +
  108 + }
  109 +
  110 + return NULL;
  111 + }
  112 +
  113 + char * lib3270_iconv_from_host(LIB3270_ICONV *conv, const char *str, int len)
  114 + {
  115 + return convert(&conv->local,str,len);
  116 + }
  117 +
  118 + char * lib3270_iconv_to_host(LIB3270_ICONV *conv, const char *str, int len)
  119 + {
  120 + return convert(&conv->host,str,len);
  121 + }
... ...
src/core/linux/connect.c
... ... @@ -42,10 +42,6 @@
42 42 #include <unistd.h>
43 43 #include <fcntl.h>
44 44  
45   -// #ifdef HAVE_ICONV
46   -// #include <iconv.h>
47   -// #endif // HAVE_ICONV
48   -
49 45 #define SOCK_CLOSE(s) close(s->connection.sock); s->connection.sock = -1;
50 46  
51 47 #include <stdlib.h>
... ...
src/include/lib3270/charset.h
... ... @@ -92,6 +92,28 @@
92 92 */
93 93 LIB3270_EXPORT unsigned short lib3270_translate_char(const char *id);
94 94  
  95 + typedef struct _lib3270_iconv LIB3270_ICONV;
  96 +
  97 + ///
  98 + /// @brief Create a new ICONV wrapper.
  99 + ///
  100 + LIB3270_EXPORT LIB3270_ICONV * lib3270_iconv_new(const char *remote, const char *local);
  101 +
  102 + ///
  103 + /// @brief Release the ICONV Wrapper.
  104 + ///
  105 + LIB3270_EXPORT void lib3270_iconv_free(LIB3270_ICONV *conv);
  106 +
  107 + ///
  108 + /// @brief Convert from host to local.
  109 + ///
  110 + LIB3270_EXPORT char * lib3270_iconv_from_host(LIB3270_ICONV *conv, const char *str, int len);
  111 +
  112 + ///
  113 + /// @brief Convert from local to host.
  114 + ///
  115 + LIB3270_EXPORT char * lib3270_iconv_to_host(LIB3270_ICONV *conv, const char *str, int len);
  116 +
95 117 #ifdef __cplusplus
96 118 }
97 119 #endif
... ...