Attribute VB_Name = "ini_code" ' ini_code.bas ' 4-7-99 by Robert Clemenzi ' Modified 1-05-01 Added the "Initialize" subroutine ' 1-12-01 Added file write routines ' 2-02-01 Fixed file write routine comments ' Made minor changes ' ' This code reads/writes data from/to an ini file ' ' Only lines which start in column 1 and ' contain an equals sign are processed ' ' Comments should begin with one of the following ' / ; ' but it is not required ' ' Leading and trailing spaces are removed ' Parameter names are not case sensitive ' All data is stored as strings ' The following are equivalent ' ' dir1=c:\test dd\test.ini ' Dir1 = c:\test dd\test.ini ' '---------------------------------------- ' ' Public Methods ' ' Initialize_Parameters() Normal entry point ' Reads - Default.ini ' ' Initialize_ini_Read(Default_Filename$) ' ' Create_ini_Array Required, must be called first ' Called by the Initialize routines ' ' Get_ini_Value(varName$, DefaultValue$) As String ' Returns either the value if it exists ' or it returns the default ' ' Set_ini_Value(varName$, Value$) ' Set/Create the named value ' ' Read_ini(ini_Filename$) Read the ini file ' ' Open_ini(CMDialog As CommonDialog) ' Let the user select a new ini file ' Save_ini() Write the current parameters to the current ini file ' SaveAs_ini(CMDialog As CommonDialog) ' Write the current parameters to a new ini file ' Create_ini(Filename$) Save all known parameters to a file ' Option Explicit Private ini_Filename$ Private ini_Array() As T_ini_Record Private Type T_ini_Record VariableName As String Value As String DefaultValue As String ' Did user change the default? InitialValue As String ' Did user change the Initial Value? UpdateAppendFlag As String ' U = Modify existing parameter 'Allowing Update can preserve user comments End Type Sub Initialize_Parameters() Call Initialize_ini_Read("Default.ini") End Sub ' Setup global constants ' Process Command-line arguments ' Read ini file Sub Initialize_ini_Read(Default_Filename$) Init_Constants ' Variables used as constants ' The current drive and path must be set ' to the exe location (App.Path) to allow ' ini files to be specified as being in the same ' directory as the exe file ChDrive App.Path ChDir App.Path Call Process_Command_Line Call Create_ini_Array ' Required 1st, this actually creates the array ini_Filename$ = Get_Parm_Value("i", Default_Filename$) Call Read_ini(ini_Filename$) End Sub ' Required, must be called first ' Erases all existing parameters Sub Create_ini_Array() ReDim ini_Array(0) ' The null value has leading and trailing space ' and is never used ini_Array(0).VariableName = " Null ini_Array Value " End Sub Sub Set_ini_Filename(temp$) ini_Filename$ = temp$ End Sub Function Get_ini_Filename() As String Get_ini_Filename = ini_Filename$ End Function Public Function Locate_ini_Record(varName$) As Integer Dim i As Integer, temp$ temp$ = UCase(varName$) For i = 0 To UBound(ini_Array) If UCase(ini_Array(i).VariableName) = temp$ Then Locate_ini_Record = i Exit Function End If Next i Locate_ini_Record = -1 ' Null / Not found End Function ' Returns either the value if it exists ' or it returns the default Public Function Get_ini_Value(varName$, DefaultValue$) As String Dim index As Integer index = Locate_ini_Record(varName$) If index <> -1 Then Get_ini_Value = ini_Array(index).Value$ Else ' Since it doesn't already exist, create an entry Call set_ini_Record(varName$, DefaultValue$, _ DefaultValue$, DefaultValue$) Get_ini_Value = DefaultValue$ End If End Function Public Sub Set_ini_Value(varName$, Value$) Dim index As Integer index = Locate_ini_Record(varName$) If index <> -1 Then ini_Array(index).Value$ = Value$ Else ' Since it doesn't already exist, create an entry Call set_ini_Record(varName$, Value$, Value$, Value$) End If End Sub Sub set_ini_Record(varName$, Value$, Initial$, Default$) Dim index As Integer index = Locate_ini_Record(varName$) If index = -1 Then index = UBound(ini_Array) + 1 ReDim Preserve ini_Array(index) ini_Array(index).VariableName = varName$ End If ini_Array(index).Value = Value$ ini_Array(index).DefaultValue = Default$ ini_Array(index).InitialValue = Initial$ End Sub Public Sub Read_ini(Filename$) Dim FileHandle As Integer Dim TextLine$ ' Test if the ini file exists If Dir(ini_Filename$) = "" Then Exit Sub ini_Filename$ = Filename$ FileHandle = FreeFile ' This is safer than assigning a number Open ini_Filename$ For Input As #FileHandle Do While Not EOF(FileHandle) ' Loop until end of file Line Input #FileHandle, TextLine$ ' Read line into variable Call Parse_ini_String(TextLine$) Loop Close #FileHandle End Sub Sub SaveAs_ini(CMDialog As CommonDialog) Dim temp$ 'Dim CMDialog As CommonDialog CMDialog.Filter = "*.ini|*.ini" CMDialog.Filename = "" CMDialog.ShowSave temp$ = CMDialog.Filename ' = "" if the Cancel button is pressed ' The dialog box returns fully qualified filenames ' However, ini_Filename$ may be relative to the current path ' If so, then there is a problem in Write_ini If temp$ = Glob_Concat_Filename(App.Path, ini_Filename$) Then temp$ = ini_Filename$ End If If temp$ <> "" Then Call Write_ini(temp$) End If End Sub Function Open_ini(CMDialog As CommonDialog) Dim temp$ Open_ini = 0 CMDialog.Filter = "*.ini|*.ini" CMDialog.Filename = "" CMDialog.ShowOpen temp$ = CMDialog.Filename ' unchanged if the Cancel button is pressed If temp$ <> "" Then Call Create_ini_Array ' Erase the current data Call Read_ini(temp$) Open_ini = 1 End If End Function ' This is used to save the current ini parameters Sub Save_ini() Write_ini (ini_Filename$) End Sub ' This routine reads the current ini file and ' only writes those parameter values the are already included ' in the file. The other lines are copied without change. Sub Write_ini(Filename$) Dim FileHandle1, FileHandle2, TextLine$ FileHandle1 = FreeFile ' This is safer than assigning a number Open ini_Filename$ For Input As #FileHandle1 FileHandle2 = FreeFile Open "_ini.tmp" For Output As #FileHandle2 Do While Not EOF(FileHandle1) ' Loop until end of file Line Input #FileHandle1, TextLine$ ' Read line into variable Call New_ini_String(TextLine$) Print #FileHandle2, TextLine$ Loop Close #FileHandle1 Close #FileHandle2 If Filename$ = ini_Filename$ Then 'Delete old ini file Name ini_Filename$ As "_ini2.tmp" Name "_ini.tmp" As ini_Filename$ Kill "_ini2.tmp" Else Name "_ini.tmp" As Filename ini_Filename$ = Filename$ End If End Sub ' This routine writes all the existing parameters to the ' specified file Sub Create_ini(Filename$) Dim FileHandle, i, temp$ FileHandle = FreeFile ' This is safer than assigning a number Open Filename$ For Output As #FileHandle For i = 1 To UBound(ini_Array) temp$ = ini_Array(i).VariableName & "=" temp$ = temp$ & ini_Array(i).Value Print #FileHandle, temp$ Next i Close #FileHandle End Sub Private Sub New_ini_String(InputString$) Dim temp$, varName$, Value$, output$ Dim i As Integer, index temp$ = InputString$ If temp$ = "" Then Exit Sub ' Blank line If InStr(" /;:", Left(temp$, 1)) Then Exit Sub ' Comment i = InStr(temp$, "=") If i = 0 Then Exit Sub varName$ = Trim(Left(temp$, i - 1)) index = Locate_ini_Record(varName$) If index = 0 Then Exit Sub ' Value not read temp$ = varName$ & "=" temp$ = temp$ & ini_Array(index).Value InputString$ = temp$ End Sub Private Sub Parse_ini_String(InputString$) Dim temp$, varName$, Value$ Dim i As Integer temp$ = InputString$ If temp$ = "" Then Exit Sub ' Blank line If InStr(" /;:", Left(temp$, 1)) Then Exit Sub ' Comment i = InStr(temp$, "=") If i = 0 Then Exit Sub ' Trim removes spaces around the equals sign ' which, in turn, should significantly reduce obscure problems varName$ = Trim(Left(temp$, i - 1)) Value$ = Trim(Mid(temp$, i + 1)) Call Set_ini_Value(varName$, Value$) End Sub '------------------------------------------------------------------------ ' Examples of how to call this ini_Code module Private Sub ExampleCalls() Call Create_ini_Array ' Required 1st, this actually creates the array ' Dir1_UIText.Text = Get_ini_Value("dir1", "test data") ' ini_Filename$ = "C:\My Documents 2\vb\ini_Access\FileCompare.ini" ' Call Read_ini(ini_Filename$) ' Dir1_UIText.Text = Get_ini_Value("dir1", "test data") End Sub 'Private Sub UIMenu_File_Open_Click() ' If Open_ini(CMDialog) Then ' ' End If 'End Sub 'Private Sub UIMenu_File_Save_Click() ' Call Save_ini 'End Sub 'Private Sub UIMenu_File_SaveAs_Click() ' Call SaveAs_ini(CMDialog) ' CMDialog must be defined on the form 'End Sub