Module CheckRoutines
    Function CheckFileName(ByRef strName As String) As Short

        On Error Resume Next
        Dim i As Short
        Dim strBad As String

        ' assume its valid
        CheckFileName = ER_NOERROR

        If strName.Length = 0 Then
            CheckFileName = ER_BADFILENAME
            Exit Function
        End If

        ' look for bad characters
        For i = 1 To LM_INVALIDCHARS.Length
            If strName.IndexOf(LM_INVALIDCHARS.Substring(i - 1, 1)) > -1 Then
                ' not valid
                CheckFileName = ER_BADFILENAME
                Exit For
            End If
        Next

    End Function
    Function CheckValidDat(ByRef strFile As String) As Short

        On Error Resume Next

        Dim strLineData As String
        Dim intNewPart As Short
        Dim intPartType As Short
        Dim intPartClass As Short
        Dim intPartDrawing As Short
        Dim intBasePart As Short
        Dim intHeading As Short
        Dim intData As Short
        Dim intEnd As Short

        CheckValidDat = ER_NOERROR

        intNewPart = False
        intPartType = False
        intPartClass = False
        intPartDrawing = False
        intBasePart = False
        intHeading = False
        intData = False
        intEnd = False

        FileOpen(1, strFile, OpenMode.Input)

        Do While Not EOF(1)

            strLineData = LineInput(1)
            If strLineData = C_NEW_PART Then
                intNewPart = True
            ElseIf strLineData.IndexOf(C_PART_TYPE) > -1 Then
                intPartType = True
            ElseIf strLineData.IndexOf(C_PART_CLASS) > -1 Then
                intPartClass = True
            ElseIf strLineData.IndexOf(C_PART_DRAWING) > -1 Then
                intPartDrawing = True
            ElseIf strLineData.IndexOf(C_PART_TEMPLATE) > -1 Then
                intBasePart = True
            ElseIf strLineData.IndexOf(C_HEADING) > -1 Then
                intHeading = True
            ElseIf strLineData.IndexOf(C_PART_DATA) > -1 Then
                intData = True
            ElseIf strLineData = C_END_PART Then
                intEnd = True
            End If

        Loop

        FileClose(1)

        If intNewPart = False Or intPartType = False Or intPartClass = False Or intPartDrawing = False Or intBasePart = False Or intHeading = False Or intData = False Or intEnd = False Then

            CheckValidDat = ER_INVALIDDAT
        End If

        If Err.Number Then
            CheckValidDat = ER_VALIDFAIL
        End If


    End Function
    Function CheckDatFile() As Short

        On Error Resume Next

        Dim chrtest As String
        CheckDatFile = ER_NOERROR

        strDataFile = strAppPath
        If strDataFile.Substring(strDataFile.Length - 1, 1) <> "\" Then
            strDataFile = strDataFile & "\"
        End If

        strAppPath = strDataFile

        ' set the file to a path and a global filename

        strDataFile = strDataFile & LF_DATAFILE

        ' the dat file is in a global path so get it
        If strGlobalPath.Substring(strGlobalPath.Length - 1, 1) <> "\" Then
            strDataFile = strGlobalPath & "\" & LF_DATAFILE
        Else
            strDataFile = strGlobalPath & LF_DATAFILE
        End If
        If Dir(strDataFile, FileAttribute.Normal).Length = 0 Then
            CheckDatFile = ER_FILENOTFOUND
        End If

    End Function
    Function CheckForFile(ByRef strFileName As String) As Short

        On Error Resume Next
        Dim strFile As String = ""
        Dim i As Short

        CheckForFile = ER_NOERROR

        For i = strFileName.Length - 1 To 0 Step -1
            If strFileName.Substring(i, 1) = "\" Then
                Exit For
            Else
                strFile = strFile & strFileName.Substring(i, 1)
            End If
        Next

        If CheckFileName(strFile) = ER_BADFILENAME Then
            CheckForFile = ER_FILENOTFOUND
            Exit Function
        End If

        If Dir(strFileName, FileAttribute.Normal).Length = 0 Then
            CheckForFile = ER_FILENOTFOUND
        Else
            CheckForFile = ER_FILEFOUND
        End If

    End Function
End Module
