Documentation Center

Example 5 - Handling of other file types

This examples demonstrate how to write handler that is able to parse and generate INI files.

The handler to be implemented is PSL_OnProcessUserFile. The parameter is used to establish the communication between Passolo and the handler.

The handler is called for several purposes and rd.Action indicates why Passolo calls the handler:

pslResUpdGetFileExtensions

Passolo wants to know the filestypes which the handler supports. In this case the handler calls rd.AddFileExtension with each filetype/file extension it supports (lines 126 to 129).

pslResUpdScanData and pslResUpdGenerate

Passolo wants to scan a source file or generate the target file. In this case rd.SourceFile contains the name of the source file and rd.TargetFile contains the name of the target file. In Passolo resource string are entries in resources, like dialog controls in a dialog. For an ini file each section can be defined as a resource and the keys can be defined as resource entries. Before you can add resource string to a string list, the parent resource must be defined by calling rd.ProcessResource (line 40). To add an entry to a string list, you must call rd.SetEntryData with the parameter pslResUpdId and the id of the entry. With a second call to rd.SetEntryData with the parameter pslResUpdText you must set the text of the entry. And finally you must call rd.ProcessEntry so that Passolo processes the resource entry (lines 46 to 48).

In case of PslResUpdGenerate the translated version of the entry text is available after the call to rd.ProcessEntry. Call rd.GetEntryData with the parameter pslResUpdText to get the translated text to generate the target file (line 51 to 54).

pslResUpdListContent

Passolo wants to know which resources in which languages are contained in the source file. In this case the handler calls rd.ListResource for each resource in each language found in the source file. The first parameter is the resource type, the second parameter is the resource name and the third parameter is the language. If the language can not be determined it is recommended to use 0 as the neutral language (line 83).

pslResUpdCheckSourceTarget

Passolo wants to know if the source file can be processed by the handler and the target file is valid. If the source file can not be processed by the handler or the source file does not exist, the handler should set rd.Error to a non-zero value (line 90 to 115). In the case of ini files it is not useful to use the source file also as the target file. This case is handled in the lines 104 to 107.

pslResUpdGetLanguages

Passolo wants to know which source languages are contained in the source file. rd.AddLanguage must be called for each language found in the source file. If the source language can not be determined, it is recommended to call rd.AddLanguage(0) so that Passolo uses the neutral language for the source file.

In case that the source file contains more than one language the handler must only process data with the requested source language when scanning the source file or generating the target file. Use ResData.SourceLanguage to determine the source language and if you need to know the target language read the attribute ResData.TargetLanguage.

How to use it

Select PslIniScan as the system macro in the dialog Macro and active the system macro with the command Tools / System Macro / Start System Macro.

If you want to add a new source file, you should now be able to filter only ini files in the file selector box. If you have selected a ini file you will be able to see the ini sections in the Info dialog (which can be opened in the properties dialog of the source list). And finally you should be able to work with ini files as you would do it with other supported files types:

0001 ' If INI-section, return section name otherwise emtpy string
0002 Function GetHeader(ln As String) As String
0003 If Left(ln,1) <> "[" Or Right(ln, 1) <> "]" Then Exit Function
0004 
0005 GetHeader = Mid(ln, 2, Len(ln) - 2)
0006 End Function
0007 
0008 ' Get Position of "=" if not found or comment return 0
0009 Function GetDataPos(ln As String) As Long
0010 GetDataPos = 0
0011  ' Comment?
0012  If Left(ln,1) = ";" Then Exit Function
0013  

