一个ini类代替缓存使用
2005/6/23 12:30:07
阅读全文(1668) | 回复(0) | 编辑 | 精华
把下面的代码保存为INI.asp即可运行: 1<% 2'Power By Tim 3'文件摘要:INI类 4'文件版本:3.0 5'文本创建日期:2:17 2004-12-14 6'================= 属性说明 ================ 7'INI.OpenFile = 文件路径(使用虚拟路径需在外部定义) 8'INI.CodeSet = 编码设置,默认为 GB2312 9'INI.IsTrue = 检测文件是否正常(存在) 10'================ 方法说明 ================= 11'IsGroup(组名) 检测组是否存在 12'IsNode(组名,节点名) 检测节点是否存在 13'GetGroup(组名) 读取组信息 14'CountGroup() 统计组数量 15'ReadNode(组名,节点名) 读取节点数据 16'WriteGroup(组名) 创建组 17'WriteNode(组,节点,节点数据) 插入/更新节点数据 18'DeleteGroup(组名) 删除组 19'DeleteNode(组名,节点名) 删除节点 20'Save() 保存文件 21'Close() 清除内部数据(释放) 22'=============================================== 23 24 25 26Class INI_Class 27'=============================================== 28 Private Stream '// Stream 对象 29 Private FilePath '// 文件路径 30 Public Content '// 文件数据 31 Public IsTrue '// 文件是否存在 32 Public IsAnsi '// 记录是否二进制 33 Public CodeSet '// 数据编码 34'================================================ 35 36 '// 初始化 37 Private Sub Class_Initialize() 38 Set Stream = Server.CreateObject("ADODB.Stream") 39 Stream.Mode = 3 40 Stream.Type = 2 41 CodeSet = "gb2312" 42 IsAnsi = True 43 IsTrue = True 44 End Sub 45 46 47 '// 二进制流转换为字符串 48 Private Function Bytes2bStr(bStr) 49 if Lenb(bStr)=0 Then 50 Bytes2bStr = "" 51 Exit Function 52 End if 53 54 Dim BytesStream,StringReturn 55 Set BytesStream = Server.CreateObject("ADODB.Stream") 56 With BytesStream 57 .Type = 2 58 .Open 59 .WriteText bStr 60 .Position = 0 61 .Charset = CodeSet 62 .Position = 2 63 StringReturn = .ReadText 64 .Close 65 End With 66 Bytes2bStr = StringReturn 67 Set BytesStream = Nothing 68 Set StringReturn = Nothing 69 End Function 70 71 72 '// 设置文件路径 73 Property Let OpenFile(INIFilePath) 74 FilePath = INIFilePath 75 Stream.Open 76 On Error Resume Next 77 Stream.LoadFromFile(FilePath) 78 '// 文件不存在时返回给 IsTrue 79 if Err.Number<>0 Then 80 IsTrue = False 81 Err.Clear 82 End if 83 Content = Stream.ReadText(Stream.Size) 84 if Not IsAnsi Then Content=Bytes2bStr(Content) 85 End Property 86 87 88 '// 检测组是否存在[参数:组名] 89 Public Function IsGroup(GroupName) 90 if Instr(Content,"["&GroupName&"]")>0 Then 91 IsGroup = True 92 Else 93 IsGroup = False 94 End if 95 End Function 96 97 98 '// 读取组信息[参数:组名] 99 Public Function GetGroup(GroupName)100 Dim TempGroup101 if Not IsGroup(GroupName) Then Exit Function102 '// 开始寻找头部截取103 TempGroup = Mid(Content,Instr(Content,"["&GroupName&"]"),Len(Content))104 '// 剔除尾部105 if Instr(TempGroup,VbCrlf&"[")>0 Then TempGroup=Left(TempGroup,Instr(TempGroup,VbCrlf&"[")-1)106 if Right(TempGroup,1)<>Chr(10) Then TempGroup=TempGroup&VbCrlf107 GetGroup = TempGroup108 End Function109 110 111 '// 检测节点是否存在[参数:组名,节点名]112 Public Function IsNode(GroupName,NodeName)113 if Instr(GetGroup(GroupName),NodeName&"=") Then114 IsNode = True115 Else116 IsNode = False117 End if118 End Function119 120 121 '// 创建组[参数:组名]122 Public Sub WriteGroup(GroupName)123 if Not IsGroup(GroupName) And GroupName<>"" Then124 Content = Content & "[" & GroupName & "]" & VbCrlf125 End if126 End Sub127 128 129 '// 读取节点数据[参数:组名,节点名]130 Public Function ReadNode(GroupName,NodeName)131 if Not IsNode(GroupName,NodeName) Then Exit Function132 Dim TempContent133 '// 取组信息134 TempContent = GetGroup(GroupName)135 '// 取当前节点数据136 TempContent = Right(TempContent,Len(TempContent)-Instr(TempContent,NodeName&"=")+1)137 TempContent = Replace(Left(TempContent,Instr(TempContent,VbCrlf)-1),NodeName&"=","")138 ReadNode = ReplaceData(TempContent,0)139 End Function140 141 142 '// 写入节点数据[参数:组名,节点名,节点数据]143 Public Sub WriteNode(GroupName,NodeName,NodeData)144 '// 组不存在时写入组145 if Not IsGroup(GroupName) Then WriteGroup(GroupName)146 147 '// 寻找位置插入数据148 '/// 获取组149 Dim TempGroup : TempGroup = GetGroup(GroupName)150 151 '/// 在组尾部追加152 Dim NewGroup153 if IsNode(GroupName,NodeName) Then154 NewGroup = Replace(TempGroup,NodeName&"="&ReplaceData(ReadNode(GroupName,NodeName),1),NodeName&"="&ReplaceData(NodeData,1))155 Else156 NewGroup = TempGroup & NodeName & "=" & ReplaceData(NodeData,1) & VbCrlf157 End if158 159 Content = Replace(Content,TempGroup,NewGroup)160 End Sub161 162 163 '// 删除组[参数:组名]164 Public Sub DeleteGroup(GroupName)165 Content = Replace(Content,GetGroup(GroupName),"")166 End Sub167 168 169 '// 删除节点[参数:组名,节点名]170 Public Sub DeleteNode(GroupName,NodeName)171 Dim TempGroup172 Dim NewGroup173 TempGroup = GetGroup(GroupName)174 NewGroup = Replace(TempGroup,NodeName&"="&ReadNode(GroupName,NodeName)&VbCrlf,"")175 if Right(NewGroup,1)<>Chr(10) Then NewGroup = NewGroup&VbCrlf176 Content = Replace(Content,TempGroup,NewGroup)177 End Sub178 179 180 '// 替换字符[实参:替换目标,数据流向方向]181 ' 字符转换[防止关键符号出错]182 ' [ ---> {(@)}183 ' ] ---> {(#)}184 ' = ---> {($)}185 ' 回车 ---> {(1310)}186 Public Function ReplaceData(Data_Str,IsIn)187 if IsIn Then188 ReplaceData = Replace(Replace(Replace(Data_Str,"[","{(@)}"),"]","{(#)}"),"=","{($)}")189 ReplaceData = Replace(ReplaceData,Chr(13)&Chr(10),"{(1310)}")190 Else191 ReplaceData = Replace(Replace(Replace(Data_Str,"{(@)}","["),"{(#)}","]"),"{($)}","=")192 ReplaceData = Replace(ReplaceData,"{(1310)}",Chr(13)&Chr(10))193 End if194 End Function195 196 197 '// 保存文件数据198 Public Sub Save()199 With Stream200 .Close201 .Open202 .WriteText Content203 .SaveToFile FilePath,2204 End With205 End Sub206 207 208 '// 关闭、释放209 Public Sub Close()210 Set Stream = Nothing211 Set Content = Nothing212 End Sub213 214End Class215216217Dim INI218Set INI = New INI_Class219INI.OpenFile = Server.MapPath("Config.ini")220'========== 这是写入ini数据 ==========221Call INI.WriteNode("SiteConfig","SiteName","Leadbbs极速论坛")222Call INI.WriteNode("SiteConfig","Mail","leadbbs@leadbbs.com")223INI.Save()224'========== 这是读取ini数据 ==========225Response.Write("站点名称:"&INI.ReadNode("SiteConfig","SiteName"))226%> [中国WEB开发者网络]
Posted by Qr on 2005/6/23 12:30:07
发表评论: |