The INI class used in lieu of caching

Source: Internet
Author: User
Tags exit chr ini save file

The idea of this class is original from the DLL, I have now written to the third version of the

<%
''================================================================
' = Power by Tim =
''=--------------------------------------------------------------=
' = File Summary: INI class =
''=--------------------------------------------------------------=
' = File version: 3.0 =
' = text creation Date: 2:17 2004-12-14 =
''================================================================
' ============================= Property Description =========================
' = INI. OpenFile = File path (need to be defined externally using virtual path) =
' = INI. CodeSet = encoding setting, defaults to GB2312 =
' = INI. IsTrue = Detect whether the file is normal (exists) =
"============================= Method Description =========================
' = Isgroup (group name) detection Group exists =
' = Isnode (group name, node naming) detects whether the nodes exist =
' = Getgroup (group name) reads Group information =
"= Countgroup () number of statistics group =
' = ReadNode (group name, node naming) Read nodes data =
' = Writegroup (group name) to create group =
' = WriteNode (Group, node, node data) Insert/update node data =
' = DeleteGroup (group name) Delete Group =
' = Deletenode (group name, node naming) Delete nodes =
' = Save () file =
' = Close () Clears internal data (release) =
''================================================================

Class Ini_class
''==================================================
Private Stream '//Stream object
Private FilePath '//file path

Public Content '//File data
Public IsTrue '//file exists
Public Isansi '//records are binary
Public CodeSet '//Data encoding
''==================================================

'//initialization
Private Sub Class_Initialize ()
Set Stream = Server.CreateObject ("ADODB.") Stream ")
Stream.mode = 3
Stream.type = 2
CodeSet = "gb2312"
Isansi = True
IsTrue = True
End Sub

'//binary stream converted to string
Private Function bytes2bstr (BSTR)
If Lenb (bStr) =0 Then
Bytes2bstr = ""
Exit Function
End If

Dim Bytesstream,stringreturn
Set Bytesstream = Server.CreateObject ("ADODB. Stream ")
With Bytesstream
. Type = 2
. Open
. WRITETEXT BSTR
. Position = 0
. Charset = CodeSet
. Position = 2
Stringreturn =. ReadText
. Close
End With
Bytes2bstr = Stringreturn
Set Bytesstream = Nothing
Set Stringreturn = Nothing
End Function

'//Set file path
Property Let OpenFile (IniFilePath)
FilePath = IniFilePath
Stream.open
On Error Resume Next
Stream.loadfromfile (FilePath)
'//File not present, return to IsTrue
If Err.number<>0 Then
IsTrue = False
Err.Clear
End If
Content = Stream.readtext (stream.size)
If not Isansi Then content=bytes2bstr (Content)
End Property

'//detection group presence [parameters: Group name]
Public Function Isgroup (groupname)
If Instr (Content, "[" &GroupName& "]") >0 Then
Isgroup = True
Else
Isgroup = False
End If
End Function

'//Read group information [parameters: Group name]
Public Function Getgroup (groupname)
Dim Tempgroup
If not Isgroup (groupname) Then Exit Function
"//start looking for head intercept
Tempgroup = Mid (content,instr (content, [&GroupName&]), Len (content))
'//Remove tail
If Instr (tempgroup,vbcrlf& "[") >0 Then tempgroup=left (Tempgroup,instr (tempgroup,vbcrlf& "[")-1)
If Right (tempgroup,1) &LT;&GT;CHR Then Tempgroup=tempgroup&vbcrlf
Getgroup = Tempgroup
End Function

'//Detect if node exists [parameters: Group name, section names]
Public Function Isnode (groupname,nodename)
If Instr (Getgroup (groupname),nodename& "=") Then
Isnode = True
Else
Isnode = False
End If
End Function

'//create Group [parameters: Group name]
Public Sub Writegroup (groupname)
If not Isgroup (groupname) and groupname<> "" Then
Content = content & "[" & GroupName & "]" & VBCRLF
End If
End Sub

'//Read node data [parameters: Group name, section Roll Call]
Public Function ReadNode (groupname,nodename)
If not Isnode (groupname,nodename) Then Exit Function
Dim tempcontent
'//FETCH group information
Tempcontent = Getgroup (groupname)
'//Fetch current node data
Tempcontent = Right (Tempcontent,len (tempcontent)-instr (tempcontent,nodename& "=") +1)
Tempcontent = Replace (Left (TEMPCONTENT,INSTR (TEMPCONTENT,VBCRLF)-1),nodename& "=", "")
ReadNode = ReplaceData (tempcontent,0)
End Function

'//write node data [parameters: Group name, node name, node data]
Public Sub WriteNode (groupname,nodename,nodedata)
Write to group if no group exists
If not Isgroup (groupname) Then Writegroup (groupname)

'/Find position insert data
'///get Group
Dim Tempgroup:tempgroup = Getgroup (groupname)

"///in Group tail append
Dim NewGroup
If Isnode (groupname,nodename) Then
NewGroup = Replace (tempgroup,nodename& "=" &replacedata (ReadNode (Groupname,nodename), 1),nodename& "=" &replacedata (nodedata,1))
Else
NewGroup = tempgroup & nodename & "=" & ReplaceData (nodedata,1) & VbCrlf
End If

Content = Replace (Content,tempgroup,newgroup)
End Sub

'//delete Group [parameters: Group name]
Public Sub DeleteGroup (groupname)
Content = Replace (Content,getgroup (groupname), "")
End Sub

'//delete node [parameters: Group name, section Roll Call]
Public Sub Deletenode (groupname,nodename)
Dim Tempgroup
Dim NewGroup
Tempgroup = Getgroup (groupname)
NewGroup = Replace (tempgroup,nodename& "=" &readnode (groupname,nodename) &vbcrlf, "")
If Right (newgroup,1) <>chr Then newgroup = Newgroup&vbcrlf
Content = Replace (Content,tempgroup,newgroup)
End Sub

'//substitution character [argument: replace target, data flow direction]
' character conversion [prevent key symbol errors]
' [---> {(@)}
']---> {(#)}
' =---> {($)}
' Carriage return---> {(1310)}
Public Function ReplaceData (data_str,isin)
If IsIn Then
ReplaceData = replace (replace (DATA_STR, "[", "{(@)}"), "]", "{(#)}"), "=", "{($)}")
ReplaceData = Replace (REPLACEDATA,CHR) &AMP;CHR (10), "{(1310)}")
Else
ReplaceData = replace (replace (Data_str, "{(@)}", "["), "{(#)}", "]"), "{($)}", "=")
ReplaceData = Replace (ReplaceData, "{(1310)}", Chr &AMP;CHR (10))
End If
End Function

'//Save file data
Public Sub Save ()
With Stream
. Close
. Open
. WRITETEXT Content
. SaveToFile filepath,2
End With
End Sub

'//Close, release
Public Sub Close ()
Set Stream = Nothing
Set Content = Nothing
End Sub

End Class

Dim INI
Set INI = New Ini_class
Ini. OpenFile = Server.MapPath ("Config.ini")
'//========== This is to write INI data ==========
Ini. WriteNode ("SiteConfig", "SiteName", "Leadbbs Speed Forum")
Ini. WriteNode ("SiteConfig", "Mail", "leadbbs@leadbbs.com")
Ini. Save ()
'//========== This is read INI data ==========
Response.Write ("Site name:" INI.) ReadNode ("SiteConfig", "SiteName"))
%>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.