Advertisement
ChiruclanDE

Configuration-Class

Feb 9th, 2013
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 6.00 KB | None | 0 0
  1. 'Code written by Chiruclan
  2.  
  3. Imports System.IO
  4.  
  5. Public Class Configuration
  6.  
  7.     Private KeyList As SortedDictionary(Of String, String) = New SortedDictionary(Of String, String)
  8.     Private CommentList As SortedDictionary(Of String, String) = New SortedDictionary(Of String, String)
  9.  
  10.     Public Function ReadConfig(ByVal filename As String) As Boolean
  11.         Try
  12.             KeyList.Clear()
  13.  
  14.             If File.Exists(filename) Then
  15.                 Using ConfigFile As New FileStream(filename, FileMode.OpenOrCreate, FileAccess.Read)
  16.                     Using ConfigReader As New StreamReader(ConfigFile)
  17.                         Dim Current As String()
  18.                         Dim CurrentLine As String
  19.                         Dim CurrentKey As String
  20.                         Dim CurrentValue As String
  21.                         Dim CurrentComment As String = ""
  22.  
  23.                         Do
  24.                             CurrentLine = ConfigReader.ReadLine()
  25.  
  26.                             If Not CurrentLine.StartsWith("#") And Not String.IsNullOrEmpty(CurrentLine) And Not String.IsNullOrWhiteSpace(CurrentLine) Then
  27.                                 Current = CurrentLine.Split(New Char() {"="c}, 2)
  28.  
  29.                                 CurrentKey = Current(0).Trim()
  30.                                 CurrentValue = Current(1).Trim()
  31.  
  32.                                 KeyList.Add(CurrentKey, CurrentValue)
  33.  
  34.                                 If Not CurrentComment = "" Then
  35.                                     CommentList.Add(CurrentKey, CurrentComment)
  36.                                     CurrentComment = ""
  37.                                 End If
  38.                             ElseIf CurrentLine.StartsWith("#") Then
  39.                                 If CurrentComment = "" Then
  40.                                     CurrentComment = CurrentLine.Substring(1).Trim()
  41.                                 Else
  42.                                     CurrentComment &= vbNewLine & CurrentLine.Substring(1).Trim()
  43.                                 End If
  44.                             End If
  45.  
  46.                             If String.IsNullOrEmpty(CurrentLine) Or String.IsNullOrWhiteSpace(CurrentLine) Then
  47.                                 CurrentLine = "#"
  48.                             End If
  49.                         Loop Until CurrentLine Is Nothing
  50.  
  51.                         Return True
  52.                     End Using
  53.                 End Using
  54.             End If
  55.  
  56.             Return False
  57.         Catch ex As Exception
  58.             Return False
  59.         End Try
  60.     End Function
  61.  
  62.     Public Function SaveConfig(ByVal filename As String) As Boolean
  63.         Try
  64.             Using ConfigFile As New FileStream(filename, FileMode.Create)
  65.                 Using ConfigWriter As New StreamWriter(ConfigFile)
  66.                     Dim PreviousFirstCharacter As String = ""
  67.                     Dim CurrentFirstCharacter As String = ""
  68.  
  69.                     For Each Key As String In KeyList.Keys()
  70.                         CurrentFirstCharacter = Key.Substring(0, 1)
  71.  
  72.                         If Not CurrentFirstCharacter = PreviousFirstCharacter Then
  73.                             If Not String.IsNullOrWhiteSpace(PreviousFirstCharacter) And Not String.IsNullOrEmpty(PreviousFirstCharacter) And Not String.IsNullOrWhiteSpace(CurrentFirstCharacter) And Not String.IsNullOrEmpty(CurrentFirstCharacter) Then
  74.                                 ConfigWriter.WriteLine()
  75.                             End If
  76.  
  77.                             PreviousFirstCharacter = CurrentFirstCharacter
  78.                         End If
  79.  
  80.                         If CommentExists(Key) Then
  81.                             For Each Comment As String In GetComment(Key).Split(New Char() {vbNewLine})
  82.                                 ConfigWriter.WriteLine("# " & Comment.Trim(vbNewLine).Trim())
  83.                             Next
  84.                         End If
  85.  
  86.                         ConfigWriter.WriteLine(Key & " = " & KeyList.Item(Key))
  87.                     Next
  88.  
  89.                     ConfigWriter.Flush()
  90.                     Return True
  91.                 End Using
  92.             End Using
  93.         Catch ex As Exception
  94.             Return False
  95.         End Try
  96.     End Function
  97.  
  98.     Public Function ClearConfig() As Boolean
  99.         Try
  100.             KeyList.Clear()
  101.             Return True
  102.         Catch ex As Exception
  103.             Return False
  104.         End Try
  105.     End Function
  106.  
  107.     Public Function KeyExists(ByVal Key As String) As Boolean
  108.         Try
  109.             If KeyList.ContainsKey(Key) Then
  110.                 Return True
  111.             End If
  112.  
  113.             Return False
  114.         Catch ex As Exception
  115.             Return False
  116.         End Try
  117.     End Function
  118.  
  119.     Public Function AddKey(ByVal Key As String, ByVal Value As String) As Boolean
  120.         Try
  121.             If Not KeyList.ContainsKey(Key) Then
  122.                 KeyList.Add(Key, Value)
  123.                 Return True
  124.             End If
  125.  
  126.             Return False
  127.         Catch ex As Exception
  128.             Return False
  129.         End Try
  130.     End Function
  131.  
  132.     Public Function RemoveKey(ByVal Key As String) As Boolean
  133.         Try
  134.             If KeyList.ContainsKey(Key) Then
  135.                 KeyList.Remove(Key)
  136.                 Return True
  137.             End If
  138.  
  139.             Return False
  140.         Catch ex As Exception
  141.             Return False
  142.         End Try
  143.     End Function
  144.  
  145.     Public Function ListKeys() As List(Of String)
  146.         Try
  147.             Return KeyList.Keys.ToList
  148.         Catch ex As Exception
  149.             Return Nothing
  150.         End Try
  151.     End Function
  152.  
  153.     Public Function GetKey(ByVal Key As String) As String
  154.         Try
  155.             If KeyList.ContainsKey(Key) Then
  156.                 Return KeyList.Item(Key)
  157.             End If
  158.  
  159.             Return Nothing
  160.         Catch ex As Exception
  161.             Return Nothing
  162.         End Try
  163.     End Function
  164.  
  165.     Public Function SetKey(ByVal Key As String, ByVal Value As String) As Boolean
  166.         Try
  167.             If KeyList.ContainsKey(Key) Then
  168.                 KeyList.Item(Key) = Value
  169.                 Return True
  170.             End If
  171.  
  172.             Return False
  173.         Catch ex As Exception
  174.             Return False
  175.         End Try
  176.     End Function
  177.  
  178.     Public Function CommentExists(ByVal Key As String) As Boolean
  179.         Try
  180.             If CommentList.ContainsKey(Key) Then
  181.                 Return True
  182.             End If
  183.  
  184.             Return False
  185.         Catch ex As Exception
  186.             Return False
  187.         End Try
  188.     End Function
  189.  
  190.     Public Function AddComment(ByVal Key As String, ByVal Value As String) As Boolean
  191.         Try
  192.             If Not CommentList.ContainsKey(Key) Then
  193.                 CommentList.Add(Key, Value)
  194.                 Return True
  195.             End If
  196.  
  197.             Return False
  198.         Catch ex As Exception
  199.             Return False
  200.         End Try
  201.     End Function
  202.  
  203.     Public Function RemoveComment(ByVal Key As String) As Boolean
  204.         Try
  205.             If CommentList.ContainsKey(Key) Then
  206.                 CommentList.Remove(Key)
  207.                 Return True
  208.             End If
  209.  
  210.             Return False
  211.         Catch ex As Exception
  212.             Return False
  213.         End Try
  214.     End Function
  215.  
  216.     Public Function ListComments() As List(Of String)
  217.         Try
  218.             Return CommentList.Keys.ToList
  219.         Catch ex As Exception
  220.             Return Nothing
  221.         End Try
  222.     End Function
  223.  
  224.     Public Function GetComment(ByVal Key As String) As String
  225.         Try
  226.             If CommentList.ContainsKey(Key) Then
  227.                 Return CommentList.Item(Key)
  228.             End If
  229.  
  230.             Return Nothing
  231.         Catch ex As Exception
  232.             Return Nothing
  233.         End Try
  234.     End Function
  235.  
  236.     Public Function SetComment(ByVal Key As String, ByVal Value As String) As Boolean
  237.         Try
  238.             If CommentList.ContainsKey(Key) Then
  239.                 CommentList.Item(Key) = Value
  240.                 Return True
  241.             End If
  242.  
  243.             Return False
  244.         Catch ex As Exception
  245.             Return False
  246.         End Try
  247.     End Function
  248. End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement