summaryrefslogtreecommitdiffstats
path: root/wizards/source/scriptforge/SF_TextStream.xba
blob: 35f1b6fb2b492e43919631427a91cd43c084a7f7 (plain)
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE script:module PUBLIC "-//OpenOffice.org//DTD OfficeDocument 1.0//EN" "module.dtd">
<script:module xmlns:script="http://openoffice.org/2000/script" script:name="SF_TextStream" script:language="StarBasic" script:moduleType="normal">REM =======================================================================================================================
REM ===			The ScriptForge library and its associated libraries are part of the LibreOffice project.				===
REM ===					Full documentation is available on https://help.libreoffice.org/								===
REM =======================================================================================================================

Option Compatible
Option ClassModule

Option Explicit

&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;
&apos;&apos;&apos;	SF_TextStream
&apos;&apos;&apos;	=============
&apos;&apos;&apos;		Class instantiated by the
&apos;&apos;&apos;			SF_FileSystem.CreateTextFile
&apos;&apos;&apos;			SF_FileSystem.OpenTextFile
&apos;&apos;&apos;		methods to facilitate the sequential processing of text files
&apos;&apos;&apos;		All open/read/write/close operations are presumed to happen during the same macro run
&apos;&apos;&apos;		The encoding to be used may be chosen by the user
&apos;&apos;&apos;			The list is in the Name column of https://www.iana.org/assignments/character-sets/character-sets.xhtml
&apos;&apos;&apos;			Note that probably not all values  are available
&apos;&apos;&apos;		Line delimiters may be chosen by the user
&apos;&apos;&apos;			In input, CR, LF or CR+LF are supported
&apos;&apos;&apos;			In output, the default value is the usual newline on the actual operating system (see SF_FileSystem.sfNEWLINE)
&apos;&apos;&apos;
&apos;&apos;&apos;		The design choices are largely inspired by
&apos;&apos;&apos;			https://docs.microsoft.com/en-us/office/vba/language/reference/user-interface-help/textstream-object
&apos;&apos;&apos;		The implementation is mainly based on the XTextInputStream and XTextOutputStream UNO interfaces
&apos;&apos;&apos;			https://api.libreoffice.org/docs/idl/ref/interfacecom_1_1sun_1_1star_1_1io_1_1XTextInputStream.html
&apos;&apos;&apos;			https://api.libreoffice.org/docs/idl/ref/interfacecom_1_1sun_1_1star_1_1io_1_1XTextOutputStream.html
&apos;&apos;&apos;
&apos;&apos;&apos;		Instantiation example:
&apos;&apos;&apos;			Dim FSO As Object, myFile As Object
&apos;&apos;&apos;				Set FSO = CreateScriptService(&quot;FileSystem&quot;)
&apos;&apos;&apos;				Set myFile = FSO.OpenTextFile(&quot;C:\Temp\ThisFile.txt&quot;, FSO.ForReading)	&apos;	Once per file
&apos;&apos;&apos;
&apos;&apos;&apos;		Detailed user documentation:
&apos;&apos;&apos;			https://help.libreoffice.org/latest/en-US/text/sbasic/shared/03/sf_textstream.html?DbPAR=BASIC
&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;

REM ================================================================== EXCEPTIONS

Const FILENOTOPENERROR			=	&quot;FILENOTOPENERROR&quot;		&apos;	The file is already closed
Const FILEOPENMODEERROR			=	&quot;FILEOPENMODEERROR&quot;		&apos;	The file is open in incompatible mode
Const ENDOFFILEERROR			=	&quot;ENDOFFILEERROR&quot;		&apos;	When file was read, an end-of-file was encountered

REM ============================================================= PRIVATE MEMBERS

Private [Me]				As Object
Private [_Parent]			As Object
Private ObjectType			As String		&apos; Must be TEXTSTREAM
Private ServiceName			As String
Private _FileName			As String		&apos; File where it is about
Private _IOMode				As Integer		&apos; ForReading, ForWriting or ForAppending
Private _Encoding			As String		&apos; https://www.iana.org/assignments/character-sets/character-sets.xhtml
Private _NewLine			As String		&apos; Line break in write mode
Private _FileExists			As Boolean		&apos; True if file exists before open
Private _LineNumber			As Long			&apos; Number of lines read or written
Private _FileHandler		As Object		&apos; com.sun.star.io.XInputStream or
											&apos; com.sun.star.io.XOutputStream or
											&apos; com.sun.star.io.XStream
