VBScript、HTA - INI 解析,允许内联注释
VBScript, HTA - INI Parsing, allow inline comments
我目前正在开发游戏启动器,并且从互联网上借用了工作代码来解析 INI。除一个问题外,一切正常。
无法解析 ini 文件的内联注释。
示例:
[Window]
Width=800
解析无误,很好。
[Window]
Width=800 ; width in pixels
但上面不是,我需要它能够在检测到a时停止读取行;如果可能的话。
这是我的完整 HTA 代码:
<Html>
<Head>
<Title>Installer</Title>
<Meta Http-Equiv="x-ua-compatible" Content="ie=9">
<Link Rel="stylesheet" Type="text/css" Href="image/appStyles.css" Media="screen" />
<Script Language="VBScript" Type="Text/VBScript">
'-- Scripts to be carried out before the installer loads in.
'-- Functions --'
Function ReadIni( myFilePath, mySection, myKey )
' This function returns a value read from an INI file
' Examples
' ReadIni( "settings.config", "Section1", "Keyname1" )
' ReadIni( "settings.config", "Section1", "Keyname2" )
' ReadIni( "settings.config", "Section2", "Keyname1" )
' ReadIni( "settings.config", "Section4", "Keyname2" )
Const ForReading = 1
Const ForWriting = 2
Const ForAppending = 8
Dim intEqualPos
Dim objFSO, objIniFile
Dim strFilePath, strKey, strLeftString, strLine, strSection
Set objFSO = CreateObject( "Scripting.FileSystemObject" )
ReadIni = ""
strFilePath = Trim( myFilePath )
strSection = Trim( mySection )
strKey = Trim( myKey )
If objFSO.FileExists( strFilePath ) Then
Set objIniFile = objFSO.OpenTextFile( strFilePath, ForReading, False )
Do While objIniFile.AtEndOfStream = False
strLine = Trim( objIniFile.ReadLine )
' Check if section is found in the current line
If LCase( strLine ) = "[" & LCase( strSection ) & "]" Then
strLine = Trim( objIniFile.ReadLine )
' Parse lines until the next section is reached
Do While Left( strLine, 1 ) <> "["
' Find position of equal sign in the line
intEqualPos = InStr( 1, strLine, "=", 1 )
If intEqualPos > 0 Then
strLeftString = Trim( Left( strLine, intEqualPos - 1 ) )
' Check if item is found in the current line
If LCase( strLeftString ) = LCase( strKey ) Then
ReadIni = Trim( Mid( strLine, intEqualPos + 1 ) )
' In case the item exists but value is blank
If ReadIni = "" Then
ReadIni = " "
End If
' Abort loop when item is found
Exit Do
End If
End If
' Abort if the end of the INI file is reached
If objIniFile.AtEndOfStream Then Exit Do
' Continue with next line
strLine = Trim( objIniFile.ReadLine )
Loop
Exit Do
End If
Loop
objIniFile.Close
Else
' WScript.Echo strFilePath & " doesn't exists. Exiting..."
' Wscript.Quit 1
End If
End Function
'-- Subroutines --'
'-- Resize & move app to center
Sub SetWindow( WidthX,HeightY )
Self.ResizeTo WidthX, HeightY
Self.MoveTo (screen.Width - WidthX)/2, (screen.Height - HeightY)/2
End Sub
'-- Close app
Sub WinClose
Self.Close
End Sub
'-- Startup --'
'-- Read the configuration settings.
IniFile = "settings.config"
WinWidth = ReadIni( IniFile, "Window", "Width" )
WinHeight = ReadIni( IniFile, "Window", "Height" )
'-- Set Window size
SetWindow WinWidth, WinHeight
</Script>
<Hta:Application Id="Installer" ApplicationName="Installer" Version="0.1"
SingleInstance="Yes"
Icon="image/appIcon.ico"
Caption="No"
Border="None"
InnerBorder="No"
ContextMenu="No"
SysMenu="None"
Scroll="No"
Selection="No"
/>
</Head>
<Body>
<Div Id="status">Hello</Div>
<Script Language="VBScript" Type="Text/VBScript">
'-- Scripts that require access to the DOM...
'-- Startup
document.getElementById("status").InnerHTML = "Idle"
document.title = ReadIni( IniFile, "App", "Title" )
</Script>
<Script Type="Text/Javascript">
//-- Javascripts that require access to the DOM...
window.onload = function() {
var a = document.getElementsByTagName("img");
a.ondragstart = function() { return false; }
}
</Script>
</Body>
</Html>
如果你们能提供帮助,那就太好了,谢谢!
WinWidth = Trim(Split(ReadIni( IniFile, "Window", "Width" ), ";")(0))
使用分号拆分值,获取列表中的第一个元素并删除 start/end 个空格(如果存在)
我目前正在开发游戏启动器,并且从互联网上借用了工作代码来解析 INI。除一个问题外,一切正常。
无法解析 ini 文件的内联注释。
示例:
[Window]
Width=800
解析无误,很好。
[Window]
Width=800 ; width in pixels
但上面不是,我需要它能够在检测到a时停止读取行;如果可能的话。
这是我的完整 HTA 代码:
<Html>
<Head>
<Title>Installer</Title>
<Meta Http-Equiv="x-ua-compatible" Content="ie=9">
<Link Rel="stylesheet" Type="text/css" Href="image/appStyles.css" Media="screen" />
<Script Language="VBScript" Type="Text/VBScript">
'-- Scripts to be carried out before the installer loads in.
'-- Functions --'
Function ReadIni( myFilePath, mySection, myKey )
' This function returns a value read from an INI file
' Examples
' ReadIni( "settings.config", "Section1", "Keyname1" )
' ReadIni( "settings.config", "Section1", "Keyname2" )
' ReadIni( "settings.config", "Section2", "Keyname1" )
' ReadIni( "settings.config", "Section4", "Keyname2" )
Const ForReading = 1
Const ForWriting = 2
Const ForAppending = 8
Dim intEqualPos
Dim objFSO, objIniFile
Dim strFilePath, strKey, strLeftString, strLine, strSection
Set objFSO = CreateObject( "Scripting.FileSystemObject" )
ReadIni = ""
strFilePath = Trim( myFilePath )
strSection = Trim( mySection )
strKey = Trim( myKey )
If objFSO.FileExists( strFilePath ) Then
Set objIniFile = objFSO.OpenTextFile( strFilePath, ForReading, False )
Do While objIniFile.AtEndOfStream = False
strLine = Trim( objIniFile.ReadLine )
' Check if section is found in the current line
If LCase( strLine ) = "[" & LCase( strSection ) & "]" Then
strLine = Trim( objIniFile.ReadLine )
' Parse lines until the next section is reached
Do While Left( strLine, 1 ) <> "["
' Find position of equal sign in the line
intEqualPos = InStr( 1, strLine, "=", 1 )
If intEqualPos > 0 Then
strLeftString = Trim( Left( strLine, intEqualPos - 1 ) )
' Check if item is found in the current line
If LCase( strLeftString ) = LCase( strKey ) Then
ReadIni = Trim( Mid( strLine, intEqualPos + 1 ) )
' In case the item exists but value is blank
If ReadIni = "" Then
ReadIni = " "
End If
' Abort loop when item is found
Exit Do
End If
End If
' Abort if the end of the INI file is reached
If objIniFile.AtEndOfStream Then Exit Do
' Continue with next line
strLine = Trim( objIniFile.ReadLine )
Loop
Exit Do
End If
Loop
objIniFile.Close
Else
' WScript.Echo strFilePath & " doesn't exists. Exiting..."
' Wscript.Quit 1
End If
End Function
'-- Subroutines --'
'-- Resize & move app to center
Sub SetWindow( WidthX,HeightY )
Self.ResizeTo WidthX, HeightY
Self.MoveTo (screen.Width - WidthX)/2, (screen.Height - HeightY)/2
End Sub
'-- Close app
Sub WinClose
Self.Close
End Sub
'-- Startup --'
'-- Read the configuration settings.
IniFile = "settings.config"
WinWidth = ReadIni( IniFile, "Window", "Width" )
WinHeight = ReadIni( IniFile, "Window", "Height" )
'-- Set Window size
SetWindow WinWidth, WinHeight
</Script>
<Hta:Application Id="Installer" ApplicationName="Installer" Version="0.1"
SingleInstance="Yes"
Icon="image/appIcon.ico"
Caption="No"
Border="None"
InnerBorder="No"
ContextMenu="No"
SysMenu="None"
Scroll="No"
Selection="No"
/>
</Head>
<Body>
<Div Id="status">Hello</Div>
<Script Language="VBScript" Type="Text/VBScript">
'-- Scripts that require access to the DOM...
'-- Startup
document.getElementById("status").InnerHTML = "Idle"
document.title = ReadIni( IniFile, "App", "Title" )
</Script>
<Script Type="Text/Javascript">
//-- Javascripts that require access to the DOM...
window.onload = function() {
var a = document.getElementsByTagName("img");
a.ondragstart = function() { return false; }
}
</Script>
</Body>
</Html>
如果你们能提供帮助,那就太好了,谢谢!
WinWidth = Trim(Split(ReadIni( IniFile, "Window", "Width" ), ";")(0))
使用分号拆分值,获取列表中的第一个元素并删除 start/end 个空格(如果存在)