Attribute VB_Name = "User"
Option Explicit

Type UserDB_
    Enable As Boolean
    CardID  As String
    Password As String
    Level   As Integer
    Name As String
    
    bAppLimit As Integer
    AppFile As String
End Type

Global UserDB(0 To 4096) As UserDB_

Global bAppLimit As Boolean



Function CheckCardID(id As String) As Integer
    Dim i As Integer
    
    For i = 0 To UBound(UserDB)
        If UserDB(i).Enable And UserDB(i).CardID = id Then
            CheckCardID = i
            Exit Function
        End If
    Next i
    CheckCardID = -1
End Function

Function CheckPassword(psw As String) As Integer
    Dim i As Integer
    
    For i = 0 To UBound(UserDB)
        If UserDB(i).Enable And UserDB(i).Password = psw Then
            CheckPassword = i
            Exit Function
        End If
    Next i
    CheckPassword = -1
End Function


Function DeSpace(txt As String) As String
    Dim f As Integer
    f = InStr(txt, " ")
    If f > 1 Then
        DeSpace = Mid$(txt, 1, f - 1)
    ElseIf f = 1 Then
        DeSpace = ""
    Else
        DeSpace = txt
    End If
End Function

Function GetFreeUserIndex() As Integer
    Dim i As Integer
    
    For i = 0 To UBound(UserDB)
        If UserDB(i).Enable = False Then
            GetFreeUserIndex = i
            Exit Function
        End If
    Next i
    GetFreeUserIndex = -1
End Function

Function Administrator() As Integer
    Dim i As Integer
    
    For i = 0 To UBound(UserDB)
        If UserDB(i).Enable And UserDB(i).Level = 0 Then
            Administrator = i
            Exit Function
        End If
    Next i
    Administrator = -1
End Function


Sub LoadUserDB()
    Dim Path As String, i As Integer
    
    Path = GetWinDirectory() + "\WSUser.DAT"
    
    If Dir(Path) <> "" Then
        On Error Resume Next
        Open Path For Input Access Read As #1
    
        Do While Not EOF(1) And i <= UBound(UserDB)
            Input #1, UserDB(i).CardID, UserDB(i).Password, UserDB(i).Level, UserDB(i).Name, UserDB(i).bAppLimit, UserDB(i).AppFile
            UserDB(i).Enable = True
            i = i + 1
        Loop
    
        Close #1
    End If
    
End Sub

Function SaveUserDB() As Boolean
    Dim Path As String, i As Integer
    
    Path = GetWinDirectory() + "\WSUser.DAT"
    
    On Error GoTo SaveUserError
    Open Path For Output Access Write As #1
    
    For i = 0 To UBound(UserDB)
        If UserDB(i).Enable Then
            Write #1, UserDB(i).CardID, UserDB(i).Password, UserDB(i).Level, UserDB(i).Name, UserDB(i).bAppLimit, UserDB(i).AppFile
        End If
    Next i
   
    Close #1
    SaveUserDB = True
    Exit Function
    
SaveUserError:
    SaveUserDB = False
End Function


