11module DBFTables
22
3- using Missings, DataFrames
3+ using DataFrames, Printf
44
55# Read DBF files in xBase format
66# Files written in this format have the extension .dbf
@@ -27,7 +27,7 @@ struct DBFHeader
2727end
2828
2929function dbf_field_type (fld:: Char , dec:: UInt8 )
30- rt = Void
30+ rt = Nothing
3131 if fld == ' C'
3232 rt = String
3333 elseif fld == ' D'
@@ -51,29 +51,29 @@ function dbf_field_type(fld::Char, dec::UInt8)
5151end
5252
5353function read_dbf_field (io:: IO )
54- field_name = strip (replace (( String (read! (io, Vector {UInt8} (11 )))), ' \0 ' , ' ' )) # 0x00
54+ field_name = strip (replace (String (read! (io, Vector {UInt8} (undef, 11 ))), ' \0 ' => ' ' )) # 0x00
5555 field_type = read (io, Char) # 0x0B
5656 read (io, Int32) # skip 0x0C
5757 field_len = read (io, UInt8) # 0x10
5858 field_dec = read (io, UInt8) # 0x11
59- read! (io, Vector {UInt8} (14 )) # reserved
59+ read! (io, Vector {UInt8} (undef, 14 )) # reserved
6060 return DBFFieldDescriptor (field_name, dbf_field_type (field_type, field_dec), field_len, field_dec)
6161end
6262
6363function read_dbf_header (io:: IO )
6464 ver = read (io, UInt8)
65- date = read! (io, Vector {UInt8} (3 )) # 0x01
65+ date = read! (io, Vector {UInt8} (undef, 3 )) # 0x01
6666 last_update = @sprintf (" %4d%02d%02d" , date[1 ]+ 1900 , date[2 ], date[3 ])
6767 records = read (io, Int32) # 0x04
6868 hsize = read (io, Int16) # 0x08
6969 rsize = read (io, Int16) # 0x0A
7070 read (io, Int16) # reserved # 0x0C
7171 incomplete = Bool (read (io, UInt8)) # 0x0E
7272 encrypted = Bool (read (io, UInt8)) # 0x0F
73- read! (io, Vector {UInt8} (12 )) # reserved
73+ read! (io, Vector {UInt8} (undef, 12 )) # reserved
7474 mdx = Bool (read (io, UInt8)) # 0x1C
7575 langId = read (io, UInt8) # 0x1D
76- read! (io, Vector {UInt8} (2 )) # reserved # 0x1E
76+ read! (io, Vector {UInt8} (undef, 2 )) # reserved # 0x1E
7777 fields = DBFFieldDescriptor[]
7878
7979 while ! eof (io)
@@ -99,7 +99,7 @@ function read_dbf_records!(io::IO, df::DataFrame, header::DBFHeader; deleted=fal
9999 r = Any[]
100100 for i = 1 : length (header. fields)
101101 # print("P: $(position(io)) ")
102- fld_data = read! (io, Vector {UInt8} (header. fields[i]. len))
102+ fld_data = read! (io, Vector {UInt8} (undef, header. fields[i]. len))
103103 # println("D: $(ascii(fld_data))")
104104 if header. fields[i]. typ == Bool
105105 logical = Char (fld_data[1 ])
@@ -112,13 +112,13 @@ function read_dbf_records!(io::IO, df::DataFrame, header::DBFHeader; deleted=fal
112112 end
113113 elseif header. fields[i]. typ == Int
114114 tmp = tryparse (header. fields[i]. typ, String (fld_data))
115- push! (r, tmp. hasvalue ? tmp . value : missing )
115+ push! (r, tmp== nothing ? missing : tmp )
116116 elseif header. fields[i]. typ == Float64
117117 tmp = tryparse (header. fields[i]. typ, String (fld_data))
118- push! (r, tmp. hasvalue ? tmp . value : missing )
118+ push! (r, tmp== nothing ? missing : tmp )
119119 elseif header. fields[i]. typ == String
120120 push! (r, strip (String (fld_data)))
121- elseif header. fields[i]. typ == Void
121+ elseif header. fields[i]. typ == Nothing
122122 push! (r, missing )
123123 else
124124 warn (" Type $(header. fields[i]. typ) is not supported" )
0 commit comments