0014  GetDataPos = InStr(ln, "=")
0015  End Function
0016  
0017 ' Update translation list and generate target file
0018 Function IniUpdate(rd As PslResData) As Long
0019     Dim fso As Scripting.FileSystemObject
0020     Dim tsi As Scripting.TextStream
0021     Dim tso As Scripting.TextStream
0022     Dim s As String, header As String, i As Integer
0023  
0024    Set fso = New Scripting.FileSystemObject
0025     Set tsi = fso.OpenTextFile(rd.SourceFile)
0026  
0027    ' Generating target file
0028    If rd.Action = pslResUpdGenerate Then
0029        Set tso = fso.CreateTextFile(rd.TargetFile)
0030    End If
0031  
0032    IniUpdate = 0
0033  
0034    On Error GoTo ende
0035    While True
0036        s = tsi.ReadLine
0037        ' Handle new section header
0038        If GetHeader(s) <> "" Then
0039            header = GetHeader(s)
0040            rd.ProcessResource "INI-Section", header
0041        Else
0042            ' Only if section header is there and line contains data
0043            If header <> "" Then
0044             i = GetDataPos(s)
0045                If i <> 0 Then
0046                    rd.SetEntryData(pslResUpdId, Left(s, i - 1))
0047                    rd.SetEntryData(pslResUpdText, Mid(s, i + 1))
0048                    rd.SetEntryData(pslResUpdCtrl, "")
0049                     rd.ProcessEntry
0050                    ' Generate changed line
0051                    If rd.Action = pslResUpdGenerate Then
0052                        s = Left(s, i - 1) & "=" & _
0053                         rd.GetEntryData(pslResUpdText)
0054          End If
0055             End If
0056            End If
0057        End If
0058  
0059        ' Write data to target file
0060        If rd.Action = 1 Then
0061            tso.WriteLine(s)
0062        End If
0063     Wend
0064 ende:
0065 End Function
0066  
0067 Function IniListContent(rd As PslResData) As Long
0068     Dim fso As Scripting.FileSystemObject
0069     Dim tsi As Scripting.TextStream
0070     Dim s As String, header As String
0071  
0072     Set fso = New Scripting.FileSystemObject
0073     Set tsi = fso.OpenTextFile(rd.SourceFile)
0074  
0075   IniListContent = 0
0076   On Error GoTo ende
0077  
0078   ' Add all sections
0079   While True
0080       s = tsi.ReadLine
0081    header = GetHeader(s)
0082    If header <> "" Then
0083     rd.ListResource "INI-Section", header, 0
0084    End If
0085   Wend
0086 ende:
0087 End Function
0088  
0089  
0090 Function IniCheckFiles(rd As PslResData) As Long
0091     Dim fso As Scripting.FileSystemObject
0092     Dim ts As Scripting.TextStream
0093     Dim s As String
0094     Set fso = New Scripting.FileSystemObject
0095  
0096   ' No INI file
0097   If UCase(Right(rd.SourceFile, 3)) <> "INI" Then GoTo failure
0098  
0099   ' File does not exists
0100     On Error GoTo failure
0101     Set ts = fso.OpenTextFile(rd.SourceFile)
0102  
0103   ' target file, should not be the same as source file
0104   If rd.TargetFile <> "" Then
0105         If UCase(rd.SourceFile) = UCase(rd.TargetFile) Then _
0106            GoTo failure
0107   End If
0108  
0109   IniCheckFiles = 0
0110     Exit Function
0111  
0112 failure:
0113     IniCheckFiles = 1
0114  
0115 End Function
0116  
0117 Function IniGetLanguages(rd As PslResData) As Long
0118     ' Only Language neutral
0119     IniGetLanguages = 0
0120     rd.AddLanguage(0)
0121 End Function
0122 
0123 Public Sub PSL_OnProcessUserFile(rd As PslResData)
0124     rd.Error = 0
0125 
0126     If rd.Action = pslResUpdGetFileExtensions Then
0127         rd.AddFileExtension("INI Files (*.ini)", "*.ini")
0128         Exit Sub
0129     End If
0130   
0131     ' No Ini file
0132     If UCase(Right(rd.SourceFile, 3)) <> "INI" Then Exit Sub
0133   
0134     Select Case rd.Action
0135          Case pslResUpdGenerate
0136              rd.Error = IniUpdate(rd)
0137          Case pslResUpdScanData
0138              rd.Error = IniUpdate(rd)
0139          Case pslResUpdListContent
0140              rd.Error = IniListContent(rd)
0141          Case pslResUpdCheckSourceTarget
0142              rd.Error = IniCheckFiles(rd)
0143          Case pslResUpdGetLanguages
0144             rd.Error = IniGetLanguages(rd)
0145          Case Else
0146             rd.Error = 1
0147     End Select
0148 End Sub