microsoft-visualbasic-runtime/ComponentModel/Settings/Inf/IniFile.vb

184 lines
6.3 KiB
VB.net
Raw Normal View History

2019-05-22 19:02:42 +08:00
#Region "Microsoft.VisualBasic::136f0568fcde1a437db9944c1b3c978f, Microsoft.VisualBasic.Core\ComponentModel\Settings\Inf\IniFile.vb"
2018-08-02 20:14:48 +08:00
2018-12-23 00:49:32 +08:00
' Author:
'
' asuka (amethyst.asuka@gcmodeller.org)
' xie (genetics@smrucc.org)
' xieguigang (xie.guigang@live.com)
'
' Copyright (c) 2018 GPL3 Licensed
'
'
' GNU GENERAL PUBLIC LICENSE (GPL3)
'
'
' This program is free software: you can redistribute it and/or modify
' it under the terms of the GNU General Public License as published by
' the Free Software Foundation, either version 3 of the License, or
' (at your option) any later version.
'
' This program is distributed in the hope that it will be useful,
' but WITHOUT ANY WARRANTY; without even the implied warranty of
' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
' GNU General Public License for more details.
'
' You should have received a copy of the GNU General Public License
' along with this program. If not, see <http://www.gnu.org/licenses/>.
2018-12-23 00:18:36 +08:00
2018-12-23 00:49:32 +08:00
' /********************************************************************************/
2018-12-23 00:18:36 +08:00
2018-12-23 00:49:32 +08:00
' Summaries:
2018-12-23 00:18:36 +08:00
2018-12-23 00:49:32 +08:00
' Class IniFile
'
' Properties: FileExists, path
'
' Constructor: (+1 Overloads) Sub New
'
' Function: ReadValue, ToString
'
2019-01-18 22:48:45 +08:00
' Sub: (+2 Overloads) Dispose, Flush, WriteComment, WriteValue
2018-12-23 00:49:32 +08:00
'
'
' /********************************************************************************/
2018-08-02 20:14:48 +08:00
#End Region
2019-02-19 18:25:51 +08:00
Imports System.IO
2018-08-02 20:14:48 +08:00
Imports System.Runtime.CompilerServices
Imports Microsoft.VisualBasic.Language.UnixBash.FileSystem
Namespace ComponentModel.Settings.Inf
''' <summary>
''' Ini file I/O handler
''' </summary>
2018-12-23 00:18:36 +08:00
Public Class IniFile : Implements IDisposable
2018-08-02 20:14:48 +08:00
Public ReadOnly Property path As String
Public ReadOnly Property FileExists As Boolean
2019-02-19 18:25:51 +08:00
<MethodImpl(MethodImplOptions.AggressiveInlining)>
Get
Return path.FileLength > -1
End Get
End Property
2018-08-02 20:14:48 +08:00
''' <summary>
2018-12-23 00:18:36 +08:00
''' 为了避免频繁的读写文件,会使用这个数组来做缓存
2018-08-02 20:14:48 +08:00
''' </summary>
2019-02-19 18:25:51 +08:00
Dim data As Dictionary(Of String, Section)
2018-08-02 20:14:48 +08:00
''' <summary>
''' Open a ini file handle.
''' </summary>
''' <param name="INIPath"></param>
Public Sub New(INIPath As String)
path = IO.Path.GetFullPath(PathMapper.GetMapPath(INIPath))
2019-02-19 18:25:51 +08:00
data = INIProfile _
.PopulateSections(path) _
.ToDictionary(Function(s)
Return s.Name
End Function)
2018-12-23 00:18:36 +08:00
End Sub
''' <summary>
''' 将缓存数据写入文件之中
''' </summary>
Public Sub Flush()
2019-02-19 18:25:51 +08:00
Using write As StreamWriter = path.OpenWriter
For Each section As Section In data.Values
Call write.WriteLine(section.CreateDocFragment)
Next
End Using
2018-08-02 20:14:48 +08:00
End Sub
Public Overrides Function ToString() As String
Return path.ToFileURL
End Function
<MethodImpl(MethodImplOptions.AggressiveInlining)>
2019-02-19 18:25:51 +08:00
Public Sub WriteValue(section$, key$, value$, Optional comments$ = Nothing)
If Not data.ContainsKey(section) Then
data(section) = New Section With {
.Name = section,
.Items = {}
}
End If
data(section).SetValue(key, value, comments)
2018-08-02 20:14:48 +08:00
End Sub
2019-01-12 00:44:26 +08:00
''' <summary>
''' 在给定的section,key上面写入注释
''' </summary>
2019-02-19 18:25:51 +08:00
''' <param name="section"></param>
''' <param name="key">如果这个键名称不存在的话,则是将注释写入到目标<paramref name="section"/>之中的</param>
2019-01-12 00:44:26 +08:00
''' <param name="comment">不需要添加注释符号,函数会自动添加</param>
'''
<MethodImpl(MethodImplOptions.AggressiveInlining)>
2019-02-19 18:25:51 +08:00
Public Sub WriteComment(section$, comment$, Optional key$ = Nothing)
' section和key都不存在的话则找不到写入注释的位置
If Not data.ContainsKey(section) Then
Return
Else
If key.StringEmpty Then
' 当key是空字符串则将comment写在section之中
data(section).Comment = comment
Return
Else
If Not data(section).Have(key) Then
Return
End If
End If
End If
data(section).SetComments(key, comment)
2019-01-12 00:44:26 +08:00
End Sub
2018-12-23 00:18:36 +08:00
<MethodImpl(MethodImplOptions.AggressiveInlining)>
Public Function ReadValue(section$, key$) As String
2019-02-19 18:25:51 +08:00
If data.ContainsKey(section) Then
Return data(section).GetValue(key)
Else
Return Nothing
End If
2018-08-02 20:14:48 +08:00
End Function
2018-12-23 00:18:36 +08:00
#Region "IDisposable Support"
Private disposedValue As Boolean ' To detect redundant calls
' IDisposable
Protected Overridable Sub Dispose(disposing As Boolean)
If Not disposedValue Then
If disposing Then
' TODO: dispose managed state (managed objects).
Call Flush()
End If
' TODO: free unmanaged resources (unmanaged objects) and override Finalize() below.
' TODO: set large fields to null.
End If
disposedValue = True
End Sub
' TODO: override Finalize() only if Dispose(disposing As Boolean) above has code to free unmanaged resources.
'Protected Overrides Sub Finalize()
' ' Do not change this code. Put cleanup code in Dispose(disposing As Boolean) above.
' Dispose(False)
' MyBase.Finalize()
'End Sub
' This code added by Visual Basic to correctly implement the disposable pattern.
Public Sub Dispose() Implements IDisposable.Dispose
' Do not change this code. Put cleanup code in Dispose(disposing As Boolean) above.
Dispose(True)
' TODO: uncomment the following line if Finalize() is overridden above.
' GC.SuppressFinalize(Me)
End Sub
#End Region
2018-08-02 20:14:48 +08:00
End Class
End Namespace