1 #Region "Microsoft.VisualBasic::d220e33675187244412f75bac45df92c, Microsoft.VisualBasic.Core\ComponentModel\Settings\Inf\IniFile.vb"
2
3     ' Author:
4     
5     '       asuka (amethyst.asuka@gcmodeller.org)
6     '       xie (genetics@smrucc.org)
7     '       xieguigang (xie.guigang@live.com)
8     
9     ' Copyright (c) 2018 GPL3 Licensed
10     
11     
12     ' GNU GENERAL PUBLIC LICENSE (GPL3)
13     
14     
15     ' This program is free software: you can redistribute it and/or modify
16     ' it under the terms of the GNU General Public License as published by
17     ' the Free Software Foundation, either version 3 of the License, or
18     ' (at your option) any later version.
19     
20     ' This program is distributed in the hope that it will be useful,
21     ' but WITHOUT ANY WARRANTY; without even the implied warranty of
22     ' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23     ' GNU General Public License for more details.
24     
25     ' You should have received a copy of the GNU General Public License
26     ' along with this program. If not, see <http://www.gnu.org/licenses/>.
27
28
29
30     ' /********************************************************************************/
31
32     ' Summaries:
33
34     '     Class IniFile
35     
36     '         Properties: FileExists, path
37     
38     '         Constructor: (+1 OverloadsSub New
39     
40     '         Function: ReadValue, ToString
41     
42     '         Sub: (+2 Overloads) Dispose, Flush, WriteValue
43     
44     
45     ' /********************************************************************************/
46
47 #End Region
48
49 Imports System.Runtime.CompilerServices
50 Imports Microsoft.VisualBasic.Language.UnixBash.FileSystem
51
52 Namespace ComponentModel.Settings.Inf
53
54     ''' <summary>
55     ''' Ini file I/O handler
56     ''' </summary>
57     Public Class IniFile : Implements IDisposable
58
59         Public ReadOnly Property path As String
60
61         Public ReadOnly Property FileExists As Boolean
62             Get
63                 Return path.FileLength > -1
64             End Get
65         End Property
66
67         ''' <summary>
68         ''' 为了避免频繁的读写文件,会使用这个数组来做缓存
69         ''' </summary>
70         Dim dataLines As String()
71
72         ''' <summary>
73         ''' Open a ini file handle.
74         ''' </summary>
75         ''' <param name="INIPath"></param>
76         Public Sub New(INIPath As String)
77             path = IO.Path.GetFullPath(PathMapper.GetMapPath(INIPath))
78             dataLines = path.ReadAllLines
79         End Sub
80
81         ''' <summary>
82         ''' 将缓存数据写入文件之中
83         ''' </summary>
84         Public Sub Flush()
85             Call dataLines.SaveTo(path)
86         End Sub
87
88         Public Overrides Function ToString() As String
89             Return path.ToFileURL
90         End Function
91
92         <MethodImpl(MethodImplOptions.AggressiveInlining)>
93         Public Sub WriteValue(section$, key$, value$)
94             dataLines = dataLines.AsList.WritePrivateProfileString(section, key, value)
95         End Sub
96
97         <MethodImpl(MethodImplOptions.AggressiveInlining)>
98         Public Function ReadValue(section$, key$) As String
99             Return dataLines.GetPrivateProfileString(section, key)
100         End Function
101
102 #Region "IDisposable Support"
103         Private disposedValue As Boolean To detect redundant calls
104
105         ' IDisposable
106         Protected Overridable Sub Dispose(disposing As Boolean)
107             If Not disposedValue Then
108                 If disposing Then
109                     ' TODO: dispose managed state (managed objects).
110                     Call Flush()
111                 End If
112
113                 ' TODO: free unmanaged resources (unmanaged objects) and override Finalize() below.
114                 ' TODO: set large fields to null.
115             End If
116             disposedValue = True
117         End Sub
118
119         ' TODO: override Finalize() only if Dispose(disposing As Boolean) above has code to free unmanaged resources.
120         'Protected Overrides Sub Finalize()
121         '    ' Do not change this code.  Put cleanup code in Dispose(disposing As Boolean) above.
122         '    Dispose(False)
123         '    MyBase.Finalize()
124         'End Sub
125
126         ' This code added by Visual Basic to correctly implement the disposable pattern.
127         Public Sub Dispose() Implements IDisposable.Dispose
128             Do not change this code.  Put cleanup code in Dispose(disposing As Boolean) above.
129             Dispose(True)
130             ' TODO: uncomment the following line if Finalize() is overridden above.
131             ' GC.SuppressFinalize(Me)
132         End Sub
133 #End Region
134     End Class
135 End Namespace