Native INI file parser Code implemented by JavaScript

Source: Internet
Author: User

The native INI file parser Code implemented using JavaScript is reproduced in this article by the Helper's house. below is the author's description:

An INI file parser written in Builder mode, which is implemented in JavaScript. It just expresses the idea of INI File Parsing and the example application of Builder mode. It is easy to implement it in other languages based on your ideas. Commenting on E is just an exercise. Don't be surprised!

Copy to ClipboardReference: [www.bkjia.com]/* trim the blank chars at the ining/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 ());
})();

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.