Private _InputStream		As Object		&apos; com.sun.star.io.TextInputStream
Private _OutputStream		As Object		&apos; com.sun.star.io.TextOutputStream
Private _ForceBlankLine		As Boolean		&apos; Workaround: XTextInputStream misses last line if file ends with newline

REM ============================================================ MODULE CONSTANTS

REM ===================================================== CONSTRUCTOR/DESTRUCTOR

REM -----------------------------------------------------------------------------
Private Sub Class_Initialize()
	Set [Me] = Nothing
	Set [_Parent] = Nothing
	ObjectType = &quot;TEXTSTREAM&quot;
	ServiceName = &quot;ScriptForge.TextStream&quot;
	_FileName = &quot;&quot;
	_IOMode = -1
	_Encoding = &quot;&quot;
	_NewLine = &quot;&quot;
	_FileExists = False
	_LineNumber = 0
	Set _FileHandler = Nothing
	Set _InputStream = Nothing
	Set _OutputStream = Nothing
	_ForceBlankLine = False
End Sub		&apos;	ScriptForge.SF_TextStream Constructor

REM -----------------------------------------------------------------------------
Private Sub Class_Terminate()
	Call Class_Initialize()
End Sub		&apos;	ScriptForge.SF_TextStream Destructor

REM -----------------------------------------------------------------------------
Public Function Dispose() As Variant
	Call Class_Terminate()
	Set Dispose = Nothing
End Function	&apos;	ScriptForge.SF_TextStream Explicit Destructor

REM ================================================================== PROPERTIES

REM -----------------------------------------------------------------------------
Property Get AtEndOfStream() As Boolean
&apos;&apos;&apos;	In reading mode, True indicates that the end of the file has been reached
&apos;&apos;&apos;	In write and append modes, or if the file is not ready =&gt; always True
&apos;&apos;&apos;	The property should be invoked BEFORE each ReadLine() method:
&apos;&apos;&apos;		A ReadLine() executed while AtEndOfStream is True will raise an error
&apos;&apos;&apos;	Example:
&apos;&apos;&apos;		Dim sLine As String
&apos;&apos;&apos;			Do While Not myFile.AtEndOfStream
&apos;&apos;&apos;				sLine = myFile.ReadLine()
&apos;&apos;&apos;				&apos; ...
&apos;&apos;&apos;			Loop

	AtEndOfStream = _PropertyGet(&quot;AtEndOfStream&quot;)

End Property	&apos;	ScriptForge.SF_TextStream.AtEndOfStream

REM -----------------------------------------------------------------------------
Property Get Encoding() As String
&apos;&apos;&apos;	Returns the name of the text file either in url or in native operating system format
&apos;&apos;&apos;	Example:
&apos;&apos;&apos;		Dim myFile As Object
&apos;&apos;&apos;			FSO.FileNaming = &quot;SYS&quot;
&apos;&apos;&apos;			Set myFile = FSO.OpenTextFile(&quot;C:\Temp\myFile.txt&quot;)
&apos;&apos;&apos;			MsgBox myFile.Encoding		&apos;	UTF-8

	Encoding = _PropertyGet(&quot;Encoding&quot;)

End Property	&apos;	ScriptForge.SF_TextStream.Encoding

REM -----------------------------------------------------------------------------
Property Get FileName() As String
&apos;&apos;&apos;	Returns the name of the text file either in url or in native operating system format
&apos;&apos;&apos;	Example:
&apos;&apos;&apos;		Dim myFile As Object
&apos;&apos;&apos;			FSO.FileNaming = &quot;SYS&quot;
&apos;&apos;&apos;			Set myFile = FSO.OpenTextFile(&quot;C:\Temp\myFile.txt&quot;)
&apos;&apos;&apos;			MsgBox myFile.FileName		&apos;	C:\Temp\myFile.txt

	FileName = _PropertyGet(&quot;FileName&quot;)

