iconv用法解读
iconv是一个字符集转换函数,原型为:
size_t iconv(iconv_t cd,
             char **inbuf, size_t *inbytesleft,
             char **outbuf, size_t *outbytesleft);
// 传递给do_convert的in_buf,所有字节数(in_buf_size指定)都是可以转换成功的
static int do_convert(iconv_t cd, const char* from, size_t from_size, std::string* to)
{
    char* in_buf_ptr = const_cast<char*>(from);
    size_t in_bytes_left = from_size;
    size_t out_bytes = in_bytes_left*3 + 1;
    size_t out_bytes_left = out_bytes;
    std::string out(out_bytes_left, '\0');
    char* out_buf_start = const_cast<char*>(out.c_str());
    char* out_buf_ptr = out_buf_start;
    int bytes = iconv(cd, &in_buf_ptr, &in_bytes_left, &out_buf_ptr, &out_bytes_left);
    if (-1 == bytes)
        return errno;
    to->assign(out_buf_start, out_bytes-out_bytes_left);
    return 0;
}
// 可忽略不能转换的部分,
// 也可以在结果中保留不能被转换的部分
// 详细实现可以浏览:
// https://github.com/eyjian/mooon/blob/master/common_library/src/utils/charset_utils.cpp
void CCharsetUtils::convert(const std::string& from_charset, const std::string& to_charset,
                            const std::string& from, std::string* to,
                            bool ignore_error, bool skip_error) throw (CException)
{
    std::string result; // 用来保存处理后的内容
    char* in_buf = const_cast<char*>(from.c_str());
    size_t in_bytes = from.size();   // 需要处理的总字节数
    size_t in_bytes_left = in_bytes; // 剩余的未被处理的字节数
    iconv_t cd = iconv_open(to_charset.c_str(), from_charset.c_str());
    if ((iconv_t)(-1) == cd)
    {
        THROW_EXCEPTION(strerror(errno), errno);
    }
    while (in_bytes_left > 0)
    {
        int errcode;
        size_t out_bytes = in_bytes_left * 3 + 1; // 保证足够大
        size_t out_bytes_left = out_bytes;
        std::string out(out_bytes_left, '\0');
        char* out_buf = const_cast<char*>(out.c_str());
        char* out_buf_start = out_buf;
        char* in_buf_start = in_buf;
        // 如果成功,返回值bytes为0
        // 如果成功,in_buf指向in的结尾符,即'\0',同时in_bytes_left值为0
        // 如果失败,in_buf指向未能转换的起始地址,而in_bytes_left值为剩余的未被转换的(可能含有可转换的)字节数
        // 如果成功,则out_bytes-out_bytes_left值为转换后的字节数
        // 如果成功,则out_buf_start存储了被转换后的结果,有效长度为out_bytes-out_bytes_left
        int bytes = iconv(cd, &in_buf, &in_bytes_left, &out_buf, &out_bytes_left);
        if (bytes != -1)
        {
            result.append(out_buf_start, out_bytes-out_bytes_left);
            break;
        }
        else if (!ignore_error)
        {
            errcode = errno;
            iconv_close(cd);
            THROW_EXCEPTION(strerror(errcode), errcode);
        }
        else
        {
            // EILSEQ An invalid multibyte sequence has been encountered in the input.
            // EINVAL An incomplete multibyte sequence has been encountered in the input.
            if ((errno != EINVAL) &&
                (errno != EILSEQ))
            {
                // E2BIG  There is not sufficient room at *outbuf.
                errcode = errno;
                iconv_close(cd);
                THROW_EXCEPTION(strerror(errcode), errcode);
            }
            else
            {
                // in_buf之前部分是可以转换的
                if (in_buf != in_buf_start)
                {
                    std::string str;
                    errcode = do_convert(cd, in_buf_start, in_buf-in_buf_start, &str);
                    if (errcode != 0)
                    {
                        iconv_close(cd);
                        THROW_EXCEPTION(strerror(errcode), errcode);
                    }
                    result.append(str);
                }
                // skip_error决定未能被转换的是否出现在结果当中
                if (!skip_error)
                {
                    result.append(in_buf, 1);
                }
                // 往前推进
                --in_bytes_left; // 将导致while语句结束
                ++in_buf;
            }
        }
    }
    if (-1 == iconv_close(cd))
    {
        THROW_EXCEPTION(strerror(errno), errno);
    }
    // 不能直接使用to,因为to可能就是from
    *to = result;
}
void CCharsetUtils::gbk_to_utf8(const std::string& from, std::string* to, bool ignore_error, bool skip_error) throw (CException)
{
    convert("gbk", "utf-8", from, to, ignore_error, skip_error);
}
void CCharsetUtils::utf8_to_gbk(const std::string& from, std::string* to, bool ignore_error, bool skip_error) throw (CException)
{
    convert("utf-8", "gbk", from, to, ignore_error, skip_error);
}
void CCharsetUtils::gb2312_to_utf8(const std::string& from, std::string* to, bool ignore_error, bool skip_error) throw (CException)
{
    convert("gb2312", "utf-8", from, to, ignore_error, skip_error);
}
void CCharsetUtils::utf8_to_gb2312(const std::string& from, std::string* to, bool ignore_error, bool skip_error) throw (CException)
{
    convert("utf-8", "gb2312", from, to, ignore_error, skip_error);
}												
											iconv用法解读的更多相关文章
