Code:
'*** Constants ***
Private Const DEFAULT_CHARSET$ = "ISO-8859-1"
'/**
' * Base FileWriter class. Normal usage scenario would we:
' * 1. Dim a new FileWriter using the filename and whether to replace any existing file.
' * 2. (Optional) Set the character set to use if different from ISO-8859-1.
' * 3. Call the Open() method to open the FileWriter.
' * 4. Call the Write(Variant) method to write data to the file.
' * 5. Call the Close() method to close the FileWriter.
' *
' * @version 1.0 (16 December 2004)
' * @author lekkim@it-inspiration.dk
' * @author it-inspiration aps
' */
Public Class FileWriter
'declarations
Private pFilename As String
Private pReplaceExisting As Boolean
Private pStream As NotesStream
Private pCharset As String
'/**
' * Constructor.
' */
Public Sub New(filename As String, replace_existing As Boolean)
Me.pFilename = filename
Me.pReplaceExisting = replace_existing
End Sub
'/**
' * Destructor.
' */
Public Sub Delete()
'make sure the stream has been closed
Call Me.Close()
End Sub
'/**
' * Sets the charset to use.
' */
Public Property Set Charset(charzet As String)
Me.pCharset = charzet
End Property
'/**
' * Opens the file.
' */
Public Sub Open()
'declarations
Dim session As New NotesSession
Dim rc As Boolean
'cannot open an open stream
If Not (Me.pStream Is Nothing) Then
Exit Sub
End If
'create stream
Set Me.pStream = session.CreateStream()
'should we replace ?
If Me.pReplaceExisting Then
'set up error handling
On Error Goto catch_fileexist
'does the file exist already
If Filelen(Me.pFilename) Then
'the file exists - delete it
Kill Me.pFilename
End If
Goto finally_fileexist
catch_fileexist:
'the file doesn't exist
Resume finally_fileexist
finally_fileexist:
'reset error handling
On Error Goto 0
End If
'try and open the file
If Me.pCharset "" Then
'use custom charset
rc = Me.pStream.Open(Me.pFilename, Me.pCharset)
Else
'use default charset
rc = Me.pStream.Open(Me.pFilename, DEFAULT_CHARSET)
End If
'did we open the file correctly ?
If Not rc Then
'could not open the file
Error 9999, "Unable to open file: " + Me.pFilename
End If
End Sub
'/**
' * Closes the file.
' */
Public Sub Close()
If Not (Me.pStream Is Nothing) Then
Call Me.pStream.Close()'
Set Me.pStream = Nothing
End If
End Sub
'/**
' * Writes text to the writer.
' */
Public Sub Write(buffer As Variant)
'make sure the stream is open
If Me.pStream Is Nothing Then
Error 9999, "The FileWriter hasn't been opened yet or has been closed."
End If
If Typename(buffer) = "STRING" Then
'write as text
Call Me.pStream.WriteText(buffer)
Else
'write as bytes
Call Me.pStream.Write(buffer)
End If
End Sub
End Class
Public Class LineFileWriter As FileWriter
'/**
' * Constructor.
' */
Public Sub New(filename As String, replace_existing As Boolean), FileWriter(filename, replace_existing)
End Sub
'/**
' * Overrides the parent implementation to make sure text is written as
' * lines. Also a check is made that only strings are written to to
' * the writer.
' *
' */
Public Sub Write(buffer As Variant)
'make sure the stream is open
If Me.pStream Is Nothing Then
Error 9999, "The FileWriter hasn't been opened yet or has been closed."
End If
If Typename(buffer) = "STRING" Then
'write as text
Call Me.pStream.WriteText(buffer, EOL_PLATFORM)
Else
'only allow text
Error 9999, "You may only write strings."
End If
End Sub
End Class