End Property	&apos;	ScriptForge.SF_TextStream.FileName

REM -----------------------------------------------------------------------------
Property Get IOMode() As String
&apos;&apos;&apos;	Returns either &quot;READ&quot;, &quot;WRITE&quot; or &quot;APPEND&quot;
&apos;&apos;&apos;	Example:
&apos;&apos;&apos;		Dim myFile As Object
&apos;&apos;&apos;			FSO.FileNaming = &quot;SYS&quot;
&apos;&apos;&apos;			Set myFile = FSO.OpenTextFile(&quot;C:\Temp\myFile.txt&quot;)
&apos;&apos;&apos;			MsgBox myFile.IOMode		&apos;	READ

	IOMode = _PropertyGet(&quot;IOMode&quot;)

End Property	&apos;	ScriptForge.SF_TextStream.IOMode

REM -----------------------------------------------------------------------------
Property Get Line() As Long
&apos;&apos;&apos;	Returns the number of lines read or written so far
&apos;&apos;&apos;	Example:
&apos;&apos;&apos;		Dim myFile As Object
&apos;&apos;&apos;			FSO.FileNaming = &quot;SYS&quot;
&apos;&apos;&apos;			Set myFile = FSO.OpenTextFile(&quot;C:\Temp\myFile.txt&quot;, FSO.ForAppending)
&apos;&apos;&apos;			MsgBox myFile.Line		&apos;	The number of lines already present in myFile

	Line = _PropertyGet(&quot;Line&quot;)

End Property	&apos;	ScriptForge.SF_TextStream.Line

REM -----------------------------------------------------------------------------
Property Get NewLine() As Variant
&apos;&apos;&apos;	Returns the current character string to be inserted between 2 successive written lines
&apos;&apos;&apos;	The default value is the native line separator in the current operating system
&apos;&apos;&apos;	Example:
&apos;&apos;&apos;		MsgBox myFile.NewLine

	NewLine = _PropertyGet(&quot;NewLine&quot;)

End Property	&apos;	ScriptForge.SF_TextStream.NewLine (get)

REM -----------------------------------------------------------------------------
Property Let NewLine(ByVal pvLineBreak As Variant)
&apos;&apos;&apos;	Sets the current character string to be inserted between 2 successive written lines
&apos;&apos;&apos;	Example:
&apos;&apos;&apos;		myFile.NewLine = Chr(13) &amp; Chr(10)

Const cstThisSub = &quot;TextStream.setNewLine&quot;

	SF_Utils._EnterFunction(cstThisSub)
	If VarType(pvLineBreak) = V_STRING Then _NewLine = pvLineBreak
	SF_Utils._ExitFunction(cstThisSub)

End Property	&apos;	ScriptForge.SF_TextStream.NewLine (let)

REM ===================================================================== METHODS

REM -----------------------------------------------------------------------------
Public Function CloseFile() As Boolean
&apos;&apos;&apos; Empties the output buffer if relevant. Closes the actual input or output stream
&apos;&apos;&apos;	Args:
&apos;&apos;&apos;	Returns:
&apos;&apos;&apos;		True if the closure was successful
&apos;&apos;&apos;	Exceptions:
&apos;&apos;&apos;		FILENOTOPENERROR		Nothing found to close
&apos;&apos;&apos;	Examples:
&apos;&apos;&apos;		myFile.CloseFile()

Dim bClose As Boolean   	    &apos;   Return value
Const cstThisSub = &quot;TextStream.CloseFile&quot;
Const cstSubArgs = &quot;&quot;

	If SF_Utils._ErrorHandling() Then On Local Error GoTo Catch
	bClose = False

Check:
	SF_Utils._EnterFunction(cstThisSub, cstSubArgs)
	If Not _IsFileOpen() Then GoTo Finally

Try:
	If Not IsNull(_InputStream) Then _InputStream.closeInput()
	If Not IsNull(_OutputStream) Then
		_OutputStream.flush()
		_OutputStream.closeOutput()
	End If
	Set _InputStream = Nothing
	Set _OutputStream = Nothing
	Set _FileHandler = Nothing
	bClose = True

Finally:
	CloseFile = bClose
	SF_Utils._ExitFunction(cstThisSub)
	Exit Function
