-
Notifications
You must be signed in to change notification settings - Fork 10
/
inifile.js
116 lines (92 loc) · 2.38 KB
/
inifile.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
//
// inifile
// Command line tool for working with ini-files
//
// Copyright (c) 2012, Ildar Shaimordanov
//
function help()
{
alert([
'IniFile Version 0.3 Beta',
'Copyright (C) 2012 Ildar Shaimordanov',
'',
'Usage: ' + WScript.ScriptName + ' OPTIONS filename',
'',
' /D - Opens the file using the system default',
' /U - Opens the file as Unicode',
' /A - Opens the file as ASCII',
' /S:value - Specifies the section name',
' /K:value - Specifies the key name',
' /L - List sections of keys of the specified section',
].join('\n'));
};
function alert(value)
{
WScript.Echo(value);
};
function quit(exitCode)
{
WScript.Quit(exitCode);
};
function error(value)
{
WScript.StdErr.WriteLine(value);
quit(1);
};
var uArgs = WScript.Arguments.Unnamed;
var nArgs = WScript.Arguments.Named;
if ( uArgs.length != 1 || WScript.FullName.match(/wscript\.exe/i) ) {
help();
quit(1);
}
///////////////////////////////////////////////////////////////////////////
//[requires[ js/INI.js ]]
///////////////////////////////////////////////////////////////////////////
var filename = uArgs.item(0);
var format = nArgs.Exists('D') ? -2 : nArgs.Exists('U') ? -1 : 0;
var iniObj = (function()
{
var fso = new ActiveXObject('Scripting.FileSystemObject');
var e;
try {
var f = fso.GetFile(filename);
} catch (e) {
error(e.message + ': ' + filename);
}
var h = f.OpenAsTextStream(1, format);
var iniTxt = h.ReadAll();
// var iniTxt = iniTxt.split(/\r\n|\r|\n/);
var result = INI.parse(iniTxt);
// var result = INI.parse(function()
// {
// return h.AtEndOfStream ? null : h.ReadLine();
// });
h.Close();
return result;
})();
var list = nArgs.Exists('L');
var section = nArgs.item('S');
var key = nArgs.item('K');
if ( section ) {
if ( ! iniObj.hasOwnProperty(section) || Object.prototype.toString.call(iniObj[section]) != '[object Object]' ) {
error('Section not found: ' + section);
}
iniObj = iniObj[section];
}
if ( list ) {
for (var p in iniObj) {
if ( ! iniObj.hasOwnProperty(p) ) {
continue;
}
alert(p);
}
quit();
}
if ( ! key ) {
error('Empty key was passed');
}
if ( ! iniObj.hasOwnProperty(key) ) {
error('Key not found: ' + key);
}
var value = [].concat(iniObj[key]).join('\n');
alert(value);