LotusScript class: FileWriter for R5.x

Example:

Dim fw As New FileWriter(|c:lekkim.txt|, True)
Call fw.WriteText(CStr(Now))
Call fw.Close()

Code:

'constants
Const FILE_NOTOPENED% = 0
Const FILE_CLOSED% = -1

Public Class FileWriter
   'declarations
   Private pFileNum As Integer
   Private pFilename As String

   '/**
   ' * Constructor.
   ' */
   Public Sub New(filename As String, replace_existing As Integer)
      'make sure we have a filename
      If filename = "" Then
         Error 9999, "You must supply a filename."
      End If

      'store filename
      Me.pFilename = filename

      'error handling
      On Error Goto catch

      'should we replace
      If replace_existing Then
         'remove the file if it exists
         Kill filename
      End If

      'exit gracefully
      Exit Sub

catch:
      Resume finally
finally:
      Exit Sub
   End Sub

   '/**
   ' * Destructor.
   ' */
   Public Sub Delete()
      'make sure we close any file we might have open
      If Me.pFileNum > 0 Then
         'we need to close the file
         Close Me.pFileNum
      End If
   End Sub

   '/**
   ' * Write text to the file.
   ' */
   Public Sub WriteText(text As String)
      'is the file open
      If Me.pFileNum = FILE_NOTOPENED Then
         'no - open the file

         'get a new filenumber
         Me.pFileNum = Freefile()

         'open the file for writing
         Open Me.pFilename For Output As Me.pFilenum
      Elseif Me.pFileNum = FILE_CLOSED Then
         'the user is trying to write to a closed file
         Error 9999, "You cannot write to a file you have closed"
      End If

      'write to the file
      Print #Me.pFileNum, text
   End Sub

   '/**
   ' * Close the file.
   ' */
   Public Sub Close()
      'close any file we might have open
      If Me.pFileNum > 0 Then
         'we need to close the file
         Close Me.pFileNum

         'set filenum to -1 to signal that we closed the file
         Me.pFilenum = FILE_CLOSED
      End If
   End Sub

End Class