Catch:
	GoTo Finally
End Function    &apos;   ScriptForge.SF_TextStream.CloseFile

REM -----------------------------------------------------------------------------
Public Function GetProperty(Optional ByVal PropertyName As Variant) As Variant
&apos;&apos;&apos;	Return the actual value of the given property
&apos;&apos;&apos;	Args:
&apos;&apos;&apos;		PropertyName: the name of the property as a string
&apos;&apos;&apos;	Returns:
&apos;&apos;&apos;		The actual value of the property
&apos;&apos;&apos;		If the property does not exist, returns Null
&apos;&apos;&apos;	Exceptions:
&apos;&apos;&apos;		see the exceptions of the individual properties
&apos;&apos;&apos;	Examples:
&apos;&apos;&apos;		myModel.GetProperty(&quot;MyProperty&quot;)

Const cstThisSub = &quot;TextStream.GetProperty&quot;
Const cstSubArgs = &quot;&quot;

	If SF_Utils._ErrorHandling() Then On Local Error GoTo Catch
	GetProperty = Null

Check:
	If SF_Utils._EnterFunction(cstThisSub, cstSubArgs) Then
		If Not SF_Utils._Validate(PropertyName, &quot;PropertyName&quot;, V_STRING, Properties()) Then GoTo Catch
	End If

Try:
	GetProperty = _PropertyGet(PropertyName)

Finally:
	SF_Utils._ExitFunction(cstThisSub)
	Exit Function
Catch:
	GoTo Finally
End Function	&apos;	ScriptForge.SF_TextStream.GetProperty

REM -----------------------------------------------------------------------------
Public Function Methods() As Variant
&apos;&apos;&apos;	Return the list of public methods of the Model service as an array

	Methods = Array( _
					&quot;CloseFile&quot; _
					, &quot;ReadAll&quot; _
					, &quot;readLine&quot; _
					, &quot;SkipLine&quot; _
					, &quot;WriteBlankLines&quot; _
					, &quot;WriteLine&quot; _
					)

End Function	&apos;	ScriptForge.SF_TextStream.Methods

REM -----------------------------------------------------------------------------
Public Function Properties() As Variant
&apos;&apos;&apos;	Return the list or properties of the Timer class as an array

	Properties = Array( _
					&quot;AtEndOfStream&quot; _
					, &quot;Encoding&quot; _
					, &quot;FileName&quot; _
					, &quot;IOMode&quot; _
					, &quot;Line&quot; _
					, &quot;NewLine&quot; _
					)

End Function	&apos;	ScriptForge.SF_TextStream.Properties

REM -----------------------------------------------------------------------------
Public Function ReadAll() As String
&apos;&apos;&apos; Returns all the remaining lines in the text stream as one string. Line breaks are NOT removed
&apos;&apos;&apos;	The resulting string can be split in lines
&apos;&apos;&apos;		either by using the usual Split Basic builtin function if the line delimiter is known
&apos;&apos;&apos;		or with the SF_String.SplitLines method
&apos;&apos;&apos;	For large files, using the ReadAll method wastes memory resources.
&apos;&apos;&apos;		Other techniques should be used to input a file, such as reading a file line-by-line
&apos;&apos;&apos;	Args:
&apos;&apos;&apos;	Returns:
&apos;&apos;&apos;		The read lines. The string may be empty.
&apos;&apos;&apos;		Note that the Line property in incremented only by 1
&apos;&apos;&apos;	Exceptions:
&apos;&apos;&apos;		FILENOTOPENERROR		File not open or already closed
&apos;&apos;&apos;		FILEOPENMODEERROR		File opened in write or append modes
&apos;&apos;&apos;		ENDOFFILEERROR			Previous reads already reached the end of the file
&apos;&apos;&apos;	Examples:
&apos;&apos;&apos;		Dim a As String
&apos;&apos;&apos;			a = myFile.ReadAll()

Dim sRead As String	   	    &apos;   Return value
Const cstThisSub = &quot;TextStream.ReadAll&quot;
Const cstSubArgs = &quot;&quot;

	If SF_Utils._ErrorHandling() Then On Local Error GoTo Catch
	sRead = &quot;&quot;

