⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 inifileloaderbase.vb

📁 一个.Net下用VB编写的用于游戏的人工智能引擎
💻 VB
字号:
Imports System.IO

Public Class iniFileLoaderBase

    '//the file the parameters are stored in
    Dim file As StreamReader

    Dim CurrentLine As String

    '//-------------------------- GetParameterValueAsString ------------------------
    '//
    '// given a line of text this function removes the parameter description
    '// and returns just the parameter as a std::string
    '//-----------------------------------------------------------------------------
    Private Sub GetParameterValueAsString(ByRef line As String)
        '//find beginning of parameter description
        Dim begIdx, endIdx As Integer

        '//define some delimiters
        Dim delims As String = " \;=,"

        begIdx = line.LastIndexOfAny(delims)
        line = line.Substring(begIdx + 1)
        Return
        '//find the end of the parameter description
        If begIdx < line.Length - 1 Then
            endIdx = line.IndexOfAny(delims, begIdx)

            '//end of word is the end of the line
            If endIdx = line.Length - 1 Then
                endIdx = line.Length() - 1
            End If
        End If

        '//find the beginning of the parameter value
        begIdx = line.IndexOfAny(delims, endIdx)
        '//find the end of the parameter value
        If begIdx < line.Length - 1 Then
            endIdx = line.IndexOfAny(delims, begIdx)

            '//end of word is the end of the line
            If endIdx = line.Length - 1 Then
                endIdx = line.Length()
            End If
        End If

        line = line.Substring(begIdx, endIdx - begIdx)
    End Sub

    '//----------------------- GetNextParameter ------------------------------------
    '//
    '//  searches the text file for the next valid parameter. Discards any comments
    '//  and returns the value as a string
    '//-----------------------------------------------------------------------------
    Private Function GetNextParameter() As String

        '//this will be the string that holds the bext parameter
        Dim line As String

        line = file.ReadLine()

        RemoveCommentingFromLine(line)

        '//if the line is of zero length, get the next line from
        '//the file
        If line.Length() = 0 Then
            Return GetNextParameter()
        End If

        GetParameterValueAsString(line)

        Return line

    End Function

    '//this ignores any comments and finds the next delimited string 
    '//--------------------------- GetNextToken ------------------------------------
    '//
    '//  ignores any commenting and gets the next string
    '//-----------------------------------------------------------------------------
    Private Function GetNextToken() As String
        '//strip the line of any commenting
        Do While CurrentLine.Length() = 0
            CurrentLine = file.ReadLine
            RemoveCommentingFromLine(CurrentLine)
        Loop

        '//find beginning of parameter description
        Dim begIdx, endIdx As Integer

        '//define some delimiters
        Dim delims As String = " \;=,"

        begIdx = CurrentLine.IndexOfAny(delims)

        '//find the end of the parameter description
        If begIdx > 0 And begIdx < CurrentLine.Length - 1 Then
            endIdx = CurrentLine.IndexOfAny(delims, begIdx)

            '//end of word is the end of the line
            If endIdx = CurrentLine.Length - 1 Then
                endIdx = CurrentLine.Length()
            End If

        End If

        Dim s As String = CurrentLine.Substring(begIdx, endIdx - begIdx)

        If endIdx < CurrentLine.Length() - 1 Then
            '//strip the token from the line
            CurrentLine = CurrentLine.Substring(endIdx + 1)
        Else
            CurrentLine = ""
        End If
        Return s
    End Function

    '//this is set to true if the file specified by the user is valid
    Dim m_bGoodFile As Boolean

    '//removes any commenting from a line of text
    Public Sub RemoveCommentingFromLine(ByRef line As String)
        '//search for any comment and remove
        Dim idx As Integer = line.IndexOf("//")

        If idx < line.Length - 1 And idx >= 0 Then
            '//cut out the comment
            line = line.Substring(0, idx)
        End If
    End Sub

    '//helper methods. They convert the next parameter value found into the 
    '//relevant type
    Public Function GetNextParameterDouble() As Double
        If m_bGoodFile Then Return CDbl(GetNextParameter())
    End Function

    Public Function GetNextParameterFloat() As Single
        If (m_bGoodFile) Then Return CSng(GetNextParameter())
    End Function

    Public Function GetNextParameterInt() As Integer
        If (m_bGoodFile) Then Return CInt(GetNextParameter())
    End Function
    Public Function GetNextParameterBool() As Boolean
        Return CBool(GetNextParameter())
    End Function

    Public Function GetNextTokenAsDouble() As Double
        If (m_bGoodFile) Then Return CDbl(GetNextToken())
    End Function

    Public Function GetNextTokenAsFloat() As Single
        If (m_bGoodFile) Then Return CSng(GetNextToken())
    End Function

    Public Function GetNextTokenAsInt() As Integer
        If (m_bGoodFile) Then Return CInt(GetNextToken())
    End Function

    Public Function GetNextTokenAsString() As String
        If (m_bGoodFile) Then Return GetNextToken()
    End Function

    Public Sub New(ByVal filename As String)
        Try
            file = New StreamReader(filename)
            m_bGoodFile = True
        Catch ex As Exception
            m_bGoodFile = False
        End Try
    End Sub

    Public Function eof() As Boolean
        'if (m_bGoodFile) then return file.re
    End Function

    Public Function FileIsGood() As Boolean
        Return m_bGoodFile
    End Function


End Class

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -