Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
' Copyright 2014 European Union.
' Licensed under the EUPL (the 'Licence');
'
' * You may not use this work except in compliance with the Licence.
' * You may obtain a copy of the Licence at: http://ec.europa.eu/idabc/eupl
' * Unless required by applicable law or agreed to in writing,
' software distributed under the Licence is distributed on an "AS IS" basis,
' WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
'
' See the LICENSE.txt for the specific language governing permissions and limitations.
Public Class cFile_V3
Private TxtFldParser As Microsoft.VisualBasic.FileIO.TextFieldParser
Private StrWrter As System.IO.StreamWriter
Private Mode As FileMode
Private Path As String
Private Sepp As String
Private SkipCom As Boolean
Private StopE As Boolean
Private FileOpen As Boolean
Private PreLine As String()
Private FileEnd As Boolean
Public Sub New()
Me.Reset()
End Sub
Private Sub Reset()
FileOpen = False
Mode = FileMode.Undefined
PreLine = Nothing
FileEnd = False
End Sub
Public Function OpenRead(ByVal FileName As String, Optional ByVal Separator As String = ",", Optional ByVal SkipComment As Boolean = True, Optional ByVal StopAtE As Boolean = False) As Boolean
Me.Reset()
StopE = StopAtE
Path = FileName
Sepp = Separator
SkipCom = SkipComment
If Not (Mode = FileMode.Undefined) Then Return False
If Not IO.File.Exists(Path) Then Return False
Mode = FileMode.Read
Try
TxtFldParser = New Microsoft.VisualBasic.FileIO.TextFieldParser(Path, System.Text.Encoding.Default)
FileOpen = True
Catch ex As Exception
Return False
End Try
TxtFldParser.TextFieldType = Microsoft.VisualBasic.FileIO.FieldType.Delimited
TxtFldParser.Delimiters = New String() {Sepp}
'If TxtFldParser.EndOfData Then Return False
Me.ReadLine()
Return True
End Function
Public Function ReadLine() As String()
Dim line As String()
Dim line0 As String
line = PreLine
lb10:
If TxtFldParser.EndOfData Then
FileEnd = True
Else
PreLine = TxtFldParser.ReadFields
line0 = UCase(Trim(PreLine(0)))
If SkipCom Then
If Left(line0, 1) = "#" Then GoTo lb10
End If
If StopE Then FileEnd = (line0 = "E")
End If
Return line
End Function
Public Sub Close()
Select Case Mode
Case FileMode.Read
If FileOpen Then TxtFldParser.Close()
TxtFldParser = Nothing
Case FileMode.Write
If FileOpen Then StrWrter.Close()
StrWrter = Nothing
End Select
Me.Reset()
End Sub
Public ReadOnly Property EndOfFile() As Boolean
Get
Return FileEnd
End Get
End Property
Public Function OpenWrite(ByVal FileName As String, Optional ByVal Separator As String = ",", Optional ByVal AutoFlush As Boolean = False, Optional ByVal Append As Boolean = False) As Boolean
Me.Reset()
Path = FileName
Sepp = Separator
If Not (Mode = FileMode.Undefined) Then Return False
Mode = FileMode.Write
Try
StrWrter = My.Computer.FileSystem.OpenTextFileWriter(Path, Append, FileFormat)
FileOpen = True
Catch ex As Exception
Return False
End Try
StrWrter.AutoFlush = AutoFlush
Return True
End Function
Public Sub WriteLine(ByVal ParamArray x() As Object)
Dim St As String
Dim StB As New System.Text.StringBuilder
Dim Skip As Boolean
Skip = True
For Each St In x
If Skip Then
StB.Append(St)
Skip = False
Else
StB.Append(Sepp & St)
End If
Next
StrWrter.WriteLine(StB.ToString)
StB = Nothing
End Sub
Public Sub WriteLine(ByVal x As String)
StrWrter.WriteLine(x)
End Sub
Private Enum FileMode
Undefined
Read
Write
End Enum
End Class