Check:
	If SF_Utils._EnterFunction(cstThisSub, cstSubArgs) Then
		If Not _IsFileOpen(&quot;READ&quot;) Then GoTo Finally
		If _InputStream.isEOF() Then GoTo CatchEOF
	End If

Try:
	sRead = _InputStream.readString(Array(), False)
	_LineNumber = _LineNumber + 1

Finally:
	ReadAll = sRead
	SF_Utils._ExitFunction(cstThisSub)
	Exit Function
Catch:
	GoTo Finally
CatchEOF:
	SF_Exception.RaiseFatal(ENDOFFILEERROR, FileName)
	GoTo Finally
End Function    &apos;   ScriptForge.SF_TextStream.ReadAll

REM -----------------------------------------------------------------------------
Public Function ReadLine() As String
&apos;&apos;&apos; Returns the next line in the text stream as a string. Line breaks are removed.
&apos;&apos;&apos;	Args:
&apos;&apos;&apos;	Returns:
&apos;&apos;&apos;		The read line. The string may be empty.
&apos;&apos;&apos;	Exceptions:
&apos;&apos;&apos;		FILENOTOPENERROR		File not open or already closed
&apos;&apos;&apos;		FILEOPENMODEERROR		File opened in write or append modes
&apos;&apos;&apos;		ENDOFFILEERROR			Previous reads already reached the end of the file
&apos;&apos;&apos;	Examples:
&apos;&apos;&apos;		Dim a As String
&apos;&apos;&apos;			a = myFile.ReadLine()

Dim sRead As String	   	    &apos;   Return value
Dim iRead As Integer		&apos;	Length of line break
Const cstThisSub = &quot;TextStream.ReadLine&quot;
Const cstSubArgs = &quot;&quot;

	If SF_Utils._ErrorHandling() Then On Local Error GoTo Catch
	sRead = &quot;&quot;

Check:
	If SF_Utils._EnterFunction(cstThisSub, cstSubArgs) Then
		If Not _IsFileOpen(&quot;READ&quot;) Then GoTo Finally
		If AtEndOfStream Then GoTo CatchEOF
	End If

Try:
	&apos;	When the text file ends with a line break,
	&apos;	XTextInputStream.readLine() returns the line break together with the last line
	&apos;	Hence the workaround to force a blank line at the end
	If _ForceBlankLine Then
		sRead = &quot;&quot;
		_ForceBlankLine = False
	Else
		sRead = _InputStream.readLine()
		&apos;	The isEOF() is set immediately after having read the last line
		If _InputStream.isEOF() And Len(sRead) &gt; 0 Then
			iRead = 0
			If SF_String.EndsWith(sRead, SF_String.sfCRLF) Then
				iRead = 2
			ElseIf SF_String.EndsWith(sRead, SF_String.sfLF) Or SF_String.EndsWith(sRead, SF_String.sfCR) Then
				iRead = 1
			End If
			If iRead &gt; 0 Then
				sRead = Left(sRead, Len(sRead) - iRead)
				_ForceBlankLine = True	&apos;	Provision for a last empty line at the next read loop
			End If
		End If
	End If
	_LineNumber = _LineNumber + 1

Finally:
	ReadLine = sRead
	SF_Utils._ExitFunction(cstThisSub)
	Exit Function
Catch:
	GoTo Finally
CatchEOF:
	SF_Exception.RaiseFatal(ENDOFFILEERROR, FileName)
	GoTo Finally
End Function    &apos;   ScriptForge.SF_TextStream.ReadLine

REM -----------------------------------------------------------------------------
Public Function SetProperty(Optional ByVal PropertyName As Variant _
								, Optional ByRef Value As Variant _
								) As Boolean
&apos;&apos;&apos;	Set a new value to the given property
&apos;&apos;&apos;	Args:
&apos;&apos;&apos;		PropertyName: the name of the property as a string
&apos;&apos;&apos;		Value: its new value
&apos;&apos;&apos;	Exceptions
&apos;&apos;&apos;		ARGUMENTERROR		The property does not exist

Dim bSet As Boolean			&apos;	Return value
Const cstThisSub = &quot;TextStream.SetProperty&quot;
Const cstSubArgs = &quot;PropertyName, Value&quot;

	If SF_Utils._ErrorHandling() Then On Local Error GoTo Catch
	bSet = False

