Questo sito utilizza cookies solo per scopi di autenticazione sul sito e nient'altro. Nessuna informazione personale viene tracciata. Leggi l'informativa sui cookies.
Username: Password: oppure
DataBase Modifier 2.0 - Class1.vb

Class1.vb

Caricato da: Totem
Scarica il programma completo

  1. Imports System.Windows.Forms
  2. Namespace DataBase
  3.     Public Class StreamAppender
  4.         Const Temp = "Temp.txt"
  5.         Private R As IO.StreamReader
  6.         Private W As IO.StreamWriter
  7.         Private GlobalFile As String
  8.         Public Sub New(ByVal file As String)
  9.             R = New IO.StreamReader(file)
  10.             W = New IO.StreamWriter(Temp)
  11.  
  12.             W.Write(R.ReadToEnd)
  13.  
  14.             R.Close()
  15.             R = Nothing
  16.             GlobalFile = file
  17.         End Sub
  18.         Public Sub Write(ByVal S As String)
  19.             W.Write(S)
  20.         End Sub
  21.         Public Sub WriteLine(ByVal S As String)
  22.             W.WriteLine(S)
  23.         End Sub
  24.         Public Sub Close()
  25.             W.Close()
  26.             W = Nothing
  27.  
  28.             FileCopy(Temp, GlobalFile)
  29.             Kill(Temp)
  30.         End Sub
  31.     End Class
  32.     Public Class SearchFunctions
  33.         Public Shared Function QuickSearch(ByVal ToSeek As String, ByVal InWhichFind As String) As Int16
  34.             Dim S As String = ""
  35.  
  36.             For i As Int16 = 0 To ToSeek.Length - 1
  37.                 S = S & InWhichFind.Chars(i)
  38.             Next
  39.  
  40.             For i As Int64 = ToSeek.Length To InWhichFind.Length - 1
  41.                 If S = ToSeek Then
  42.                     Return i
  43.                 End If
  44.                 S = S.Remove(0, 1)
  45.                 S = S & InWhichFind.Chars(i)
  46.             Next
  47.         End Function
  48.         Public Shared Function Exists(ByVal File As String) As Boolean
  49.             Try
  50.                 Dim R As New IO.StreamReader(File)
  51.                 R.Close()
  52.                 R = Nothing
  53.                 Return True
  54.             Catch ex As IO.FileNotFoundException
  55.                 Return False
  56.             End Try
  57.         End Function
  58.     End Class
  59.     Public Enum DBException
  60.         SuccesfulOperation = 0
  61.         ExistingField
  62.         UnexistingField
  63.         ExistingValue
  64.         UnexistingValue
  65.     End Enum
  66.     Public Enum DBParameter
  67.         'Parametri di aggiunta membri alle liste:
  68.         'Field: aggiunge solo i nomi dei campi
  69.         'ValueNameOnly: aggiunge solo i nomi dei valori
  70.         'ValueValOnly: aggiunge solo i valori dei valori
  71.         'ValueNameAndVal: aggiunge i valori, specificando prima il nome e poi il valore
  72.         'ALL: (solo per TreeView) aggiunge i campi con i sotto-valori; può essere sommato a uno dei precedenti
  73.         '     (tranne Field) per ottenere un diverso effetto di visualizzazione dei valori
  74.         Field
  75.         ValueNameOnly
  76.         ValueValOnly
  77.         ValueNameAndVal
  78.         ALL
  79.     End Enum
  80.     Public Class DataBase
  81.         Public File As String
  82.         Public Structure Value
  83.             Dim Name As String
  84.             Dim Val As Object
  85.         End Structure
  86.         Public Structure Node
  87.             Dim Text As String
  88.             Dim Items As ArrayList
  89.         End Structure
  90.         Public Main As Node
  91.         Private Re As IO.StreamReader
  92.         Private Function BreakLine(ByVal S As String) As Value
  93.             Dim Nome As String = ""
  94.             Dim ValueV As String = ""
  95.             Dim T As Boolean = False
  96.             Dim V As Value
  97.  
  98.             For i As Int32 = 0 To S.Length - 1
  99.                 If S(i) = "=" Then
  100.                     T = True
  101.                 Else
  102.                     If Not T Then
  103.                         Nome = Nome & S(i)
  104.                     Else
  105.                         ValueV = ValueV & S(i)
  106.                     End If
  107.                 End If
  108.             Next
  109.  
  110.             V.Name = Nome
  111.             V.Val = ValueV
  112.  
  113.             Return V
  114.         End Function
  115.         Public Sub New(ByVal s As String)
  116.             File = s
  117.             Main.Items = New ArrayList
  118.             Try
  119.                 Dim R As New IO.StreamReader(File)
  120.                 Dim F As String = ""
  121.                 Dim a As Int64
  122.                 Dim T As Node = Nothing
  123.                 Dim Line As Value
  124.  
  125.                 Main.Items = New ArrayList
  126.  
  127.                 a = 0
  128.                 While Not R.EndOfStream
  129.                     F = R.ReadLine
  130.                     If F <> Nothing Then
  131.                         If F.Chars(0) = "<" Then
  132.                             F = F.Remove(0, 1)
  133.                             F = F.Remove(F.Length - 1, 1)
  134.                             T.Text = F
  135.                             T.Items = New ArrayList
  136.                             Main.Items.Add(T)
  137.                             a += 1
  138.                         Else
  139.                             If F.Chars(0) <> ":" Then
  140.                                 Line = BreakLine(F)
  141.                                 Main.Items.Item(a - 1).Items.Add(Line)
  142.                             End If
  143.                         End If
  144.                     End If
  145.                 End While
  146.  
  147.                 R.Close()
  148.                 R = Nothing
  149.             Catch ex As IO.FileNotFoundException
  150.  
  151.             End Try
  152.         End Sub
  153.         Public Function GetValue(ByVal Field As String, ByVal ValueN As String) As Object
  154.             Dim T As Node
  155.             Dim B As Value
  156.             Dim i As Int64 = 0
  157.  
  158.             For Each T In Main.Items
  159.                 If T.Text = Field Then
  160.                     For Each B In Main.Items.Item(i).items
  161.                         If B.Name = ValueN Then
  162.                             Return B.Val
  163.                         End If
  164.                     Next
  165.                 End If
  166.                 i += 1
  167.             Next
  168.  
  169.             Return Nothing
  170.         End Function
  171.         Public Sub AddValue(ByVal Field As String, ByVal Name As String, ByVal Val As Object)
  172.             Dim T As Node
  173.             Dim V As Value
  174.             Dim i As Int64
  175.             Dim Found As Boolean = False
  176.  
  177.             V.Name = Name
  178.             V.Val = Val
  179.  
  180.             For Each T In Main.Items
  181.                 If T.Text = Field Then
  182.                     Found = True
  183.                     Exit For
  184.                 End If
  185.                 i += 1
  186.             Next
  187.  
  188.             If Found = True Then
  189.                 Main.Items.Item(i).Items.Add(V)
  190.             End If
  191.         End Sub
  192.         Public Sub SetValue(ByVal Field As String, ByVal ValueN As String, ByVal Val As Object)
  193.             Dim T As Node
  194.             Dim B As Value
  195.             Dim i As Int64 = 0
  196.             Dim a As Int64 = 0
  197.             Dim V As Value
  198.  
  199.             V.Val = Val
  200.             V.Name = ValueN
  201.  
  202.             For Each T In Main.Items
  203.                 If T.Text = Field Then
  204.                     For Each B In Main.Items.Item(i).items
  205.                         If B.Name = ValueN Then
  206.                             Main.Items.Item(i).Items.Item(a) = V
  207.                             Exit Sub
  208.                         End If
  209.                         a += 1
  210.                     Next
  211.                 End If
  212.                 i += 1
  213.             Next
  214.         End Sub
  215.         Public Sub EraseValue(ByVal Field As String, ByVal ValueN As String)
  216.             Dim T As Node
  217.             Dim B As Value
  218.             Dim i As Int64 = 0
  219.             Dim a As Int64 = 0
  220.  
  221.  
  222.             For Each T In Main.Items
  223.                 If T.Text = Field Then
  224.                     For Each B In Main.Items(i).Items
  225.                         If B.Name = ValueN Then
  226.                             Main.Items.Item(i).Items.remove(B)
  227.                             Exit Sub
  228.                         End If
  229.                         a += 1
  230.                     Next
  231.                 End If
  232.                 i += 1
  233.             Next
  234.  
  235.         End Sub
  236.         Public Sub AddField(ByVal Name As String)
  237.             Dim T As Node
  238.  
  239.             T.Text = Name
  240.             T.Items = New ArrayList
  241.             Main.Items.Add(T)
  242.         End Sub
  243.         Public Sub EraseField(ByVal Name As String, Optional ByVal First As Boolean = True)
  244.             Dim T As Node
  245.             Dim i As Int64 = 0
  246.  
  247.             For Each T In Main.Items
  248.                 If T.Text = Name Then
  249.                     Main.Items.Remove(T)
  250.                     If First Then
  251.                         Exit Sub
  252.                     End If
  253.                 End If
  254.                 i += 1
  255.             Next
  256.         End Sub
  257.         Public Function GetValues(ByVal Field As String) As ArrayList
  258.             Dim T As Node
  259.             Dim i As Int64
  260.  
  261.             For Each T In Main.Items
  262.                 If T.Text = Field Then
  263.                     Return Main.Items.Item(i).Items
  264.                 End If
  265.                 i += 1
  266.             Next
  267.  
  268.             Return Nothing
  269.         End Function
  270.         Public Function GetFields() As ArrayList
  271.             Dim S As New ArrayList
  272.             Dim N As Node
  273.  
  274.             For Each N In Main.Items
  275.                 S.Add(N)
  276.             Next
  277.  
  278.             Return S
  279.         End Function
  280.         Public Function GetValuesNames(ByVal FieldName As String) As ArrayList
  281.             Dim T As Node
  282.             Dim V As Value
  283.             Dim S As New ArrayList
  284.             Dim i As Int64
  285.  
  286.             For Each T In Main.Items
  287.                 If T.Text = FieldName Then
  288.                     For Each V In T.Items
  289.                         S.Add(V.Name)
  290.                     Next
  291.                     Exit For
  292.                 End If
  293.                 i += 1
  294.             Next
  295.  
  296.             Return S
  297.         End Function
  298.         Public Function GetFieldsNames() As ArrayList
  299.             Dim S As New ArrayList
  300.             Dim N As Node
  301.  
  302.             For Each N In Main.Items
  303.                 S.Add(N.Text)
  304.             Next
  305.  
  306.             Return S
  307.         End Function
  308.         Public Sub Save()
  309.             Dim B As Value
  310.             Dim R As New IO.StreamWriter(File)
  311.  
  312.             For i As Int64 = 0 To Main.Items.Count - 1
  313.                 R.WriteLine("<" & Main.Items.Item(i).text & ">")
  314.                 For Each B In Main.Items(i).Items
  315.                     R.WriteLine(B.Name & "=" & B.Val)
  316.                 Next
  317.             Next
  318.  
  319.             R.Close()
  320.             R = Nothing
  321.         End Sub
  322.         Public Sub SaveAs(ByVal FileN As String)
  323.             Dim B As Value
  324.             Dim R As New IO.StreamWriter(FileN)
  325.  
  326.             For i As Int64 = 0 To Main.Items.Count - 1
  327.                 R.WriteLine("<" & Main.Items.Item(i).text & ">")
  328.                 For Each B In Main.Items(i).Items
  329.                     R.WriteLine(B.Name & "=" & B.Val)
  330.                 Next
  331.             Next
  332.  
  333.             R.Close()
  334.             R = Nothing
  335.         End Sub
  336.         'In tutte le funzioni che seguono il parametro FieldName è sempre obbligatorio, ma non sempre necessario
  337.         'Infatti è obbligatorio solo se si sceglie uno stile di visualizzazione fra questi: ValueNameOnly, ValueValOnly,
  338.         'ValueNameAndVal, altrimenti può essere lasciato vuoto
  339.         Public Function TransferInListBox(ByVal FieldName As String, ByRef List As Windows.Forms.ListBox, Optional ByVal What As DBParameter = DBParameter.ValueNameOnly) As ArrayList
  340.             If What = DBParameter.Field Then
  341.                 Dim AllFields As ArrayList = GetFieldsNames()
  342.                 Dim S As String
  343.  
  344.                 For Each S In AllFields
  345.                     List.Items.Add(S)
  346.                 Next
  347.  
  348.                 Return AllFields
  349.             Else
  350.                 Dim AllItems As ArrayList = GetValues(FieldName)
  351.                 Dim V As Value
  352.  
  353.                 For Each V In AllItems
  354.                     If What = DBParameter.ValueNameOnly Then
  355.                         List.Items.Add(V.Name)
  356.                     End If
  357.                     If What = DBParameter.ValueValOnly Then
  358.                         List.Items.Add(V.Val)
  359.                     End If
  360.                     If What = DBParameter.ValueNameAndVal Then
  361.                         List.Items.Add(V.Name + "=" + V.Val)
  362.                     End If
  363.                 Next
  364.  
  365.                 Return AllItems
  366.             End If
  367.         End Function
  368.         Public Function TransferInListView(ByVal FieldName As String, ByRef List As Windows.Forms.ListView, Optional ByVal CreateGroup As Boolean = True, Optional ByVal Mode As DBParameter = DBParameter.ValueNameAndVal) As ArrayList
  369.             Dim G As New Windows.Forms.ListViewGroup(FieldName)
  370.             Dim L As Windows.Forms.ListViewItem
  371.  
  372.             If CreateGroup Then
  373.                 List.Groups.Add(G)
  374.             End If
  375.             List.View = View.Details
  376.  
  377.             If Mode = DBParameter.Field Then
  378.                 Dim AllFields As ArrayList = GetFieldsNames()
  379.                 Dim S As String
  380.  
  381.                 For Each S In AllFields
  382.                     L = New ListViewItem(S)
  383.                     List.Items.Add(L)
  384.                 Next
  385.  
  386.                 Return AllFields
  387.             Else
  388.                 Dim AllItems As ArrayList = GetValues(FieldName)
  389.                 Dim S(1) As String
  390.                 Dim V As Value
  391.  
  392.                 For Each V In AllItems
  393.                     If Mode = DBParameter.ValueNameOnly Or Mode = DBParameter.ValueNameAndVal Then
  394.                         S(0) = V.Name
  395.                     End If
  396.                     If Mode = DBParameter.ValueValOnly Or Mode = DBParameter.ValueNameAndVal Then
  397.                         S(1) = V.Val
  398.                     End If
  399.                     If CreateGroup Then
  400.                         L = New ListViewItem(S, G)
  401.                     Else
  402.                         L = New ListViewItem(S)
  403.                     End If
  404.                     List.Items.Add(L)
  405.                 Next
  406.  
  407.                 Return AllItems
  408.             End If
  409.         End Function
  410.         Public Function TransferInTreeView(ByVal FieldName As String, ByRef List As TreeView, Optional ByVal Mode As DBParameter = DBParameter.Field) As ArrayList
  411.             If Mode >= DBParameter.ALL Then
  412.                 Dim AllFields As ArrayList = GetFieldsNames()
  413.                 Dim S As String
  414.                 Dim N As TreeNode = Nothing
  415.                 Dim V As Value
  416.  
  417.                 For Each S In AllFields
  418.                     N.Text = S
  419.                     For Each V In GetValues(S)
  420.                         If Mode - DBParameter.ALL = DBParameter.ValueNameOnly Then
  421.                             N.Nodes.Add(V.Name)
  422.                         End If
  423.                         If Mode - DBParameter.ALL = DBParameter.ValueValOnly Then
  424.                             N.Nodes.Add(V.Val)
  425.                         End If
  426.                         If Mode - DBParameter.ALL = DBParameter.ValueNameAndVal Then
  427.                             N.Nodes.Add(V.Name + "=" + V.Val)
  428.                         End If
  429.                     Next
  430.                     List.Nodes.Add(N)
  431.                 Next
  432.  
  433.                 Return AllFields
  434.             Else
  435.                 Dim AllItems As ArrayList = GetValues(FieldName)
  436.                 Dim N As TreeNode = Nothing
  437.                 Dim V As Value
  438.  
  439.                 For Each V In AllItems
  440.                     If Mode = DBParameter.ValueNameOnly Then
  441.                         N.Text = V.Name
  442.                     End If
  443.                     If Mode = DBParameter.ValueValOnly Then
  444.                         N.Text = V.Val
  445.                     End If
  446.                     If Mode = DBParameter.ValueNameAndVal Then
  447.                         N.Text = V.Name + "=" + V.Val
  448.                     End If
  449.                     List.Nodes.Add(N)
  450.                 Next
  451.  
  452.                 Return AllItems
  453.             End If
  454.         End Function
  455.         Public Function TransferInComboBox(ByVal FieldName As String, ByRef List As Windows.Forms.ComboBox, Optional ByVal What As DBParameter = DBParameter.ValueNameOnly) As ArrayList
  456.             If What = DBParameter.Field Then
  457.                 Dim AllFields As ArrayList = GetFieldsNames()
  458.                 Dim S As String
  459.  
  460.                 For Each S In AllFields
  461.                     List.Items.Add(S)
  462.                 Next
  463.  
  464.                 Return AllFields
  465.             Else
  466.                 Dim AllItems As ArrayList = GetValues(FieldName)
  467.                 Dim V As Value
  468.  
  469.                 For Each V In AllItems
  470.                     If What = DBParameter.ValueNameOnly Then
  471.                         List.Items.Add(V.Name)
  472.                     End If
  473.                     If What = DBParameter.ValueValOnly Then
  474.                         List.Items.Add(V.Val)
  475.                     End If
  476.                     If What = DBParameter.ValueNameAndVal Then
  477.                         List.Items.Add(V.Name + "=" + V.Val)
  478.                     End If
  479.                 Next
  480.  
  481.                 Return AllItems
  482.             End If
  483.         End Function
  484.         Public Function TransferInDomain(ByVal FieldName As String, ByRef List As Windows.Forms.DomainUpDown, Optional ByVal What As DBParameter = DBParameter.ValueNameOnly) As ArrayList
  485.             If What = DBParameter.Field Then
  486.                 Dim AllFields As ArrayList = GetFieldsNames()
  487.                 Dim S As String
  488.  
  489.                 For Each S In AllFields
  490.                     List.Items.Add(S)
  491.                 Next
  492.  
  493.                 Return AllFields
  494.             Else
  495.                 Dim AllItems As ArrayList = GetValues(FieldName)
  496.                 Dim V As Value
  497.  
  498.                 For Each V In AllItems
  499.                     If What = DBParameter.ValueNameOnly Then
  500.                         List.Items.Add(V.Name)
  501.                     End If
  502.                     If What = DBParameter.ValueValOnly Then
  503.                         List.Items.Add(V.Val)
  504.                     End If
  505.                     If What = DBParameter.ValueNameAndVal Then
  506.                         List.Items.Add(V.Name + "=" + V.Val)
  507.                     End If
  508.                 Next
  509.  
  510.                 Return AllItems
  511.             End If
  512.         End Function
  513.     End Class
  514. End Namespace