Skip to content

Commit

Permalink
rename to string_copy_free
Browse files Browse the repository at this point in the history
Also adds the context argument, though it is not used yet.
  • Loading branch information
visr committed Jul 31, 2022
1 parent c9df580 commit 6fd06db
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 26 deletions.
2 changes: 1 addition & 1 deletion gen/generator.jl
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ function rewrite(ex::Expr, cursor::Clang.CLFunctionDecl)
if return_type_is_const_char(cursor)
:(unsafe_string($cc))
else
:(transform_c_string($cc))
:(string_copy_free($cc))
end
else
cc
Expand Down
20 changes: 10 additions & 10 deletions src/LibGEOS.jl
Original file line number Diff line number Diff line change
Expand Up @@ -92,15 +92,6 @@ export Point,
STRtree,
query

function transform_c_string(s::Cstring)::String
copy = unsafe_string(s)
global _context
GEOSFree_r(_context.ptr, Ptr{Cvoid}(s))
return copy
end

include("libgeos_api.jl")

mutable struct GEOSError <: Exception
msg::String
end
Expand All @@ -115,7 +106,7 @@ function geosjl_errorhandler(message::Ptr{UInt8}, userdata)
end

mutable struct GEOSContext
ptr::GEOSContextHandle_t
ptr::Ptr{Cvoid} # GEOSContextHandle_t

function GEOSContext()
context = new(GEOS_init_r())
Expand All @@ -129,6 +120,15 @@ mutable struct GEOSContext
end
end

"Get a copy of a string from GEOS, freeing the GEOS managed memory."
function string_copy_free(s::Cstring, context::GEOSContext = _context)::String
copy = unsafe_string(s)
GEOSFree_r(context.ptr, pointer(s))
return copy
end

include("libgeos_api.jl")

mutable struct WKTReader
ptr::Ptr{GEOSWKTReader}

Expand Down
28 changes: 14 additions & 14 deletions src/libgeos_api.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1289,7 +1289,7 @@ function GEOSRelatePattern_r(handle, g1, g2, pat)
end