Check:
	If SF_Utils._EnterFunction(cstThisSub, cstSubArgs) Then
		If Not SF_Utils._Validate(PropertyName, &quot;PropertyName&quot;, V_STRING, Properties()) Then GoTo Catch
	End If

Try:
	bSet = True
	Select Case UCase(PropertyName)
		Case &quot;NEWLINE&quot;
			If Not SF_Utils._Validate(Value, &quot;Value&quot;, V_STRING) Then GoTo Catch
			NewLine = Value
		Case Else
			bSet = False
	End Select

Finally:
	SetProperty = bSet
	SF_Utils._ExitFunction(cstThisSub)
	Exit Function
Catch:
	GoTo Finally
End Function	&apos;	ScriptForge.SF_TextStream.SetProperty

REM -----------------------------------------------------------------------------
Public Sub SkipLine()
&apos;&apos;&apos; Skips the next line when reading a TextStream file.
&apos;&apos;&apos;	Args:
&apos;&apos;&apos;	Exceptions:
&apos;&apos;&apos;		FILENOTOPENERROR		File not open or already closed
&apos;&apos;&apos;		FILEOPENMODEERROR		File opened in write or append modes
&apos;&apos;&apos;		ENDOFFILEERROR			Previous reads already reached the end of the file
&apos;&apos;&apos;	Examples:
&apos;&apos;&apos;		myFile.SkipLine()

Dim sRead As String	   	    &apos;   Read buffer
Const cstThisSub = &quot;TextStream.SkipLine&quot;
Const cstSubArgs = &quot;&quot;

	If SF_Utils._ErrorHandling() Then On Local Error GoTo Catch

Check:
	If SF_Utils._EnterFunction(cstThisSub, cstSubArgs) Then
		If Not _IsFileOpen(&quot;READ&quot;) Then GoTo Finally
		If Not _ForceBlankLine Then		&apos;	The file ends with a newline =&gt; return one empty line more
			If _InputStream.isEOF() Then GoTo CatchEOF
		End If
	End If

Try:
	sRead = ReadLine()

Finally:
	SF_Utils._ExitFunction(cstThisSub)
	Exit Sub
Catch:
	GoTo Finally
CatchEOF:
	SF_Exception.RaiseFatal(ENDOFFILEERROR, FileName)
	GoTo Finally
End Sub		    &apos;   ScriptForge.SF_TextStream.SkipLine

REM -----------------------------------------------------------------------------
Public Sub WriteBlankLines(Optional ByVal Lines As Variant)
&apos;&apos;&apos; Writes a number of empty lines in the output stream
&apos;&apos;&apos;	Args:
&apos;&apos;&apos;		Lines: the number of lines to write
&apos;&apos;&apos;	Returns:
&apos;&apos;&apos;	Exceptions:
&apos;&apos;&apos;		FILENOTOPENERROR		File not open or already closed
&apos;&apos;&apos;		FILEOPENMODEERROR		File opened in read mode
&apos;&apos;&apos;	Examples:
&apos;&apos;&apos;			myFile.WriteBlankLines(10)
Dim i As Long
Const cstThisSub = &quot;TextStream.WriteBlankLines&quot;
Const cstSubArgs = &quot;Lines&quot;

	If SF_Utils._ErrorHandling() Then On Local Error GoTo Catch

Check:
	If SF_Utils._EnterFunction(cstThisSub, cstSubArgs) Then
		If Not _IsFileOpen(&quot;WRITE&quot;) Then GoTo Finally
		If Not SF_Utils._Validate(Lines, &quot;Lines&quot;, V_NUMERIC) Then GoTo Finally
	End If

Try:
	For i = 1 To Lines
		_OutputStream.writeString(_NewLine)
	Next i
	_LineNumber = _LineNumber + Lines

Finally:
	SF_Utils._ExitFunction(cstThisSub)
	Exit Sub
Catch:
	GoTo Finally
End Sub		    &apos;   ScriptForge.SF_TextStream.WriteBlankLines

