1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932
| '*****************************************************************
' Classe INI Version 1.3 par Christian Lamothe
'*****************************************************************
'
'- CREATION DE L'OBJET ET CHARGEMENT DU FICHIER :
' INI = New Ini (NomDuFichier)
'
'- LIRE UNE CLE :
' Valeur = INI.GetKey(of Type)(Section, Clé)
'
'- ENREGISTRER UNE CLE :
' INI.SetKey(Section, Clé, Valeur) ' Encapsule l'enregistrement avec AutoSave = True
'
'
'
Imports System
Imports System.IO
Imports System.Text
Imports System.Collections
Imports System.Text.RegularExpressions
' Un Objet clsFileINI contient un Dictionary (SectionName as String, KeysData)
' chaque objet KeysData contient un Dictionary (Key, Valeur) du type (String, String)
Public Class INI
#Region "Déclarations"
Public Enum NullValueINI
EmptyKeyAsError
EmptyKeyAsValue
IsNull
IsNotNull
End Enum
Private _Sections As New Dictionary(Of String, KeysData)
Private _CommentSections As New Dictionary(Of String, String)
'Private _CommentHeadSections As New Dictionary(Of String, String()) ' Non utilisé
Private _CommentHeadDocument As New List(Of String)
Private _FileName As String
Private _EncodingText As System.Text.Encoding
Private _OptionForceDefautEncoding As Boolean = True
Private _FileINI As FileINI
Private _AutoSave As Boolean = True
'Private _ToSave As Boolean
#End Region
#Region "Constructeurs"
Public Sub New(ByVal fileName As String, ByVal EncodingText As System.Text.Encoding)
If fileName = Nothing Or fileName = "" Then Throw New Exception("Un argument 'fileName' n'est pas valide") : Exit Sub
If fileName = Path.GetFileName(fileName) Then
' c'est un fichier
_FileName = Application.StartupPath & "\" & fileName
Else
' c'est un chemin complet
_FileName = fileName
End If
_EncodingText = EncodingText
_FileINI = New FileINI(fileName, EncodingText)
Call Load(_FileName, EncodingText)
End Sub
''' <summary>
''' Crée une nouvelle instance de IniFile et charge le fichier ini
''' </summary>
''' <param name="fileName">Chemin du fichier ini</param>
Public Sub New(ByVal fileName As String)
Me.New(fileName, System.Text.Encoding.Default)
End Sub
#End Region
#Region "Accesseurs"
' Elements sections
Default Property Item(ByVal Section As String) As KeysData
Get
If Not _Sections.ContainsKey(Section) Then
SetSection(Section)
End If
Return DirectCast(_Sections(Section), KeysData)
End Get
Set(ByVal value As KeysData)
If Not _Sections.ContainsKey(Section) Then
SetSection(Section)
End If
_Sections(Section) = value
End Set
End Property
#End Region
#Region "Méthodes"
Public Sub AddComment(Comment As String)
If Comment Is Nothing Or Comment = "" Then Exit Sub
_CommentHeadDocument.Add(Comment)
'_ToSave = True
If _AutoSave Then Me.Save()
End Sub
Public Sub DeleteAllComment()
_CommentHeadDocument.Clear()
'_ToSave = True
If _AutoSave Then Me.Save()
End Sub
''' <summary>
''' Ajoute une section [section] au fichier ini
''' </summary>
''' <param name="Section">Nom de la section à créer</param>
Public Sub SetSection(ByVal Section As String)
'Try
_SetSection(Section)
If _AutoSave Then Me.Save()
'Return True
'Catch ex As Exception
'Return False
'End Try
End Sub
Public Sub SetSection(ByVal Section As String, Comment As String)
'Try
_SetSection(Section, Comment)
If _AutoSave Then Me.Save()
'Return True
'Catch ex As Exception
'Return False
'End Try
End Sub
Public Sub SetSection(ByVal Section As String, DeleteComment As Boolean)
'Try
_SetSection(Section, , DeleteComment)
If _AutoSave Then Me.Save()
'Return True
'Catch ex As Exception
'Return False
'End Try
End Sub
Private Sub _SetSection(ByVal Section As String, Optional Comment As String = Nothing, Optional DeleteComment As Boolean = False)
If Section Is Nothing Or Section = "" Then Throw New Exception("Un argument ne peut pas être 'Null'") : Exit Sub
If Not _Sections.ContainsKey(Section) Then
_Sections.Add(Section, New KeysData())
End If
'If Not Comment Is Nothing And Not _CommentSections.ContainsKey(Section) Then _CommentSections.Add(Section, Comment)
If Not Comment Is Nothing Then
If Not _CommentSections.ContainsKey(Section) Then
_CommentSections.Add(Section, Comment)
Else
_CommentSections(Section) = Comment
End If
End If
If DeleteComment = True Then
If _CommentSections.ContainsKey(Section) Then _CommentSections.Remove(Section)
End If
'_ToSave = True
'If _AutoSave Then Me.Save()
End Sub
''' <summary>
''' Retire une section du fichier
''' </summary>
''' <param name="Section">Nom de la section à enlever</param>
Public Sub RemoveSection(ByVal Section As String)
If Section = Nothing Then Throw New Exception("L'argument 'Section' ne doit pas être 'Null'") : Exit Sub
If _Sections.ContainsKey(Section) Then
_Sections.Remove(Section)
'_ToSave = True
End If
If _AutoSave Then Me.Save()
End Sub
''' <summary>
''' Suprime une clé. Supprime la section si elle est vide.
''' </summary>
''' <param name="Section">Nom de la section</param>
''' <param name="KeyName">Nom de la clef à supprimer</param>
Public Sub RemoveKey(ByVal Section As String, ByVal KeyName As String)
If Section = Nothing Or KeyName = Nothing Then Throw New Exception("Un argument ne peut pas être 'Null'") : Exit Sub
Me(Section).DeleteKey(KeyName)
If Me(Section).Keys.Count = 0 Then
_Sections.Remove(Section)
'_ToSave = True
End If
If _AutoSave Then Me.Save()
End Sub
''' <summary>
''' Modifie ou crée une valeur d'une clef dans une section
''' Crée la section si elle n'existe pas
''' Crée la clé si elle n'existe pas.
''' </summary>
''' <param name="section">Nom de la section</param>
''' <param name="KeyName">Nom de la clef</param>
''' <param name="KeyValue">Valeur de la clef</param>
Public Function SetKey(Section As String, KeyName As String, KeyValue As Object) As Boolean
Try
'Return SetKey(0, Section, KeyName, KeyValue)
Me(Section).SetKey(KeyName, KeyValue.ToString)
If _AutoSave Then Me.Save()
Return True
Catch ex As Exception
Return False
End Try
End Function
Public Function SetKey(Section As String, KeyName As String, KeyValue As Object, Comment As String) As Boolean
Try
'Return SetKey(1, Section, KeyName, KeyValue, Comment)
Me(Section).SetKey(KeyName, KeyValue.ToString, Comment)
If _AutoSave Then Me.Save()
Return True
Catch ex As Exception
Return False
End Try
End Function
Public Function SetKey(Section As String, KeyName As String, KeyValue As Object, DeleteComment As Boolean) As Boolean
Try
'Return SetKey(2, Section, KeyName, KeyValue, , DeleteComment)
Me(Section).SetKey(KeyName, KeyValue.ToString, DeleteComment)
If _AutoSave Then Me.Save()
Return True
Catch ex As Exception
Return False
End Try
End Function
''' <summary>
''' Retourne la valeur d'une clef dans une section
''' </summary>
''' <param name="Section">Nom de la section</param>
''' <param name="KeyName">Nom de la clef</param>
''' <returns>Valeur de la clef, ou la valeur entrée par défaut</returns>
Public Function GetKey(Of T)(ByVal Section As String, ByVal KeyName As String, Optional ByRef NullValue As NullValueINI = NullValueINI.EmptyKeyAsError) As T
If Section = Nothing Or KeyName = Nothing Then Throw New Exception("Un argument ne peut pas être 'Null'") : Exit Function
Dim _strValue As Object = Nothing
Dim _Type As Type = GetType(T)
Dim _TypeCode As TypeCode = Type.GetTypeCode(GetType(T))
_strValue = Me(Section).GetKey(KeyName)
If _strValue Is Nothing Then
If NullValue = NullValueINI.EmptyKeyAsError Then
Throw New INIExceptionUnableToGetValue("INI Error Exception : Impossible d'obtenir la valeur correspondante à la clé spécifiée : " & KeyName)
Exit Function
Else
NullValue = NullValueINI.IsNull
End If
Else
NullValue = NullValueINI.IsNotNull
End If
Dim _objValue As T = Nothing
Try
_objValue = CType(Convert.ChangeType(_strValue, _Type), T)
'_objValue = CType(_strValue, T)
Catch ex As Exception
Throw New INIExceptionUnableToConvertValue("INI Error Exception : La valeur obtenue ne peux pas être convertie vers le type attendue !")
End Try
Return _objValue
'Return _strValue
End Function
''' <summary>
''' Retourne la valeur d'une clef dans une section
''' </summary>
''' <param name="Section">Nom de la section</param>
''' <param name="KeyName">Nom de la clef</param>
''' <param name="defaut">valeur de la clé par défaut</param>
''' <returns>Valeur de la clef, ou la valeur entrée par défaut</returns>
Public Function GetKey(Of T)(ByVal Section As String, ByVal KeyName As String, ByVal defaut As T) As T
If Section = Nothing Or KeyName = Nothing Then Throw New Exception("Un argument ne peut pas être 'Null'") : Exit Function
Dim _strValue As Object = Nothing
_strValue = Me(Section).GetKey(KeyName)
If _strValue Is Nothing Then
Me(Section).SetKey(KeyName, defaut.ToString)
If _AutoSave Then Me.Save()
Return defaut
Else
Dim _objValue As T = Nothing
Try
'_objValue = CType(Convert.ChangeType(_strValue, VarType), T)
_objValue = CType(_strValue, T)
Catch ex As Exception
Throw New INIExceptionUnableToConvertValue("INI Error Exception : La valeur obtenue ne peux pas être convertie vers le type attendue !")
End Try
Return _objValue
End If
End Function
#End Region
#Region "Chargement et Sauvegarde"
Private Sub Load(ByVal fileName As String, ByVal EncodingText As System.Text.Encoding)
_FileName = fileName
_EncodingText = EncodingText
Dim CurrentSection As String = String.Empty
For Each Line As Line In _FileINI.Lines
If Line.HasSection Then
CurrentSection = Line.SectionName
_SetSection(CurrentSection)
ElseIf Line.HasKey Then
'If CurrentSection Is Nothing Or CurrentSection = "" Then Throw New Exception("Section invalide, vérifier si une ligne de commentaire contient le caractére = !") : Exit Sub
If Not CurrentSection Is Nothing And Not CurrentSection = "" Then
' La Section est valide, on peut ajouter la clé
If Line.HasComment Then
Me(CurrentSection).SetKey(Line.KeyName, Line.KeyValue, Line.Comment)
Else
Me(CurrentSection).SetKey(Line.KeyName, Line.KeyValue)
End If
Else
' Si la section est invalide on transforme la ligne en ligne de commentaire :
_CommentHeadDocument.Add(Line.LineTextIn)
End If
ElseIf Line.HasComment Then
_CommentHeadDocument.Add(Line.Comment)
End If
Next
End Sub
''' <summary>
''' Charge un fichier INI
''' </summary>
''' <param name="fileName">Nom du fichier à charger</param>
Private Sub Load(ByVal fileName As String)
Call Load(fileName, _EncodingText)
End Sub
''' <summary>
''' Sauvegarde le fichier INI en cours
''' </summary>
Public Sub Save()
If _OptionForceDefautEncoding Then _EncodingText = System.Text.Encoding.Default
Call SaveAs(_FileName, _EncodingText)
End Sub
Public Sub SaveAs(FileName As String)
_FileName = FileName
If _OptionForceDefautEncoding Then _EncodingText = System.Text.Encoding.Default
Call SaveAs(FileName, _EncodingText)
End Sub
Public Sub SaveAs(FileName As String, ByVal EncodingText As System.Text.Encoding)
_FileName = FileName
_EncodingText = EncodingText
_FileINI.Lines.Clear()
For Each Comment As String In _CommentHeadDocument
_FileINI.Lines.Add(New Line(Comment))
Next
For Each Section As String In _Sections.Keys
If _CommentSections.ContainsKey(Section) Then
_FileINI.Lines.Add(New Line(Section, _CommentSections(Section)))
Else
_FileINI.Lines.Add(New Line(Section, ))
End If
Dim KeysData As KeysData = DirectCast(_Sections(Section), KeysData)
For Each key As String In (KeysData.Keys)
If KeysData.Comment.ContainsKey(key) Then
_FileINI.Lines.Add(New Line(key, KeysData(key), KeysData.Comment(key)))
Else
_FileINI.Lines.Add(New Line(key, KeysData(key), ))
End If
Next
Next
Try
_FileINI.Save(FileName, EncodingText)
Catch ex As Exception
Throw New System.IO.IOException
End Try
End Sub
#End Region
#Region "Classe KeysData"
' Structure de donnée des clés
Public Class KeysData
#Region "Déclarations"
Private _Keys As New Dictionary(Of String, String)
Private _CommentKeys As New Dictionary(Of String, String)
#End Region
#Region "Constructeurs"
Public Sub New()
End Sub
#End Region
#Region "Accesseurs"
''' <summary>
''' Les clefs contenues dans la section
''' </summary>
Public ReadOnly Property Keys() As ICollection
Get
Return _Keys.Keys
End Get
End Property
''' <summary>
''' Element Clé
''' </summary>
Default Property Item(ByVal KeyName As String) As String
Get
If _Keys.ContainsKey(KeyName) Then
Return _Keys(KeyName).ToString()
Else
'SetKey(KeyName, "")
Return Nothing
End If
End Get
Set(ByVal value As String)
SetKey(KeyName, value)
End Set
End Property
Public ReadOnly Property Comment() As Dictionary(Of String, String)
Get
Return _CommentKeys
End Get
End Property
#End Region
#Region "Méthodes"
''' <summary>
''' Obtient la valeur d'une clef et léve une exception si elle n'existe pas
''' </summary>
''' <param name="KeyName">Nom de la clef</param>
Public Function GetKey(ByVal KeyName As String) As String
Dim _val As String
If _Keys.ContainsKey(KeyName) Then
' la clé existe
_val = CStr(_Keys(KeyName))
Else
' La clé n'existe pas
_val = Nothing
End If
Return _val
End Function
''' <summary>
''' Affecte une valeur à une clef et la crée si elle n'existe pas
''' </summary>
''' <param name="KeyName">Nom de la clef</param>
''' <param name="KeyValue">Valeur de la clef</param>
Public Sub SetKey(KeyName As String, KeyValue As String)
_SetKey(KeyName, KeyValue)
End Sub
Public Sub SetKey(KeyName As String, KeyValue As String, Comment As String)
_SetKey(KeyName, KeyValue)
If _CommentKeys.ContainsKey(KeyName) Then
_CommentKeys(KeyName) = Comment
Else
_CommentKeys.Add(KeyName, Comment)
End If
End Sub
Public Sub SetKey(KeyName As String, KeyValue As String, DeleteComment As Boolean)
_SetKey(KeyName, KeyValue)
If DeleteComment And _CommentKeys.ContainsKey(KeyName) Then _CommentKeys.Remove(KeyName)
End Sub
Private Sub _SetKey(Keyname As String, KeyValue As String)
' Corrigé : KeyName ne peut pas contenir la caratére "="
If Keyname.Contains("=") Then Throw New Exception("Caractère '=' interdit") : Exit Sub
If _Keys.ContainsKey(Keyname) Then
_Keys(Keyname) = KeyValue
Else
_Keys.Add(Keyname, KeyValue)
End If
End Sub
''' <summary>
''' Supprime une clefs
''' </summary>
''' <param name="KeyName">Nom de la clef à supprimer</param>
Public Sub DeleteKey(ByVal KeyName As String)
If KeyName = Nothing Then Throw New Exception("Un argument ne peut pas être 'Null'") : Exit Sub
If _Keys.ContainsKey(KeyName) Then _Keys.Remove(KeyName)
If _CommentKeys.ContainsKey(KeyName) Then _CommentKeys.Remove(KeyName)
End Sub
#End Region
End Class
#End Region
#Region "clsFile"
Private Class FileINI
#Region "Déclarations"
Private _Lines As New List(Of Line)
Private _sr As StreamReader
Private _sw As StreamWriter
'Private _OptionSpacesOnSection As Boolean
#End Region
#Region "Constructeurs"
Public Sub New(ByVal fileName As String, ByVal EncodingText As System.Text.Encoding)
Call Load(fileName, EncodingText)
End Sub
#End Region
#Region "Accesseurs"
ReadOnly Property Lines As List(Of Line)
Get
Lines = _Lines
End Get
End Property
'Public Property OptionSpacesOnSection() As Boolean
' Get
' Return _OptionSpacesOnSection
' End Get
' Set(ByVal value As Boolean)
' _OptionSpacesOnSection = value
' 'Call LoadData()
' End Set
'End Property
#End Region
#Region "Méthodes Public"
Public Sub Load(ByVal fileName As String, ByVal EncodingText As System.Text.Encoding)
_sr = New StreamReader(File.Open(fileName, FileMode.OpenOrCreate), EncodingText)
'' lit chaque ligne du fichier INI
Dim id As Integer
While Not _sr.EndOfStream
' 'Get current line
Dim Line As String = _sr.ReadLine
Dim LineData As New Line(Line, INI.Line.LineType.Indefinie)
_Lines.Add(LineData)
id += 1
End While
_sr.Close()
End Sub
Public Sub Save(ByVal fileName As String, ByVal EncodingText As System.Text.Encoding)
_sw = New StreamWriter(fileName, False, EncodingText)
Try
For Each LineData As Line In _Lines
_sw.Write(LineData.LineTextOut + Environment.NewLine)
Next
_sw.Flush()
_sw.Close()
Catch ex As Exception
Throw New System.IO.IOException
End Try
End Sub
#End Region
End Class
#End Region
#Region "Class clsLine"
Shadows Class Line
#Region "Déclarations"
Private _LineData As LineData
'Private _LineParameters As LineParameters
Private _OptionAutorisedSpaceInSection As Boolean = True
Private _OptionAutorisedSpaceInKeyName As Boolean = True
Const COM As Char = CChar(";")
Const CS1 As Char = CChar("[")
Const CS2 As Char = CChar("]")
Const EGAL As Char = CChar("=")
Const DQUOTES As Char = CChar("""")
Private _rgSection As Regex
Private _rgSectionAndComment As Regex
Private _rgKey As Regex
Private _rgKeyAndComment As Regex
Private _rgKeyWithQuotes As Regex
Private _rgKeyWithQuotesAndComment As Regex
Private _rgComment As Regex
Public Enum LineType
Indefinie
Section
Clé
Commentaire
End Enum
Structure LineData
'Public Id As Integer
Public LineTextIn As String
Public LineTextOut As String
Public HasSection As Boolean
Public HasKey As Boolean
Public HasComment As Boolean
Public SectionName As String
Public KeyName As String
Public KeyValue As String
Public Comment As String
End Structure
#End Region
#Region "Constructeurs"
Public Sub New()
_LineData = New LineData
Const P1 As String = "(.*)" 'Pattern avec espace
Const P2 As String = "[\S]+" ' Pattern sans espace
Dim pS As String 'Pattern Section
Dim pK As String ' Pattern Key
If _OptionAutorisedSpaceInSection = True Then pS = P1 Else pS = P2
If _OptionAutorisedSpaceInKeyName = True Then pK = P1 Else pK = P2
_rgSectionAndComment = New Regex(String.Format("(\{1}{0}\{2})([\s]*){3}(.*)", pS, CS1, CS2, COM))
_rgSection = New Regex(String.Format("\{1}{0}\{2}", pS, CS1, CS2))
_rgKeyWithQuotesAndComment = New Regex(String.Format("({0})([\s]*){1}([\s]*)(\{2})(.*)(\{2})([\s]*){3}(.*)", pK, EGAL, DQUOTES, COM))
_rgKeyWithQuotes = New Regex(String.Format("({0})([\s]*){1}([\s]*)(\{2})(.*)(\{2})", pK, EGAL, DQUOTES)) '\x22 = "
_rgKeyAndComment = New Regex(String.Format("({0})([\s]*){1}([\s]*)(.*)([\s]*){2}(.*)", pK, EGAL, COM))
_rgKey = New Regex(String.Format("({0})([\s]*){1}([\s]*)(.*)", pK, EGAL))
_rgComment = New Regex(String.Format("{0}(.*)", COM))
End Sub
Public Sub New(LineText As String, ByRef LineType As LineType)
Me.New()
Call DecodeLine(LineText)
Call EncodeLine()
End Sub
Public Sub New(Section As String, Optional Commentaire As String = Nothing)
Me.New()
If Not Section Is Nothing And Not Section = "" Then
_LineData.HasSection = True
_LineData.SectionName = Section
End If
If Not Commentaire Is Nothing And Not Commentaire = "" Then
_LineData.HasComment = True
_LineData.Comment = Commentaire
End If
Call EncodeLine()
End Sub
Public Sub New(KeyName As String, KeyValue As String, Optional Commentaire As String = Nothing)
Me.New()
If (Not KeyName Is Nothing And Not KeyName = "") And (Not KeyValue Is Nothing And Not KeyValue = "") Then
_LineData.HasKey = True
_LineData.KeyName = KeyName
_LineData.KeyValue = KeyValue
End If
If Not Commentaire Is Nothing And Not Commentaire = "" Then
_LineData.HasComment = True
_LineData.Comment = Commentaire
End If
Call EncodeLine()
End Sub
Public Sub New(Commentaire As String)
Me.New()
If Not Commentaire Is Nothing And Not Commentaire = "" Then
_LineData.HasComment = True
_LineData.Comment = Commentaire
End If
Call EncodeLine()
End Sub
#End Region
#Region "Accesseurs"
ReadOnly Property LineTextIn As String
Get
Return _LineData.LineTextIn
End Get
End Property
ReadOnly Property LineTextOut As String
Get
Return _LineData.LineTextOut
End Get
End Property
ReadOnly Property HasSection As Boolean
Get
Return _LineData.HasSection
End Get
End Property
ReadOnly Property HasKey As Boolean
Get
Return _LineData.HasKey
End Get
End Property
ReadOnly Property HasComment As Boolean
Get
Return _LineData.HasComment
End Get
End Property
Property SectionName As String
Get
Return _LineData.SectionName
End Get
Set(value As String)
_LineData.SectionName = value
Call EncodeLine()
End Set
End Property
Property KeyName As String
Get
Return _LineData.KeyName
End Get
Set(value As String)
_LineData.KeyName = value
Call EncodeLine()
End Set
End Property
Property KeyValue As String
Get
Return _LineData.KeyValue
End Get
Set(value As String)
_LineData.KeyValue = value
Call EncodeLine()
End Set
End Property
Property Comment As String
Get
Return _LineData.Comment
End Get
Set(value As String)
_LineData.Comment = value
Call EncodeLine()
End Set
End Property
#End Region
#Region "Méthodes Privé"
Private Sub DecodeLine(LineTextIn As String)
' Régles d'encodage et de décodage utilisées pour la lecture/Ecriture des fichiers INI :
' "KeyName" et "Comment" ne peuvent pas contenir le caratére "="
' "KeyValue" peut contenir le caratére "="
' "KeyName" et "Comment" peuvent contenir le caractére ";"
' "KeyValue" peut contenir la caratére ";", il est alors borné par des Guillements dans le fichier INI, Ex: "...;...." .
_LineData.LineTextIn = LineTextIn
Dim mac As MatchCollection
Debug.Print(String.Format(">>{0}", _LineData.LineTextIn))
If _rgSectionAndComment.IsMatch(LineTextIn) Then
_LineData.HasSection = True
_LineData.HasComment = True
mac = _rgSection.Matches(LineTextIn)
If mac.Count > 0 Then
Dim Text As String
Text = mac(0).Value
_LineData.SectionName = mac(0).Value.TrimStart(CS1).TrimEnd(CS2) ' GetSection
Text = LineTextIn.Remove(0, Text.Length)
mac = _rgComment.Matches(Text)
_LineData.Comment = mac(0).Value.TrimStart(COM).Trim() ' GetComment
End If
Debug.Print(String.Format("{1}{0}{2}{0}", "|", _LineData.SectionName, _LineData.Comment))
ElseIf _rgSection.IsMatch(LineTextIn) Then
_LineData.HasSection = True
mac = _rgSection.Matches(LineTextIn)
If mac.Count > 0 Then
_LineData.SectionName = mac(0).Value.TrimStart(CS1).TrimEnd(CS2) ' GetSection
End If
Debug.Print(String.Format("{1}{0}", "|", _LineData.SectionName))
ElseIf _rgKeyWithQuotesAndComment.IsMatch(LineTextIn) Then
_LineData.HasKey = True
_LineData.HasComment = True
mac = _rgKeyWithQuotesAndComment.Matches(LineTextIn)
If mac.Count > 0 Then
Dim Text As String
' Value peut contenir le carctére ";"
Dim rg As New Regex(String.Format("([\s]*)[^\{0}]+(\{0})", EGAL))
mac = rg.Matches(LineTextIn)
_LineData.KeyName = mac(0).Value.TrimEnd(EGAL).Trim
Text = LineTextIn.Remove(0, mac(0).Value.Length)
rg = New Regex(String.Format("([\s]*)(\{0})[^\{0}]+(\{0})", DQUOTES))
mac = rg.Matches(Text)
_LineData.KeyValue = mac(0).Value.Trim.Trim(DQUOTES)
Text = Text.Remove(0, mac(0).Value.Length)
rg = New Regex(String.Format("(\{0})(.*)", COM))
mac = rg.Matches(Text)
_LineData.Comment = mac(0).Value.TrimStart(COM).Trim ' GetComment
Debug.Print(String.Format("{1}{0}{2}{0}{3}{0}", "|", _LineData.KeyName, _LineData.KeyValue, _LineData.Comment))
End If
ElseIf _rgKeyWithQuotes.IsMatch(LineTextIn) Then
_LineData.HasKey = True
mac = _rgKeyWithQuotes.Matches(LineTextIn)
If mac.Count > 0 Then
Dim Text As String
' Value peut contenir le carctére ";"
Dim rg As New Regex(String.Format("([\s]*)[^\{0}]+(\{0})", EGAL))
mac = rg.Matches(LineTextIn)
_LineData.KeyName = mac(0).Value.TrimEnd(EGAL).Trim
Text = LineTextIn.Remove(0, mac(0).Value.Length)
rg = New Regex(String.Format("(\{0})(.*)(\{0})", DQUOTES))
mac = rg.Matches(Text)
_LineData.KeyValue = mac(0).Value.Trim.Trim(DQUOTES)
Debug.Print(String.Format("{1}{0}{2}{0}", "|", _LineData.KeyName, _LineData.KeyValue))
End If
ElseIf _rgKeyAndComment.IsMatch(LineTextIn) Then
_LineData.HasKey = True
_LineData.HasComment = True
mac = _rgKeyAndComment.Matches(LineTextIn)
If mac.Count > 0 Then
Dim Text As String
' Value ne peut pas contenir le carctére ";"
Dim rg As New Regex(String.Format("([\s]*)[^\{0}]+(\{0})", EGAL))
mac = rg.Matches(LineTextIn)
_LineData.KeyName = mac(0).Value.TrimEnd(EGAL).Trim
Text = LineTextIn.Remove(0, mac(0).Value.Length)
rg = New Regex(String.Format("(\{0})[^\{1}]+", EGAL, COM))
mac = rg.Matches(LineTextIn)
_LineData.KeyValue = mac(0).Value.TrimStart(EGAL).Trim
rg = New Regex(String.Format("(\{0})(.*)", COM))
mac = rg.Matches(Text)
_LineData.Comment = mac(0).Value.TrimStart(COM).Trim ' GetComment
Debug.Print(String.Format("{1}{0}{2}{0}{3}{0}", "|", _LineData.KeyName, _LineData.KeyValue, _LineData.Comment))
End If
ElseIf _rgKey.IsMatch(LineTextIn) Then
_LineData.HasKey = True
mac = _rgKey.Matches(LineTextIn)
If mac.Count > 0 Then
Dim rg As New Regex(String.Format("([\s]*)[^\{0}]+(\{0})", EGAL))
mac = rg.Matches(LineTextIn)
_LineData.KeyName = mac(0).Value.TrimEnd(EGAL).Trim
rg = New Regex(String.Format("(\{0})(.*)", EGAL))
mac = rg.Matches(LineTextIn)
_LineData.KeyValue = mac(0).Value.TrimStart(EGAL).Trim
Debug.Print(String.Format("{1}{0}{2}{0}", "|", _LineData.KeyName, _LineData.KeyValue))
End If
ElseIf _rgComment.IsMatch(LineTextIn) Then
_LineData.HasComment = True
mac = _rgComment.Matches(LineTextIn)
If mac.Count > 0 Then
_LineData.Comment = mac(0).Value.TrimStart(COM).Trim() ' GetComment
Debug.Print(String.Format("{1}{0}", "|", _LineData.Comment))
End If
End If
End Sub
Private Sub EncodeLine()
_LineData.LineTextOut = EncodeLine(_LineData)
End Sub
Private Function EncodeLine(Line As LineData) As String
Dim LineText As String = String.Empty
If Line.HasSection Then
LineText = String.Format("{1}{0}{2}", Line.SectionName.Trim, CS1, CS2)
ElseIf Line.HasKey Then
If Line.KeyValue.Contains(COM) Then
LineText = String.Format("{0} {2} {3}{1}{3}", Line.KeyName.Trim, Line.KeyValue.Trim, EGAL, DQUOTES)
Else
LineText = String.Format("{0} {2} {1}", Line.KeyName.Trim, Line.KeyValue.Trim, EGAL)
End If
End If
If Line.HasComment Then
Dim IndentComment As Integer ' + Int(1 + StringLine.Length / 10) * 10
If Line.HasSection Then
IndentComment = 50
ElseIf Line.HasKey Then
IndentComment = 50
Else
IndentComment = 0
End If
If LineText.Length >= IndentComment Then IndentComment = LineText.Length
Dim Espace As Integer = IndentComment - LineText.Length
'Dim Tab As Integer = Int(Espace / 4) ' 1 Tab=4 espace
'StringLine += New String(ControlChars.Tab, 2) & " ; " & LineData.Comment
'LineText += New String(" ", Espace) & " ; " & Line.Comment
LineText += String.Format("{0} {1} {2}", New String(CChar(" "), Espace), COM, Line.Comment)
End If
Return LineText
End Function
Private Function xCode(c As Char) As String
Dim u As Short = Convert.ToInt16(c) ' Convertie un caractéres Unicode
'Dim h As UInt32 = Convert.ToInt32(u, 16)
Dim h As String = u.ToString("X")
xCode = "x" & h.ToString
Return xCode
End Function
#End Region
End Class
'Public Class LineParameters
' Event ValueChanged(ByVal PropertyName As String, ByVal Value As Object)
' Private _Param1 As String
' Public Property Param1() As String
' Get
' Return _Param1
' End Get
' Set(ByVal value As String)
' _Param1 = value
' RaiseEvent ValueChanged("Param1", value)
' End Set
' End Property
'End Class
#End Region
End Class
#Region "Exceptions personnalisées"
Class INIExceptionUnableToGetValue
Inherits System.ApplicationException
Public Sub New()
End Sub
Public Sub New(ByVal message As String)
MyBase.New(message)
End Sub
Public Sub New(ByVal message As String, ByVal inner As Exception)
MyBase.New(message, inner)
End Sub
End Class
Class INIExceptionUnableToConvertValue
Inherits System.ApplicationException
Public Sub New()
End Sub
Public Sub New(ByVal message As String)
MyBase.New(message)
End Sub
Public Sub New(ByVal message As String, ByVal inner As Exception)
MyBase.New(message, inner)
End Sub
End Class
#End Region |
Partager