Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1487484
  • 博文数量: 226
  • 博客积分: 3997
  • 博客等级: 少校
  • 技术积分: 2369
  • 用 户 组: 普通用户
  • 注册时间: 2010-06-19 17:26
个人简介

Never save something for a special occasion. Every day in your life is a special occasion.

文章分类

全部博文(226)

文章存档

2018年(5)

2017年(11)

2016年(1)

2015年(17)

2014年(14)

2013年(30)

2012年(5)

2011年(52)

2010年(107)

分类: WINDOWS

2011-07-26 20:36:38

Source Insight完美转换UTF-8 GB2312

/蒹葭

前言

很多人用source insight 打开某些源码文件时,汉字显示为一堆乱码。这个问题是因为编码方式不同。记事本和一些编辑器默认编码方式是ANSI,在这种方式下输入汉字,其实就是GB系列的编码方式。不幸的是,广收欢迎的代码查看工具Source insight 虽然支持汉字,但是它不支持UTF-8。笔者感到疑惑的是,当初开发source insight的这帮人现在哪里去了?这么好的工具,却不再更新了,实在让人可惜。

可惜归可惜,程序还是要看。乱码怎么办?用记事本打开源代码逐个转换的笨方法虽然简单,但当遇到大量UTF-8编码的文件时就繁琐了。这里介绍一种批处理方法。

概述

本程序是参考网上源代码修改而成。感谢原作者将该代码开源,我只花了一个下午就改成所想要的程序。能帮助人们更快的开发出更好的软件,开源万岁。希望读者也能为开源事业贡献自己的一份力量。改进后的程序有一些优点:

l 命令行执行  

可以命令行执行或由bat调用,也可集成到编辑器如SourceInsight中。

l 智能识别编码方式

自动识别 UTF-8(BOM格式、非BOM格式)、纯ASCII码文件,决定是否需要转换。

目前,这个程序只针对源代码文件编写,支持后缀名为 .c .cpp .cxx .h .xml .java .txt等文件。同理支持其它后缀的文件转换。

 

如何集成到 source insight?

下面,先介绍如何集成到 source insight里。

转换文件

source insight里,选择 “选项”→“自定义命令”→“添加”,输入新命令名 CodeConvert File 。确定后,点击“浏览”,选择我们的codeConvert.exe程序路径。在输入框里加上参数” -u2g %f” (注意空格,双引号不要. %f的含义见SI的帮助文档“Command Line Substitutions”小节)。

这样,这个文件转换命令就添加成功。打开某个文件,按照上面步骤,选择该自定义命令点击“运行”,即可将当前打开的文件进行 UTF-8GB2312编码转换。

转换目录

若转换的对象是目录(及子目录)则只需将 %f 换成 %d 即可。


主要源码部分

这是检测文件是否是UTF-8格式的函数,其他部分不再一一列出,请参考源代码。

  1. /*! detect if the fiel is UTF-8 code
  2. \param LPCTSTR filename
  3. \return true/false
  4. */
  5. bool detect_utf8file(LPCTSTR filename)
  6. {
  7.     const unsigned char BOM[3] = {0xEF,0xBB,0xBF};
  8.     int count_good_utf = 0;
  9.     int count_bad_utf = 0;
  10.     int begin = 0;
  11.     char buf[3]={0,};
  12.     CStdioFile file_r ;
  13.     if(!file_r.Open((char*)(LPCTSTR)filename, CStdioFile::modeReadWrite))
  14.         return false;

  15.     file_r.Read(buf,3);
  16.     if(!strncmp(buf,(char *)BOM,3)) {
  17.         std::cout<<"this is utf(BOM). "<<endl;
  18.         file_r.Close();
  19.         return true;
  20.     }
  21.     else { // detect the whole file
  22.     //    UTF-8 text encoding auto-detection
  23.     //    count the following pairs of consecutive bytes as shown in the table:
  24.     //    11..    10..    good
  25.     //    00..    10..    bad
  26.     //    10..    10..    don't care
  27.     //    11..    00..    bad
  28.     //    11..    11..    bad
  29.     //    00..    00..    don't care
  30.     //    10..    00..    don't care
  31.     //    00..    11..    don't care
  32.     //    10..    11..    don't care
  33.         char current_byte,previous_byte;
  34.         file_r.SeekToBegin();
  35.         file_r.Read(&current_byte,1);
  36.         previous_byte = current_byte;
  37.         while(file_r.Read(&current_byte,1)) {
  38.             if ((current_byte & 0xC0) == 0x80) {
  39.                 if ((previous_byte & 0xC0) == 0xC0) {
  40.                     count_good_utf ++;
  41.                 } else if ((previous_byte & 0x80) == 0x00) {
  42.                     count_bad_utf ++;
  43.                 }
  44.             } else if ((previous_byte & 0xC0) == 0xC0 ){
  45.                 count_bad_utf ++;
  46.             }
  47.             previous_byte = current_byte;
  48.         }
  49.         //    the comparison ">=" handles pure ASCII files as UTF-8,
  50.         //    replace it with ">" to change that

  51.         if(count_good_utf > count_bad_utf) {
  52.             file_r.Close();
  53.             std::cout<<"the code of this file is utf(no BOM). "<<endl;
  54.             return true;
  55.         } else if(count_good_utf == count_bad_utf) {
  56.             file_r.Close();
  57.             std::cout<<"the code of this file is pure ASCII file as UTF-8. "<<endl;    
  58.             return true;
  59.         }
  60.         else {
  61.             file_r.Close();
  62.             std::cout<<"the code of this file is not utf. "<<endl;
  63.             return false;    
  64.         }
  65.     }
  66. }

 

程序能运行,但仍可能有bug,期待大家指正错误/完善,大家共同进步。

 CodeConvert.rar   

URLhttp://blog.csdn.net/flylonginsky/article/details/4670188


阅读(25064) | 评论(0) | 转发(2) |
给主人留下些什么吧!~~