0fe2867113d22629d88645a464eb50d3508c57f2
[platform/upstream/dotnet/runtime.git] /
1 ' Licensed to the .NET Foundation under one or more agreements.
2 ' The .NET Foundation licenses this file to you under the MIT license.
3 ' See the LICENSE file in the project root for more information.
4
5 Imports System
6 Imports System.IO
7 Imports System.Security
8
9 Imports Microsoft.VisualBasic.CompilerServices.ExceptionUtils
10 Imports Microsoft.VisualBasic.CompilerServices.Utils
11
12 Namespace Microsoft.VisualBasic.CompilerServices
13
14     <System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)>
15     Friend Class VB6OutputFile
16
17         '============================================================================
18         ' Declarations
19         '============================================================================
20         Inherits VB6File
21
22         '============================================================================
23         ' Constructor
24         '============================================================================
25         Friend Sub New()
26             MyBase.New()
27         End Sub
28
29         Friend Sub New(ByVal FileName As String, ByVal share As OpenShare, ByVal fAppend As Boolean)
30             MyBase.New(FileName, OpenAccess.Write, share, -1)
31             m_fAppend = fAppend
32         End Sub
33
34         '============================================================================
35         ' Operations
36         '============================================================================
37         Friend Overrides Sub OpenFile()
38             'MyBase.OpenFile()
39
40             Try
41                 If m_fAppend Then
42                     'consider checking WRITE if cannot open READWRITE
43                     If File.Exists(m_sFullPath) Then
44                         m_file = New FileStream(m_sFullPath, FileMode.Open, CType(m_access, FileAccess), CType(m_share, FileShare))
45                     Else
46                         m_file = New FileStream(m_sFullPath, FileMode.Create, CType(m_access, FileAccess), CType(m_share, FileShare))
47                     End If
48                 Else
49                     m_file = New FileStream(m_sFullPath, FileMode.Create, CType(m_access, FileAccess), CType(m_share, FileShare))
50                 End If
51             Catch ex As FileNotFoundException
52                 Throw VbMakeException(ex, vbErrors.FileNotFound)
53             Catch ex As SecurityException
54                 Throw VbMakeException(ex, vbErrors.FileNotFound)
55             Catch ex As DirectoryNotFoundException
56                 Throw VbMakeException(ex, vbErrors.PathNotFound)
57             Catch ex As IOException
58                 Throw VbMakeException(ex, vbErrors.PathFileAccess)
59             End Try
60
61             m_Encoding = GetFileIOEncoding()
62             m_sw = New StreamWriter(m_file, m_Encoding)
63             m_sw.AutoFlush = True
64
65             If m_fAppend Then
66                 'Now position at end of file
67                 Dim lEndOfFile As Long
68                 lEndOfFile = m_file.Length
69                 m_file.Position = lEndOfFile
70                 m_position = lEndOfFile
71             End If
72         End Sub
73
74         Friend Overrides Sub WriteLine(ByVal s As String)
75             If s Is Nothing Then
76                 m_sw.WriteLine()
77                 m_position += 2
78             Else
79                 If m_bPrint AndAlso (m_lWidth <> 0) Then
80                     If m_lCurrentColumn >= m_lWidth Then
81                         m_sw.WriteLine()
82                         m_position += 2
83                     End If
84                 End If
85
86                 m_sw.WriteLine(s)
87                 Diagnostics.Debug.Assert(Not m_Encoding Is Nothing)
88                 m_position += m_Encoding.GetByteCount(s) + 2
89             End If
90
91             m_lCurrentColumn = 0
92         End Sub
93
94         Friend Overrides Sub WriteString(ByVal s As String)
95             If (s Is Nothing) OrElse (s.Length = 0) Then
96                 Exit Sub
97             End If
98
99             If m_bPrint AndAlso (m_lWidth <> 0) Then
100                 If (m_lCurrentColumn >= m_lWidth) OrElse
101                    (m_lCurrentColumn <> 0 AndAlso (m_lCurrentColumn + s.Length) > m_lWidth) Then
102                     m_sw.WriteLine()
103                     m_position += 2
104                     m_lCurrentColumn = 0
105                 End If
106             End If
107
108             m_sw.Write(s)
109             Diagnostics.Debug.Assert(Not m_Encoding Is Nothing)
110             Dim ByteLength As Integer = m_Encoding.GetByteCount(s)
111             m_position += ByteLength
112             m_lCurrentColumn += s.Length
113         End Sub
114
115         Friend Overrides Function CanWrite() As Boolean
116             CanWrite = True
117         End Function
118
119         Public Overrides Function GetMode() As OpenMode
120             If m_fAppend Then
121                 GetMode = OpenMode.Append
122             Else
123                 GetMode = OpenMode.Output
124             End If
125         End Function
126
127         Friend Overrides Function EOF() As Boolean
128             EOF = True
129         End Function
130
131         Friend Overrides Function LOC() As Long
132             Return ((m_position + 127) \ 128)
133         End Function
134
135     End Class
136
137 End Namespace