日本搞逼视频_黄色一级片免费在线观看_色99久久_性明星video另类hd_欧美77_综合在线视频

國(guó)內(nèi)最全I(xiàn)T社區(qū)平臺(tái) 聯(lián)系我們 | 收藏本站
阿里云優(yōu)惠2
您當(dāng)前位置:首頁 > web前端 > jscript > JavaScript實(shí)現(xiàn)的原生的INI文件解析器代碼

JavaScript實(shí)現(xiàn)的原生的INI文件解析器代碼

來源:程序員人生   發(fā)布時(shí)間:2013-11-28 11:40:31 閱讀次數(shù):4012次

利用JavaScript實(shí)現(xiàn)的原生的INI文件解析器代碼,本文系網(wǎng)轉(zhuǎn)載,下邊是作者的說明:

基于Builder模式寫的一個(gè)INI文件解析器,用JavaScript實(shí)現(xiàn)。只是表述INI文件解析的思路和Builder模式的實(shí)例應(yīng)用,貽笑大方之作。很容易根據(jù)思路用其他語言實(shí)現(xiàn)出來。注釋用E文,也只是練習(xí),不要見怪啊!

/* 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());
})();
生活不易,碼農(nóng)辛苦
如果您覺得本網(wǎng)站對(duì)您的學(xué)習(xí)有所幫助,可以手機(jī)掃描二維碼進(jìn)行捐贈(zèng)
程序員人生
------分隔線----------------------------
分享到:
------分隔線----------------------------
為碼而活
積分:4237
15粉絲
7關(guān)注
欄目熱點(diǎn)
關(guān)閉
程序員人生
主站蜘蛛池模板: 久久99亚洲精品 | 国产一区二区三区免费视频 | 欧美成人午夜免费视在线看片 | 日本久久久久久 | 日本久久网 | 久久精品99国产精品日本 | 日韩欧美在线不卡 | 欧美日本韩国一区二区三区 | 不卡中文一二三区 | 色综合久久久久综合99 | 亚洲欧洲成人 | 国产一卡二卡三卡 | 国产精品xxx在线观看www | 伊人网站 | 亚洲欧美在线视频 | 亚洲一区二区三区精品在线 | 日韩精品一区在线 | 可以免费看的毛片 | 男女免费网站 | 精品久久av | jizz在线观看视频 | 在线观看国产麻豆 | 国产三级电影在线播放 | 久久成人欧美 | 国产一区二区三区观看 | 国产一二| 亚洲一区视频 | 91三级| a天堂视频 | 黄色在线观看网站 | 久久精品一区 | 91成人综合| 91免费观看 | 在线观看免费av网 | 亚洲国产精品国自产拍av秋霞 | 久久55| 日韩在线视频一区二区三区 | 精品在线 | 曰韩三级 | 日韩视频久久 | 日韩在线看片 |