-
Notifications
You must be signed in to change notification settings - Fork 2
/
MLibrary.bas
1263 lines (1063 loc) · 32.4 KB
/
MLibrary.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
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
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
Attribute VB_Name = "MLibrary"
Option Compare Database
Option Explicit
Private Const HDRINFO_APPINFO As String = "appinfo"
Private Const HDRINFO_DATECREATED As String = "datecreated"
Private Const HDRINFO_GUID As String = "guid"
Private Const HDRINFO_AUTHOR As String = "author"
Private Const HDRINFO_COPYRIGHT As String = "copyright"
Private Const HDRINFO_DESCRIPTION As String = "description"
Public Const BLOCKTYPE_CROW As Integer = 1
Public Const BLOCKTYPE_BINARY As Integer = 10
Public Const BLOCKNAME_LIBHEADER As String = "\"
Public Const BLOCKNAME_LIBCUSTPROPS As String = ".customproperties"
'Bloc attributes
Public Const BA_DELETED As Long = 4&
Public Const BA_NONMOVEABLE As Long = 8&
Public Const BA_NONDELETEABLE As Long = 16&
Public Const HBLOCK_INVALID As Long = 0&
Public Const HLIB_INVALID As Long = 0&
Public Enum eBlockPointer
eNext = 0
ePrev = 1
End Enum
'Error context
Private mlErr As Long
Private msErr As String
Private msErrCtx As String
Private Sub ClearErr()
mlErr = 0&
msErr = ""
End Sub
Private Sub SetErr(ByVal psErrCtx As String, ByVal plErr As Long, ByVal psErr As String)
msErrCtx = psErrCtx
mlErr = plErr
msErr = psErr
End Sub
Public Function LibLastErr() As Long
LibLastErr = mlErr
End Function
Public Function LibLastErrDesc() As String
LibLastErrDesc = msErr
End Function
Public Function LibLastErrCtx() As String
LibLastErrCtx = msErrCtx
End Function
Private Sub AdvanceInt(ByVal phLib As Long)
Dim iDummy As Integer
Get #phLib, , iDummy
End Sub
Private Sub AdvanceLong(ByVal phLib As Long)
Dim lDummy As Long
Get #phLib, , lDummy
End Sub
Public Function OpenLibraryFile(ByVal psLibraryFile As String, phRetLib As Integer, Optional ByVal pfForWrite As Boolean = False) As Boolean
Const LOCAL_ERR_CTX As String = "OpenLibraryFile"
On Error GoTo OpenLibraryFile_Err
ClearErr
Dim fOK As Boolean
Dim hFile As Integer
phRetLib = HLIB_INVALID
hFile = FreeFile
'Always open in RW or failure when reopening same file on different mode
'If Not pfForWrite Then
' Open psLibraryFile For Binary Access Read Lock Read Write As #hFile
'Else
Open psLibraryFile For Binary Access Read Write Lock Read Write As #hFile
'End If
phRetLib = hFile
fOK = True
OpenLibraryFile_Exit:
OpenLibraryFile = fOK
If Not fOK Then
If hFile <> HLIB_INVALID Then
On Error Resume Next
Close #hFile
End If
End If
Exit Function
OpenLibraryFile_Err:
SetErr LOCAL_ERR_CTX, Err.Number, Err.Description
fOK = False
Resume OpenLibraryFile_Exit
Resume
End Function
Public Function CloseLibrary(ByVal phLib As Integer) As Boolean
On Error Resume Next
If phLib > HLIB_INVALID Then
Close #phLib
CloseLibrary = True
End If
End Function
Private Sub DefineHeaderRow(ByRef poRow As CRow)
poRow.ArrayDefine _
Array( _
HDRINFO_APPINFO, HDRINFO_DATECREATED, HDRINFO_GUID, HDRINFO_AUTHOR, _
HDRINFO_COPYRIGHT, HDRINFO_DESCRIPTION _
), _
Array( _
vbString, vbString, vbString, vbString, _
vbString, vbString _
)
End Sub
Public Function LibGetMaxBlockNameLen() As Integer
LibGetMaxBlockNameLen = 250
End Function
Private Function PadBlockName(ByVal psBlockName As String) As String
If Len(psBlockName) < LibGetMaxBlockNameLen() Then
psBlockName = psBlockName & Space$(LibGetMaxBlockNameLen() - Len(psBlockName))
End If
PadBlockName = psBlockName
End Function
'*************************
'
' Wrapping Blocks
'
'*************************
Private Function CreateBlock( _
ByVal phLib As Integer, _
ByVal psBlockName As String, _
ByVal phPrevBlock As Long, _
ByVal phNextBlock As Long, _
ByVal piBlockType As Integer, _
ByVal plAttribs As Long, _
Optional ByVal psTag As String = "" _
) As Long
Const LOCAL_ERR_CTX As String = "CreateBlock"
Dim lBlockSize As Long
Dim hNewBlock As Long
On Error GoTo CreateBlock_Err
ClearErr
hNewBlock = HBLOCK_INVALID
If Len(psBlockName) > LibGetMaxBlockNameLen() Then
SetErr LOCAL_ERR_CTX, -1&, "Block element name too long"
GoTo CreateBlock_Exit
End If
psBlockName = PadBlockName(psBlockName)
'old, INCORRECT: iBlockSize = 18& + Len(psBlockName) * 2 + Len(psTag) * 2 + 4 '2 is for the string length added by FilePutUnicodeString
lBlockSize = 0 ' blocksize is the len of data after the header
hNewBlock = Seek(phLib)
Put #phLib, , piBlockType
Put #phLib, , lBlockSize
Put #phLib, , phNextBlock
Put #phLib, , phPrevBlock
Put #phLib, , plAttribs
FilePutUnicodeString phLib, psBlockName, True
FilePutUnicodeString phLib, psTag, True
CreateBlock_Exit:
CreateBlock = hNewBlock
Exit Function
CreateBlock_Err:
SetErr LOCAL_ERR_CTX, Err.Number, Err.Description
hNewBlock = HBLOCK_INVALID
Resume CreateBlock_Exit
Resume
End Function
Private Sub SkipBlockHeader(ByVal phLib As Integer)
Dim lCurPos As Long
Dim sTag As String
AdvanceInt phLib 'block type
AdvanceLong phLib 'BlockSize
AdvanceLong phLib 'hNextBlock
AdvanceLong phLib 'hPrevBlock
AdvanceLong phLib 'lAttribs
'seek over blockname and tag
lCurPos = Seek(phLib)
Seek phLib, lCurPos + LibGetMaxBlockNameLen() * 2 + 2
sTag = FileGetUnicodeString(phLib)
End Sub
Public Function LibWriteBlockPointers( _
ByVal phLib As Integer, _
ByVal phBlock As Long, _
ByVal phPrevBlock As Long, _
ByVal phNextBlock As Long _
) As Boolean
Const LOCAL_ERR_CTX As String = "LibWriteBlockPointers"
Dim lCurPos As Long
On Error GoTo LibWriteBlockPointers_Err
ClearErr
lCurPos = Seek(phLib)
Seek #phLib, phBlock
AdvanceInt phLib 'iBlockType
AdvanceLong phLib 'lBlockSize
Put #phLib, , phNextBlock
Put #phLib, , phPrevBlock
Seek #phLib, lCurPos
LibWriteBlockPointers = True
LibWriteBlockPointers_Exit:
Exit Function
LibWriteBlockPointers_Err:
SetErr LOCAL_ERR_CTX, Err.Number, Err.Description
Resume LibWriteBlockPointers_Exit
End Function
Public Function LibReadBlockPointers( _
ByVal phLib As Integer, _
ByVal phBlock As Long, _
ByRef phRetPrevBlock As Long, _
ByRef phRetNextBlock As Long _
) As Boolean
Const LOCAL_ERR_CTX As String = "LibReadBlockPointers"
Dim lCurPos As Long
Dim iBlockType As Integer
Dim lBlockSize As Long
On Error GoTo LibReadBlockPointers_Err
ClearErr
phRetPrevBlock = HBLOCK_INVALID
phRetNextBlock = HBLOCK_INVALID
lCurPos = Seek(phLib)
Seek #phLib, phBlock
AdvanceInt phLib 'iBlockType
AdvanceLong phLib 'lBlockSize
Get #phLib, , phRetNextBlock
Get #phLib, , phRetPrevBlock
Seek #phLib, lCurPos
LibReadBlockPointers = True
LibReadBlockPointers_Exit:
Exit Function
LibReadBlockPointers_Err:
SetErr LOCAL_ERR_CTX, Err.Number, Err.Description
Resume LibReadBlockPointers_Exit
End Function
Public Function LibReadBlockPointer( _
ByVal phLib As Integer, _
ByVal phBlock As Long, _
ByRef phRethBlock As Long, _
ByVal peBlockPointer As eBlockPointer _
) As Boolean
Const LOCAL_ERR_CTX As String = "LibReadBlockPointer"
Dim lCurPos As Long
Dim iBlockType As Integer
Dim lBlockSize As Long
On Error GoTo LibReadBlockPointer_Err
ClearErr
phRethBlock = HBLOCK_INVALID
lCurPos = Seek(phLib)
Seek #phLib, phBlock
AdvanceInt phLib 'iBlockType
AdvanceLong phLib 'lBlockSize
If peBlockPointer = eNext Then
Get #phLib, , phRethBlock
Else
AdvanceLong phLib
Get #phLib, , phRethBlock
End If
Seek #phLib, lCurPos
LibReadBlockPointer = True
LibReadBlockPointer_Exit:
Exit Function
LibReadBlockPointer_Err:
SetErr LOCAL_ERR_CTX, Err.Number, Err.Description
Resume LibReadBlockPointer_Exit
End Function
Public Function LibWriteBlockAttributes( _
ByVal phLib As Integer, _
ByVal phBlock As Long, _
ByVal plAttributes As Long _
) As Boolean
Const LOCAL_ERR_CTX As String = "LibWriteBlockAttributes"
Dim lCurPos As Long
Dim iBlockType As Integer
Dim lBlockSize As Long
Dim hBlock As Long
On Error GoTo LibWriteBlockAttributes_Err
ClearErr
lCurPos = Seek(phLib)
Seek #phLib, phBlock
AdvanceInt phLib 'iBlockType
AdvanceLong phLib 'lBlockSize
AdvanceLong phLib 'next
AdvanceLong phLib 'prev
Put #phLib, , plAttributes
Seek #phLib, lCurPos
LibWriteBlockAttributes = True
LibWriteBlockAttributes_Exit:
Exit Function
LibWriteBlockAttributes_Err:
SetErr LOCAL_ERR_CTX, Err.Number, Err.Description
Resume LibWriteBlockAttributes_Exit
End Function
Public Function LibRenameBlock( _
ByVal phLib As Integer, _
ByVal phBlock As Long, _
ByVal psNewName As String _
) As Boolean
Const LOCAL_ERR_CTX As String = "LibRenameBlock"
Dim lCurPos As Long
Dim iBlockType As Integer
Dim lBlockSize As Long
Dim hBlock As Long
On Error GoTo LibRenameBlock_Err
ClearErr
psNewName = Trim$(psNewName)
If Len(psNewName) > LibGetMaxBlockNameLen() Then
SetErr LOCAL_ERR_CTX, -1&, "Block element name too long"
GoTo LibRenameBlock_Exit
End If
If (psNewName = BLOCKNAME_LIBHEADER) Or (psNewName = BLOCKNAME_LIBCUSTPROPS) Then
SetErr LOCAL_ERR_CTX, -2&, "Invalid block name"
GoTo LibRenameBlock_Exit
End If
lCurPos = Seek(phLib)
Seek #phLib, phBlock
AdvanceInt phLib 'iBlockType
AdvanceLong phLib 'lBlockSize
AdvanceLong phLib 'next
AdvanceLong phLib 'prev
AdvanceLong phLib 'attribs
FilePutUnicodeString phLib, psNewName, True
Seek #phLib, lCurPos
LibRenameBlock = True
LibRenameBlock_Exit:
Exit Function
LibRenameBlock_Err:
SetErr LOCAL_ERR_CTX, Err.Number, Err.Description
Resume LibRenameBlock_Exit
End Function
Public Function LibReadBlockAttributes( _
ByVal phLib As Integer, _
ByVal phBlock As Long, _
ByRef plRetAttribs As Long _
) As Boolean
Const LOCAL_ERR_CTX As String = "LibReadBlockAttributes"
Dim lCurPos As Long
Dim iBlockType As Integer
Dim lBlockSize As Long
Dim hBlock As Long
On Error GoTo LibReadBlockAttributes_Err
ClearErr
plRetAttribs = 0&
lCurPos = Seek(phLib)
Seek #phLib, phBlock
AdvanceInt phLib 'iBlockType
AdvanceLong phLib 'lBlockSize
AdvanceLong phLib 'next
AdvanceLong phLib 'prev
Get #phLib, , plRetAttribs
Seek #phLib, lCurPos
LibReadBlockAttributes = True
LibReadBlockAttributes_Exit:
Exit Function
LibReadBlockAttributes_Err:
SetErr LOCAL_ERR_CTX, Err.Number, Err.Description
Resume LibReadBlockAttributes_Exit
End Function
Private Function WriteBlockClose(ByVal phLib As Integer, ByVal phBlock As Long, ByVal plDataStartPos As Long) As Boolean
Const LOCAL_ERR_CTX As String = "WriteBlockClose"
Dim lPos As Long
Dim lBlockSize As Long
Dim iBlockType As Integer
On Error GoTo WriteBlockClose_Err
ClearErr
lPos = Seek(phLib)
lBlockSize = lPos - plDataStartPos
Seek #phLib, phBlock
AdvanceInt phLib 'iBlockType
Put #phLib, , lBlockSize
Seek #phLib, lPos
WriteBlockClose = True
WriteBlockClose_Exit:
Exit Function
WriteBlockClose_Err:
SetErr LOCAL_ERR_CTX, Err.Number, Err.Description
Resume WriteBlockClose_Exit
End Function
Public Function LibReadBlockHeader( _
ByVal phLib As Integer, _
ByVal phBlock As Long, _
ByRef piRetBlockType As Integer, _
ByRef plRetBlockSize As Long, _
ByRef phRetNextBlock As Long, _
ByRef phRetPrevBlock As Long, _
ByRef plRetAttribs As Long, _
ByRef psBlockName As String, _
ByRef psTag As String _
) As Boolean
Const LOCAL_ERR_CTX As String = "LibReadBlockHeader"
'NO, dont move back: Dim lCurPos As Long
Dim iBlockType As Integer
Dim lBlockSize As Long
On Error GoTo LibReadBlockHeader_Err
ClearErr
piRetBlockType = 0&
plRetBlockSize = 0&
phRetNextBlock = HBLOCK_INVALID
phRetPrevBlock = HBLOCK_INVALID
plRetAttribs = 0&
psBlockName = ""
psTag = ""
'NO, dont move back: lCurPos = Seek(phLib)
Seek #phLib, phBlock
Get #phLib, , piRetBlockType
Get #phLib, , plRetBlockSize
Get #phLib, , phRetNextBlock
Get #phLib, , phRetPrevBlock
Get #phLib, , plRetAttribs
psBlockName = Trim$(FileGetUnicodeString(phLib))
psTag = FileGetUnicodeString(phLib)
'NO, dont move back: Seek #phLib, lCurPos
LibReadBlockHeader = True
LibReadBlockHeader_Exit:
Exit Function
LibReadBlockHeader_Err:
SetErr LOCAL_ERR_CTX, Err.Number, Err.Description
Resume LibReadBlockHeader_Exit
End Function
Public Function LibIsValidBlockHandle(ByVal phLib As Integer, ByVal phBlock As Long) As Boolean
On Error GoTo LibIsValidBlockHandle_Err
LibIsValidBlockHandle = CBool((phBlock <> HBLOCK_INVALID) And (phBlock <= LOF(phLib)))
Exit Function
LibIsValidBlockHandle_Err:
End Function
'*************************
'
' CRow blocks
'
'*************************
Public Function LibCreateCRowBlock( _
ByVal phLib As Integer, _
ByVal psBlockName As String, _
ByRef prowData As CRow, _
Optional ByVal plAttribs As Long = 0& _
) As Long
Const LOCAL_ERR_CTX As String = "LibCreateCRowBlock"
On Error GoTo LibCreateCRowBlock_Err
ClearErr
Dim hBlock As Long
Dim iPairCt As Integer
Dim iPair As Integer
Dim fOK As Boolean
Dim lBlockDataStartPos As Long
'go to end of lib file
Seek #phLib, LOF(phLib) + 1&
hBlock = CreateBlock(phLib, psBlockName, HBLOCK_INVALID, HBLOCK_INVALID, BLOCKTYPE_CROW, plAttribs)
If hBlock = HBLOCK_INVALID Then
GoTo LibCreateCRowBlock_Exit
End If
lBlockDataStartPos = Seek(phLib)
iPairCt = prowData.ColCount
Put #phLib, , iPairCt
For iPair = 1 To iPairCt
FilePutUnicodeString phLib, prowData.ColName(iPair)
FilePutUnicodeString phLib, prowData.ColValue(iPair)
Next iPair
fOK = WriteBlockClose(phLib, hBlock, lBlockDataStartPos)
If Not fOK Then
hBlock = HBLOCK_INVALID
End If
LibCreateCRowBlock_Exit:
LibCreateCRowBlock = hBlock
Exit Function
LibCreateCRowBlock_Err:
SetErr LOCAL_ERR_CTX, Err.Number, Err.Description
Resume LibCreateCRowBlock_Exit
Resume
End Function
Public Function ReadCRowBlock( _
ByVal phLib As Integer, _
ByVal phBlock As Long, _
ByRef poRetNewRow As CRow _
) As Boolean
Const LOCAL_ERR_CTX As String = "ReadCRowBlock"
On Error GoTo ReadCRowBlock_Err
ClearErr
Dim sValue As String
Dim rowData As CRow
Dim iKeyPairCt As Integer
Dim i As Integer
Dim sColName As String
Dim lCurPos As Long
Set poRetNewRow = Nothing
Set rowData = New CRow
lCurPos = Seek(phLib)
Seek #phLib, phBlock
SkipBlockHeader phLib
Get #phLib, , iKeyPairCt
For i = 1 To iKeyPairCt
sColName = FileGetUnicodeString(phLib)
sValue = FileGetUnicodeString(phLib)
'If we have a duplicate key, we append the content
'to the existing column
If Not rowData.ColExists(sColName) Then
rowData.AddCol sColName, sValue, Len(sValue), 0&
Else
rowData(sColName) = rowData(sColName) & sValue
End If
Next i
Set poRetNewRow = rowData
ReadCRowBlock = True
ReadCRowBlock_Exit:
On Error Resume Next
If lCurPos > 0& Then
Seek #phLib, lCurPos
End If
Set rowData = Nothing
Exit Function
ReadCRowBlock_Err:
SetErr LOCAL_ERR_CTX, Err.Number, Err.Description
Resume ReadCRowBlock_Exit
Resume
End Function
Public Function LibGotoBlock(ByVal phLib As Integer, ByVal phBlock As Long) As Long
Const LOCAL_ERR_CTX As String = "LibGotoBlock"
On Error GoTo LibGotoBlock_Err
ClearErr
Dim lCurPos As Long
Seek #phLib, phBlock
lCurPos = Seek(phLib)
LibGotoBlock = lCurPos
LibGotoBlock_Exit:
Exit Function
LibGotoBlock_Err:
SetErr LOCAL_ERR_CTX, Err.Number, Err.Description
Resume LibGotoBlock_Exit
Resume
End Function
Public Function LibGotoNextBlock(ByVal phLib As Integer, ByVal phBlock As Long) As Long
Const LOCAL_ERR_CTX As String = "LibGotoNextBlock"
On Error GoTo LibGotoNextBlock_Err
ClearErr
Dim hNextBlock As Long
Dim fOK As Boolean
fOK = LibReadBlockPointer(phLib, phBlock, hNextBlock, eNext)
If fOK Then
LibGotoNextBlock = LibGotoBlock(phLib, hNextBlock)
Else
LibGotoNextBlock = HBLOCK_INVALID
End If
LibGotoNextBlock_Exit:
Exit Function
LibGotoNextBlock_Err:
SetErr LOCAL_ERR_CTX, Err.Number, Err.Description
Resume LibGotoNextBlock_Exit
Resume
End Function
'*************************
'
' Library header
'
'*************************
Private Function LibWriteHeader( _
ByVal phLib As Integer, _
ByVal psAuthorName As String, _
ByVal psCopyright As String, _
ByVal psDescription As String, _
ByRef prowCustomProps As CRow _
) As Long
Const LOCAL_ERR_CTX As String = "LibWriteHeader"
On Error GoTo LibWriteHeader_Err
ClearErr
Dim rowInfo As CRow
Dim hBlock As Long
Dim hBlock2 As Long
Dim fOK As Boolean
Set rowInfo = New CRow
DefineHeaderRow rowInfo
rowInfo(HDRINFO_APPINFO) = APP_NAME & " v" & APP_VERSION
rowInfo(HDRINFO_DATECREATED) = Format$(Now, "yyyymmddhh:mm:ss")
rowInfo(HDRINFO_GUID) = CreateGUID()
rowInfo(HDRINFO_AUTHOR) = psAuthorName
rowInfo(HDRINFO_COPYRIGHT) = psCopyright
rowInfo(HDRINFO_DESCRIPTION) = psDescription
fOK = True
hBlock = LibCreateCRowBlock(phLib, BLOCKNAME_LIBHEADER, rowInfo, BA_NONMOVEABLE Or BA_NONDELETEABLE)
If hBlock > HBLOCK_INVALID Then
'Link the two blocks
If Not prowCustomProps Is Nothing Then
hBlock2 = LibCreateCRowBlock(phLib, BLOCKNAME_LIBCUSTPROPS, prowCustomProps)
fOK = LibWriteBlockPointers(phLib, hBlock2, hBlock, HBLOCK_INVALID)
End If
If fOK Then
fOK = LibWriteBlockPointers(phLib, hBlock, HBLOCK_INVALID, hBlock2)
If Not fOK Then
hBlock = HBLOCK_INVALID
GoTo LibWriteHeader_Exit
End If
Else
hBlock = HBLOCK_INVALID
GoTo LibWriteHeader_Exit
End If
End If
LibWriteHeader = hBlock
LibWriteHeader_Exit:
Exit Function
LibWriteHeader_Err:
SetErr LOCAL_ERR_CTX, Err.Number, Err.Description
Resume LibWriteHeader_Exit
End Function
Public Function LibReadHeader( _
ByVal phLib As Integer, _
ByRef poRetNewRow As CRow, _
ByRef prowRetNewCustProps As CRow, _
Optional ByVal pfReadCustProps As Boolean = True _
) As Boolean
Const LOCAL_ERR_CTX As String = "LibReadHeader"
On Error GoTo LibReadHeader_Err
ClearErr
Dim sValue As String
Dim rowData As CRow
Dim fOK As Boolean
Dim hNextBlock As Long
Set poRetNewRow = Nothing
Set prowRetNewCustProps = Nothing
'The lib header is the CRow block at position 1
fOK = ReadCRowBlock(phLib, 1&, poRetNewRow)
If pfReadCustProps Then
fOK = LibReadBlockPointer(phLib, 1&, hNextBlock, eNext)
If fOK Then
fOK = ReadCRowBlock(phLib, hNextBlock, prowRetNewCustProps)
End If
End If
LibReadHeader = fOK
LibReadHeader_Exit:
Set rowData = Nothing
Exit Function
LibReadHeader_Err:
SetErr LOCAL_ERR_CTX, Err.Number, Err.Description
Resume LibReadHeader_Exit
Resume
End Function
Public Function LibCreateLibrary( _
ByVal psLibraryFile As String, _
ByVal psAuthorName As String, _
ByVal psCopyright As String, _
ByVal psDescription As String, _
ByRef prowCustomProps As CRow _
) As Boolean
Const LOCAL_ERR_CTX As String = "LibCreateLibrary"
On Error GoTo LibCreateLibrary_Err
ClearErr
Dim fIsOpen As Boolean
Dim fOK As Boolean
Dim hLibFile As Integer
If ExistFile(psLibraryFile) Then
SetErr LOCAL_ERR_CTX, -1&, "Library file already exists"
GoTo LibCreateLibrary_Exit
End If
hLibFile = FreeFile
Open psLibraryFile For Output Access Write Lock Read Write As #hLibFile
Close hLibFile
hLibFile = HLIB_INVALID
fOK = OpenLibraryFile(psLibraryFile, hLibFile, True)
If Not fOK Then
GoTo LibCreateLibrary_Exit
End If
fOK = LibWriteHeader(hLibFile, psAuthorName, psCopyright, psDescription, prowCustomProps)
CloseLibrary hLibFile
hLibFile = HLIB_INVALID
LibCreateLibrary = fOK
LibCreateLibrary_Exit:
On Error Resume Next
If hLibFile > HLIB_INVALID Then
CloseLibrary hLibFile
End If
Exit Function
LibCreateLibrary_Err:
SetErr LOCAL_ERR_CTX, Err.Number, Err.Description
Resume LibCreateLibrary_Exit
Resume
End Function
'*************************
'
' Add content
'
'*************************
Public Function IsValidBlockName(psBlockName) As Boolean
IsValidBlockName = CBool( _
(Len(psBlockName) > 0) And _
(psBlockName <> BLOCKNAME_LIBHEADER) And _
(psBlockName <> BLOCKNAME_LIBCUSTPROPS) _
)
End Function
'returns hBlock
Public Function LibAddFile( _
ByVal phLib As Long, _
ByVal psBlockName As String, _
ByVal psFilename As String, _
ByRef prowCustomProps As CRow _
) As Long
Const LOCAL_ERR_CTX As String = "LibAddFile"
On Error GoTo LibAddFile_Err
ClearErr
Dim abData() As Byte
Dim hFile As Integer
Dim fIsOpen As Boolean
Dim fOK As Boolean
Dim hBlock As Long
Dim hBlock2 As Long
Dim lBlockDataStartPos As Long
If Not IsValidBlockName(psBlockName) Then
SetErr LOCAL_ERR_CTX, -1&, "[" & psBlockName & "] is not a valid block name"
LibAddFile = False
GoTo LibAddFile_Exit
End If
hFile = FreeFile
Open psFilename For Binary Access Read As #hFile
fIsOpen = True
'read all the bytes
ReDim abData(1 To LOF(hFile)) As Byte
Get #hFile, , abData
Close hFile
fIsOpen = False
'go to end of lib file
Seek #phLib, LOF(phLib) + 1&
'create binary block
hBlock = CreateBlock(phLib, psBlockName, HBLOCK_INVALID, HBLOCK_INVALID, BLOCKTYPE_BINARY, 0&)
If hBlock <> HBLOCK_INVALID Then
lBlockDataStartPos = Seek(phLib)
Put #phLib, , abData
fOK = WriteBlockClose(phLib, hBlock, lBlockDataStartPos)
If fOK Then
'write custom props and link the blocks
If Not prowCustomProps Is Nothing Then
hBlock2 = LibCreateCRowBlock(phLib, BLOCKNAME_LIBCUSTPROPS, prowCustomProps)
fOK = LibWriteBlockPointers(phLib, hBlock2, hBlock, HBLOCK_INVALID)
End If
If fOK Then
fOK = LibWriteBlockPointers(phLib, hBlock, HBLOCK_INVALID, hBlock2)
If Not fOK Then
hBlock = HBLOCK_INVALID
GoTo LibAddFile_Exit
End If
Else
hBlock = HBLOCK_INVALID
GoTo LibAddFile_Exit
End If
Else
LibAddFile = HBLOCK_INVALID
GoTo LibAddFile_Exit
End If
Else
LibAddFile = HBLOCK_INVALID
GoTo LibAddFile_Exit
End If
LibAddFile = hBlock
LibAddFile_Exit:
On Error Resume Next
If fIsOpen Then
Close #hFile
End If
Exit Function
LibAddFile_Err:
SetErr LOCAL_ERR_CTX, Err.Number, Err.Description
Resume LibAddFile_Exit
Resume
End Function
Private Function ExportCRowBlock(ByVal phFile As Integer, ByRef prowData As CRow) As Boolean
Const LOCAL_ERR_CTX As String = "ExportCRowBlock"
On Error GoTo ExportCRowBlock_Err
Dim fOK As Boolean
Dim i As Integer
ClearErr
If prowData Is Nothing Then
fOK = True
GoTo ExportCRowBlock_Exit
End If
For i = 1 To prowData.ColCount
FilePutUnicodeString phFile, prowData.ColName(i) & "=" & prowData(i) & vbCrLf, False
Next i
fOK = True
ExportCRowBlock_Exit:
ExportCRowBlock = fOK
Exit Function
ExportCRowBlock_Err:
SetErr LOCAL_ERR_CTX, Err.Number, Err.Description
Resume ExportCRowBlock_Exit
Resume
End Function
'*************************
'
' Directory
'
'*************************
Private Sub DefineDirectoryList(plstDir As CList)
plstDir.ArrayDefine Array( _
"hblock", _
"blocktype", _
"blocksize", _
"nextsibling", _
"prevsibling", _
"attribs", _
"blockname", _
"tag" _
), Array( _
vbLong, _
vbInteger, _
vbLong, _
vbLong, _
vbLong, _
vbLong, _
vbString, _
vbString _
)
End Sub
Public Function LibLoadDirectory( _
ByVal phLib As Integer, _
ByRef plstRetNewDir As CList _
) As Boolean
Const LOCAL_ERR_CTX As String = "LibLoadDirectory"
On Error GoTo LibLoadDirectory_Err
ClearErr
Dim lstDir As CList
Dim fOK As Boolean
Dim rowHeader As CRow
Dim i As Integer
Dim rowCustProps As New CRow
Dim hBlock As Long
Dim iBlockType As Integer
Dim lBlockSize As Long
Dim hNextSibling As Long
Dim hPrevSibling As Long
Dim hNextBlock As Long
Dim lAttribs As Long
Dim sBlockName As String
Dim sTag As String
Dim iLinkedBlockCt As Integer
Dim iFind As Long
'Read library header (but not the custom props)
fOK = LibReadHeader(phLib, rowHeader, Nothing, False)
If Not fOK Then
GoTo LibLoadDirectory_Exit
End If
Set lstDir = New CList
DefineDirectoryList lstDir
hBlock = 1&
Do While LibIsValidBlockHandle(phLib, hBlock)
iFind = lstDir.Find("hblock", hBlock)
If iFind > 0 Then
'If we're reading the same block twice, then there's a block chaining error somewhere
'and we can only stop reading the affected library.
SetErr LOCAL_ERR_CTX, -1&, "Reading same block #" & hBlock & " twice. Library integrity is broken"
GoTo LibLoadDirectory_Exit
End If
fOK = LibReadBlockHeader(phLib, hBlock, iBlockType, lBlockSize, hNextSibling, hPrevSibling, lAttribs, sBlockName, sTag)
If Not fOK Then
GoTo LibLoadDirectory_Exit
End If
'Jump over block data
hNextBlock = Seek(phLib) + lBlockSize
'we list only "parent" blocks, blocks that not chained or first