How to make a custom configuration file to use with your ASP scripts.
Optimized GetConfig Routine
This one is a little more complicated
(requires two procedures because Tim still can't figure out how to create dynamic
variables on-the-fly), but is 100x more efficient than the 1st version because it doesn't
require multiple accessing of the text file. Instead it opens and closes the file
only once and makes a single pass thru the file for querying all data
Designed by Tim Lasek.
Below is the Source Code, Example Config File, and Example Usage
Code
<%
Sub GetConfigObj(ReturnObj, VarQuery, TxtFileName)
Dim fs, TxtFile
ReturnObj = ""
VarNames = Split(VarQuery, ",")
For Index = 0 To UBound(VarNames)
VarNames(Index) = Trim(VarNames(Index))
Next
Set fs = CreateObject("Scripting.FileSystemObject")
Set TxtFile = fs.OpenTextFile(Server.MapPath(TxtFileName))
While TxtFile.AtEndOfStream = False
InputStr = TxtFile.ReadLine
If InputStr <> "" Then
isStr = Split(InputStr, "==")
For Index = 0 To UBound(isStr)
isStr(Index) = Trim(isStr(Index))
Next
For Index = 0 To UBound(VarNames)
If LCase(VarNames(Index)) = LCase(isStr(0)) Then
ReturnObj = ReturnObj & isStr(0) & Chr(149) & isStr(1) & Chr(161)
Index = UBound(VarNames)
End If
Next
End If
Wend
TxtFile.Close
If Right(ReturnObj, 1) = Chr(161) Then ReturnObj = Left(ReturnObj, Len(ReturnObj) - 1)
End Sub
Function Config(VarName, cfgObj)
NVpairs = Split(cfgObj, Chr(161))
For Index = 0 To UBound(NVpairs)
NVpair = Split(NVpairs(Index), Chr(149))
If LCase(NVpair(0)) = LCase(VarName) Then
Config = NVpair(1)
End If
Next
End Function
%>
<%
GetConfigObj ConfigObj, "cfg_FontFace, cfg_FontColor, cfg_BorderSize,
cfg_TableSpacing, cfg_TablePadding, cfg_SomeText", "config.txt"
cfg_FontFace = Config("cfg_FontFace", ConfigObj)
cfg_FontColor = Config("cfg_FontColor", ConfigObj)
cfg_BorderSize = Config("cfg_BorderSize", ConfigObj)
cfg_TableSpacing = Config("cfg_TableSpacing", ConfigObj)
cfg_TablePadding = Config("cfg_TablePadding", ConfigObj)
cfg_SomeText = Config("cfg_SomeText", ConfigObj)
%>
<%
Response.Write("cfg_FontFace = " & cfg_FontFace &
"<br>")
Response.Write("cfg_FontColor = " & cfg_FontColor &
"<br>")
Response.Write("cfg_BorderSize = " & cfg_BorderSize &
"<br>")
Response.Write("cfg_TableSpacing = " & cfg_TableSpacing &
"<br>")
Response.Write("cfg_TablePadding = " & cfg_TablePadding &
"<br>")
Response.Write("cfg_SomeText = " & cfg_SomeText &
"<br>")
%>
Would result in the following output.
cfg_FontFace = Arial, Helvetica cfg_FontColor = #000000 cfg_BorderSize = 0 cfg_TableSpacing = 1 cfg_TablePadding = 3 cfg_SomeText = Jack and Jill... went up the hill!
However, you would probably use the variables for other
than just displaying them on the page. This is just an example of how to use this function
and subroutine.
The config file in this example looks like this.
All variables are to be declared on a single line.
All variables declared using format: [var name] [delimiter] [value].
All lines that don't contain the delimiter are ignored as comments.
cfg_FontFace == Arial, Helvetica
cfg_FontColor == #000000
cfg_BorderSize == 0
cfg_TableSpacing == 1
cfg_TablePadding == 3
cfg_SomeText == Jack and Jill... went up the hill! |
Download the example config file.
config.txt
Also if running this on NT make sure the anonymous webserver account has read permissions
on this text file. Most likely it probably will have this by default.
|