-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.rb
1386 lines (1277 loc) · 33 KB
/
main.rb
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
require 'rubygems'
require 'iconv'
#require 'dbi'
module XBase
# Base input output module for XBase suite
class Base
def fh
@fh
end
def initialize(file,opts)
SEEK_VIA_READ(false)
self.open(file,opts)
end
# Seek to absolute position
def seek_to_seek(offset)
unless @fh
raise "Cannot seek on unopened file\n"
end
@fh.seek(offset)
true
end
def SEEK_VIA_READ(arg)
if arg
alias :seek_to :seek_to_read
@SEEK_VIA_READ=true
else
alias :seek_to :seek_to_seek
@SEEK_VIA_READ=false
end
end
# Open the specified file. Use the read_header to load the header data
def open(*args)
options={}
options[:name]=args.shift if !args[0].nil? and args[0].class==String
@openoptions=options.merge(args[0]) unless @openoptions!=nil
options=options.merge(args[0])
if @fh!=nil
self.close
end
external_fh=false
if options[:name]=='-'
else
ok=true
if !options[:readonly]
fh=File.new(options[:name],'w+')
rw=true
else
fh=File.new(options[:name],'r+')
rw=false
ok=true
end
if !ok
raise "Error opening file #{options[:name]}: $!\n"
end
end
@tell=0 if @SEEK_VIA_READ
fh.flush
fh.binmode unless external_fh
@fh,@filename,@rw=fh,options[:name],rw
self.read_header options
end
# Read from the filehandle
def read(*args)
fh=@fh or return
if args[1]!=nil
fh.seek(args[1],IO::SEEK_CUR)
end
result=fh.read(args[0])
if result!=nil and @tell!=nil
@tell+=result
end
result
end
# Tell the position
def tell
if @tell!=nil
return @tell
end
return @fh.tell
end
# Read the record of given number. The second parameter is the length of
# the record to read. It can be undefined, meaning read the whole record
# and it can be negative, meaning at most the length
def read_record(*args)
num,in_length=args
if num.nil?
raise "Number of the record must be defined when reading it\n"
end
if self.last_record>0 and num>self.last_record
puts "Can't read record #{num}, there is not so many of them\n"
end
if in_length.nil?
in_length=@record_len
end
if in_length<0
in_length=-@record_len
end
offset=self.get_record_offset(num)
return if offset.nil?
self.read_from(offset,in_length)
end
# Compute the offset of the record
def get_record_offset(num)
header_len,record_len=@header_len,@record_len
unless (header_len!=nil and record_len!=nil)
raise "Header and record lengths not known in get_record_offset\n"
end
unless num!=nil
raise "Number of the record must be specified in get_record_offset\n"
end
return header_len+num*record_len
end
def read_from(offset,in_length)
if offset==nil
raise "Offset to read from must be specified\n"
end
self.seek_to(offset) or return
length=in_length
length=-length if length<0
buffer=self.read(length)
read=buffer.length
if read==0 or (in_length>0 and read!=in_length)
raise "Error reading #{in_length} bytes from #{@filename}\n"
end
buffer
end
# Close the file
def close
if @fh==nil
raise "Can't close file that is not opened\n"
end
@fh.close
@fh=nil
true
end
def self.drop(filename)
raise "Not implemented yet"
if filename!=nil
end
end
# Drop (unlink) the file
def drop
if !@filename.nil?
filename=@filename
self.close if !@fh.nil?
if !FileUtils.rm(filename)
raise "Error unlinking file #{filename}: $!\n"
return
end
end
return true
end
# Create new file
def create_file(args)
filename,perms=args
if filename.nil?
raise "Name has to be specified when creating new file\n"
end
if File.file?(filename)
raise "File #{filename} already exists\n"
end
perms=0644 unless !perms.nil?
fh=File.new(filename,'w',perms) or return
fh.binmode
@fh,@filename,@rw=fh,filename,true
#return self
end
# Seek to start of the record
def seek_to_record(num)
offset=self.get_record_offset(num)
return if offset==nil
self.seek_to(offset)
end
def seek_to_read(offset)
unless @fh!=nil
raise "Cannot seek on unopened file\n"
end
tell=self.tell
if offset<tell
raise "Cannot seek backwards without using seek (#{offset} < #{tell})\n"
end
if offset>tell
udef=self.read(offset-tell)
tell=self.tell
end
if tell!=offset
raise "Some error occured during read-seek: $!\n"
end
true
end
# Write the given record
def write_record(num,args)
offset=self.get_record_offset(num)
return if offset==nil
w=self.write_to(offset,args)
return if w==nil
num==0 ? '0E0' :num
end
# Write data directly to offset
def write_to(offset,args)
if !@rw
raise "The file #{@filename} is not writable\n"
end
self.seek_to(offset) or return
@fh.print(args) or raise "Error writing to offset #{offset} in file #{@filename}: $!\n"
offset==0 ? '0E0' : offset
end
def _locksh(fh)
fh.flock(1)
end
def _lockex(fh)
fh.flock(2)
end
def _unlock(fh)
fh.flock(8)
end
def locksh
_locksh(@fh)
end
def lockex
_lockex(@fh)
end
def unlock
_unlock(@fh)
end
end
end
module XBase
class Index < Base
def initialize(file,*args)
opts=args
#@xbase=xbase
ext=file.scan(/\.(...)$/).first.last.downcase
if ext=='sdbm' or ext=='pag' or ext=='dir'
end
if ext=='cdx'
object=Cdx.new(file,opts[0])
end
return object if object!=nil
raise "Error loading index: unknown extension\n"
return
end
def record_len
@record_len
end
def last_record
@total_pages
end
def get_key_val_left(num)
printkey=@keys[num]
#printkey=printkey.scan(/\s+\n/)
#printkey=printkey.scan("\000/\\0")#\\g
return nil if (@keys==nil or @keys[num]==nil) and (@values==nil or @values[num]==nil) and (@lefts==nil or @lefts[num]==nil)
return [@keys[num],@values[num],@lefts==nil ? nil : @lefts[num]] if num<=@keys.length
return nil
end
def prepare_select
@level=nil
@pages=nil
@rows=nil
true
end
def fetch
level, page, row, key, val, left=nil
while val.nil?
level=@level
if level.nil?
level=@level=0
page=self.get_record(@start_page)
if page.nil?
raise "Index corrupt: no root page @#{start_page}\n"
end
@pages=[page]
@rows=[]
end
page=@pages[level]
if page==nil
raise "Index corrupt: page for level #{level} lost in normal course\n"
end
row=@rows[level]
if row.nil?
row=@rows[level]=0
else
row+=1
@rows[level]= row
end
key,val,left=page.get_key_val_left(row)
if !left.nil?
level+=1
oldpage=page
page=self.get_record(left)
if page.nil?
raise "Index corrupt: no page #{left}, ref'd from #{oldpage}, row #{row}, level #{level}\n"
end
@pages[level]=page
@rows[level]=nil
@level=level
val=nil
next
end
if val!=nil
return key,val
else
level-=1
@level= level
return nil,nil if level<0
page=@pages[level]
if page==nil
raise "Index corrupt: page for level #{level} lost when backtracking\n"
end
row=@rows[level]
backkey,backval,backleft=page.get_key_val_left(row)
if @lastkeyisjustoverflow==nil and backleft!=nil and backval!=nil
return backkey,backval
end
end
end
return nil,nil
end
def get_record(num)
return @pages_cache[num] if @pages_cache!=nil and @pages_cache[num]!=nil
page=Page.new self,num
if page!=nil
@pages_cache[num]=page
end
page
end
def key_length
@key_length
end
def key_record_length
@key_record_length
end
def key_type
@key_type
end
# def prepare_select_eq(*args)
# eq,recno=args
# self.prepare_select
# left=@start_page
# level=0
# parent=nil
# numdate=(@key_type!=0||@key_type!=nil)
# while true
# page=self.get_record(left)
# if page==nil
# raise "Index corrupt: no page #{left} for level #{level}\n"
# end
# row=0
# while arr=page.get_key_val_left(row)
# key,val,newleft=arr
# left=newleft
# if key==nil
# break
# end
# if numdate == 1 ? key >= eq : key >= eq
# break
# end
# row+=1
# end
# @pages[level]=page
# @rows[level]=row
# if left==nil
# @rows[level]=row ? row-1 : nil
# @level=level
# break
# end
# page.parent=parent[num] if parent!=nil
# parent=page
# level+=1
# end
# if recno!=nil
# key,val=self.fetch_current
# while val!=nil
# break if numdate ? key>eq : key>=eq
# break if val==recno
# key,val=self.fetch
# end
# end
# true
# end
def tags
@tags if @tags!=nil
end
def fetch_current
level=@level
page=@pages[level]
row=@rows[level]
key,val,left=page.get_key_val_left(row)
return key,val
end
def num_keys
@keys.length
end
end
end
module XBase
class Cdx < Index
def tag=(value)
@tag=value
end
def initialize(file,*args)
#@xbase=xbase
@pages_cache=[]
@tags=[]
self.class.superclass.instance_method(:open).bind(self).call(file,args[0])
end
def read_header(*opts)
opts=opts[0]
#opts[:tag]=opts[:tag]
header=@fh.read(1024)
raise "Error reading header of #{@filename}: $!\n" if header.length!=1024
@start_page, @start_free_list, @total_pages,
@key_length, @index_options, @index_signature,
@sort_order, @total_expr_length, @for_expression_length,
@key_expression_length,
@key_string=header.unpack('VVNv CC @502 vvv @510 v @512 a512')
@total_pages=-1
@key_string=@for_string#=@key_string.scan(/^([^\000]*)\000([^\000]*)/).first.last !!!
@key_record_length=@key_length+4
@record_len=512
@start_page/=@record_len
@start_free_list!=@record_len
@header_len=0
@key_type=0
if @tag.nil?
self.prepare_select
while tag=self.fetch[0]
@tags.push tag
end
end
if opts[:tag]!=nil
self.prepare_select_eq opts[:tag]
foundkey,value=self.fetch
if foundkey==nil
raise "No tag #{opts[:tag]} found in index file #{@filename}.\n"
end
subidx=self
subidx.fh.seek(value,0)
subidx.tag=opts[:tag]
subidx.read_header
end
self
end
def last_record
@total_pages
end
def prepare_select_eq(*args)
eq,recno=args
self.prepare_select
left=@start_page
level=0
parent=nil
numdate=@key_type!=0 ? true : false
while true
page=self.get_record(left)
if page==nil
raise "Index corrupt: no page #{left} for level #{level}\n"
end
row=0
while arr=page.get_key_val_left(row)
break if arr.nil? or arr.size==0
key,val,newleft=arr
left=newleft
if key.nil?
break
end
if numdate ? key >= eq : (key <=> eq.to_s)!=nil and (key <=> eq.to_s)>0
break
end
row+=1
end
@pages=[] if @pages==nil
@rows=[] if @rows==nil
@pages[level]=page
@rows[level]=row
if left==nil
@rows[level]=row!=0 ? row-1 : nil
@level=level
break
end
page.parent=parent[num] if parent!=nil
parent=page
level+=1
end
if recno!=nil
key,val=self.fetch_current
while val!=nil
break if numdate ? key>eq : key>=eq
break if val==recno
key,val=self.fetch
end
end
true
end
end
end
module XBase
class Page < Cdx
def initialize(indexfile,num)
data=indexfile.read_record(num) or return
origdata=data
attributes,noentries,left_brother,right_brother=data.unpack 'vvVV'
keylength=indexfile.key_length
keyreclength=indexfile.key_record_length
numdate=indexfile.key_type
keys,values,lefts=[],[],nil
if attributes&2!=0
free_space,recno_mask,duplicate_count_mask,trailing_count_mask, recno_count,duplicate_count,
trailing_count, holding_recno=data.unpack '@12 vVCCCCCC'
@recno_count,@duplicate_count,@trailing_count,@holding_recno=
recno_count,duplicate_count,trailing_count,holding_recno
prevkeyval=''
(0..noentries-1).each do |i|
one_item=data[24+i*holding_recno,holding_recno]+("\0" * 4)
numeric_one_item=one_item.unpack('V').first
recno=numeric_one_item & recno_mask
bytes_of_recno=recno_count/8
one_item=one_item[bytes_of_recno..one_item.length-1]
numeric_one_item=one_item.unpack('V').first
numeric_one_item >>= recno_count-8*bytes_of_recno
dupl=numeric_one_item & duplicate_count_mask
numeric_one_item >>= duplicate_count
trail=numeric_one_item&trailing_count_mask
getlength=keylength-trail-dupl
key=prevkeyval[0,dupl]
(key = key + data[-getlength,getlength]) if getlength!=0
key = key + ("\000" * trail)
data[-getlength,getlength]='' if getlength!=0
prevkeyval=key
if numdate!=0
if 0x80 & key.unpack('C')
key[0,1]&="\177"
else
key= ~key
end
if keylength==8
key=key.reverse unless @BIGEND
key=key.unpack 'd'
else
key=key.unpack 'N'
end
if numdate==2 and (key!=nil and key!=0)
#key=sprintf "%04d%02d%02d",!!!inverse_julian_day(key)
end
else
key[-trail,trail]='' if trail!=0
end
keys.push key
values.push recno
end
else
(0..noentries-1).each do |i|
offset=12+i*(keylength+8)
key,recno,page=data.unpack("@#{offset} a#{keylength} NN")
if numdate!=0
if 0x80&key.unpack('C')!=0
key[0,1]&="\177"
else
key= ~key
end
if keylength==8
key=key.reverse unless @BIGEND
key=key.unpack 'd'
else
key=key.unpack 'N'
end
if numdate==2 and key!=0
#$key = sprintf "%04d%02d%02d",Time::JulianDay::inverse_julian_day($key);
end
else
key=key.scan(/\000+"\n"/).first.last#$/
end
keys.push key
values.push value
lefts=[] unless lefts!=nil
lefts.push page/512
end
@last_key_is_just_overflow=1
end
@keys,@values,@num,@key_length,@lefts,@indexfile,@attributes,@left_brother,@right_brother=
keys,values,num,keylength,lefts,indexfile,attributes,left_brother,right_brother
#_self=Pag
outdata=self.prepare_scalar_for_write
if 0 and outdata!=origdata
#puts "I won't be able to write this page back.\n",
# outdata.unpack("H*"),"\n ++\n",
# origdata.unpack("H*"),"\n"
else
end
end
def prepare_scalar_for_write
attributes,noentries,left_brother,right_brother=@attributes,@keys.length,@left_brother,@right_brother
data=[attributes,noentries,left_brother,right_brother].pack('vvVV')
indexfile=@indexfile
numdate=indexfile.key_type
record_len=indexfile.record_len
keylength=@key_length
if attributes&2!=0
recno_count,duplicate_count,trailing_count,holding_recno=[16,4,4,3]
if @recno_count!=nil
recno_count,duplicate_count,trailing_count,holding_recno=
@recno_count,@duplicate_count,@trailing_count,@holding_recno
end
recno_mask,duplicate_mask,trailing_mask=[2**recno_count-1,2**duplicate_count-1,2**trailing_count-1]
recno_data=''
keys_string=''
prevkey=''
row=0
@keys.each do |key|
dupl=0
out=key
if numdate!=0
if keylength==8
out=out.pack('d')
out=out.reverse unless @BIGEND
else
out=out.pack 'N'
end
unless (0x80&out.unpack('C'))
out[0,1]|="\200"
else
out= ~out
end
end
(0..out.length-1).each do |i|
unless (out[i,1]==prevkey[i,1])
break
end
dupl+=1
end
trail=keylength-out.length
while out[-1,1]=="\000"
out=out[0,out.length-1]
trail+=1
end
keys_string="#{out[dupl,out.length]}#{keys_string}" #substr(out,dupl)
numdata=((((trail & trailing_mask) << duplicate_count) | (dupl & duplicate_mask)) << recno_count) | (@values[row] & recno_mask)
recno_data=recno_data+[numdata].pack('V')[0,holding_recno]
prevkey=out
row+=1
end
data=data+[(record_len - recno_data.length - keys_string.length- 24),
recno_mask, duplicate_mask,trailing_mask, recno_count,
duplicate_count,trailing_count, holding_recno].pack('vVCCCCCC')
data=data+recno_data
data=data+"\000" * (record_len-data.length-keys_string.length)
data=data+keys_string
else
row=0
@keys.each do |key|
out=key
if numdate
if keylength==8
out=out.pack('d')
out=out.reverse unless @BIGEND
else
out=out.pack('N')
end
unless (0x80&out.unpack('C'))!=0
out[0,1]!="\200"
else
out= ~out
end
end
data=data+[out,@values[row],@lefts[row]*512].pack("a#{keylength} NN")
row+=1
end
data=data+"\000"*(record_len-data.length)
end
data
end
def get_parent_page
parent_num=self.get_parent_page_num or return
indexfile=@indexfile
return indexfile.get_record(parent_num)
end
end
end
module XBase
class Cursor < Base
def initialize(*args)
@xbase,@recno,@fieldnums,@fieldnames=args
end
def fetch
xbase, recno, fieldnums, fieldnames=@xbase,@recno,@fieldnums,@fieldnames
if recno!=nil
recno+=1
else
recno=0
end
lasrec=xbase.last_record
while recno<=lasrec
result=xbase.get_record_nf(recno,fieldnums)
del=result.shift
if result.length>0 and !del
@recno=recno
return result
end
recno+=1
end
return
end
def fetch_hashref
data=self.fetch
hashref={}
if data!=nil
hashref[@hashnames]=data
return hashref
end
return
end
def last_fetched
#shift->[1]
end
def table
#shift->[0]
end
def names
#shift->[3]
end
def rewind
#shift[1]=nil
'0E0'
end
def attach_index
include Index
end
end
end
module XBase
class IndexCursor < Cursor
def fetch
xbase,recno,fieldnums,fieldnames,index=@xbase,@recno,@fieldnums,@fieldnames,@index
p=true
while val=index.fetch[1]
del,result=xbase.get_record_nf(val-1,fieldnums)
unless del
@val=val
return result
end
end
return
end
def last_fetched
@val-1
end
def find_eq(val)
@index.prepare_select_eq(val)
end
def initialize(xbase,recno,fieldnums,fieldnames,index)
@xbase,@recno,@fieldnums,@fieldnames,@index=xbase,recno,fieldnums,fieldnames,index
end
def index
@index
end
end
end
module XBase
class XBase < Base
def last_record
@num_rec-1
end
def open(*args)
options={}
if args.length%2
options[:name]=args.shift
end
@openoptions=options.merge(args[0])
locoptions={}
locoptions[:name],locoptions[:readonly],locoptions[:ignorememo],locoptions[:fh]=
@openoptions[:name],@openoptions[:readonly],@openoptions[:ignorememo],@openoptions[:fh]
return self.class.superclass.instance_method(:open).bind(self).call(locoptions)
end
def read_header(*opts)
fh=@fh
header=self.read(32)
#p header
if header.length!=32
raise "Error reading header of #{@filename}: $!\n"
end
@version,@last_update,@num_rec,@header_len,@record_len,@encrypted=header.unpack('Ca3Vvv@15a1')
header_len=@header_len
names,types,lengths,decimals=[],[],[],[]
unpacks,readproc,writeproc=[],[],[]
lastoffset=1
while self.tell<header_len-1
field_def=self.read(1)
#p field_def
break if field_def=="\r"
field_def=field_def+self.read(31)
read=field_def.length
if read!=32
raise "Error reading field description: $!\n"
end
name,type,length,decimal=field_def.unpack('A11a1 @16CC')
if type=='C'
if decimal!=0 and !@openoptions['nolongchars']
length+=256*decimal
decimal=0
end
rproc=proc do |value|
if @ChopBlanks
#value=value.scan("\s+#{$/}")
end
return value==''?nil:Iconv.iconv('UTF-8','866',value).first.strip
end
wproc=proc do |value|
sprintf '%-*.*s', length, length,(value!=nil ? value : '')
end
end
#name=name.scan("[\000 ].*#{$/}").first.last
name.upcase!
names.push name
types.push type
lengths.push length
decimals.push decimal
unpacks.push "@#{lastoffset}a#{length}"
readproc.push rproc
writeproc.push wproc
lastoffset+=length
end
if lastoffset>@record_len and !@openoptions['nolongchars']
self.seek_to(0)
@openoptions['nolongchars']=1
return self.read_header
end
if lastoffset!=@record_len and @openoptions['ignorebadheader']==nil
raise "Mismatch in header of #{@filename}: record_len #{@record_len} but offset #{lastoffset}\n"
end
if @openoptions['recompute_lastrecno']
#
end
hashnames=names.to_h
@field_names, @field_types ,@field_lengths ,@field_decimals,
@hash_names ,@last_field ,@field_unpacks,
@field_rproc ,@field_wproc ,@ChopBlanks=names, types, lengths, decimals,
hashnames, names.length-1, unpacks,
readproc, writeproc, @CLEARNULLS
return true
end
def get_record(num,*args)
self.get_record_nf(num,args.map{|i|self.field_name_to_num(i)})
end
def field_name_to_num(name)
@hash_names[name.upcase]
end
def get_record_nf(num,fieldnums)
data=self.read_record(num) or return
if fieldnums==nil or fieldnums.length==0
fieldnums=(0..self.last_field)
end
unpack=['@0a1'].concat fieldnums.map{|value|e=@field_unpacks[value] if value!=nil;e!=nil ? e : '@0a0'}
unpack=unpack.join(' ')
rproc=@field_rproc
fns=fieldnums.map{|i|i!=nil and rproc[i]!=nil ? rproc[i] : proc{|value| nil}}
out=data.unpack(unpack)
out[0]=read_deleted(out[0])
(1..out.length-1).each{|i|out[i]=fns[i-1].call(out[i])}
out
end
def read_deleted(value)
if value=='*'
return true
elsif value==' '
return false
end
nil
end
def prepare_select(*args)
fieldnames=*args
if fieldnames==nil or fieldnames.length==0
fieldnames=self.field_names
end
fieldnums=fieldnames.map{|value|self.field_name_to_num(value)}
return Cursor.new(self,nil,fieldnums,fieldnames)
end
def prepare_select_with_index(f,*args)
if f.class==Array
tagopts={:tag=>f[1]}
if !f[2].nil?
tagopts.push :type=>f[2]
end
file=f[0]
end
fieldnames=args
if fieldnames==nil or fieldnames.length==0
fieldnames=self.field_names
end
fieldnums=fieldnames.map{|i|self.field_name_to_num(i)}
#index=Index.new(file,true,self)
index=Index.new(file,{:dbf=>self,:readonly=>true}.merge(tagopts))
index.prepare_select
return IndexCursor.new self,nil,fieldnums,fieldnames,index
end
def field_names
@field_names
end
def init_memo_field
return @memo if @memo!=nil
options={'dbf_version'=>@version,'memosep'=>@memosep}
if @memofile!=nil
return Memo.new(@memofile,options)
end
['dbt', 'DBT', 'fpt', 'FPT', 'smt', 'SMT'].each do |i|
memoname=@filename
raise "Not implemented yet"
end
return
end
def close
if @memo!=nil
@memo.close
@memo=nil
end
self.class.superclass.instance_method(:close).bind(self).call
end
def version
@version
end
def last_field
@last_field
end
def field_names