Skip to content

Commit 1deac7f

Browse files
committed
ECPTextStream v1.0.5
-Bug fixed: CRLF character could not be handled as a stream end. -The class can now output normalized line breaks, through the [unifiedLFOutput property](https://ws-garcia.github.io/ECPTextStream/api/properties/unifiedlfoutput.html). -Method name changed: RestartPointer -> [RestartStreamReader](https://ws-garcia.github.io/ECPTextStream/api/methods/restartstreamreader.html)
1 parent aaa5192 commit 1deac7f

File tree

1 file changed

+28
-4
lines changed

1 file changed

+28
-4
lines changed

src/ECPTextStream.cls

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,21 @@ Option Explicit
2323
'#
2424
'////////////////////////////////////////////////////////////////////////////////////////////
2525
' CONSTANTS:
26+
Private Const DualLFchar As String = vbLf & vbLf
27+
Private Const InverseCRLF As String = vbLf & vbCr
2628
Private Const SizeFactor As Long = 524288
2729
'////////////////////////////////////////////////////////////////////////////////////////////
2830
'#
2931
'////////////////////////////////////////////////////////////////////////////////////////////
3032
' PROPERTIES VARIABLES:
3133
Private P_ATENDOFSTREAM As Boolean '---------------Indicates if the file's end is reached.
3234
Private P_BUFFERLENGTH As Long '-------------------Number of chars in buffer.
33-
Private P_BUFFERSIZE As Single '-------------------Buffer's size in MB (0.5 by default).
35+
Private P_BUFFERSIZE As Single '-------------------Buffer's size x10 MB (0.5 by default).
3436
Private P_ENDSTREAMONLINEBREAK As Boolean '--------If true, each stream ends on a line break.
3537
Private P_ISOPENSTREAM As Boolean '----------------Indicates if the object is linked to a file
3638
Private P_LINEBREAK As String '--------------------Holds the char used to end a Stream.
39+
Private P_UNIFIEDLFOUTPUT As Boolean '-------------If true, the buffer string will be returned
40+
' with the LF char as Line Break.
3741
Private P_STREAMLENGTH As Long '-------------------File len.
3842
Private P_TEXT As String '-------------------------Holds the current stream's text.
3943
'////////////////////////////////////////////////////////////////////////////////////////////
@@ -84,6 +88,9 @@ Public Property Let bufferSize(value As Single)
8488
End Property
8589
Public Property Get bufferString() As String
8690
Attribute bufferString.VB_Description = "Gets the text data stored in the buffer."
91+
If P_UNIFIEDLFOUTPUT Then
92+
NormalizeLineBreaks
93+
End If
8794
bufferString = P_TEXT
8895
End Property
8996
Public Property Get endStreamOnLineBreak() As Boolean
@@ -112,6 +119,13 @@ Public Property Get streamLength() As Long
112119
Attribute streamLength.VB_Description = "Gets the current opened file’s size, in Bytes."
113120
streamLength = P_STREAMLENGTH
114121
End Property
122+
Public Property Get unifiedLFOutput() As Boolean
123+
Attribute unifiedLFOutput.VB_Description = "Determines whether the buffer string is returned using only the LF character as a linefeed."
124+
unifiedLFOutput = P_UNIFIEDLFOUTPUT
125+
End Property
126+
Public Property Let unifiedLFOutput(value As Boolean)
127+
P_UNIFIEDLFOUTPUT = value
128+
End Property
115129
'////////////////////////////////////////////////////////////////////////////////////////////
116130
'#
117131
'////////////////////////////////////////////////////////////////////////////////////////////
@@ -136,10 +150,10 @@ Private Sub FindEOLcharacter()
136150

137151
Do
138152
bufferReverse = StrReverse(Buffer)
139-
LastCrLfPos = InStrB(1, bufferReverse, vbCrLf)
153+
LastCrLfPos = InStrB(1, bufferReverse, InverseCRLF) + 2
140154
LastCrPos = InStrB(1, bufferReverse, vbCr)
141155
LastLfPos = InStrB(1, bufferReverse, vbLf)
142-
missingEOLchar = (LastCrLfPos = 0 And LastCrPos = 0 And LastLfPos = 0)
156+
missingEOLchar = (LastCrLfPos - 2 = 0 And LastCrPos = 0 And LastLfPos = 0)
143157
If missingEOLchar Then
144158
tmpBuffer = Buffer
145159
Get #FileHandled, , Buffer
@@ -186,6 +200,15 @@ Private Sub FindEOLcharacter()
186200
End If
187201
Seek #FileHandled, CorrectedPos
188202
End Sub
203+
Private Sub NormalizeLineBreaks()
204+
If InStrB(1, P_TEXT, vbCr, vbBinaryCompare) > 0 Then
205+
P_TEXT = Replace(P_TEXT, vbCr, vbLf, 1)
206+
End If
207+
Do While InStrB(1, P_TEXT, DualLFchar, vbBinaryCompare) > 0
208+
P_TEXT = Replace(P_TEXT, DualLFchar, vbLf, 1)
209+
Loop
210+
P_LINEBREAK = vbLf
211+
End Sub
189212
Public Sub OpenStream(filePath As String)
190213
Attribute OpenStream.VB_Description = "Opens a stream over a text file."
191214
FileHandled = FreeFile
@@ -285,11 +308,12 @@ Private Sub Class_Initialize()
285308
P_BUFFERSIZE = 0.5
286309
P_BUFFERLENGTH = CLng(P_BUFFERSIZE * SizeFactor)
287310
P_ENDSTREAMONLINEBREAK = False
311+
P_UNIFIEDLFOUTPUT = False
288312
Buffer = SPACE$(P_BUFFERLENGTH)
289313
NullChar = ChrW(0)
290314
End Sub
291315
Private Sub Class_Terminate()
292316
If P_ISOPENSTREAM Then
293317
CloseStream
294318
End If
295-
End Sub
319+
End Sub

0 commit comments

Comments
 (0)