1 #Region "Microsoft.VisualBasic::a4e6b13ccb1c44cbe01adce2f1a796e1, Microsoft.VisualBasic.Core\ComponentModel\Settings\DataModels\ProfileTable.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 ProfileTable
35     
36     '         Properties: Description, Name, Type, value
37     
38     '         Constructor: (+2 OverloadsSub New
39     
40     '         FunctionToString
41     
42     '         SubSetValue
43     
44     '     Interface IProfileTable
45     
46     '         Properties: Description, Name, Type, value
47     
48     '     Module IniExtensions
49     
50     '         Function: LoadIni
51     
52     '         Sub: WriteIni
53     
54     
55     ' /********************************************************************************/
56
57 #End Region
58
59 Imports System.Runtime.CompilerServices
60 Imports Microsoft.VisualBasic.ComponentModel.Collection.Generic
61 Imports Microsoft.VisualBasic.ComponentModel.Settings.Inf
62 Imports Microsoft.VisualBasic.Serialization.JSON
63
64 Namespace ComponentModel.Settings
65
66     Public Class ProfileTable : Implements INamedValue
67         Implements IProfileTable
68
69         Public Property Name As String Implements INamedValue.Key, IProfileTable.Name
70         Public Property value As String Implements IProfileTable.value
71         Public Property Type As ValueTypes Implements IProfileTable.Type
72         Public Property Description As String Implements IProfileTable.Description
73
74         Sub New()
75         End Sub
76
77         Sub New(x As BindMapping)
78             Name = x.Name
79             value = x.Value
80             Type = x.Type
81             Description = x.Description
82         End Sub
83
84         <MethodImpl(MethodImplOptions.AggressiveInlining)>
85         Public Sub SetValue(Of T As {New, IProfile})(config As Settings(Of T))
86             Call config.Set(Name.ToLower, value)
87         End Sub
88
89         Public Overrides Function ToString() As String
90             Return Me.GetJson
91         End Function
92     End Class
93
94     Public Interface IProfileTable
95
96         Property Name As String
97         ReadOnly Property value As String
98         Property Type As ValueTypes
99         Property Description As String
100
101     End Interface
102
103     ''' <summary>
104     ''' 读写Ini文件的拓展
105     ''' </summary>
106     Public Module IniExtensions
107
108         ''' <summary>
109         ''' The section name of the profile data is the type name
110         ''' </summary>
111         ''' <typeparam name="T"></typeparam>
112         ''' <param name="x"></param>
113         ''' <param name="path"></param>
114         <Extension>
115         Public Sub WriteIni(Of T As {New, IProfile})(x As IProfile, path As String)
116             Dim settings As New Settings(Of T)(x)
117             Dim ini As New IniFile(path)
118             Dim name As String = GetType(T).Name
119
120             For Each config As BindMapping In settings.AllItems
121                 Call ini.WriteValue(name, config.Name, config.Value)
122             Next
123
124             Call ini.Flush()
125         End Sub
126
127         ''' <summary>
128         ''' The section name of the profile data is the type name
129         ''' </summary>
130         ''' <typeparam name="T"></typeparam>
131         ''' <param name="path"></param>
132         ''' <returns></returns>
133         <Extension>
134         Public Function LoadIni(Of T As {New, IProfile})(path As StringAs T
135             Dim config As Settings(Of T) = Settings(Of T).CreateEmpty
136             Dim ini As New IniFile(path)
137             Dim name As String = GetType(T).Name
138
139             For Each x As BindMapping In config.AllItems
140                 Dim value As String = ini.ReadValue(name, x.Name)
141                 Call x.Set(value)
142             Next
143
144             Return config.SettingsData
145         End Function
146     End Module
147 End Namespace