JavaScript实现的原生的INI文件解析器代码
发布时间:2018-10-24 22:31:48 所属栏目:模式 来源:站长网
导读:利用 Java Script实现的原生的INI文件解析器代码,本文系烈火网转载,下边是作者的说明: 基于Builder模式写的一个INI文件解析器,用JavaScript实现。只是表述INI文件解析的思路和Builder模式的实例应用,贻笑大方之作。很容易根据思路用其他语言实现出来
利用JavaScript实现的原生的INI文件解析器代码,本文系烈火网转载,下边是作者的说明: 基于Builder模式写的一个INI文件解析器,用JavaScript实现。只是表述INI文件解析的思路和Builder模式的实例应用,贻笑大方之作。很容易根据思路用其他语言实现出来。注释用E文,也只是练习,不要见怪啊! Copy to Clipboard![]() 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()); })(); (编辑:PHP编程网 - 黄冈站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |