Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for read_xport #50

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions src/C_interface.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ function readstat_get_file_format_version(metadata::Ptr{Nothing})
end

function readstat_get_row_count(metadata::Ptr{Nothing})
return ccall((:readstat_get_row_count, libreadstat), UInt, (Ptr{Nothing},), metadata)
return ccall((:readstat_get_row_count, libreadstat), Int, (Ptr{Nothing},), metadata)
end

function readstat_get_var_count(metadata::Ptr{Nothing})
Expand Down Expand Up @@ -69,4 +69,8 @@ end

function readstat_parse(filename::String, type::Val{:sas7bdat}, parser::Ptr{Nothing}, ds::ReadStatDataFrame)
return ccall((:readstat_parse_sas7bdat, libreadstat), Int, (Ptr{Nothing}, Cstring, Any), parser, string(filename), ds)
end
end

function readstat_parse(filename::String, type::Val{:xport}, parser::Ptr{Nothing}, ds::ReadStatDataFrame)
return ccall((:readstat_parse_xport, libreadstat), Int, (Ptr{Nothing}, Cstring, Any), parser, string(filename), ds)
end
27 changes: 25 additions & 2 deletions src/ReadStat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ include(depsjl_path)
using DataValues: DataValueVector
using Dates

export ReadStatDataFrame, read_dta, read_sav, read_por, read_sas7bdat
export ReadStatDataFrame, read_dta, read_sav, read_por, read_sas7bdat, read_xport

##############################################################################
##
Expand Down Expand Up @@ -83,6 +83,8 @@ end

include("C_interface.jl")

const READSTAT_NALLOC_ROWS = 100000

##############################################################################
##
## Julia functions
Expand Down Expand Up @@ -154,7 +156,13 @@ function handle_variable!(var_index::Cint, variable::Ptr{Nothing},
push!(ds.formats, get_format(variable))
jtype = get_type(variable)
push!(ds.types, jtype)
push!(ds.data, DataValueVector{jtype}(ds.rows))

if ds.rows < 0
push!(ds.data, DataValueVector{jtype}(READSTAT_NALLOC_ROWS))
else
push!(ds.data, DataValueVector{jtype}(ds.rows))
end

push!(ds.storagewidths, get_storagewidth(variable))
push!(ds.measures, get_measure(variable))
push!(ds.alignments, get_alignment(variable))
Expand Down Expand Up @@ -182,6 +190,16 @@ function handle_value!(obs_index::Cint, variable::Ptr{Nothing},
value::ReadStatValue, ds_ptr::Ptr{ReadStatDataFrame})
ds = unsafe_pointer_to_objref(ds_ptr)
var_index = readstat_variable_get_index(variable)

nl = length(ds.data[var_index + 1])
if (obs_index + 1) >= nl
resize!(ds.data[var_index + 1], nl + READSTAT_NALLOC_ROWS)
end

if obs_index > ds.rows
ds.rows = obs_index + 1
end

if !readstat_value_is_missing(value, variable)
readfield!(ds.data[var_index + 1], obs_index + 1, value)
end
Expand Down Expand Up @@ -232,6 +250,10 @@ function read_data_file(filename::AbstractString, filetype::Val)
parser = Parser()
# parse
parse_data_file!(ds, parser, filename, filetype)
# Resize
for i in 1:length(ds.data)
resize!(ds.data[i], ds.rows)
end
# return dataframe instead of ReadStatDataFrame
return ds
end
Expand Down Expand Up @@ -260,5 +282,6 @@ read_dta(filename::AbstractString) = read_data_file(filename, Val(:dta))
read_sav(filename::AbstractString) = read_data_file(filename, Val(:sav))
read_por(filename::AbstractString) = read_data_file(filename, Val(:por))
read_sas7bdat(filename::AbstractString) = read_data_file(filename, Val(:sas7bdat))
read_xport(filename::AbstractString) = read_data_file(filename, Val(:xport))

end #module ReadStat
16 changes: 16 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,20 @@ data = rsdf.data
@test data[6] == DataValueArray{String}(["2", "7", ""])
end

@testset "XPT files" begin

dtafile = joinpath(dirname(@__FILE__), "types.xpt")
rsdf = read_xport(dtafile)
data = rsdf.data

@test length(data) == 6
@test rsdf.headers == [:vfloat, :vdouble, :vlong, :vint, :vbyte, :vstring]
@test data[1] == DataValueArray{Float32}([3.14, 7., NA])
@test data[2] == DataValueArray{Float64}([3.14, 7., NA])
@test data[3] == DataValueArray{Int32}([2, 7, NA])
@test data[4] == DataValueArray{Int16}([2, 7, NA])
@test data[5] == DataValueArray{Int8}([2, 7., NA])
@test data[6] == DataValueArray{String}(["2", "7", ""])
end

end
Binary file added test/types.xpt
Binary file not shown.