REM -----------------------------------------------------------------------------
Public Sub WriteLine(Optional ByVal Line As Variant)
&apos;&apos;&apos; Writes the given line to the output stream. A newline is inserted if relevant
&apos;&apos;&apos;	Args:
&apos;&apos;&apos;		Line: the line to write, may be empty
&apos;&apos;&apos;	Returns:
&apos;&apos;&apos;	Exceptions:
&apos;&apos;&apos;		FILENOTOPENERROR		File not open or already closed
&apos;&apos;&apos;		FILEOPENMODEERROR		File opened in in read mode
&apos;&apos;&apos;	Examples:
&apos;&apos;&apos;			myFile.WriteLine(&quot;Next line&quot;)
Dim i As Long
Const cstThisSub = &quot;TextStream.WriteLine&quot;
Const cstSubArgs = &quot;Line&quot;

	If SF_Utils._ErrorHandling() Then On Local Error GoTo Catch

Check:
	If SF_Utils._EnterFunction(cstThisSub, cstSubArgs) Then
		If Not _IsFileOpen(&quot;WRITE&quot;) Then GoTo Finally
		If Not SF_Utils._Validate(Line, &quot;Line&quot;, V_STRING) Then GoTo Finally
	End If

Try:
	_OutputStream.writeString(Iif(_LineNumber &gt; 0, _NewLine, &quot;&quot;) &amp; Line)
	_LineNumber = _LineNumber + 1

Finally:
	SF_Utils._ExitFunction(cstThisSub)
	Exit Sub
Catch:
	GoTo Finally
End Sub		    &apos;   ScriptForge.SF_TextStream.WriteLine

REM =========================================================== PRIVATE FUNCTIONS

REM -----------------------------------------------------------------------------
Public Sub _Initialize()
&apos;&apos;&apos;	Opens file and setup input and/or output streams (ForAppending requires both)

Dim oSfa As Object				&apos;	com.sun.star.ucb.SimpleFileAccess

	&apos;	Default newline related to current operating system
	_NewLine = SF_String.sfNEWLINE

	Set oSfa = SF_Utils._GetUNOService(&quot;FileAccess&quot;)

	&apos;	Setup input and/or output streams based on READ/WRITE/APPEND IO modes
	Select Case _IOMode
		Case SF_FileSystem.ForReading
			Set _FileHandler = oSfa.openFileRead(_FileName)
			Set _InputStream = CreateUnoService(&quot;com.sun.star.io.TextInputStream&quot;)
			_InputStream.setInputStream(_FileHandler)
		Case SF_FileSystem.ForWriting
			&apos;	Output file is deleted beforehand
			If _FileExists Then oSfa.kill(_FileName)
			Set _FileHandler = oSfa.openFileWrite(_FileName)
			Set _OutputStream = CreateUnoService(&quot;com.sun.star.io.TextOutputStream&quot;)
			_OutputStream.setOutputStream(_FileHandler)
		Case SF_FileSystem.ForAppending
			Set _FileHandler = oSfa.openFileReadWrite(_FileName)
			Set _InputStream = CreateUnoService(&quot;com.sun.star.io.TextInputStream&quot;)
			Set _OutputStream = CreateUnoService(&quot;com.sun.star.io.TextOutputStream&quot;)
			_InputStream.setInputStream(_FileHandler)
			&apos;	Position at end of file: Skip and count existing lines
			_LineNumber = 0
			Do While Not _InputStream.isEOF()
				_InputStream.readLine()
				_LineNumber = _LineNumber + 1
			Loop
			_OutputStream.setOutputStream(_FileHandler)
	End Select

	If _Encoding = &quot;&quot; Then _Encoding = &quot;UTF-8&quot;
	If Not IsNull(_InputStream) Then _InputStream.setEncoding(_Encoding)
	If Not IsNull(_OutputStream) Then _OutputStream.setEncoding(_Encoding)

End Sub			&apos;	ScriptForge.SF_TextStream._Initialize

