国内最全IT社区平台 联系我们 | 收藏本站
华晨云阿里云优惠2
您当前位置:首页 > web前端 > jscript > JavaScript实现的原生的INI文件解析器代码

JavaScript实现的原生的INI文件解析器代码

来源:程序员人生   发布时间:2013-11-28 11:40:31 阅读次数:3794次

利用JavaScript实现的原生的INI文件解析器代码,本文系网转载,下边是作者的说明:

基于Builder模式写的一个INI文件解析器,用JavaScript实现。只是表述INI文件解析的思路和Builder模式的实例应用,贻笑大方之作。很容易根据思路用其他语言实现出来。注释用E文,也只是练习,不要见怪啊!

/* trim the blank chars at the begining/end of the string. */
String.prototype.trim = function(){
return this.replace(/^s+|s+$/g, '');
};

function print(e) { WScript.Echo(e);}

var JSON = {
/*
encode any give object into a string in JSON format.
*/
encode: function(obj){
var strDump = '';
var Temp = [];

if(obj === undefined)
{
strDump = 'undefined';
}
else if(obj === null)
{
strDump = 'null';
}
else if((typeof obj) === 'string')
{
strDump = ''' + obj + ''';
}
else if((typeof obj) === 'number')
{
strDump = obj.toString();
}
else if(obj instanceof Array)
{
Temp = [];

strDump = '[';
var i = 0;
for(; i < obj.length; ++i)
{
Temp.push(this.encode(obj[i]));
}
strDump += Temp.join(',') + ']';
}
else if(obj instanceof Object)
{
Temp = [];

strDump = '{';
var p = null;
for(p in obj)
{
Temp.push(p + ':' + this.encode(obj[p]));
}
strDump += Temp.join(',') + '}';
}

return strDump;
},
decode: function(strJSON){
return eval('(' + strJSON + ')');
}
};

/*
parse the INI file, get the sections, keys and values.
IniBuilder builds the final result.
*/
function IniParser()
{
this._iniContent = [];
}

IniParser.prototype.loadIni = function(file){
var fso = new ActiveXObject("Scripting.FileSystemObject");
var text = fso.OpenTextFile(file, 1, false);

var idx = 0;
while(!text.AtEndOfStream)
{
this._iniContent[idx++] = text.ReadLine();
}
text.Close();
};

/*
launch the parse process.
*/
IniParser.prototype.doParse = function(iniBuilder){
if(this._iniContent.length === 0)
{
return;
}

if(iniBuilder === null ||
iniBuilder === undefined)
{
throw new Error("Bad Builder !");
}

var row = 0;
var line = '';
for(; row < this._iniContent.length; ++row)
{
line = this._iniContent[row].toString().trim();

/* if it's a blank line or a comment, skip it. */
if(line.length === 0 ||
line.charAt(0) === ';')
{
continue;
}

var Temp = [];

/* whether is a section element. */
var foundSection = false;

var col = 0;
for(; col < line.length; col++)
{
/* the begining of a section element */
if(line.charAt(col) === '[')
{
foundSection = true;
continue;
}

/* the end of a section element */
else if(foundSection && line.charAt(col) === ']')
{
break;
}

Temp.push(line.charAt(col));
}
var strTemp = Temp.join('');

/* It can be only section element or key-value pair element. */
if(foundSection)
{
iniBuilder.onSection(strTemp);
}
else
{
var pair = strTemp.split('=');
var key = pair[0].toString().trim();
var value = pair[1].toString().trim();
iniBuilder.onKeyValue(key, value);
}
}
};

/*
build the final result of ini according to the given elements(sections keys and values).
*/
function IniBuilder()
{
this._result = {};

/* the last section name */
this._lastsection = 'DEFAULT';
}

/*
assemble the section elements
*/
IniBuilder.prototype.onSection = function(section){
if(section === null ||
section === undefined ||
section.toString().length === 0)
{
section = 'DEFAULT';
}

this._result[section] = {};
this._lastsection = section.toString();
};

/*
assemble the keys and values pairs. Their parent section is the last give one.
If there is none, then use "DEFAULT" as the default.
*/
IniBuilder.prototype.onKeyValue = function(key, value){
if(key === null || key === undefined)
{
throw new Error("Bad Key !");
}

if(this._lastsection.length === 0)
{
this._lastsection = 'DEFAULT';
}

this._result[this._lastsection][key.toString()] = value;
};

/*
Get the parse result in JSON string format.
*/
IniBuilder.prototype.outJSON = function(){
return JSON.encode(this._result);
};

/*
Get the parse result in XML string format.
*/
IniBuilder.prototype.outXML = function(){
/* TO be implemented */
};


/*
Director for IniParser and IniBuilder.
*/
function IniDirector(iniParser, iniBuilder)
{
this._iniParser = iniParser;
this._iniBuilder = iniBuilder;
}

IniDirector.prototype.parseIni = function(file){
this._iniParser.loadIni(file);
this._iniParser.doParse(this._iniBuilder);
};

IniDirector.prototype.getJSON = function(){
return this._iniBuilder.outJSON();
};

IniDirector.prototype.getXML = function(){
return this._iniBuilder.outXML();
};

/* test code */
(function(){
var parser = new IniParser();
var builder = new IniBuilder();
var director = new IniDirector(parser, builder);
/* please provide your own INI file. */
director.parseIni('Data.ini');

print(director.getJSON());
})();
生活不易,码农辛苦
如果您觉得本网站对您的学习有所帮助,可以手机扫描二维码进行捐赠
程序员人生
------分隔线----------------------------
分享到:
------分隔线----------------------------
为码而活
积分:4237
15粉丝
7关注
栏目热点
关闭
程序员人生