Commit 2c5467eb44909f6eebba3f004c35a7a512fdde15
1 parent
b4a8defb
Exists in
master
and in
3 other branches
Working on C++ library.
Showing
2 changed files
with
68 additions
and
14 deletions
Show diff stats
src/lib3270++/abstract.cc
@@ -47,8 +47,8 @@ | @@ -47,8 +47,8 @@ | ||
47 | Abstract::Session::Session() { | 47 | Abstract::Session::Session() { |
48 | 48 | ||
49 | #ifdef HAVE_ICONV | 49 | #ifdef HAVE_ICONV |
50 | - this->iconv.local = (iconv_t) (-1); | ||
51 | - this->iconv.host = (iconv_t) (-1); | 50 | + this->converter.local = (iconv_t) (-1); |
51 | + this->converter.host = (iconv_t) (-1); | ||
52 | #endif | 52 | #endif |
53 | 53 | ||
54 | this->baddr = 0; | 54 | this->baddr = 0; |
@@ -59,11 +59,11 @@ | @@ -59,11 +59,11 @@ | ||
59 | 59 | ||
60 | #ifdef HAVE_ICONV | 60 | #ifdef HAVE_ICONV |
61 | 61 | ||
62 | - if(this->iconv.local != (iconv_t) (-1)) | ||
63 | - iconv_close(this->iconv.local); | 62 | + if(this->converter.local != (iconv_t) (-1)) |
63 | + iconv_close(this->converter.local); | ||
64 | 64 | ||
65 | - if(this->iconv.host != (iconv_t) (-1)) | ||
66 | - iconv_close(this->iconv.host); | 65 | + if(this->converter.host != (iconv_t) (-1)) |
66 | + iconv_close(this->converter.host); | ||
67 | 67 | ||
68 | #endif | 68 | #endif |
69 | 69 | ||
@@ -74,21 +74,21 @@ | @@ -74,21 +74,21 @@ | ||
74 | 74 | ||
75 | #ifdef HAVE_ICONV | 75 | #ifdef HAVE_ICONV |
76 | 76 | ||
77 | - if(this->iconv.local != (iconv_t) (-1)) | ||
78 | - iconv_close(iconv.local); | 77 | + if(this->converter.local != (iconv_t) (-1)) |
78 | + iconv_close(converter.local); | ||
79 | 79 | ||
80 | - if(this->iconv.host != (iconv_t) (-1)) | ||
81 | - iconv_close(iconv.host); | 80 | + if(this->converter.host != (iconv_t) (-1)) |
81 | + iconv_close(converter.host); | ||
82 | 82 | ||
83 | if(strcmp(local,remote)) { | 83 | if(strcmp(local,remote)) { |
84 | 84 | ||
85 | // Local and remote charsets aren't the same, setup conversion | 85 | // Local and remote charsets aren't the same, setup conversion |
86 | - iconv.local = iconv_open(local, remote); | ||
87 | - iconv.host = iconv_open(remote,local); | 86 | + converter.local = iconv_open(local, remote); |
87 | + converter.host = iconv_open(remote,local); | ||
88 | 88 | ||
89 | } else { | 89 | } else { |
90 | // Same charset, doesn't convert | 90 | // Same charset, doesn't convert |
91 | - iconv.local = iconv.host = (iconv_t)(-1); | 91 | + converter.local = converter.host = (iconv_t)(-1); |
92 | } | 92 | } |
93 | 93 | ||
94 | #else | 94 | #else |
@@ -100,6 +100,51 @@ | @@ -100,6 +100,51 @@ | ||
100 | 100 | ||
101 | } | 101 | } |
102 | 102 | ||
103 | + /// @brief Converte charset. | ||
104 | + std::string Abstract::Session::convertCharset(iconv_t &converter, const char *str) { | ||
105 | + | ||
106 | + std::string rc; | ||
107 | + | ||
108 | +#ifdef HAVE_ICONV | ||
109 | + size_t in = strlen(str); | ||
110 | + | ||
111 | + if(in && converter != (iconv_t)(-1)) { | ||
112 | + | ||
113 | + size_t out = (in << 1); | ||
114 | + char * ptr; | ||
115 | + char * outBuffer = (char *) malloc(out); | ||
116 | + char * inBuffer = (char *) str; | ||
117 | + | ||
118 | + memset(ptr=outBuffer,0,out); | ||
119 | + | ||
120 | + iconv(converter,NULL,NULL,NULL,NULL); // Reset state | ||
121 | + | ||
122 | + if(iconv(converter,&inBuffer,&in,&ptr,&out) != ((size_t) -1)) | ||
123 | + rc.assign(outBuffer); | ||
124 | + | ||
125 | + free(outBuffer); | ||
126 | + | ||
127 | + } | ||
128 | + | ||
129 | +#else | ||
130 | + | ||
131 | + rc = str; | ||
132 | + | ||
133 | +#endif // HAVE_ICONV | ||
134 | + | ||
135 | + return rc; | ||
136 | + } | ||
137 | + | ||
138 | + /// @brief Converte string recebida do host para o charset atual. | ||
139 | + std::string Abstract::Session::convertFromHost(const char *str) const { | ||
140 | + return convertCharset(const_cast<Abstract::Session *>(this)->converter.local,str); | ||
141 | + } | ||
142 | + | ||
143 | + /// @brief Converte string do charset atual para o charset do host. | ||
144 | + std::string Abstract::Session::convertToHost(const char *str) const { | ||
145 | + return convertCharset(const_cast<Abstract::Session *>(this)->converter.host,str); | ||
146 | + } | ||
147 | + | ||
103 | 148 | ||
104 | } | 149 | } |
105 | 150 |
src/lib3270++/private.h
@@ -72,9 +72,12 @@ | @@ -72,9 +72,12 @@ | ||
72 | /// @brief Convert string from local codepage to host codepage. | 72 | /// @brief Convert string from local codepage to host codepage. |
73 | iconv_t host; | 73 | iconv_t host; |
74 | 74 | ||
75 | - } iconv; | 75 | + } converter; |
76 | #endif | 76 | #endif |
77 | 77 | ||
78 | + /// @brief Converte charset. | ||
79 | + static std::string convertCharset(iconv_t &converter, const char *str); | ||
80 | + | ||
78 | protected: | 81 | protected: |
79 | 82 | ||
80 | /// @brief Current in/out position. | 83 | /// @brief Current in/out position. |
@@ -86,6 +89,12 @@ | @@ -86,6 +89,12 @@ | ||
86 | /// @brief Setup charsets | 89 | /// @brief Setup charsets |
87 | void setCharSet(const char *remote, const char *local = SYSTEM_CHARSET); | 90 | void setCharSet(const char *remote, const char *local = SYSTEM_CHARSET); |
88 | 91 | ||
92 | + /// @brief Converte string recebida do host para o charset atual. | ||
93 | + std::string convertFromHost(const char *str) const; | ||
94 | + | ||
95 | + /// @brief Converte string do charset atual para o charset do host. | ||
96 | + std::string convertToHost(const char *str) const; | ||
97 | + | ||
89 | }; | 98 | }; |
90 | 99 | ||
91 | } | 100 | } |