From bc64e7a8abd95e3bf0a7ddf2ac0e0ae498f8eb3a Mon Sep 17 00:00:00 2001 From: Paul Ramsey Date: Wed, 15 Dec 2010 00:24:34 +0000 Subject: [PATCH] Remove some calls to memcpy in favor of API functions. git-svn-id: http://svn.osgeo.org/postgis/trunk@6384 b70326c6-7e19-0410-871a-916f4a2858ee --- postgis/lwgeom_geos_clean.c | 2 +- postgis/lwgeom_in_gml.c | 7 ++----- postgis/lwgeom_in_kml.c | 7 ++----- postgis/lwgeom_ogc.c | 17 ++++++----------- postgis/lwgeom_transform.c | 13 ++++--------- 5 files changed, 15 insertions(+), 31 deletions(-) diff --git a/postgis/lwgeom_geos_clean.c b/postgis/lwgeom_geos_clean.c index e7d7ea8bd..716cc237e 100644 --- a/postgis/lwgeom_geos_clean.c +++ b/postgis/lwgeom_geos_clean.c @@ -166,7 +166,7 @@ lwgeom_make_geos_friendly(LWGEOM *geom) case MULTISURFACETYPE: case MULTICURVETYPE: default: - lwerror("unsupported input geometry type: %d", geom->type); + lwerror("unsupported input geometry type: %s (%d)", lwtype_name(geom->type), geom->type); break; } return 0; diff --git a/postgis/lwgeom_in_gml.c b/postgis/lwgeom_in_gml.c index 6a6dbab0c..91e995b3e 100644 --- a/postgis/lwgeom_in_gml.c +++ b/postgis/lwgeom_in_gml.c @@ -89,11 +89,8 @@ Datum geom_from_gml(PG_FUNCTION_ARGS) /* Get the GML stream */ if (PG_ARGISNULL(0)) PG_RETURN_NULL(); xml_input = PG_GETARG_TEXT_P(0); - - xml_size = VARSIZE(xml_input) - VARHDRSZ; /* actual letters */ - xml = palloc(xml_size + 1); /* +1 for null */ - memcpy(xml, VARDATA(xml_input), xml_size); - xml[xml_size] = 0; /* null term */ + xml = text2cstring(xml_input); + xml_size = VARSIZE(xml_input) - VARHDRSZ; /* Begin to Parse XML doc */ xmlInitParser(); diff --git a/postgis/lwgeom_in_kml.c b/postgis/lwgeom_in_kml.c index 1be11d770..5952c4f7c 100644 --- a/postgis/lwgeom_in_kml.c +++ b/postgis/lwgeom_in_kml.c @@ -70,11 +70,8 @@ Datum geom_from_kml(PG_FUNCTION_ARGS) /* Get the KML stream */ if (PG_ARGISNULL(0)) PG_RETURN_NULL(); xml_input = PG_GETARG_TEXT_P(0); - - xml_size = VARSIZE(xml_input) - VARHDRSZ; /* actual letters */ - xml = palloc(xml_size + 1); /* +1 for null */ - memcpy(xml, VARDATA(xml_input), xml_size); - xml[xml_size] = 0; /* null term */ + xml = text2cstring(xml_input); + xml_size = VARSIZE(xml_input) - VARHDRSZ; /* Begin to Parse XML doc */ xmlInitParser(); diff --git a/postgis/lwgeom_ogc.c b/postgis/lwgeom_ogc.c index d495b88c3..507d28e77 100644 --- a/postgis/lwgeom_ogc.c +++ b/postgis/lwgeom_ogc.c @@ -169,9 +169,8 @@ PG_FUNCTION_INFO_V1(geometry_geometrytype); Datum geometry_geometrytype(PG_FUNCTION_ARGS) { PG_LWGEOM *lwgeom; - char *type_text; + text *type_text; char *type_str = palloc(32); - size_t size; lwgeom = (PG_LWGEOM*)PG_DETOAST_DATUM(PG_GETARG_DATUM(0)); @@ -181,15 +180,13 @@ Datum geometry_geometrytype(PG_FUNCTION_ARGS) /* Build up the output string */ strncat(type_str, "ST_", 32); strncat(type_str, lwtype_name(pglwgeom_get_type(lwgeom)), 32); - size = strlen(type_str) + VARHDRSZ; - + /* Build a text type to store things in */ - type_text = lwalloc(size); - memcpy(VARDATA(type_text),type_str, size - VARHDRSZ); + type_text = cstring2text(type_str); pfree(type_str); - SET_VARSIZE(type_text, size); + PG_FREE_IF_COPY(lwgeom, 0); - PG_RETURN_POINTER(type_text); + PG_RETURN_TEXT_P(type_text); } @@ -808,9 +805,7 @@ Datum LWGEOM_asText(PG_FUNCTION_ARGS) lwgeom_free(lwgeom); /* Write to text and free the WKT */ - result = palloc(wkt_size - 1 + VARHDRSZ); - memcpy(VARDATA(result), wkt, wkt_size - 1); - SET_VARSIZE(result, wkt_size - 1 + VARHDRSZ); + result = cstring2text(wkt); pfree(wkt); /* Return the text */ diff --git a/postgis/lwgeom_transform.c b/postgis/lwgeom_transform.c index c83b084cb..8cb2cb43e 100644 --- a/postgis/lwgeom_transform.c +++ b/postgis/lwgeom_transform.c @@ -910,18 +910,13 @@ Datum transform_geom(PG_FUNCTION_ARGS) if (!IsPROJ4LibPathSet) SetPROJ4LibPath(); + /* Read the arguments */ input_proj4_text = (PG_GETARG_TEXT_P(1)); output_proj4_text = (PG_GETARG_TEXT_P(2)); - input_proj4 = (char *)palloc(VARSIZE(input_proj4_text)+1-4); - memcpy(input_proj4, VARDATA(input_proj4_text), - VARSIZE(input_proj4_text)-VARHDRSZ); - input_proj4[VARSIZE(input_proj4_text)-VARHDRSZ] = 0; /* null terminate */ - - output_proj4 = (char *) palloc(VARSIZE(output_proj4_text) +1-VARHDRSZ); - memcpy(output_proj4, VARDATA(output_proj4_text), - VARSIZE(output_proj4_text)-VARHDRSZ); - output_proj4[VARSIZE(output_proj4_text)-VARHDRSZ] = 0; /* null terminate */ + /* Convert from text to cstring for libproj */ + input_proj4 = text2cstring(input_proj4_text); + output_proj4 = text2cstring(output_proj4_text); /* make input and output projection objects */ input_pj = make_project(input_proj4);