Imports System.Windows.Forms
Namespace DataBase
Public Class StreamAppender
Const Temp = "Temp.txt"
Private R As IO.StreamReader
Private W As IO.StreamWriter
Private GlobalFile As String
Public Sub New(ByVal file As String)
R
= New IO.
StreamReader(file)
W = New IO.StreamWriter(Temp)
W.Write(R.ReadToEnd)
R.Close()
R = Nothing
End Sub
Public Sub Write(ByVal S As String)
W.Write(S)
End Sub
Public Sub WriteLine(ByVal S As String)
W.WriteLine(S)
End Sub
Public Sub Close()
W.Close()
W = Nothing
FileCopy(Temp, GlobalFile)
Kill(Temp)
End Sub
End Class
Public Class SearchFunctions
Public Shared Function QuickSearch(ByVal ToSeek As String, ByVal InWhichFind As String) As Int16
Dim S As String = ""
For i As Int16 = 0 To ToSeek.Length - 1
S = S & InWhichFind.Chars(i)
Next
For i As Int64 = ToSeek.Length To InWhichFind.Length - 1
If S = ToSeek Then
Return i
End If
S = S.Remove(0, 1)
S = S & InWhichFind.Chars(i)
Next
End Function
Public Shared Function Exists
(ByVal File As String) As Boolean
Try
Dim R
As New IO.
StreamReader(File)
R.Close()
R = Nothing
Return True
Catch ex As IO.FileNotFoundException
Return False
End Try
End Function
End Class
Public Enum DBException
SuccesfulOperation = 0
ExistingField
UnexistingField
ExistingValue
UnexistingValue
End Enum
Public Enum DBParameter
'Parametri di aggiunta membri alle liste:
'Field: aggiunge solo i nomi dei campi
'ValueNameOnly: aggiunge solo i nomi dei valori
'ValueValOnly: aggiunge solo i valori dei valori
'ValueNameAndVal: aggiunge i valori, specificando prima il nome e poi il valore
'ALL: (solo per TreeView) aggiunge i campi con i sotto-valori; può essere sommato a uno dei precedenti
' (tranne Field) per ottenere un diverso effetto di visualizzazione dei valori
Field
ValueNameOnly
ValueValOnly
ValueNameAndVal
ALL
End Enum
Public Class DataBase
Public Structure Value
Dim Name As String
Dim Val As Object
End Structure
Public Structure Node
Dim Text As String
Dim Items As ArrayList
End Structure
Public Main As Node
Private Re As IO.StreamReader
Private Function BreakLine(ByVal S As String) As Value
Dim Nome As String = ""
Dim ValueV As String = ""
Dim T As Boolean = False
Dim V As Value
For i As Int32 = 0 To S.Length - 1
If S(i) = "=" Then
T = True
Else
If Not T Then
Nome = Nome & S(i)
Else
ValueV = ValueV & S(i)
End If
End If
Next
V.Name = Nome
V.Val = ValueV
Return V
End Function
Public Sub New(ByVal s As String)
Main.Items = New ArrayList
Try
Dim R
As New IO.
StreamReader(File)
Dim F As String = ""
Dim a As Int64
Dim T As Node = Nothing
Dim Line As Value
Main.Items = New ArrayList
a = 0
While Not R.EndOfStream
F = R.ReadLine
If F <> Nothing Then
If F.Chars(0) = "<" Then
F = F.Remove(0, 1)
F = F.Remove(F.Length - 1, 1)
T.Text = F
T.Items = New ArrayList
Main.Items.Add(T)
a += 1
Else
If F.Chars(0) <> ":" Then
Line = BreakLine(F)
Main.Items.Item(a - 1).Items.Add(Line)
End If
End If
End If
End While
R.Close()
R = Nothing
Catch ex As IO.FileNotFoundException
End Try
End Sub
Public Function GetValue(ByVal Field As String, ByVal ValueN As String) As Object
Dim T As Node
Dim B As Value
Dim i As Int64 = 0
For Each T In Main.Items
If T.Text = Field Then
For Each B In Main.Items.Item(i).items
If B.Name = ValueN Then
Return B.Val
End If
Next
End If
i += 1
Next
Return Nothing
End Function
Public Sub AddValue(ByVal Field As String, ByVal Name As String, ByVal Val As Object)
Dim T As Node
Dim V As Value
Dim i As Int64
Dim Found As Boolean = False
V.Name = Name
V.Val = Val
For Each T In Main.Items
If T.Text = Field Then
Found = True
Exit For
End If
i += 1
Next
If Found = True Then
Main.Items.Item(i).Items.Add(V)
End If
End Sub
Public Sub SetValue(ByVal Field As String, ByVal ValueN As String, ByVal Val As Object)
Dim T As Node
Dim B As Value
Dim i As Int64 = 0
Dim a As Int64 = 0
Dim V As Value
V.Val = Val
V.Name = ValueN
For Each T In Main.Items
If T.Text = Field Then
For Each B In Main.Items.Item(i).items
If B.Name = ValueN Then
Main.Items.Item(i).Items.Item(a) = V
Exit Sub
End If
a += 1
Next
End If
i += 1
Next
End Sub
Public Sub EraseValue(ByVal Field As String, ByVal ValueN As String)
Dim T As Node
Dim B As Value
Dim i As Int64 = 0
Dim a As Int64 = 0
For Each T In Main.Items
If T.Text = Field Then
For Each B In Main.Items(i).Items
If B.Name = ValueN Then
Main.Items.Item(i).Items.remove(B)
Exit Sub
End If
a += 1
Next
End If
i += 1
Next
End Sub
Public Sub AddField(ByVal Name As String)
Dim T As Node
T.Text = Name
T.Items = New ArrayList
Main.Items.Add(T)
End Sub
Public Sub EraseField(ByVal Name As String, Optional ByVal First As Boolean = True)
Dim T As Node
Dim i As Int64 = 0
For Each T In Main.Items
If T.Text = Name Then
Main.Items.Remove(T)
If First Then
Exit Sub
End If
End If
i += 1
Next
End Sub
Public Function GetValues(ByVal Field As String) As ArrayList
Dim T As Node
Dim i As Int64
For Each T In Main.Items
If T.Text = Field Then
Return Main.Items.Item(i).Items
End If
i += 1
Next
Return Nothing
End Function
Public Function GetFields() As ArrayList
Dim S As New ArrayList
Dim N As Node
For Each N In Main.Items
S.Add(N)
Next
Return S
End Function
Public Function GetValuesNames(ByVal FieldName As String) As ArrayList
Dim T As Node
Dim V As Value
Dim S As New ArrayList
Dim i As Int64
For Each T In Main.Items
If T.Text = FieldName Then
For Each V In T.Items
S.Add(V.Name)
Next
Exit For
End If
i += 1
Next
Return S
End Function
Public Function GetFieldsNames() As ArrayList
Dim S As New ArrayList
Dim N As Node
For Each N In Main.Items
S.Add(N.Text)
Next
Return S
End Function
Public Sub Save()
Dim B As Value
Dim R
As New IO.
StreamWriter(File)
For i As Int64 = 0 To Main.Items.Count - 1
R.WriteLine("<" & Main.Items.Item(i).text & ">")
For Each B In Main.Items(i).Items
R.WriteLine(B.Name & "=" & B.Val)
Next
Next
R.Close()
R = Nothing
End Sub
Public Sub SaveAs(ByVal FileN As String)
Dim B As Value
Dim R As New IO.StreamWriter(FileN)
For i As Int64 = 0 To Main.Items.Count - 1
R.WriteLine("<" & Main.Items.Item(i).text & ">")
For Each B In Main.Items(i).Items
R.WriteLine(B.Name & "=" & B.Val)
Next
Next
R.Close()
R = Nothing
End Sub
'In tutte le funzioni che seguono il parametro FieldName è sempre obbligatorio, ma non sempre necessario
'Infatti è obbligatorio solo se si sceglie uno stile di visualizzazione fra questi: ValueNameOnly, ValueValOnly,
'ValueNameAndVal, altrimenti può essere lasciato vuoto
Public Function TransferInListBox(ByVal FieldName As String, ByRef List As Windows.Forms.ListBox, Optional ByVal What As DBParameter = DBParameter.ValueNameOnly) As ArrayList
If What = DBParameter.Field Then
Dim AllFields As ArrayList = GetFieldsNames()
Dim S As String
For Each S In AllFields
List.Items.Add(S)
Next
Return AllFields
Else
Dim AllItems As ArrayList = GetValues(FieldName)
Dim V As Value
For Each V In AllItems
If What = DBParameter.ValueNameOnly Then
List.Items.Add(V.Name)
End If
If What = DBParameter.ValueValOnly Then
List.Items.Add(V.Val)
End If
If What = DBParameter.ValueNameAndVal Then
List.Items.Add(V.Name + "=" + V.Val)
End If
Next
Return AllItems
End If
End Function
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
Dim G As New Windows.Forms.ListViewGroup(FieldName)
Dim L As Windows.Forms.ListViewItem
If CreateGroup Then
List.Groups.Add(G)
End If
List.View = View.Details
If Mode = DBParameter.Field Then
Dim AllFields As ArrayList = GetFieldsNames()
Dim S As String
For Each S In AllFields
L = New ListViewItem(S)
List.Items.Add(L)
Next
Return AllFields
Else
Dim AllItems As ArrayList = GetValues(FieldName)
Dim S(1) As String
Dim V As Value
For Each V In AllItems
If Mode = DBParameter.ValueNameOnly Or Mode = DBParameter.ValueNameAndVal Then
S(0) = V.Name
End If
If Mode = DBParameter.ValueValOnly Or Mode = DBParameter.ValueNameAndVal Then
S(1) = V.Val
End If
If CreateGroup Then
L = New ListViewItem(S, G)
Else
L = New ListViewItem(S)
End If
List.Items.Add(L)
Next
Return AllItems
End If
End Function
Public Function TransferInTreeView(ByVal FieldName As String, ByRef List As TreeView, Optional ByVal Mode As DBParameter = DBParameter.Field) As ArrayList
If Mode >= DBParameter.ALL Then
Dim AllFields As ArrayList = GetFieldsNames()
Dim S As String
Dim N As TreeNode = Nothing
Dim V As Value
For Each S In AllFields
N.Text = S
For Each V In GetValues(S)
If Mode - DBParameter.ALL = DBParameter.ValueNameOnly Then
N.Nodes.Add(V.Name)
End If
If Mode - DBParameter.ALL = DBParameter.ValueValOnly Then
N.Nodes.Add(V.Val)
End If
If Mode - DBParameter.ALL = DBParameter.ValueNameAndVal Then
N.Nodes.Add(V.Name + "=" + V.Val)
End If
Next
List.Nodes.Add(N)
Next
Return AllFields
Else
Dim AllItems As ArrayList = GetValues(FieldName)
Dim N As TreeNode = Nothing
Dim V As Value
For Each V In AllItems
If Mode = DBParameter.ValueNameOnly Then
N.Text = V.Name
End If
If Mode = DBParameter.ValueValOnly Then
N.Text = V.Val
End If
If Mode = DBParameter.ValueNameAndVal Then
N.Text = V.Name + "=" + V.Val
End If
List.Nodes.Add(N)
Next
Return AllItems
End If
End Function
Public Function TransferInComboBox(ByVal FieldName As String, ByRef List As Windows.Forms.ComboBox, Optional ByVal What As DBParameter = DBParameter.ValueNameOnly) As ArrayList
If What = DBParameter.Field Then
Dim AllFields As ArrayList = GetFieldsNames()
Dim S As String
For Each S In AllFields
List.Items.Add(S)
Next
Return AllFields
Else
Dim AllItems As ArrayList = GetValues(FieldName)
Dim V As Value
For Each V In AllItems
If What = DBParameter.ValueNameOnly Then
List.Items.Add(V.Name)
End If
If What = DBParameter.ValueValOnly Then
List.Items.Add(V.Val)
End If
If What = DBParameter.ValueNameAndVal Then
List.Items.Add(V.Name + "=" + V.Val)
End If
Next
Return AllItems
End If
End Function
Public Function TransferInDomain(ByVal FieldName As String, ByRef List As Windows.Forms.DomainUpDown, Optional ByVal What As DBParameter = DBParameter.ValueNameOnly) As ArrayList
If What = DBParameter.Field Then
Dim AllFields As ArrayList = GetFieldsNames()
Dim S As String
For Each S In AllFields
List.Items.Add(S)
Next
Return AllFields
Else
Dim AllItems As ArrayList = GetValues(FieldName)
Dim V As Value
For Each V In AllItems
If What = DBParameter.ValueNameOnly Then
List.Items.Add(V.Name)
End If
If What = DBParameter.ValueValOnly Then
List.Items.Add(V.Val)
End If
If What = DBParameter.ValueNameAndVal Then
List.Items.Add(V.Name + "=" + V.Val)
End If
Next
Return AllItems
End If
End Function
End Class
End Namespace