1/*
2
3Read a ini file
4
5[Section1]
6Param1=value1
7[Section2]
8Param2=value2
9
10*/
11var fs = require("fs");
12
13function parseINIString(data){
14 var regex = {
15 section: /^\s*\[\s*([^\]]*)\s*\]\s*$/,
16 param: /^\s*([^=]+?)\s*=\s*(.*?)\s*$/,
17 comment: /^\s*;.*$/
18 };
19 var value = {};
20 var lines = data.split(/[\r\n]+/);
21 var section = null;
22 lines.forEach(function(line){
23 if(regex.comment.test(line)){
24 return;
25 }else if(regex.param.test(line)){
26 var match = line.match(regex.param);
27 if(section){
28 value[section][match[1]] = match[2];
29 }else{
30 value[match[1]] = match[2];
31 }
32 }else if(regex.section.test(line)){
33 var match = line.match(regex.section);
34 value[match[1]] = {};
35 section = match[1];
36 }else if(line.length == 0 && section){
37 section = null;
38 };
39 });
40 return value;
41}
42
43try {
44 var data = fs.readFileSync('C:\\data\\data.dat', 'utf8');
45
46 var javascript_ini = parseINIString(data);
47 console.log(javascript_ini['Section1']);
48
49}
50catch(e) {
51 console.log(e);
52}
53