Iconv

[Mobile version(QRCode)] Total views:49,910 / Applauds for blog:1
Welcome to my page. I am an adminisrator of this site.
If you are this db's user, please contact with me by private mail. If not, please contact with me by email or twitter or facebook
Access record[Graph / PV Info.(Past 1 day / Past 1 week) / Access from outside (Yesterday / Past 1 week) / Vistors's list]
ProfilePmail(Mail)
Inbox   /Send   /Sent
Reviews(List   /Limit)
Poll   /Agree:Got   /Sent
Fan
Works/Music
Blog
[Write]
Links
My Play List
<=Newer article2013/01/20 Japanese spam blog list which I found these days
=>Older article2012/10/21 Spammer IP list

1.
2012/10/24 "Programming > C++ > Iconv"
[Show only this article / Modify / Delete / Send trackback / Add to the shared category]

1. How to use iconv from C++
2. Install iconv first
3. The source code

1. How to use iconv from C++

Here is how to use iconv from C++ code.
2. Install iconv first

VERSION=1.14;
wget http://ftp.gnu.org/pub/gnu/libiconv/libiconv-$VERSION.tar.gz
tar xvfz libiconv-$VERSION.tar.gz; 
cd libiconv-$VERSION;
./configure 
make
make install
3. The source code

You should compile the code with the following option.
-L/usr/local/lib/ -I/usr/local/include/ -liconv

And here are the source codes.

IconvWrapper.hpp
#ifndef INCLUDED_IconvWrapper
#define INCLUDED_IconvWrapper

#include <iconv.h>
#include <string>
#include <iostream>
#include <errno.h>
#include <string.h>

using namespace std;

class IconvWrapper{
public:
 IconvWrapper();
 ~IconvWrapper();
 string from_to(string src, string fromcode, string tocode);

protected:
 iconv_t open(const char* tocode, const char* fromcode);
 void close();

private:
 iconv_t conversion_descriptor;
 char* tocode_cache;
 char* fromcode_cache;
};

#endif

IconvWrapper.cpp
#include "IconvWrapper.hpp"

using namespace std;

IconvWrapper::IconvWrapper(){
 conversion_descriptor = 0;
 tocode_cache = NULL;
 fromcode_cache = NULL;
}

IconvWrapper::~IconvWrapper(){
 close();
}

iconv_t IconvWrapper::open(const char* tocode, const char* fromcode){
 if(conversion_descriptor > 0){
 close();
 }
 iconv_t ret = iconv_open(tocode, fromcode);
 if(ret > 0) {
 conversion_descriptor = ret;
 tocode_cache = new char[strlen(tocode) + 1];
 strcpy(tocode_cache, tocode);
 fromcode_cache = new char[strlen(fromcode) + 1];
 strcpy(fromcode_cache, fromcode);
 }
 else{
 conversion_descriptor = 0;
 }
 return conversion_descriptor ;
}

void IconvWrapper::close(){
 if(conversion_descriptor > 0){
 iconv_close(conversion_descriptor );
 conversion_descriptor = 0;
 }
 
 if(tocode_cache){
 delete [] tocode_cache;
 tocode_cache = NULL;
 }
 
 if(fromcode_cache){
 delete [] fromcode_cache;
 fromcode_cache = NULL;
 }
}

string IconvWrapper::from_to(string src, string fromcode, string tocode){
 string ret;
 int break_flag=0;
 
 if (!tocode_cache || !fromcode_cache || tocode != tocode_cache || fromcode != fromcode_cache) {
 open(tocode.c_str(), fromcode.c_str());
 }
 char* inbuf = const_cast<char*>(src.c_str());
 size_t inbytesleft = src.size();

 char* dst = new char[inbytesleft*2+1];
 char* outbuf = dst;
 size_t outbytesleft = inbytesleft;
 size_t dstmax = inbytesleft;

 while (inbytesleft > 0) {
 char* inbuf_org = inbuf;
 size_t n = iconv(conversion_descriptor , &inbuf, &inbytesleft, &outbuf, &outbytesleft);
 if ((n != (size_t)-1 && inbytesleft == 0) || (errno == EINVAL)) {
 /* EINVAL = An incomplete multibyte sequence has been encountered in the input. */
 inbytesleft = 0;
 ret.append(dst, 0, dstmax-outbytesleft);
 }
 else {
 
 switch (errno) {
 case E2BIG: /* There is not sufficient room at *outbuf. And something wrong can happen, so break the loop*/
 ret.append(dst, 0, dstmax-outbytesleft);
 outbuf = dst;
 outbytesleft = dstmax;
 break_flag++;
 break;
 case EILSEQ: /* An invalid multibyte sequence has been encountered in the input. */
 ret.append(dst, 0, dstmax-outbytesleft);
 ret.append(inbuf, 0, 1);
 inbuf++;
 inbytesleft--;
 outbuf = dst;
 outbytesleft = dstmax;
 break;
 default:
 ret.append(inbuf_org);
 inbytesleft = 0;
 break;
 }
 
 if(break_flag){
 break;
 }
 }
 }
 outbuf = dst;
 outbytesleft = dstmax;
 if ( iconv(conversion_descriptor , NULL, NULL, &outbuf, &outbytesleft) != (size_t)-1) {
 ret.append(dst, 0, dstmax-outbytesleft);
 }
 delete [] dst;
 return ret;
}

Caller
#include "IconvWrapper.hpp"
...
IconvWrapper iconv;
...
string euc_string;
euc_string=iconv.from_to(sjis_string,"shift_jis", "euc-jp");

Example of make
.PHONY: all
all: LogAnalyzer

LogAnalyzer: LogAnalyzer.cpp
 g++ -Wall -O3 -finline-functions -o -L/usr/local/lib:/usr/lib -I/usr/local/include/boost -I/usr/include/boost-1_33_1/ -I/usr/local/include/ -I../common/ -o LogAnalyzer LogAnalyzer.cpp ../common/IconvWrapper.cpp ../common/CGICommon.cpp ../common/CharConverter.cpp -liconv /usr/local/lib/libboost_regex.so

.PHONY: install
install:
 mkdir -p /home/www/bin/;
 mv -f LogAnalyzer /home/www/bin/;
 cp /home/www/bin/LogAnalyzer .;

.PHONY: clean
clean:
 rm LogAnalyzer;
 rm *~;
 rm *.o;


Add comment to this article


[Read other articles]
<=Newer article2013/01/20 Japanese spam blog list which I found these days
=>Older article2012/10/21 Spammer IP list


Articles categorized as "Programming by this user"
All articles of this user
Subscribe to RSS
RSS
Display Style of blog
List/Mobile(QRCode)
Term
Category
All
1.Japan
2.Atlassian's products
3.Self
4.Development of this site
5.Japanese comics
6.Japanese anime
7.Weekly hot news of Japanese culture
8.OP/ED/PV
9.Japanese game
10.Ranking
11.Japanese Comics (Manga)
12.Search Engine
13.Japanese drama
14.Japanese otaku culture
15.Programming
16.Ineternet world
17.Movie
18.C/C++
19.BerkeleyDB
20.Apache programming
21.Spam
22.Meteor
23.Marketing
24.Python
25.Scrum
26.JIRA
27.Git
28.CI
29.Jenkins
30.AWS
31.Operation
32.Singapore
33.Cloud
34.Mysql Cluster
35.DevOps
36.Bitbucket
37.Xamarin
38.Good and new
39.Monitoring
40.node.js
Sayings from S-Cry-Ed

Rule in this world is speed. Even stupid person can write cool novel if he can spend 20 years for it.

If someone helped me, I will help him in return, which is my rule.

To become stronger, consider what is cowardliest thinking. And rebel against the thinking, which will make you stronger.



I am Japanese but working for some English sites.

Doctor Job Career
Nurse Job Career
Top Page top MetaSeachJP Works