Skip to content

Commit

Permalink
New wrapper script for functions (#141)
Browse files Browse the repository at this point in the history
  • Loading branch information
meggart committed Feb 22, 2021
1 parent 8e25ee6 commit 4179cd6
Show file tree
Hide file tree
Showing 5 changed files with 1,958 additions and 2,248 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ uuid = "30363a11-5582-574a-97bb-aa9a979735b9"
keywords = ["NetCDF", "IO"]
license = "MIT"
desc = "NetCDF file reading and writing"
version = "0.11.1"
version = "0.11.2"

[deps]
DiskArrays = "3c3547ce-8d99-4f5e-a174-61eb10b00ae3"
Expand Down
103 changes: 103 additions & 0 deletions gen/wrap_functions.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
#This is an additional wrapper to generate julia function definitions
# from the netcdf.h header file. This is necessary because it looks like
# Clang.jl had problems translating size_t and ptrdiff_t to the correct
# Julia types

#A small translator for c types:
tdict = Dict(
"int"=>"Cint",
"size_t"=>"Csize_t",
"char"=>"Cchar",
"NC_Dispatch"=>"Cvoid",
"nc_type"=>"Cint",
"nc_vlen_t"=>"nc_vlen_t",
"void"=>"Cvoid",
"long long"=>"Clonglong",
"ptrdiff_t"=>"Cptrdiff_t",
"unsigned int"=>"Cuint",
"float"=>"Cfloat",
"unsigned char"=>"Cuchar",
"signed char"=>"Cchar",
"short"=>"Cshort",
"long"=>"Clong",
"double"=>"Cdouble",
"unsigned short"=>"Cushort",
"unsigned long"=>"Culong",
"unsigned long long"=>"Culonglong",
)

"Parses the signature from the c function argument and returns the Julia type as well as the argument name"
function parsesig(s,tdict)
s = strip(replace(s,"const"=>""))
nstar = count(isequal('*'),s)
s = replace(s,"*"=>"")
if endswith(s,"[]")
nstar += 1
s = replace(s,"[]"=>"")
end
spl = split(strip(s)," ", keepempty=false)
if length(spl) == 1
return tdict[spl[1]],"x"
end
if length(spl)>2
spl = (join(spl[1:end-1]," "), spl[end])
end
t, argname = spl
if nstar == 2
return "Ptr{Ptr{$(tdict[t])}}", argname
elseif nstar == 1
return "Ptr{$(tdict[t])}", argname[2:end]
else
return tdict[t], argname
end
end

"Write the function with name `fname` and signature `sig` to the IO stream `outp`"
function writefun(outp,fname,sig)
print(outp,"function ")
print(outp,fname)
print(outp,"(")
for (t,name) in sig
print(outp,name)
# if parse(t) <: Integer
# print(outp,"::Integer")
# end
print(outp,", ")
end
print(outp, ")\n")
println(outp, " check(ccall(")
println(outp, " (:$(fname), libnetcdf),")
println(outp, " Cint,")
print(outp, " (")
foreach(i->print(outp,string(i[1],", ")),sig)
println(outp,"),")
foreach(i->println(outp," $(i[2]),"),sig)
println(outp, " ))")
println(outp, "end")
end

include(joinpath(@__DIR__,"..","src","netcdf_constants.jl"))

function parseheader(input, output)
cheader = eachline(input) |> collect;
open(output,"w") do outp
ii = 1
while true
i1 = findnext(startswith("EXTERNL int"), cheader,ii)
i1 === nothing && break
i2 = findnext(contains(";"), cheader,i1)
cdef = join(cheader[i1:i2])
sig = match(r"int\s*(\w+)\((.*)\)",cdef)
if sig !== nothing
fname = sig.captures[1]
sig = parsesig.(split(sig.captures[2],","), Ref(tdict))
writefun(outp,fname,sig)
end
ii = i2+1
end
end
end

input = eachline(`locate netcdf.h`) |> first
output = joinpath(@__DIR__, "..", "src","netcdf_c.jl")
parseheader(input, output)
1 change: 1 addition & 0 deletions src/NetCDF.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ using Base.Cartesian
import DiskArrays: readblock!, writeblock!, AbstractDiskArray, eachchunk, GridChunks,
estimate_chunksize, haschunks, Chunked, Unchunked

include("netcdf_constants.jl")
include("netcdf_c.jl")

import Base.show
Expand Down
Loading

2 comments on commit 4179cd6

@meggart
Copy link
Member Author

Choose a reason for hiding this comment

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

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

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

Registration pull request created: JuliaRegistries/General/30600

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.11.2 -m "<description of version>" 4179cd6bddfe6335b0c1b2315b1240eb23d83fd3
git push origin v0.11.2

Please sign in to comment.