Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
odow committed Mar 26, 2024
1 parent 646381c commit edbbe58
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 31 deletions.
35 changes: 29 additions & 6 deletions scripts/generate.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,26 @@ context = create_context(
)
build!(context)

function replace_line_cstring(line, signature)
changes = getindex.(
signature,
findall(r"char(\s*?\*| [a-z0-9]+?\[\])", signature),
)
last_index = 1
outputs = String[]
for (change, range) in zip(changes, findall("Ptr{UInt8}", line))
push!(outputs, line[last_index:(first(range)-1)])
if endswith(change, "*")
push!(outputs, "Cstring")
else
push!(outputs, line[range])
end
last_index = last(range) + 1
end
push!(outputs, line[last_index:end])
return join(outputs)
end

function postprocess(filename)
contents = read(filename, String);
# Remove the deprecated if-else blocks
Expand Down Expand Up @@ -46,7 +66,7 @@ function postprocess(filename)
# XPRSsetcheckedmode(int checkedmode)
xprs_signatures = Dict{String,String}()
for line in readlines(xprs_filename)
if (m = match(r"(XPRS[a-z0-9\_]+?)\(.+?\)", line)) !== nothing
if (m = match(r"XPRS_CC (XPRS[a-z0-9\_]+?)\(.+?\)", line)) !== nothing
xprs_signatures[m.captures[1]] = m.match
end
end
Expand Down Expand Up @@ -85,11 +105,14 @@ function postprocess(filename)
if occursin("Ptr{UInt8}", line)
# Replace Ptr{Cchar} with Cstring for backward compatibility
# with older versions of Xpress.jl
if (m = match(r"ccall\(\(:(XPRS.+?)\,", line)) !== nothing
if occursin("char*", xprs_signatures[m.captures[1]])
line = replace(line, "Ptr{UInt8}" => "Cstring")
end
end
#
# We need to replace char* with Ptr{UInt8} and char[] with
# Cstring.
map = Dict("char*" => "Cstring", "char" => "Ptr{UInt8}")
m = match(r"ccall\(\(:(XPRS.+?)\,", line)
@assert m !== nothing
signature = xprs_signatures[m.captures[1]]
line = replace_line_cstring(line, signature)
end
if !(isempty(line) && isempty(last_line))
println(io, line)
Expand Down
Loading

0 comments on commit edbbe58

Please sign in to comment.