一个ini类代替缓存使用

  1   
  2'================================================================   
  3'= Power By Tim =   
  4'=--------------------------------------------------------------=   
  5'= 文件摘要:INI类 =   
  6'=--------------------------------------------------------------=   
  7'= 文件版本:3.0 =   
  8'= 文本创建日期:2:17 2004-12-14 =   
  9'================================================================   
 10'============================= 属性说明 =========================   
 11'= INI.OpenFile = 文件路径(使用虚拟路径需在外部定义) =   
 12'= INI.CodeSet = 编码设置,默认为 GB2312 =   
 13'= INI.IsTrue = 检测文件是否正常(存在) =   
 14'============================= 方法说明 =========================   
 15'= IsGroup(组名) 检测组是否存在 =   
 16'= IsNode(组名,节点名) 检测节点是否存在 =   
 17'= GetGroup(组名) 读取组信息 =   
 18'= CountGroup() 统计组数量 =   
 19'= ReadNode(组名,节点名) 读取节点数据 =   
 20'= WriteGroup(组名) 创建组 =   
 21'= WriteNode(组,节点,节点数据) 插入/更新节点数据 =   
 22'= DeleteGroup(组名) 删除组 =   
 23'= DeleteNode(组名,节点名) 删除节点 =   
 24'= Save() 保存文件 =   
 25'= Close() 清除内部数据(释放) =   
 26'================================================================   
 27  
 28  
 29  
 30Class INI_Class   
 31'==================================================   
 32Private Stream '// Stream 对象   
 33Private FilePath '// 文件路径   
 34  
 35Public Content '// 文件数据   
 36Public IsTrue '// 文件是否存在   
 37Public IsAnsi '// 记录是否二进制   
 38Public CodeSet '// 数据编码   
 39'==================================================   
 40  
 41'// 初始化   
 42Private Sub Class_Initialize()   
 43Set Stream = Server.CreateObject("ADODB.Stream")   
 44Stream.Mode = 3   
 45Stream.Type = 2   
 46CodeSet = "gb2312"   
 47IsAnsi = True   
 48IsTrue = True   
 49End Sub   
 50  
 51  
 52'// 二进制流转换为字符串   
 53Private Function Bytes2bStr(bStr)   
 54if Lenb(bStr)=0 Then   
 55Bytes2bStr = ""   
 56Exit Function   
 57End if   
 58  
 59Dim BytesStream,StringReturn   
 60Set BytesStream = Server.CreateObject("ADODB.Stream")   
 61With BytesStream   
 62.Type = 2   
 63.Open   
 64.WriteText bStr   
 65.Position = 0   
 66.Charset = CodeSet   
 67.Position = 2   
 68StringReturn = .ReadText   
 69.Close   
 70End With   
 71Bytes2bStr = StringReturn   
 72Set BytesStream = Nothing   
 73Set StringReturn = Nothing   
 74End Function   
 75  
 76  
 77'// 设置文件路径   
 78Property Let OpenFile(INIFilePath)   
 79FilePath = INIFilePath   
 80Stream.Open   
 81On Error Resume Next   
 82Stream.LoadFromFile(FilePath)   
 83'// 文件不存在时返回给 IsTrue   
 84if Err.Number<>0 Then   
 85IsTrue = False   
 86Err.Clear   
 87End if   
 88Content = Stream.ReadText(Stream.Size)   
 89if Not IsAnsi Then Content=Bytes2bStr(Content)   
 90End Property   
 91  
 92  
 93'// 检测组是否存在[参数:组名]   
 94Public Function IsGroup(GroupName)   
 95if Instr(Content,"["&GroupName&"]")>0 Then   
 96IsGroup = True   
 97Else   
 98IsGroup = False   
 99End if   
