-
Notifications
You must be signed in to change notification settings - Fork 2
/
DBFReadWrite.PS1
924 lines (786 loc) · 34.5 KB
/
DBFReadWrite.PS1
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
# encountered problems: returning arrays of objects if one statement writes something to the console ==> return just one value!
# everything looks right but FileStream.Read returns always the default values of the buffer ==> set the datatype to bytearray! [System.Byte[]]
# codepages ==> use System.Text.Encoding with the right codepage, i.e. 850 or 437
# empty date fields ==> [System.DateTime]::Minvalue
# converting hashset to object ==> $o = new-object PSObject -Property $h.Fields
# returns the type of the dbf-file (dBaseIII, dBaseIV, FoxPro) and memofile-info (DBT, FPT)
function Test-DBF {
[CmdletBinding()]
param(
[Parameter(Mandatory=$true,
ValueFromPipeline=$true,
HelpMessage="Provide DBF-filename")]
[string]$DBFFilename )
$DBFFileHandle = [System.IO.File]::Open($DBFFilename,
[System.IO.FileMode]::Open,
[System.IO.FileAccess]::Read,
[System.IO.FileShare]::ReadWrite)
$DBFType = $DBFFileHandle.ReadByte()
$DBFObject = _NewDBFObject -DbfType $DBFType
$DBFFileHandle.Close()
# determining memofield blocksize when FPT-memos
if ($DBFObject.hasMemoFile -and $DBFObject.memoExt -eq "FPT")
{
$DBFFileHandle = [System.IO.File]::Open([System.IO.Path]::ChangeExtension($DBFFilename, $DBFObject.memoExt),
[System.IO.FileMode]::Open,
[System.IO.FileAccess]::Read,
[System.IO.FileShare]::ReadWrite)
[System.Byte[]] $Buffer = @(0x00) * 2
$Position = $DBFFileHandle.Seek(6, [System.IO.SeekOrigin]::Begin)
$BytesRead = $DBFFileHandle.Read($Buffer, 0, 2)
$DBFObject.memoBlockSize = $Buffer[1]+$Buffer[0]*256
$DBFFileHandle.Close()
}
return $DBFObject
}
# returns header data and field definitions of the specified dbf-file
function Get-DBFStructure () {
[CmdletBinding()]
param(
[Parameter(Mandatory=$true,
ValueFromPipeline=$true,
HelpMessage="Provide DBF-filename")]
[string]$DBFFilename
)
$DBFFileHandle = [System.IO.File]::Open($DBFFilename,
[System.IO.FileMode]::Open,
[System.IO.FileAccess]::Read,
[System.IO.FileShare]::ReadWrite)
$DBFField = New-Object PSObject
$DBFStructure = New-Object PSObject
$DBFStructure | Add-Member -Type NoteProperty -Name LastUpdate -Value 0
$DBFStructure | Add-Member -Type NoteProperty -Name DataOffset -Value 0
$DBFStructure | Add-Member -Type NoteProperty -Name RecordSize -Value 0
$DBFStructure | Add-Member -Type NoteProperty -Name NumRecords -Value 0
$DBFStructure | Add-Member -Type NoteProperty -Name NumFields -Value 0
#$DBFStructure | Add-Member -Type NoteProperty -Name Fields -Value []$DBFFields
$DBFStructure | Add-Member -Type NoteProperty -Name Fields -Value @()
$DBFStructure | Add-Member -Type NoteProperty -Name Encoding -Value @()
# PropertySet: http://mow001.blogspot.de/2006/01/some-fun-with-monads-add-member-mp3.html
# to ask for propertySet use select from the returned object: $x | select HeaderInfo
$DBFStructure | Add-Member -Type PropertySet -Name HeaderInfo -Value ([string[]]("LastUpdate", "DataOffset", "RecordSize", "NumRecords", "NumFields"))
[System.Byte[]] $NumTemp = @(0x00) * 4
# reading number of records
$Position = $DBFFileHandle.Seek(4, [System.IO.SeekOrigin]::Begin)
$BytesRead = $DBFFileHandle.Read($NumTemp, 0, 4)
$DBFStructure.NumRecords = Bin2Int32 ($NumTemp)
$NumTemp = @(0x00) * 4
# reading length of header
$Position = $DBFFileHandle.Seek(8, [System.IO.SeekOrigin]::Begin)
$BytesRead = $DBFFileHandle.Read($NumTemp, 0, 2)
$DBFStructure.DataOffset = Bin2Int32 ($NumTemp)
$NumTemp = @(0x00) * 4
# reading length of record, including deleted byte
$Position = $DBFFileHandle.Seek(10, [System.IO.SeekOrigin]::Begin)
$BytesRead = $DBFFileHandle.Read($NumTemp, 0, 2)
$DBFStructure.RecordSize = Bin2Int32 ($NumTemp)
$NumTemp = @(0x00) * 4
# reading date of last update
$Position = $DBFFileHandle.Seek(1, [System.IO.SeekOrigin]::Begin)
$BytesRead = $DBFFileHandle.Read($NumTemp, 0, 3)
$Year=$NumTemp[0]+1900
$Month=$NumTemp[1]
$Day=$NumTemp[2]
$DBFStructure.LastUpdate = New-Object System.DateTime($Year,$Month,$Day)
$NumTemp = @(0x00) * 4
# reading language driver byte also known as encoding
$Position = $DBFFileHandle.Seek(29, [System.IO.SeekOrigin]::Begin)
$BytesRead = $DBFFileHandle.Read($NumTemp, 0, 1)
$DBFStructure.Encoding = ConvertLanguageDriver2CodePage -LanguageDriver (Bin2Int32 ($NumTemp))
# calculating number of fields, 32 bytes header, 2 bytes end of data and 32 bytes per field definition
$DBFStructure.NumFields = ($DBFStructure.DataOffset - 32 -2) / 32
# reading field defintions
$Position = $DBFFileHandle.Seek(32, [System.IO.SeekOrigin]::Begin)
$FNr = 1
$Encode = [System.Text.Encoding]::GetEncoding(850)
$FieldPos = 0
while($FNr -le $DBFStructure.NumFields)
{
$NumTemp = @(0x00) * 32
# reading field structure, per field 32bytes
$BytesRead = $DBFFileHandle.Read($NumTemp, 0, 32)
$DBFFieldDef = New-Object PSObject
$DBFFieldDef | Add-Member -Type NoteProperty -Name Nr -Value 1
$DBFFieldDef | Add-Member -Type NoteProperty -Name Name -Value "Name"
$DBFFieldDef | Add-Member -Type NoteProperty -Name Type -Value "C"
$DBFFieldDef | Add-Member -Type NoteProperty -Name Length -Value 0
$DBFFieldDef | Add-Member -Type NoteProperty -Name Decimal -Value 0
$DBFFieldDef | Add-Member -Type NoteProperty -Name FieldPos -Value 0
$DBFFieldDef.Nr = $FNr
$ZeroIndex=[array]::IndexOf($NumTemp, [byte]0)
If (($ZeroIndex -eq -1) -or ($ZeroIndex -gt 9)) {$ZeroIndex=9}
$DBFFieldDef.Name = $Encode.GetString($NumTemp[0..$ZeroIndex]).TrimEnd(0)
$DBFFieldDef.Type = $Encode.GetString($NumTemp[11])
$DBFFieldDef.FieldPos = $FieldPos
if ($DBFFieldDef.Type -eq "C")
{
# Clipper + FoxPro special case
$DBFFieldDef.Length = $NumTemp[16] + $NumTemp[17] * 256
$DBFFieldDef.Decimal = 0
}
else
{
$DBFFieldDef.Length = $NumTemp[16]
$DBFFieldDef.Decimal = $NumTemp[17]
}
$FieldPos += $DBFFieldDef.Length
$FNr++
$DBFStructure.Fields += $DBFFieldDef
}
$DBFFileHandle.Close()
$DBFStructure
}
function ConvertLanguageDriver2CodePage
{
Param ([int]$LanguageDriver)
# Details https://www.clicketyclick.dk/databases/xbase/format/dbf.html#DBF_NOTE_5_TARGET
switch ($LanguageDriver) {
1 {437}
2 {850}
3 {1252}
# UTF-8?
}
}
# returns integer from byte array
function Bin2Int16BE ()
{
[CmdletBinding()]
#TODO: set OutputType to UINT16
param([System.Byte[]] $Bytes)
return $Bytes[1]+$Bytes[0]*256 #attention BIG ENDIAN
}
# returns integer from byte array
function Bin2Int32 ()
{
[CmdletBinding()]
#TODO: set OutputType to UINT32
param([System.Byte[]] $Bytes)
return $Bytes[0]+$Bytes[1]*256+$Bytes[2]*256*256+$Bytes[3]*256*256*256
}
# returns byte array from integer
function Int322Bin ()
{
[CmdletBinding()]
Param([UInt32]$Value)
[System.Byte[]]$NumTemp=@(0x00)*4
$NumTemp[3] = [Math]::Floor($Value/(256*256*256))
$Rest = $Value % (256*256*256)
$NumTemp[2] = [Math]::Floor($Rest/(256*256))
$Rest = $Value % (256*256)
$NumTemp[1] = [Math]::Floor($Rest/(256))
$Rest = $Value % 256
$NumTemp[0] = $Rest
return $NumTemp
}
# returns byte array from integer
function Int322BinBE ()
{
[CmdletBinding()]
Param([UInt32]$Value)
[System.Byte[]]$NumTemp=@(0x00)*4
$NumTemp[0] = [Math]::Floor($Value/(256*256*256))
$Rest = $Value % (256*256*256)
$NumTemp[1] = [Math]::Floor($Rest/(256*256))
$Rest = $Value % (256*256)
$NumTemp[2] = [Math]::Floor($Rest/(256))
$Rest = $Value % 256
$NumTemp[3] = $Rest
return $NumTemp
}
# opens a DBF-file with active record pattern
function Use-DBF () {
[CmdletBinding()]
param(
[Parameter(Mandatory=$true,
ValueFromPipeline=$true,
HelpMessage="Provide DBF-filename")]
[string]$DBFFilename,
[Parameter(Mandatory=$false,
HelpMessage="Open DBF in ReadWrite-mode")]
[Switch]$ReadWrite=$false
)
$DBFInfo = New-Object PSObject
_AddPropertiesAndMethods $DBFInfo
$DBFInfo.Type = Test-DBF $DBFFilename
$DBFInfo.Header = Get-DBFStructure $DBFFilename
foreach ($field in $DBFInfo.Header.Fields)
{
# TODO: every field should get its own getter and setter function:
# $DBFInfo.Fields | Add-Member -MemberType ScriptProperty -Name "Fieldname" -Value {$this.Fieldbuffer[Fieldname]} -SecondValue {Param($Value);$Value;$this.Fieldbuffer[Fieldname]=$Value}
# there should also be a dirty flag if a field has changed
$DBFInfo.Fields[$field.Name] = "val" # TODO: check val, better empty
}
if ($ReadWrite)
{
$DBFInfo.FilehandleOfDBF = [System.IO.File]::Open($DBFFilename,
[System.IO.FileMode]::Open,
[System.IO.FileAccess]::ReadWrite,
[System.IO.FileShare]::ReadWrite)
}
else
{
$DBFInfo.FilehandleOfDBF = [System.IO.File]::Open($DBFFilename,
[System.IO.FileMode]::Open,
[System.IO.FileAccess]::Read,
[System.IO.FileShare]::ReadWrite)
}
if ($DBFInfo.Type.hasMemoFile)
{
$DBFInfo.FilehandleOfMemo = [System.IO.File]::Open([System.IO.Path]::ChangeExtension($DBFFilename, $DBFInfo.Type.MemoExt),
[System.IO.FileMode]::Open,
[System.IO.FileAccess]::Read,
[System.IO.FileShare]::ReadWrite)
}
$DBFInfo
}
# adds Properties and Methods to DBFInfo-Object
Function _AddPropertiesAndMethods {
Param(
$DBFInfo
)
$DBFInfo | Add-Member -Type NoteProperty -Name Header -Value $null
$DBFInfo | Add-Member -Type NoteProperty -Name Type -Value $null
$DBFInfo | Add-Member -Type NoteProperty -Name ReadOnly -Value ($ReadWrite -eq $false)
$DBFInfo | Add-Member -Type NoteProperty -Name RecNo -Value 0
$DBFInfo | Add-Member -Type NoteProperty -Name BOF -Value $true
$DBFInfo | Add-Member -Type NoteProperty -Name EOF -Value $true
$DBFInfo | Add-Member -Type NoteProperty -Name Deleted -Value $false
$DBFInfo | Add-Member -Type NoteProperty -Name Fields -Value @{}
$DBFInfo | Add-Member -Type NoteProperty -Name FilehandleOfDBF -Value $null
$DBFInfo | Add-Member -Type NoteProperty -Name FilehandleOfMemo -Value $null
$DBFInfo | Add-Member -Type ScriptMethod -Name GoTo -Value { param($recNo) _GoTo $this $recNo }
$DBFInfo | Add-Member -Type ScriptMethod -Name GoTop -Value { _GoTop $this }
$DBFInfo | Add-Member -Type ScriptMethod -Name GoBottom -Value { _GoBottom $this }
$DBFInfo | Add-Member -Type ScriptMethod -Name Close -Value { _CloseDBF $this }
$DBFInfo | Add-Member -Type ScriptMethod -Name Skip -Value { param($records) _Skip $this $records }
$DBFInfo | Add-Member -Type ScriptMethod -Name ListAll -Value { _ListAll $this }
$DBFInfo | Add-Member -Type ScriptMethod -Name ReadRecord -Value { param([Parameter(ValueFromPipeline=$true)]$recNo) _ReadRecord $this $recNo }
$DBFInfo | Add-Member -Type ScriptMethod -Name GetMemofieldInternals -Value { param($fieldname) _MemofieldInternals $this $fieldname }
$DBFInfo | Add-Member -Type ScriptMethod -Name AppendBlank -Value { _AppendBlank $this }
}
# creates an object about the type of DBF-file and if it has a memofile
Function _NewDBFObject {
Param(
[int]$DBFType
)
$DBFObject = New-Object PSObject
$DBFObject | Add-Member -Type NoteProperty -Name ID -Value 0
$DBFObject | Add-Member -Type NoteProperty -Name Type -Value ""
$DBFObject | Add-Member -Type NoteProperty -Name hasMemoFile -Value $false
$DBFObject | Add-Member -Type NoteProperty -Name memoExt -Value "DBT"
$DBFObject | Add-Member -Type NoteProperty -Name memoBlockSize -Value 512 # DBT Blocksize is always 512Bytes
$DBFObject.ID = $DBFType
switch ($DBFType)
{
3 {$DBFObject.Type = "DBF-file"}
131 {$DBFObject.Type = "DBF+DBT-files"; $DBFObject.hasMemoFile = $true}
139 {$DBFObject.Type = "DBF(dBase IV)+DBT-files"; $DBFObject.hasMemoFile = $true}
245 {$DBFObject.Type = "DBF+FPT-files"; $DBFObject.hasMemoFile = $true; $DBFObject.memoExt = "FPT"}
default {$DBFObject.Type = "unknown"}
}
$DBFObject
}
# create new field definition
Function New-Field {
[CmdletBinding()]
Param (
[String]$Name,
[Validateset('Character', 'Logical', 'Numeric', 'Memo', 'Date')]
[String]$Type,
[int]$Length,
[int]$Decimal
)
[PSCustomObject]@{
PSTypeName = "PSDBF.Field";
Name = $Name;
Type = $Type;
Length = $Length;
Decimal = $Decimal;
}
}
# opens a DBF-file with active record pattern
function New-DBF () {
[CmdletBinding()]
Param(
[Parameter(Mandatory=$true,
ValueFromPipeline=$true,
HelpMessage="Provide DBF-filename")]
[string]$DBFFilename,
[Parameter(Mandatory=$true,
HelpMessage="DBF-fields structure")]
[PSTypeName('PSDBF.Field')]
$Fields
)
$ReadWrite = $true
$DBFInfo = New-Object PSObject
_AddPropertiesAndMethods $DBFInfo
$hasMemofield = $false
foreach ($field in $Fields)
{
# set default values depending on type of field
switch ($field.type) {
'C' {$DBFInfo.Fields[$field.Name] = ""}
'D' {$DBFInfo.Fields[$field.Name] = (Get-Date).Date}
'L' {$DBFInfo.Fields[$field.Name] = $false}
'M' {$DBFInfo.Fields[$field.Name] = ""; $hasMemofield = $true}
'D' {$DBFInfo.Fields[$field.Name] = 0}
}
}
$DBFInfo.FilehandleOfDBF = [System.IO.File]::Open($DBFFilename,
[System.IO.FileMode]::CreateNew,
[System.IO.FileAccess]::ReadWrite,
[System.IO.FileShare]::ReadWrite)
# writing DBF-Header
If ($hasMemofield) {
# TODO: support DBT!
$DbfHeaderByte = 245
} else {
$DbfHeaderByte = 3
}
$DBFInfo.FileHandleOfDBF.WriteByte($DbfHeaderByte)
$DBFObject = _NewDBFObject -DBFType $DbfHeaderByte
# writing number of fields
# calculating number of fields, 32 bytes header, 2 bytes end of data and 32 bytes per field definition
$DBFStructure.NumFields = ($DBFStructure.DataOffset - 32 -2) / 32
# reading field defintions
$Position = $DBFFileHandle.Seek(32, [System.IO.SeekOrigin]::Begin)
$FNr = 1
$Encode = [System.Text.Encoding]::GetEncoding(850)
$FieldPos = 0
while($FNr -le $DBFStructure.NumFields)
{
$NumTemp = @(0x00) * 32
# reading field structure, per field 32bytes
$BytesRead = $DBFFileHandle.Read($NumTemp, 0, 32)
$DBFFieldDef = New-Object PSObject
$DBFFieldDef | Add-Member -Type NoteProperty -Name Nr -Value 1
$DBFFieldDef | Add-Member -Type NoteProperty -Name Name -Value "Name"
$DBFFieldDef | Add-Member -Type NoteProperty -Name Type -Value "C"
$DBFFieldDef | Add-Member -Type NoteProperty -Name Length -Value 0
$DBFFieldDef | Add-Member -Type NoteProperty -Name Decimal -Value 0
$DBFFieldDef | Add-Member -Type NoteProperty -Name FieldPos -Value 0
$DBFFieldDef.Nr = $FNr
$DBFFieldDef.Name = $Encode.GetString($NumTemp[0..9]).TrimEnd(0)
$DBFFieldDef.Type = $Encode.GetString($NumTemp[11])
$DBFFieldDef.FieldPos = $FieldPos
if ($DBFFieldDef.Type -eq "C")
{
# Clipper + FoxPro special case
$DBFFieldDef.Length = $NumTemp[16] + $NumTemp[17] * 256
$DBFFieldDef.Decimal = 0
}
else
{
$DBFFieldDef.Length = $NumTemp[16]
$DBFFieldDef.Decimal = $NumTemp[17]
}
$FieldPos += $DBFFieldDef.Length
$FNr++
$DBFStructure.Fields += $DBFFieldDef
}
$DBFInfo
}
# Standalone ZAP
function Zap_DBF () {
[CmdletBinding()]
param(
[Parameter(Mandatory=$true,
ValueFromPipeline=$true,
HelpMessage="Provide DBF-filename")]
[string]$DBFFilename
)
[System.Byte[]]$Numtemp=@() # important to set the type!
$Type = Test-DBF $DBFFilename
$Header = Get-DBFStructure $DBFFilename
$DBFFileHandle = [System.IO.File]::Open($DBFFilename,
[System.IO.FileMode]::Open,
[System.IO.FileAccess]::Write,
[System.IO.FileShare]::Write)
$RecordPos = $Header.DataOffset
$Position = $DBFFileHandle.Seek($RecordPos, [System.IO.SeekOrigin]::Begin)
$DBFFileHandle.SetLength($Position)
# to conform to dbase file handling:
$DBFFileHandle.WriteByte(0x1A)
# set RecordCount
$NumTemp=@(0x00)*4
$Position = $DBFFileHandle.Seek(4, [System.IO.SeekOrigin]::Begin)
$BytesWritten = $DBFFileHandle.Write($NumTemp, 0, 4)
# write modification date in header
$NumTemp=@(0x00)*3
$Date = Get-Date
$NumTemp[0] = $Date.Year -1900
$NumTemp[1] = $Date.Month
$NumTemp[2] = $Date.Day
$Position = $DBFFileHandle.Seek(1, [System.IO.SeekOrigin]::Begin)
$BytesWritten = $DBFFileHandle.Write($NumTemp, 0, 3)
$DBFFileHandle.Close()
if ($Type.hasMemoFile)
{
switch($Type.memoExt) {
"DBT" {throw "not yet implemented! DBT"}
"FPT" { $FPTFile = [System.IO.Path]::ChangeExtension($DBFFilename, $Type.MemoExt)
$FPTFileHandle = [System.IO.File]::Open($FptFile,
[System.IO.FileMode]::Open,
[System.IO.FileAccess]::ReadWrite,
[System.IO.FileShare]::ReadWrite)
$NumTemp = @(0x00)*2
$Position = $FPTFileHandle.Seek(6, [System.IO.SeekOrigin]::Begin)
$BytesRead = $FPTFileHandle.Read($NumTemp, 0, 2)
$BlockSize = Bin2Int16BE $Numtemp
$HeaderSize = 512
# Check if Flexfile3 is there!
$Flexfile3ID = "FlexFile3"
$NumTemp = @(0x00)*$Flexfile3ID.Length
$Position = $FPTFileHandle.Seek($HeaderSize, [System.IO.SeekOrigin]::Begin)
$BytesRead = $FPTFileHandle.Read($NumTemp, 0, $Flexfile3ID.Length)
If (([System.Text.Encoding]::ASCII.GetString($Numtemp)) -eq $Flexfile3ID) {
$HeaderSize = 1024
}
$NextBlock = $HeaderSize / $BlockSize
# set next available Block
$Position = $FPTFileHandle.Seek(0, [System.IO.SeekOrigin]::Begin)
$Numtemp = Int322BinBE $NextBlock
$FPTFileHandle.Write($Numtemp, 0, 4)
$Position = $FPTFileHandle.Seek($HeaderSize, [System.IO.SeekOrigin]::Begin)
$FPTFileHandle.SetLength($Position)
$FPTFileHandle.Close()
}
default {throw "not yet implemented! $($Type.memoExt)"}
}
}
}
# should be internal functions
function _AppendBlank ($DBFInfo)
{
[System.Byte[]]$Numtemp=@()
if ($DBFInfo.ReadOnly) { throw "file is readonly"} # $DbfInfo.Name}
$DBFFileHandle = $DBFInfo.FileHandleOfDBF
# append new Record
$RecordPos = $DBFInfo.Header.DataOffset + $DBFInfo.Header.NumRecords * $DBFInfo.Header.RecordSize
$Position = $DBFFileHandle.Seek($RecordPos, [System.IO.SeekOrigin]::Begin)
$EmptyRecord = [Byte[]]0x20*$DBFInfo.Header.RecordSize
$DBFFileHandle.Write($EmptyRecord, 0, $DBFInfo.Header.RecordSize)
# increase RecordCount
$NumTemp=@(0x00)*4
$Position = $DBFFileHandle.Seek(4, [System.IO.SeekOrigin]::Begin)
$BytesRead = $DBFFileHandle.Read($NumTemp, 0, 4)
$DBFInfo.Header.NumRecords = (Bin2Int32 $NumTemp) +1
$NumTemp = Int322Bin $DBFInfo.Header.NumRecords
$Position = $DBFFileHandle.Seek(4, [System.IO.SeekOrigin]::Begin)
$BytesWritten = $DBFFileHandle.Write($NumTemp, 0, 4)
# write modification date in header
$NumTemp=@(0x00)*3
$Date = Get-Date
$NumTemp[0] = $Date.Year -1900
$NumTemp[1] = $Date.Month
$NumTemp[2] = $Date.Day
$Position = $DBFFileHandle.Seek(1, [System.IO.SeekOrigin]::Begin)
$BytesWritten = $DBFFileHandle.Write($NumTemp, 0, 3)
$DBFInfo.Header.LastUpdate = $Date
$FlushToDisk = $True
$DBFFileHandle.Flush()
$DBFFileHandle.Flush($FlushToDisk)
# make the new record the current record
_GoBottom($DBFInfo)
}
function _CloseDBF ($DBFInfo)
{
if ($DBFInfo.FilehandleOfMemo -ne $null)
{
$this.FilehandleOfMemo.Close()
$this.FilehandleOfMemo = $null
}
if ($DBFInfo.FilehandleOfDBF -ne $null)
{
$this.FilehandleOfDBF.Close()
$this.FilehandleOfDBF = $null
}
}
function _GoBottom ($DBFInfo)
{
$DBFInfo.RecNo = $DBFInfo.Header.NumRecords
_ReadRecord $DBFInfo
}
function _GoTop ($DBFInfo)
{
$DBFInfo.RecNo = 1
_ReadRecord $DBFInfo
}
function _Skip ($DBFInfo, $SkipRecords)
{
$Rel = 1
if ($SkipRecords -ne $null)
{
$Rel = $SkipRecords
}
$DBFInfo.RecNo += $Rel
_ReadRecord $DBFInfo
}
function _Goto ($DBFInfo, $RecNo)
{
$DBFInfo.RecNo = $RecNo
_ReadRecord $DBFInfo
}
function _ListAll ($DBFInfo)
{
1..$DBFInfo.Header.NumRecords
}
# refreshes the current fieldvalues in the fields hashset
function _ReadRecord ($DBFInfo, $recNo)
{
if ($recNo -ne $null)
{
$DBFInfo.RecNo = $RecNo
}
$Culture = New-Object System.Globalization.CultureInfo "en-us"
#$Encode = New-Object System.Text.ASCIIEncoding
$Encode = [System.Text.Encoding]::GetEncoding(850)
[System.Byte[]]$RecordBuffer = @(0x20) * $DBFInfo.Header.RecordSize
$RecordPos = $DBFInfo.Header.DataOffset + ($DBFInfo.RecNo -1) * $DBFInfo.Header.RecordSize
#if ($RecordPos -le
$Position = $DBFInfo.FilehandleOfDBF.Seek($RecordPos, [System.IO.SeekOrigin]::Begin)
$BytesRead = $DBFInfo.FilehandleOfDBF.Read($RecordBuffer, 0, $DBFInfo.Header.RecordSize)
#if ($Position -gt 0)
$DBFInfo.Deleted = $RecordBuffer[0] -ne 32
foreach ($field in $DBFInfo.Header.Fields)
{
switch ($field.Type)
{
"C" {
$DBFInfo.Fields[$field.Name] = $Encode.GetString($RecordBuffer[($field.FieldPos+1)..($field.FieldPos + $field.Length)])
}
"D" {
$Year=$Encode.GetString($RecordBuffer[($field.FieldPos+1)..($field.FieldPos + 4)])
$Month=$Encode.GetString($RecordBuffer[($field.FieldPos+5)..($field.FieldPos + 6)])
$Day=$Encode.GetString($RecordBuffer[($field.FieldPos+7)..($field.FieldPos + 8)])
if ($Year.Trim().Length -eq 0)
{
$DBFInfo.Fields[$field.Name] = [System.DateTime]::Minvalue
}
else
{
$DBFInfo.Fields[$field.Name] = New-Object System.DateTime($Year,$Month,$Day)
}
}
"L" {
$DBFInfo.Fields[$field.Name] = $RecordBuffer[($field.FieldPos+1)] -eq 84 #"T"
}
"N" {
$Data=$Encode.GetString($RecordBuffer[($field.FieldPos+1)..($field.FieldPos + $field.Length)])
if($Data.Trim().Length -eq 0)
{
$Data = "0.0"
}
$DBFInfo.Fields[$field.Name] = [Convert]::ToDecimal($Data,$Culture)
}
"M" {
$Data = _Get-MemofieldData $DBFInfo $field.Name
$DBFInfo.Fields[$field.Name] = $Data
}
}
}
if ($recNo -ne $null)
{
new-object PSObject -Property $DBFInfo.Fields
}
}
# retrieves the data of a given memo-field
function _Get-MemofieldData ($DBFInfo, $Fieldname)
{
$retVal = ""
if ($DBFInfo.Type.hasMemoFile)
{
if ($DBFInfo.FileHandleOfMemo)
{
$FieldPos = $DBFInfo.Header.Fields | Where-Object {$_.Name -eq $Fieldname}
if ($FieldPos)
{
if ($DBFInfo.Type.memoExt -eq "DBT")
{
$retval = _Get-MemofieldDataDBT $DBFInfo $FieldPos
}
else
{
$retval = _Get-MemofieldDataFPT $DBFInfo $FieldPos
}
}
}
}
$retVal
}
function _Get-MemofieldDataDBT ($DBFInfo, $FieldPos)
{
$retval = "_Get-MemofieldDataDBT() needs to be implemented!"
}
function _Get-MemofieldDataFPT ($DBFInfo, $FieldPos)
{
$retVal = ""
$MemoPointer = _GetMemoBlockNumberFromField $DBFInfo $FieldPos
#$MemofieldSize = 10
#$Encode = [System.Text.Encoding]::GetEncoding(850)
#[System.Byte[]]$MemoFieldBuffer = @(0x20) * $MemofieldSize
#$RecordFieldPos = $DBFInfo.Header.DataOffset + ($DBFInfo.RecNo -1) * $DBFInfo.Header.RecordSize + $FieldPos.FieldPos +1
#$Position = $DBFInfo.FilehandleOfDBF.Seek($RecordFieldPos, [System.IO.SeekOrigin]::Begin)
#$BytesRead = $DBFInfo.FilehandleOfDBF.Read($MemoFieldBuffer, 0, $MemofieldSize)
#$MemoPointer = $Encode.GetString($MemoFieldBuffer)
if ($MemoPointer.Trim().Length -gt 0)
{
$MemoPointer = [System.Convert]::ToInt32($MemoPointer)
$MemoPointer = $MemoPointer * $DBFInfo.Type.memoBlockSize
[System.Byte[]]$MemoFieldHeaderBuffer = @(0x0) * 4
$Position = $DBFInfo.FilehandleOfMemo.Seek($MemoPointer, [System.IO.SeekOrigin]::Begin)
$BytesRead = $DBFInfo.FilehandleOfMemo.Read($MemoFieldHeaderBuffer, 0, 4)
#check if it's a text memo entry
if ((Compare-Object $MemoFieldHeaderBuffer @(0,0,0,1)).length -eq 0)
{
# get the length of the memofield
$BytesRead = $DBFInfo.FilehandleOfMemo.Read($MemoFieldHeaderBuffer, 0, 4)
$MemoFieldLength = $MemoFieldHeaderBuffer[0]*256*256*256+$MemoFieldHeaderBuffer[1]*256*256+$MemoFieldHeaderBuffer[2]*256+$MemoFieldHeaderBuffer[3]
#read the memofield data
[System.Byte[]]$MemoFieldBuffer = @(0x20) * $MemoFieldLength
$BytesRead = $DBFInfo.FilehandleOfMemo.Read($MemoFieldBuffer, 0, $MemoFieldLength)
$retVal = $Encode.GetString($MemoFieldBuffer)
}
}
$retVal
}
# retrieves the Pointer to the Block in the memofile
function _GetMemoBlockNumberFromField ($DBFInfo, $FieldPos)
{
$MemofieldSize = 10
$Encode = [System.Text.Encoding]::GetEncoding(850) # http://en.wikipedia.org/wiki/Code_page_850
[System.Byte[]]$MemoFieldBuffer = @(0x20) * $MemofieldSize
$RecordFieldPos = $DBFInfo.Header.DataOffset + ($DBFInfo.RecNo -1) * $DBFInfo.Header.RecordSize + $FieldPos.FieldPos +1
$Position = $DBFInfo.FilehandleOfDBF.Seek($RecordFieldPos, [System.IO.SeekOrigin]::Begin)
$BytesRead = $DBFInfo.FilehandleOfDBF.Read($MemoFieldBuffer, 0, $MemofieldSize)
$MemoPointer = $Encode.GetString($MemoFieldBuffer)
$MemoPointer
}
# checks the integrity of the memofields in the current record
# it checks if the memofields are reachable an the memofield headertype is text
function _Check-MemofieldIntegrity ($DBFInfo)
{
$retVal = @()
if ($DBFInfo.Type.hasMemoFile)
{
if ($DBFInfo.FileHandleOfMemo)
{
$FieldPos = $DBFInfo.Header.Fields | Where-Object {$_.Type -eq "M"}
foreach ($fp in $FieldPos)
{
if ($DBFInfo.Type.memoExt -eq "DBT")
{
$checkresult = _Check-MemofieldDataDBT $DBFInfo $fp
}
else
{
$checkresult = _Check-MemofieldDataFPT $DBFInfo $fp
}
if ($checkresult -ne $null)
{
$retval += $checkresult
}
}
}
}
$retVal
}
function _Check-MemofieldDataDBT ($DBFInfo, $FieldPos)
{
$retval = "_Get-MemofieldDataDBT() needs to be implemented!"
}
function _Check-MemofieldDataFPT ($DBFInfo, $FieldPos)
{
$retVal = $null
$MemoPointer = _GetMemoBlockNumberFromField $DBFInfo $FieldPos
if ($MemoPointer.Trim().Length -gt 0)
{
$MemoPointer = [System.Convert]::ToInt32($MemoPointer)
$MemoPointer = $MemoPointer * $DBFInfo.Type.memoBlockSize
[System.Byte[]]$MemoFieldHeaderBuffer = @(0x0) * 4
$Position = $DBFInfo.FilehandleOfMemo.Seek($MemoPointer, [System.IO.SeekOrigin]::Begin)
$BytesRead = $DBFInfo.FilehandleOfMemo.Read($MemoFieldHeaderBuffer, 0, 4)
#check if it's a text memo entry
if ((Compare-Object $MemoFieldHeaderBuffer @(0,0,0,1)).length -eq 0)
{
# get the length of the memofield
$BytesRead = $DBFInfo.FilehandleOfMemo.Read($MemoFieldHeaderBuffer, 0, 4)
$MemoFieldLength = $MemoFieldHeaderBuffer[0]*256*256*256+$MemoFieldHeaderBuffer[1]*256*256+$MemoFieldHeaderBuffer[2]*256+$MemoFieldHeaderBuffer[3]
#read the memofield data
[System.Byte[]]$MemoFieldBuffer = @(0x20) * $MemoFieldLength
$BytesRead = $DBFInfo.FilehandleOfMemo.Read($MemoFieldBuffer, 0, $MemoFieldLength)
#$retVal = $Encode.GetString($MemoFieldBuffer)
}
else
{
$Integrity = New-Object PSObject
$Integrity | Add-Member -Type NoteProperty -Name RecNo -Value $DBFInfo.RecNo
$Integrity | Add-Member -Type NoteProperty -Name FieldPos -Value $FieldPos
$Integrity | Add-Member -Type NoteProperty -Name HeaderBytes -Value $MemoFieldHeaderBuffer
$retval = $Integrity
#Write-Host $FieldPos.Name " Unknown Memofield-Header: $MemoFieldHeaderBuffer"
}
}
$retVal
}
function _Reset-MemofieldPointer ($DBFInfo, $FieldName)
{
$retVal = ""
if ($DBFInfo.Type.hasMemoFile)
{
if ($DBFInfo.FileHandleOfMemo)
{
$FieldPos = $DBFInfo.Header.Fields | Where-Object {$_.Type -eq "M" -and $_.Name -eq $FieldName}
foreach ($fp in $FieldPos)
{
if ($DBFInfo.Type.memoExt -eq "DBT")
{
$retval = _Reset-MemofieldPointerDBT $DBFInfo $fp
}
else
{
$retval = _Reset-MemofieldPointerFPT $DBFInfo $fp
}
}
}
}
$retVal
}
function _Reset-MemofieldPointerFPT ($DBFInfo, $FieldPos)
{
$MemofieldSize = 10
[System.Byte[]]$MemoFieldBuffer = @(0x20) * $MemofieldSize
$RecordFieldPos = $DBFInfo.Header.DataOffset + ($DBFInfo.RecNo -1) * $DBFInfo.Header.RecordSize + $FieldPos.FieldPos +1
$Position = $DBFInfo.FilehandleOfDBF.Seek($RecordFieldPos, [System.IO.SeekOrigin]::Begin)
$BytesRead = $DBFInfo.FilehandleOfDBF.Write($MemoFieldBuffer, 0, $MemofieldSize)
}
# test
# $a=Use-DBF C:\delacdx\auftrag.dbf
# foreach($nr in 500..510) { $a.GoTo($nr); $a.Fields.Nummer; $a.Fields.Patient; $a.Fields.Patient.Length }
# $a.Close()
#
# possible syntax:
#
# $a.All().Where($_.Patient -eq "Müller")
# $a.Next(100).Where($_.Patient -eq "Müller")
#
# $a.Fields|where {$_.Nummer -eq "001004"}
# $daten = foreach($nr in $tr.ListAll()) {$tr.Goto($nr); new-object -property $tr.Fields -typename psobject}
#
# compare data:
# $diff=Compare-Object -ReferenceObject $orgbeb -DifferenceObject $trbeb -Property BEBart -PassThru
# getting all memofields
# $b.Header.Fields| where type -eq "M"
# checks memofield-integrity:
# foreach($nr in $b.ListAll()) {$b.Goto($nr); _Check-MemofieldIntegrity $b }
# clear all mispointing memofields, handles multiple memofields
# $b = Use-DBF (Resolve-Path .\artikel.dbf) -ReadWrite
# $bk = foreach($nr in $b.ListAll()) {$b.Goto($nr); _Check-MemofieldIntegrity $b }
# # $bk now holds a special structure of memofields to be repaired
# foreach ($r in $bk) {$b.GoTo($r.recno); _Reset-MemofieldPointer $b $r.FieldPos.Name}
# $bkN = foreach($nr in $b.ListAll()) {$b.Goto($nr); _Check-MemofieldIntegrity $b }
# $bkN.Length
# $b.Close()
#
#
# Zap_DBF C:\temp\PSDBFTest\temp\test.dbf