Skip to content

Add writing support #46

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

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
28 changes: 28 additions & 0 deletions src/C_interface.jl
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,32 @@ 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

function readstat_begin_row(writer)
return ccall((:readstat_begin_row), Int, (Ptr{Nothing},), writer)
end

function readstat_end_row(writer)
return ccall((:readstat_end_row), Int, (Ptr{Nothing},), writer)
end

function readstat_begin_writing(writer, filetype::Val{:dta}, io, row_count)
return ccall((:readstat_begin_writing_dta, libreadstat), Int, (Ptr{Nothing}, Ptr{Nothing}, Cint), writer, io, Cint(row_count))
end

function readstat_begin_writing(writer, filetype::Val{:sav}, io, row_count)
return ccall((:readstat_begin_writing_sav, libreadstat), Int, (Ptr{Nothing}, Ptr{Nothing}, Cint), writer, io, Cint(row_count))
end

function readstat_begin_writing(writer, filetype::Val{:por}, io, row_count)
return ccall((:readstat_begin_writing_por, libreadstat), Int, (Ptr{Nothing}, Ptr{Nothing}, Cint), writer, io, Cint(row_count))
end

function readstat_begin_writing(writer, filetype::Val{:sas7bdat}, io, row_count)
return ccall((:readstat_begin_writing_sas7bdat, libreadstat), Int, (Ptr{Nothing}, Ptr{Nothing}, Cint), writer, io, Cint(row_count))
end

function readstat_insert_double_value(writer, variable, item)
return ccall((:readstat_insert_double_value, libreadstat), Int, (Ptr{Nothing}, Ptr{Nothing}, Any), writer, variable, item)
end
55 changes: 54 additions & 1 deletion src/ReadStat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -248,17 +248,70 @@ function Parser()
ccall((:readstat_set_value_handler, libreadstat), Int, (Ptr{Nothing}, Ptr{Nothing}), parser, val_fxn)
ccall((:readstat_set_value_label_handler, libreadstat), Int, (Ptr{Nothing}, Ptr{Nothing}), parser, label_fxn)
return parser
end
end

function parse_data_file!(ds::ReadStatDataFrame, parser::Ptr{Nothing}, filename::AbstractString, filetype::Val)
retval = readstat_parse(filename, filetype, parser, ds)
readstat_parser_free(parser)
retval == 0 || error("Error parsing $filename: $retval")
end


function handle_write!()

end
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function probably has to look something like:

 function handle_!(data::Ptr, len::Int, ctx::Ptr)
          io = unsafe_pointer_to_objref(ctx)
          actual_data = unsafe_wrap(data, len)
          write(io, actual_data)
       end


function Writer(source; file_label="File Label")
writer = ccall((:readstat_writer_init, libreadstat), Ptr{Nothing}, ())
write_bytes = @cfunction(handle_write!, Cint, (Cint, Cint, Ptr{ReadStatDataFrame}))
ccall((:readstat_set_data_writer, libreadstat), Int, (Ptr{Nothing}, ), parser, write_bytes)
ccall((:readstat_writer_set_file_label, libreadstat, Cvoid, (Ptr{Nothing}, Cstring), writer, file_label)
return writer
end

function write_data_file(filename::AbstractString, filetype::Val, io::IO, source)
writer = Writer(source)
fields = fieldnames(eltype(source))
variables_array = []

for field in fields:
variable = ccall((:readstat_add_variable, libreadstat), Ptr{Nothing}, (Ptr{Nothing}, Cstring, Cint, Cint), writer, String(field), READSTAT_TYPE_DOUBLE, 0); # TODO: know width
readstat_variable_set_label(variable, String(field))
variables_array.push!(variable)

variables = NamedTuple{(fields...,)}((variables...,)) # generate a NamedTuple for variables


if Base.IteratorSize(source) == Base.HasLength(): # TODO: what about HasShape
row_count = length(q)
else: #fallback
row_count = 0
for _ in source
row_count += 1
end

readstat_begin_writing(writer, filetype, io, row_count)

for row in source
readstat_begin_row(writer)
for field in fields
readstat_insert_double_value(writer, variables[field], row[field]) # TODO: more than double
end
readstat_end_row(writer);
end

ccall((:readstat_end_writing, libreadstat), Int, (Ptr{Nothing}), writer)
ccall((:readstat_writer_free, libreadstat), Cvoid, (Ptr{Nothing}), writer)
end

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))

write_dta(filename::AbstractString, io::IO, source) = write_data_file(filename, Val(:dta), io, source)
write_sav(filename::AbstractString, io::IO, source) = write_data_file(filename, Val(:sav), io, source)
write_por(filename::AbstractString, io::IO, source) = write_data_file(filename, Val(:por), io, source)
write_sas7bdat(filename::AbstractString, io::IO, source) = write_data_file(filename, Val(:sas7bdat), io, source)

end #module ReadStat