一个ini类代替缓存使用


把下面的代码保存为INI.asp即可运行:
1 < %
2 ' PowerByTim
3 ' 文件摘要:INI类
4 ' 文件版本:3.0
5 ' 文本创建日期:2:172004-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
26 ClassINI_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 EndSub
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 .WriteTextbStr
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 EndFunction
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 EndProperty
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 EndFunction
96
97
98 ' //读取组信息[参数:组名]
99 Public Function GetGroup(GroupName)
100 Dim TempGroup
101 if Not IsGroup(GroupName) Then Exit Function
102 ' //开始寻找头部截取
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 & VbCrlf
107 GetGroup = TempGroup
108 EndFunction
109
110
111 ' //检测节点是否存在[参数:组名,节点名]
112 Public Function IsNode(GroupName,NodeName)
113 if Instr (GetGroup(GroupName),NodeName & " = " ) Then
114 IsNode = True
115 Else
116 IsNode = False
117 End if
118 EndFunction
119
120
121 ' //创建组[参数:组名]
122 Public Sub WriteGroup(GroupName)
123 if Not IsGroup(GroupName) And GroupName <> "" Then
124 Content = Content & " [ " & GroupName & " ] " & VbCrlf
125 End if
126 EndSub
127
128
129 ' //读取节点数据[参数:组名,节点名]
130 Public Function ReadNode(GroupName,NodeName)
131 if Not IsNode(GroupName,NodeName) Then Exit Function
132 Dim TempContent
133 ' //取组信息
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 EndFunction
140
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 NewGroup
153 if IsNode(GroupName,NodeName) Then
154 NewGroup = Replace (TempGroup,NodeName & " = " & ReplaceData(ReadNode(GroupName,NodeName), 1 ),NodeName & " = " & ReplaceData(NodeData, 1 ))
155 Else
156 NewGroup = TempGroup & NodeName & " = " & ReplaceData(NodeData, 1 ) & VbCrlf
157 End if
158
159 Content = Replace (Content,TempGroup,NewGroup)
160 EndSub
161
162
163 ' //删除组[参数:组名]
164 Public Sub DeleteGroup(GroupName)
165 Content = Replace (Content,GetGroup(GroupName), "" )
166 EndSub
167
168
169 ' //删除节点[参数:组名,节点名]
170 Public Sub DeleteNode(GroupName,NodeName)
171 Dim TempGroup
172 Dim NewGroup
173 TempGroup = GetGroup(GroupName)
174 NewGroup = Replace (TempGroup,NodeName & " = " & ReadNode(GroupName,NodeName) & VbCrlf, "" )
175 if Right (NewGroup, 1 ) <> Chr ( 10 ) Then NewGroup = NewGroup & VbCrlf
176 Content = Replace (Content,TempGroup,NewGroup)
177 EndSub
178
179
180 ' //替换字符[实参:替换目标,数据流向方向]
181 ' 字符转换[防止关键符号出错]
182 ' [--->{(@)}
183 ' ]--->{(#)}
184 ' =--->{($)}
185 ' 回车--->{(1310)}
186 Public Function ReplaceData(Data_Str,IsIn)
187 if IsIn Then
188 ReplaceData = Replace ( Replace ( Replace (Data_Str, " [ " , " {(@)} " ), " ] " , " {(#)} " ), " = " , " {($)} " )
189 ReplaceData = Replace (ReplaceData, Chr ( 13 ) & Chr (<span st
分享到:
评论

你可能感兴趣的:(asp)