-
Notifications
You must be signed in to change notification settings - Fork 0
/
MFile.bas
203 lines (172 loc) · 5.62 KB
/
MFile.bas
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
Attribute VB_Name = "MFile"
Option Explicit
'
' Copyright (c) 2020 Koki Takeyama
'
' Permission is hereby granted, free of charge, to any person obtaining
' a copy of this software and associated documentation files (the "Software"),
' to deal in the Software without restriction, including without limitation
' the rights to use, copy, modify, merge, publish, distribute, sublicense,
' and/or sell copies of the Software, and to permit persons to whom the
' Software is furnished to do so, subject to the following conditions:
'
' The above copyright notice and this permission notice shall be included
' in all copies or substantial portions of the Software.
'
' THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
' IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
' FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
' AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
' LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
' FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
' IN THE SOFTWARE.
'
'
' Microsoft Scripting Runtime
' - Scripting.File
'
'
' === TextFile ===
'
'
' ReadTextFileW
' - Reads an entire file and returns the resulting string (Unicode).
'
' ReadTextFileA
' - Reads an entire file and returns the resulting string (ASCII).
'
'
' FileObject:
' Required. The name of a File object.
'
Public Function ReadTextFileW(FileObject As Scripting.File) As String
ReadTextFileW = ReadTextFile(FileObject, Scripting.TristateTrue)
End Function
Public Function ReadTextFileA(FileObject As Scripting.File) As String
ReadTextFileA = ReadTextFile(FileObject, Scripting.TristateFalse)
End Function
Private Function ReadTextFile( _
FileObject As Scripting.File, _
Optional Format As Scripting.Tristate) As String
If FileObject Is Nothing Then Exit Function
If FileObject.Size = 0 Then Exit Function
ReadTextFile = OpenAsTextStreamAndReadAll(FileObject, Format)
End Function
'
' WriteTextFileW
' - Writes a specified string (Unicode) to a file.
'
' WriteTextFileA
' - Writes a specified string (ASCII) to a file.
'
' AppendTextFileW
' - Writes a specified string (Unicode) to the end of a file.
'
' AppendTextFileA
' - Writes a specified string (ASCII) to the end of a file.
'
'
' FileObject:
' Required. The name of a File object.
'
' Text:
' Required. The text you want to write to the file.
'
Public Sub WriteTextFileW(FileObject As Scripting.File, Text As String)
WriteTextFile _
FileObject, _
Text, _
Scripting.ForWriting, _
Scripting.TristateTrue
End Sub
Public Sub WriteTextFileA(FileObject As Scripting.File, Text As String)
WriteTextFile _
FileObject, _
Text, _
Scripting.ForWriting, _
Scripting.TristateFalse
End Sub
Public Sub AppendTextFileW(FileObject As Scripting.File, Text As String)
WriteTextFile _
FileObject, _
Text, _
Scripting.ForAppending, _
Scripting.TristateTrue
End Sub
Public Sub AppendTextFileA(FileObject As Scripting.File, Text As String)
WriteTextFile _
FileObject, _
Text, _
Scripting.ForAppending, _
Scripting.TristateFalse
End Sub
Private Sub WriteTextFile( _
FileObject As Scripting.File, _
Text As String, _
Optional IOMode As Scripting.IOMode = Scripting.ForWriting, _
Optional Format As Scripting.Tristate)
If FileObject Is Nothing Then Exit Sub
If (FileObject.Attributes And ReadOnly) = ReadOnly Then Exit Sub
If IOMode = Scripting.ForReading Then Exit Sub
OpenAsTextStreamAndWrite FileObject, Text, IOMode, Format
End Sub
'
' --- TextFile ---
'
'
' OpenAsTextStreamAndReadAll
' - Reads an entire file and returns the resulting string.
'
'
' FileObject:
' Required. The name of a File object.
'
' Format:
' Optional. One of three Tristate values used to indicate the format of
' the opened file. If omitted, the file is opened as ASCII.
' TristateUseDefault(-2): Opens the file by using the system default.
' TristateTrue(-1): Opens the file as Unicode.
' TristateFalse(0): Opens the file as ASCII.
'
Public Function OpenAsTextStreamAndReadAll( _
FileObject As Scripting.File, _
Optional Format As Scripting.Tristate) As String
On Error Resume Next
With FileObject.OpenAsTextStream(Format:=Format)
OpenAsTextStreamAndReadAll = .ReadAll
.Close
End With
End Function
'
' OpenAsTextStreamAndWrite
' - Writes a specified string to a file.
'
'
' FileObject:
' Required. The name of a File object.
'
' Text:
' Required. The text you want to write to the file.
'
' IOMode:
' Optional. Indicates input/output mode.
' Can be one of two constants: ForWriting(2), or ForAppending(8).
'
' Format:
' Optional. One of three Tristate values used to indicate the format of
' the opened file. If omitted, the file is opened as ASCII.
' TristateUseDefault(-2): Opens the file by using the system default.
' TristateTrue(-1): Opens the file as Unicode.
' TristateFalse(0): Opens the file as ASCII.
'
Public Sub OpenAsTextStreamAndWrite( _
FileObject As Scripting.File, _
Text As String, _
Optional IOMode As Scripting.IOMode = Scripting.ForWriting, _
Optional Format As Scripting.Tristate)
On Error Resume Next
With FileObject.OpenAsTextStream(IOMode, Format)
.Write (Text)
.Close
End With
End Sub