解决wins VS编译的文件到xCode乱码的JS简单小样

  • 在iOS混编的时候,经常会共用跨平台文件,导入到xcode中,有的格式就可能会识别不了,就会变成乱码,比如说GBKGB2312(理论上有的文件xcode是可以识别的)。为了可以统一转化这些文件的编码,所以我就写了一个node.js的小样,下面贴出代码。

  • 在转化之前需要一定的条件
    1.node.js的环境,网上有很多教程,安装环境很简单。
    2.需要 iconv-lite这个插件,输入npm install iconv-lite就可以安装了。
    3.敢于尝试的精神。(略略略,其实我也是试出来的。)

  • 完整代码

// 文件名 index.js
const fs = require('fs');  
const path = require('path');  
const iconv = require('iconv-lite');

console.log('爬虫程序开始运行...')
var root = path.join(__dirname)  
readDir(path.join(root)) 
console.log('爬虫程序结束运行...')

function readDir(subPath){  
    fs.readdir(subPath,function(err,menu){   
        if(!menu)  
            return;  
        menu.forEach(function(ele){   
            fs.stat(subPath+"/"+ele,function(err,info){  
                if(info.isDirectory()){  
                    readDir(subPath+"/"+ele);  
                }else{    
                    // 先判断后缀名
                    if (isContains(ele, '.h') ||
                        isContains(ele, '.hpp') ||
                        isContains(ele, '.cpp') ||
                        isContains(ele, '.c') ||
                        isContains(ele, '.m') ||
                        isContains(ele, '.mm')) {

                        transStr(subPath, ele)
                    }
                }     
            })  
        })            
    })  
} 

// 判断是是否包含字符串
function isContains(str, substr) {
    return str.indexOf(substr) >= 0;
}

// 转化文件中的编码方式
function transStr(fontPath, subPath) {
    var filePath = path.resolve(fontPath, subPath);  
    console.log("file: " + filePath)

    var data = fs.readFileSync(filePath);  
    var change_data = iconv.decode(data,'gb2312'); 
    var aie = iconv.encode(change_data,'utf8'); 
    
    fs.writeFileSync(filePath, aie);
}
  • 有哪个文件夹中的文件需要转化,那就把这个文件(index.js命名可以随便)放在哪个文件夹或者是上一层,只要在终端中,跳转到当前的目录下,然后执行node index.js就可以了。

  • 现在我们简单的分析一下代码的流程

1. 导入模块

const fs = require('fs');  
const path = require('path');  
const iconv = require('iconv-lite');
  • 导入需要用到的三个node的模块,fs是处理文件流的,path是处理路径的,iconv-lite是进行编码转化的。

2. 获取路径

var root = path.join(__dirname)  
readDir(path.join(root)) 
  • __dirname 获得当前文件所在目录的完整目录名

3. 遍历所有文件夹中的文件

function readDir(subPath){  
    fs.readdir(subPath,function(err,menu){   
        if(!menu)  
            return;  
        menu.forEach(function(ele){   
            fs.stat(subPath+"/"+ele,function(err,info){  
                if(info.isDirectory()){  
                    readDir(subPath+"/"+ele);  
                }else{    
                    // 先判断后缀名
                    if (isContains(ele, '.h') ||
                        isContains(ele, '.hpp') ||
                        isContains(ele, '.cpp') ||
                        isContains(ele, '.c') ||
                        isContains(ele, '.m') ||
                        isContains(ele, '.mm')) {

                        transStr(subPath, ele)
                    }
                }     
            })  
        })            
    })  
}
  • 这里是利用递归的方式来获取文件夹的所有目录的。
  • fs.readdir(path, [callback(err,files)]) 以异步的方式读取文件目录。
  • fs.stat(path, [callback(err, stats)]) 获取文件信息
  • 在这里加了一个判断,如果文件的后缀名是.h .hpp .c .cpp .m .mm的的时候,才会进行编码的转化。

4. 转化文件中的编码方式

function transStr(fontPath, subPath) {
    var filePath = path.resolve(fontPath, subPath);  
    console.log("file: " + filePath)

    var data = fs.readFileSync(filePath);  
    var change_data = iconv.decode(data,'gb2312'); 
    var aie = iconv.encode(change_data,'utf8'); 
    
    fs.writeFileSync(filePath, aie);
}
  • 最后一部分,才是本文的重点
  • path.resolve([from ...], to) 将参数 to 位置的字符解析到一个绝对路径里,这里解析出文件的绝对路径。
  • fs.readFileSync(filename, [encoding]) 异步获取文件中的数据。
  • fs.writeFileSync(filename, data, [options]) 异步将数据写入到文件。
  • iconv.decode() iconv.encode() 解码和编码数据的格式,这里的gb2312 utf8只是一个例子,还可以替换成其他的格式(比如gbk ISO-8859),这个就需要大家们的尝试精神,因为有的时候我们也不知道他到底是什么编码。
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 一、核心模块和对象 核心模块的意义 常用内置模块path:处理文件路径fs:操作文件系统child_process...
    EndEvent阅读 4,438评论 0 1
  • 个人入门学习用笔记、不过多作为参考依据。如有错误欢迎斧正 目录 简书好像不支持锚点、复制搜索(反正也是写给我自己看...
    kirito_song阅读 2,491评论 1 37
  • Node.js是目前非常火热的技术,但是它的诞生经历却很奇特。 众所周知,在Netscape设计出JavaScri...
    w_zhuan阅读 3,638评论 2 41
  • https://nodejs.org/api/documentation.html 工具模块 Assert 测试 ...
    KeKeMars阅读 6,368评论 0 6
  • Node.js 常用工具 util 是一个Node.js 核心模块,提供常用函数的集合,用于弥补核心JavaScr...
    FTOLsXD阅读 544评论 0 2