100End Function   
101  
102  
103'// 读取组信息[参数:组名]   
104Public Function GetGroup(GroupName)   
105Dim TempGroup   
106if Not IsGroup(GroupName) Then Exit Function   
107'// 开始寻找头部截取   
108TempGroup = Mid(Content,Instr(Content,"["&GroupName&"]"),Len(Content))   
109'// 剔除尾部   
110if Instr(TempGroup,VbCrlf&"[")>0 Then TempGroup=Left(TempGroup,Instr(TempGroup,VbCrlf&"[")-1)   
111if Right(TempGroup,1)<>Chr(10) Then TempGroup=TempGroup&VbCrlf   
112GetGroup = TempGroup   
113End Function   
114  
115  
116'// 检测节点是否存在[参数:组名,节点名]   
117Public Function IsNode(GroupName,NodeName)   
118if Instr(GetGroup(GroupName),NodeName&"=") Then   
119IsNode = True   
120Else   
121IsNode = False   
122End if   
123End Function   
124  
125  
126'// 创建组[参数:组名]   
127Public Sub WriteGroup(GroupName)   
128if Not IsGroup(GroupName) And GroupName<>"" Then   
129Content = Content & "[" & GroupName & "]" & VbCrlf   
130End if   
131End Sub   
132  
133  
134'// 读取节点数据[参数:组名,节点名]   
135Public Function ReadNode(GroupName,NodeName)   
136if Not IsNode(GroupName,NodeName) Then Exit Function   
137Dim TempContent   
138'// 取组信息   
139TempContent = GetGroup(GroupName)   
140'// 取当前节点数据   
141TempContent = Right(TempContent,Len(TempContent)-Instr(TempContent,NodeName&"=")+1)   
142TempContent = Replace(Left(TempContent,Instr(TempContent,VbCrlf)-1),NodeName&"=","")   
143ReadNode = ReplaceData(TempContent,0)   
144End Function   
145  
146  
147'// 写入节点数据[参数:组名,节点名,节点数据]   
148Public Sub WriteNode(GroupName,NodeName,NodeData)   
149'// 组不存在时写入组   
150if Not IsGroup(GroupName) Then WriteGroup(GroupName)   
151  
152'// 寻找位置插入数据   
153'/// 获取组   
154Dim TempGroup : TempGroup = GetGroup(GroupName)   
155  
156'/// 在组尾部追加   
157Dim NewGroup   
158if IsNode(GroupName,NodeName) Then   
159NewGroup = Replace(TempGroup,NodeName&"="&ReplaceData(ReadNode(GroupName,NodeName),1),NodeName&"="&ReplaceData(NodeData,1))   
160Else   
161NewGroup = TempGroup & NodeName & "=" & ReplaceData(NodeData,1) & VbCrlf   
162End if   
163  
164Content = Replace(Content,TempGroup,NewGroup)   
165End Sub   
166  
167  
168'// 删除组[参数:组名]   
169Public Sub DeleteGroup(GroupName)   
170Content = Replace(Content,GetGroup(GroupName),"")   
171End Sub   
172  
173  
174'// 删除节点[参数:组名,节点名]   
175Public Sub DeleteNode(GroupName,NodeName)   
176Dim TempGroup   
177Dim NewGroup   
178TempGroup = GetGroup(GroupName)   
179NewGroup = Replace(TempGroup,NodeName&"="&ReadNode(GroupName,NodeName)&VbCrlf,"")   
180if Right(NewGroup,1)<>Chr(10) Then NewGroup = NewGroup&VbCrlf   
181Content = Replace(Content,TempGroup,NewGroup)   
182End Sub   
183  
184  
185'// 替换字符[实参:替换目标,数据流向方向]   
186' 字符转换[防止关键符号出错]   
187' [ ---> {(@)}   
188' ] ---> {(#)}   
189' = ---> {($)}   
190' 回车 ---> {(1310)}   
191Public Function ReplaceData(Data_Str,IsIn)   
192if IsIn Then   
193ReplaceData = Replace(Replace(Replace(Data_Str,"[","{(@)}"),"]","{(#)}"),"=","{($)}")   
194ReplaceData = Replace(ReplaceData,Chr(13)&Chr(10),"{(1310)}")   
195Else   
196ReplaceData = Replace(Replace(Replace(Data_Str,"{(@)}","["),"{(#)}","]"),"{($)}","=")   
197ReplaceData = Replace(ReplaceData,"{(1310)}",Chr(13)&Chr(10))   
198End if   
199End Function   
200  
201  
202'// 保存文件数据   
203Public Sub Save()   
204With Stream   
205.Close   
206.Open   
207.WriteText Content   
208.SaveToFile FilePath,2   
209End With   
210End Sub   
211  
212  
213'// 关闭、释放   
214Public Sub Close()   
215Set Stream = Nothing   
216Set Content = Nothing   
217End Sub   
218  
219End Class   
220  
221  
222Dim INI   
223Set INI = New INI_Class   
224INI.OpenFile = Server.MapPath("Config.ini")   
225'// ========== 这是写入ini数据 ==========   
226INI.WriteNode("SiteConfig","SiteName","Leadbbs极速论坛")   
227INI.WriteNode("SiteConfig","Mail","[email protected]")   
228INI.Save()   
229'// ========== 这是读取ini数据 ==========   
230Response.Write("站点名称:"INI.ReadNode("SiteConfig","SiteName"))   
Published At
Categories with Web编程
Tagged with
comments powered by Disqus