function GEOSRelate_r(handle, g1, g2)
transform_c_string(
string_copy_free(
@ccall(
libgeos.GEOSRelate_r(
handle::GEOSContextHandle_t,
Expand All @@ -1309,7 +1309,7 @@ function GEOSRelatePatternMatch_r(handle, mat, pat)
end

function GEOSRelateBoundaryNodeRule_r(handle, g1, g2, bnr)
transform_c_string(
string_copy_free(
@ccall(
libgeos.GEOSRelateBoundaryNodeRule_r(
handle::GEOSContextHandle_t,
Expand All @@ -1330,7 +1330,7 @@ function GEOSisValid_r(handle, g)
end

function GEOSisValidReason_r(handle, g)
transform_c_string(
string_copy_free(
@ccall(
libgeos.GEOSisValidReason_r(
handle::GEOSContextHandle_t,
Expand Down Expand Up @@ -1408,7 +1408,7 @@ function GEOSRemoveRepeatedPoints_r(handle, g, tolerance)
end

function GEOSGeomType_r(handle, g)
transform_c_string(
string_copy_free(
@ccall(
libgeos.GEOSGeomType_r(
handle::GEOSContextHandle_t,
Expand Down Expand Up @@ -1827,7 +1827,7 @@ function GEOSWKTWriter_destroy_r(handle, writer)
end

function GEOSWKTWriter_write_r(handle, writer, g)
transform_c_string(
string_copy_free(
@ccall(
libgeos.GEOSWKTWriter_write_r(
handle::GEOSContextHandle_t,
Expand Down Expand Up @@ -2038,7 +2038,7 @@ function GEOSGeoJSONWriter_destroy_r(handle, writer)
end

function GEOSGeoJSONWriter_writeGeometry_r(handle, writer, g, indent)
transform_c_string(
string_copy_free(
@ccall(
libgeos.GEOSGeoJSONWriter_writeGeometry_r(
handle::GEOSContextHandle_t,
Expand Down Expand Up @@ -2308,7 +2308,7 @@ function GEOSGeom_destroy(g)
end

function GEOSGeomType(g)
transform_c_string(@ccall(libgeos.GEOSGeomType(g::Ptr{GEOSGeometry})::Cstring))
string_copy_free(@ccall(libgeos.GEOSGeomType(g::Ptr{GEOSGeometry})::Cstring))
end

function GEOSGeomTypeId(g)
Expand Down Expand Up @@ -2454,7 +2454,7 @@ function GEOSisValid(g)
end

function GEOSisValidReason(g)
transform_c_string(@ccall(libgeos.GEOSisValidReason(g::Ptr{GEOSGeometry})::Cstring))
string_copy_free(@ccall(libgeos.GEOSisValidReason(g::Ptr{GEOSGeometry})::Cstring))
end

function GEOSisValidDetail(g, flags, reason, location)
Expand Down Expand Up @@ -3057,7 +3057,7 @@ function GEOSRelatePattern(g1, g2, pat)
end

function GEOSRelate(g1, g2)
transform_c_string(
string_copy_free(
@ccall(libgeos.GEOSRelate(g1::Ptr{GEOSGeometry}, g2::Ptr{GEOSGeometry})::Cstring)
)
end
Expand All @@ -3067,7 +3067,7 @@ function GEOSRelatePatternMatch(mat, pat)
end

function GEOSRelateBoundaryNodeRule(g1, g2, bnr)
transform_c_string(
string_copy_free(
@ccall(
libgeos.GEOSRelateBoundaryNodeRule(
g1::Ptr{GEOSGeometry},
Expand Down Expand Up @@ -3294,7 +3294,7 @@ function GEOSWKTWriter_destroy(writer)
end

function GEOSWKTWriter_write(writer, g)
transform_c_string(
string_copy_free(
@ccall(
libgeos.GEOSWKTWriter_write(
writer::Ptr{GEOSWKTWriter},
Expand Down Expand Up @@ -3450,7 +3450,7 @@ function GEOSGeoJSONWriter_destroy(writer)
end

function GEOSGeoJSONWriter_writeGeometry(writer, g, indent)
transform_c_string(
string_copy_free(
@ccall(
libgeos.GEOSGeoJSONWriter_writeGeometry(
writer::Ptr{GEOSGeoJSONWriter},
Expand Down Expand Up @@ -3511,7 +3511,7 @@ function GEOSGeomFromWKT_r(handle, wkt)
end

function GEOSGeomToWKT_r(handle, g)
transform_c_string(
string_copy_free(
@ccall(
libgeos.GEOSGeomToWKT_r(
handle::GEOSContextHandle_t,
Expand Down Expand Up @@ -3577,7 +3577,7 @@ function GEOSGeomFromWKT(wkt)
end

function GEOSGeomToWKT(g)
transform_c_string(@ccall(libgeos.GEOSGeomToWKT(g::Ptr{GEOSGeometry})::Cstring))
string_copy_free(@ccall(libgeos.GEOSGeomToWKT(g::Ptr{GEOSGeometry})::Cstring))
end

function GEOS_getWKBOutputDims()
Expand Down
3 changes: 2 additions & 1 deletion test/test_invalid_geometry.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@
polygon = LibGEOS._readgeom("POLYGON((0 0, 10 0, 10 10, 0 10, 0 0),
(15 15, 15 20, 20 20, 20 15, 15 15))")
@test !LibGEOS.isValid(polygon)
@test LibGEOS.GEOSisValidReason_r(LibGEOS._context.ptr, polygon) == "Hole lies outside shell[15 15]"
@test LibGEOS.GEOSisValidReason_r(LibGEOS._context.ptr, polygon) ==
"Hole lies outside shell[15 15]"
end

0 comments on commit 6fd06db

Please sign in to comment.