REM -----------------------------------------------------------------------------
Private Function _IsFileOpen(Optional ByVal psMode As String) As Boolean
&apos;&apos;&apos;	Checks if file is open with the right mode (READ or WRITE)
&apos;&apos;&apos;	Raises an exception if the file is not open at all or not in the right mode
&apos;&apos;&apos;	Args:
&apos;&apos;&apos;		psMode: READ or WRITE or zero-length string
&apos;&apos;&apos;	Exceptions:
&apos;&apos;&apos;		FILENOTOPENERROR		File not open or already closed
&apos;&apos;&apos;		FILEOPENMODEERROR		File opened in incompatible mode

	_IsFileOpen = False
	If IsMissing(psMode) Then psMode = &quot;&quot;
	If IsNull(_InputStream) And IsNull(_OutputStream) Then GoTo CatchNotOpen
	Select Case psMode
		Case &quot;READ&quot;
			If IsNull(_InputStream) Then GoTo CatchOpenMode
			If _IOMode &lt;&gt; SF_FileSystem.ForReading Then GoTo CatchOpenMode
		Case &quot;WRITE&quot;
			If IsNull(_OutputStream) Then GoTo CatchOpenMode
			If _IOMode = SF_FileSystem.ForReading Then GoTo CatchOpenMode
		Case Else
	End Select
	_IsFileOpen = True

Finally:
	Exit Function
CatchNotOpen:
	SF_Exception.RaiseFatal(FILENOTOPENERROR, FileName)
	GoTo Finally
CatchOpenMode:
	SF_Exception.RaiseFatal(FILEOPENMODEERROR, FileName, IOMode)
	GoTo Finally
End Function	&apos;	ScriptForge.SF_TextStream._IsFileOpen

REM -----------------------------------------------------------------------------
Private Function _PropertyGet(Optional ByVal psProperty As String)
&apos;&apos;&apos;	Return the value of the named property
&apos;&apos;&apos;	Args:
&apos;&apos;&apos;		psProperty: the name of the property

Dim cstThisSub As String
Dim cstSubArgs As String

	cstThisSub = &quot;TextStream.get&quot; &amp; psProperty
	cstSubArgs = &quot;&quot;
	SF_Utils._EnterFunction(cstThisSub, cstSubArgs)

	Select Case UCase(psProperty)
		Case UCase(&quot;AtEndOfStream&quot;)
			Select Case _IOMode
				Case SF_FileSystem.ForReading
					If IsNull(_InputStream) Then _PropertyGet = True Else _PropertyGet = CBool(_InputStream.isEOF() And Not _ForceBlankLine)
				Case Else	:	_PropertyGet = True
			End Select
		Case UCase(&quot;Encoding&quot;)
			_PropertyGet = _Encoding
		Case UCase(&quot;FileName&quot;)
			_PropertyGet = SF_FileSystem._ConvertFromUrl(_FileName)		&apos; Depends on FileNaming
		Case UCase(&quot;IOMode&quot;)
			With SF_FileSystem
				Select Case _IOMode
					Case .ForReading		: _PropertyGet = &quot;READ&quot;
					Case .ForWriting		: _PropertyGet = &quot;WRITE&quot;
					Case .ForAppending		: _PropertyGet = &quot;APPEND&quot;
					Case Else				: _PropertyGet = &quot;&quot;
				End Select
			End With
		Case UCase(&quot;Line&quot;)
			_PropertyGet = _LineNumber
		Case UCase(&quot;NewLine&quot;)
			_PropertyGet = _NewLine
		Case Else
			_PropertyGet = Null
	End Select

Finally:
	SF_Utils._ExitFunction(cstThisSub)
	Exit Function
End Function	&apos;	ScriptForge.SF_TextStream._PropertyGet

REM -----------------------------------------------------------------------------
Private Function _Repr() As String
&apos;&apos;&apos;	Convert the TextStream instance to a readable string, typically for debugging purposes (DebugPrint ...)
&apos;&apos;&apos;	Args:
&apos;&apos;&apos;	Return:
&apos;&apos;&apos;		&quot;[TextStream]: File name, IOMode, LineNumber&quot;

	_Repr = &quot;[TextStream]: &quot; &amp; FileName &amp; &quot;,&quot; &amp; IOMode &amp; &quot;,&quot; &amp; CStr(Line)

End Function	&apos;	ScriptForge.SF_TextStream._Repr

REM ============================================ END OF SCRIPTFORGE.SF_TextStream
</script:module>