- CMake的含义和用法解读
		
什么是 CMake 你或许听过好几种 Make 工具,例如 GNU Make ,QT 的 qmake ,微软的 MS nmake,BSD Make(pmake),Makepp,等等.这些 Make 工 ...
 - 学习AngularJs:Directive指令用法
		
跟我学AngularJs:Directive指令用法解读(上) http://blog.csdn.net/evankaka/article/details/51232895 跟我学AngularJs: ...
 - const和typedef的常见用法详解
		
一.说说const 一般而言,const主要是用来防止定义的对象再次被修改,定义对象变量时要初始化变量. 常见用法如下: 1.用于定义常量变量,这样这个变量在后面就不可以再被修改 const int ...
 - iconv命令
		
iconv 用法: Usage: iconv [OPTION...] [FILE...] Convert encoding of given files from one encoding to an ...
 - ralitive absolute
		
3.relative与absolute的主要区别: 首先,是上面已经提到过的在正常流中的位置存在与否. 其次,relative定位的层总是相对于其最近的父元素,无论其父元素是何种定位方式.如图3: 图 ...
 - php生成CSV格式(转)
		
参考网址: php对csv文件的常用操作集合 http://blog.xhbin.com/archives/748 1,下载CSV格式文档 唯一需要特别注意的是编码. <? include_on ...
 - Linux下windows中文文本文件乱码问题
		
table of content: 乱码问题 用gedit选择正确的字符编码打开文件 文件转码 总结 §乱码 Fedora安装时默认用UTF-8字符编码方式, 这么做有国际化的好处(和很多用utf-8 ...
 - PHP iconv()编码转换函数用法示例
		
PHP iconv()字符编码转换函数的用法,iconv()函数,在php5中是内置的,语法格式:iconv("UTF- 8","GB2312//IGNORE" ...
 - linux下iconv()函数的用法(转载并修改)
		
linux shell 配置文件中默认的字符集编码为UTF-8 .UTF-8是unicode的一种表达方式,gb2312是和unicode都是字符的编码方式,所以说gb2312跟utf-8的概念应该不 ...
 
随机推荐
- 4.Appium实现自动化安装apk
			
一.代码如下所示: from appium import webdriver import os apk_path = os.path.abspath(os.path.join(os.path.dir ...
 - RK3288 GT触摸屏移植调试
			
CPU:RK3288 系统:Android 5.1 IC:GT911 1.在 menuconfig 或者 rockchip_defconfig 中支持触摸屏.具体用哪种方式需要结合编译方法. 按照瑞芯 ...
 - TextRank in Python
			
运用到nltk,sklearn,networkx等很多好用的库,值得参考 https://joshbohde.com/blog/document-summarization
 - (转)SqlServer为大数据量表建索引
			
本文转载自:http://blog.csdn.net/iangujun/article/details/8136764 之前从没有用SqlServer数据库处理过大数据量的表,都是用Oracle,然后 ...
 - Linux 开发环境搭建与使用——SlickEdit 的安装与配置
			
SlickEdit 的介绍 vim 是公认很好用很完美,但是对新手来说,上手毕竟不是很容易.Windows 下程序员很多都很喜欢Source Insight 这个工具来看代码,各种语法高亮看着很舒服. ...
 - NGINX 添加MP4、FLV视频支持模块
			
由于公司网站需要放置视频,但是默认的服务器环境是没有编译这个支持的模块,视频文件只能缓冲完了在播放,非常麻烦. 之前呢也安装了一个nginx_mod_h264_streaming来支持,效果很不错 ...
 - 走迷宫(用队列bfs并输出走的路径)
			
#include <iostream> #include <stack> #include <string.h> #include <stdio.h> ...
 - 学习笔记之PHP
			
学习 PHP,第 1 部分: 注册帐户.上传需要批准的文件.并查看和下载已批准的文件 https://www.ibm.com/developerworks/cn/opensource/tutorial ...
 - CentOS6.5 安装mysql-5.7.9
			
转自:http://forrest-lv.iteye.com/blog/2260703 安装前,需要检查是否已经有mysql服务进程,是否已经装过mysql; 这点很重要,我之前安装CentOS的同 ...
 - ES6系列_14之promise对象的简单使用
			
1.产生原因 在前端开发中,最常见的的就是"回调",我相信很多人对于这个"回调"可谓是印象深刻呢.究其原因是因为层层回调会造成所谓的“回调地狱 (callbac ...