Skip to content
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

Drop cstring2text/text2cstring in favor of Postgresql functions #213

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
34 changes: 0 additions & 34 deletions libpgcommon/lwgeom_pg.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,40 +31,6 @@

#define PGC_ERRMSG_MAXLEN 2048 //256

/**
* Utility to convert cstrings to textp pointers
*/
text*
cstring2text(const char *cstring)
{
text *output;
size_t sz;

/* Guard against null input */
if( !cstring )
return NULL;

sz = strlen(cstring);
output = palloc(sz + VARHDRSZ);
if ( ! output )
return NULL;
SET_VARSIZE(output, sz + VARHDRSZ);
if ( sz )
memcpy(VARDATA(output),cstring,sz);
return output;
}

char*
text2cstring(const text *textptr)
{
size_t size = VARSIZE(textptr) - VARHDRSZ;
char *str = lwalloc(size+1);
memcpy(str, VARDATA(textptr), size);
str[size]='\0';
return str;
}


/*
* Error message parsing functions
*
Expand Down
12 changes: 0 additions & 12 deletions libpgcommon/lwgeom_pg.h
Original file line number Diff line number Diff line change
Expand Up @@ -124,18 +124,6 @@ GSERIALIZED* geography_serialize(LWGEOM *lwgeom);
*/
int gserialized_datum_get_gbox_p(Datum gsdatum, GBOX *gbox);

/**
* Convert cstrings (null-terminated byte array) to textp pointers
* (PgSQL varlena structure with VARSIZE header).
*/
text* cstring2text(const char *cstring);

/**
* Convert textp (PgSQL varlena structure with VARSIZE header) to
* cstrings (null-terminated byte array).
*/
char* text2cstring(const text *textptr);

/*
* For PostgreSQL >= 8.5 redefine the STATRELATT macro to its
* new value of STATRELATTINH
Expand Down
10 changes: 5 additions & 5 deletions postgis/geography_inout.c
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ Datum geography_as_gml(PG_FUNCTION_ARGS)
PG_RETURN_NULL();

/* Turn string result into text for return */
result = cstring2text(gml);
result = cstring_to_text(gml);
lwfree(gml);

PG_RETURN_TEXT_P(result);
Expand Down Expand Up @@ -415,7 +415,7 @@ Datum geography_as_kml(PG_FUNCTION_ARGS)
if ( ! kml )
PG_RETURN_NULL();

result = cstring2text(kml);
result = cstring_to_text(kml);
lwfree(kml);

PG_RETURN_TEXT_P(result);
Expand Down Expand Up @@ -460,7 +460,7 @@ Datum geography_as_svg(PG_FUNCTION_ARGS)
lwgeom_free(lwgeom);
PG_FREE_IF_COPY(g, 0);

result = cstring2text(svg);
result = cstring_to_text(svg);
lwfree(svg);

PG_RETURN_TEXT_P(result);
Expand Down Expand Up @@ -537,7 +537,7 @@ Datum geography_as_geojson(PG_FUNCTION_ARGS)
PG_FREE_IF_COPY(g, 1);
if (srs) pfree(srs);

result = cstring2text(geojson);
result = cstring_to_text(geojson);
lwfree(geojson);

PG_RETURN_TEXT_P(result);
Expand All @@ -557,7 +557,7 @@ Datum geography_from_text(PG_FUNCTION_ARGS)
text *wkt_text = PG_GETARG_TEXT_P(0);

/* Extract the cstring from the varlena */
char *wkt = text2cstring(wkt_text);
char *wkt = text_to_cstring(wkt_text);

/* Pass the cstring to the input parser, and magic occurs! */
if ( lwgeom_parse_wkt(&lwg_parser_result, wkt, LW_PARSER_CHECK_ALL) == LW_FAILURE )
Expand Down
30 changes: 15 additions & 15 deletions postgis/gserialized_estimate.c
Original file line number Diff line number Diff line change
Expand Up @@ -976,7 +976,7 @@ pg_get_nd_stats(const Oid table_oid, AttrNumber att_num, int mode, bool only_par
static ND_STATS*
pg_get_nd_stats_by_name(const Oid table_oid, const text *att_text, int mode, bool only_parent)
{
const char *att_name = text2cstring(att_text);
const char *att_name = text_to_cstring(att_text);
AttrNumber att_num;

/* We know the name? Look up the num */
Expand Down Expand Up @@ -2052,11 +2052,11 @@ Datum _postgis_gserialized_stats(PG_FUNCTION_ARGS)
/* Retrieve the stats object */
nd_stats = pg_get_nd_stats_by_name(table_oid, att_text, mode, only_parent);
if ( ! nd_stats )
elog(ERROR, "stats for \"%s.%s\" do not exist", get_rel_name(table_oid), text2cstring(att_text));
elog(ERROR, "stats for \"%s.%s\" do not exist", get_rel_name(table_oid), text_to_cstring(att_text));

/* Convert to JSON */
str = nd_stats_to_json(nd_stats);
json = cstring2text(str);
json = cstring_to_text(str);
pfree(str);
pfree(nd_stats);
PG_RETURN_TEXT_P(json);
Expand Down Expand Up @@ -2086,7 +2086,7 @@ Datum _postgis_gserialized_sel(PG_FUNCTION_ARGS)
nd_stats = pg_get_nd_stats_by_name(table_oid, att_text, mode, false);

if ( ! nd_stats )
elog(ERROR, "stats for \"%s.%s\" do not exist", get_rel_name(table_oid), text2cstring(att_text));
elog(ERROR, "stats for \"%s.%s\" do not exist", get_rel_name(table_oid), text_to_cstring(att_text));

/* Calculate the gbox */
if ( ! gserialized_datum_get_gbox_p(geom_datum, &gbox) )
Expand Down Expand Up @@ -2123,16 +2123,16 @@ Datum _postgis_gserialized_joinsel(PG_FUNCTION_ARGS)
nd_stats2 = pg_get_nd_stats_by_name(table_oid2, att_text2, mode, false);

if ( ! nd_stats1 )
elog(ERROR, "stats for \"%s.%s\" do not exist", get_rel_name(table_oid1), text2cstring(att_text1));
elog(ERROR, "stats for \"%s.%s\" do not exist", get_rel_name(table_oid1), text_to_cstring(att_text1));

if ( ! nd_stats2 )
elog(ERROR, "stats for \"%s.%s\" do not exist", get_rel_name(table_oid2), text2cstring(att_text2));
elog(ERROR, "stats for \"%s.%s\" do not exist", get_rel_name(table_oid2), text_to_cstring(att_text2));

/* Check if we've been asked to not use 2d mode */
if ( ! PG_ARGISNULL(4) )
{
text *modetxt = PG_GETARG_TEXT_P(4);
char *modestr = text2cstring(modetxt);
char *modestr = text_to_cstring(modetxt);
if ( modestr[0] == 'N' )
mode = 0;
}
Expand Down Expand Up @@ -2294,8 +2294,8 @@ Datum gserialized_estimated_extent(PG_FUNCTION_ARGS)

if ( PG_NARGS() == 4 )
{
nsp = text2cstring(PG_GETARG_TEXT_P(0));
tbl = text2cstring(PG_GETARG_TEXT_P(1));
nsp = text_to_cstring(PG_GETARG_TEXT_P(0));
tbl = text_to_cstring(PG_GETARG_TEXT_P(1));
col = PG_GETARG_TEXT_P(2);
only_parent = PG_GETARG_BOOL(3);
nsp_tbl = palloc(strlen(nsp) + strlen(tbl) + 6);
Expand All @@ -2305,8 +2305,8 @@ Datum gserialized_estimated_extent(PG_FUNCTION_ARGS)
}
else if ( PG_NARGS() == 3 )
{
nsp = text2cstring(PG_GETARG_TEXT_P(0));
tbl = text2cstring(PG_GETARG_TEXT_P(1));
nsp = text_to_cstring(PG_GETARG_TEXT_P(0));
tbl = text_to_cstring(PG_GETARG_TEXT_P(1));
col = PG_GETARG_TEXT_P(2);
nsp_tbl = palloc(strlen(nsp) + strlen(tbl) + 6);
sprintf(nsp_tbl, "\"%s\".\"%s\"", nsp, tbl);
Expand All @@ -2315,7 +2315,7 @@ Datum gserialized_estimated_extent(PG_FUNCTION_ARGS)
}
else if ( PG_NARGS() == 2 )
{
tbl = text2cstring(PG_GETARG_TEXT_P(0));
tbl = text_to_cstring(PG_GETARG_TEXT_P(0));
col = PG_GETARG_TEXT_P(1);
nsp_tbl = palloc(strlen(tbl) + 3);
sprintf(nsp_tbl, "\"%s\"", tbl);
Expand All @@ -2332,7 +2332,7 @@ Datum gserialized_estimated_extent(PG_FUNCTION_ARGS)
/* Read the extent from the head of the spatial index, if there is one */
idx_oid = table_get_spatial_index(tbl_oid, col, &key_type);
if (!idx_oid)
elog(DEBUG2, "index for \"%s.%s\" does not exist", tbl, text2cstring(col));
elog(DEBUG2, "index for \"%s.%s\" does not exist", tbl, text_to_cstring(col));
gbox = spatial_index_read_extent(idx_oid, key_type);
#endif

Expand All @@ -2344,7 +2344,7 @@ Datum gserialized_estimated_extent(PG_FUNCTION_ARGS)

/* Error out on no stats */
if ( ! nd_stats ) {
elog(WARNING, "stats for \"%s.%s\" do not exist", tbl, text2cstring(col));
elog(WARNING, "stats for \"%s.%s\" do not exist", tbl, text_to_cstring(col));
PG_RETURN_NULL();
}

Expand Down Expand Up @@ -2411,7 +2411,7 @@ table_get_spatial_index(Oid tbl_oid, text *col, int *key_type)
ListCell *lc;
List *idx_list;
Oid result = InvalidOid;
char *colname = text2cstring(col);
char *colname = text_to_cstring(col);

/* Lookup our spatial index key types */
Oid b2d_oid = typname_to_oid(INDEX_KEY_2D);
Expand Down
2 changes: 1 addition & 1 deletion postgis/gserialized_typmod.c
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@ Datum postgis_typmod_type(PG_FUNCTION_ARGS)
if ( typmod >= 0 && TYPMOD_GET_M(typmod) )
ptr += sprintf(ptr, "%s", "M");

stext = cstring2text(s);
stext = cstring_to_text(s);
pfree(s);
PG_RETURN_TEXT_P(stext);
}
Expand Down
13 changes: 7 additions & 6 deletions postgis/lwgeom_export.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include "float.h" /* for DBL_DIG */
#include "postgres.h"
#include "executor/spi.h"
#include "utils/builtins.h"

#include "../postgis_config.h"
#include "lwgeom_pg.h"
Expand Down Expand Up @@ -304,7 +305,7 @@ Datum LWGEOM_asGML(PG_FUNCTION_ARGS)
if ( ! gml )
PG_RETURN_NULL();

result = cstring2text(gml);
result = cstring_to_text(gml);
lwfree(gml);
PG_RETURN_TEXT_P(result);
}
Expand Down Expand Up @@ -379,7 +380,7 @@ Datum LWGEOM_asKML(PG_FUNCTION_ARGS)
if( ! kml )
PG_RETURN_NULL();

result = cstring2text(kml);
result = cstring_to_text(kml);
lwfree(kml);

PG_RETURN_POINTER(result);
Expand Down Expand Up @@ -480,7 +481,7 @@ Datum LWGEOM_asGeoJson(PG_FUNCTION_ARGS)

if (srs) pfree(srs);

result = cstring2text(geojson);
result = cstring_to_text(geojson);
lwfree(geojson);

PG_FREE_IF_COPY(geom, 0);
Expand Down Expand Up @@ -520,7 +521,7 @@ Datum LWGEOM_asSVG(PG_FUNCTION_ARGS)

lwgeom = lwgeom_from_gserialized(geom);
svg = lwgeom_to_svg(lwgeom, precision, relative);
result = cstring2text(svg);
result = cstring_to_text(svg);
lwgeom_free(lwgeom);
pfree(svg);
PG_FREE_IF_COPY(geom, 0);
Expand Down Expand Up @@ -619,7 +620,7 @@ Datum LWGEOM_asX3D(PG_FUNCTION_ARGS)
lwgeom_free(lwgeom);
PG_FREE_IF_COPY(geom, 1);

result = cstring2text(x3d);
result = cstring_to_text(x3d);
lwfree(x3d);

PG_RETURN_TEXT_P(result);
Expand Down Expand Up @@ -657,7 +658,7 @@ Datum LWGEOM_asEncodedPolyline(PG_FUNCTION_ARGS)
lwgeom_free(lwgeom);
PG_FREE_IF_COPY(geom, 0);

result = cstring2text(encodedpolyline);
result = cstring_to_text(encodedpolyline);
lwfree(encodedpolyline);

PG_RETURN_TEXT_P(result);
Expand Down
23 changes: 12 additions & 11 deletions postgis/lwgeom_functions_basic.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@

#include "postgres.h"
#include "fmgr.h"
#include "utils/elog.h"
#include "utils/array.h"
#include "utils/builtins.h"
#include "utils/elog.h"
#include "utils/geo_decls.h"

#include "../postgis_config.h"
Expand Down Expand Up @@ -140,7 +141,7 @@ Datum LWGEOM_summary(PG_FUNCTION_ARGS)
lwgeom_free(lwgeom);

/* create a text obj to return */
mytext = cstring2text(result);
mytext = cstring_to_text(result);
pfree(result);

PG_FREE_IF_COPY(geom,0);
Expand All @@ -151,23 +152,23 @@ PG_FUNCTION_INFO_V1(postgis_version);
Datum postgis_version(PG_FUNCTION_ARGS)
{
char *ver = POSTGIS_VERSION;
text *result = cstring2text(ver);
text *result = cstring_to_text(ver);
PG_RETURN_TEXT_P(result);
}

PG_FUNCTION_INFO_V1(postgis_liblwgeom_version);
Datum postgis_liblwgeom_version(PG_FUNCTION_ARGS)
{
const char *ver = lwgeom_version();
text *result = cstring2text(ver);
text *result = cstring_to_text(ver);
PG_RETURN_TEXT_P(result);
}

PG_FUNCTION_INFO_V1(postgis_lib_version);
Datum postgis_lib_version(PG_FUNCTION_ARGS)
{
char *ver = POSTGIS_LIB_VERSION;
text *result = cstring2text(ver);
text *result = cstring_to_text(ver);
PG_RETURN_TEXT_P(result);
}

Expand All @@ -179,7 +180,7 @@ Datum postgis_svn_version(PG_FUNCTION_ARGS)
if ( rev > 0 )
{
snprintf(ver, 32, "%d", rev);
PG_RETURN_TEXT_P(cstring2text(ver));
PG_RETURN_TEXT_P(cstring_to_text(ver));
}
else
PG_RETURN_NULL();
Expand All @@ -189,7 +190,7 @@ PG_FUNCTION_INFO_V1(postgis_lib_build_date);
Datum postgis_lib_build_date(PG_FUNCTION_ARGS)
{
char *ver = POSTGIS_BUILD_DATE;
text *result = cstring2text(ver);
text *result = cstring_to_text(ver);
PG_RETURN_TEXT_P(result);
}

Expand All @@ -202,7 +203,7 @@ Datum postgis_scripts_released(PG_FUNCTION_ARGS)
snprintf(ver, 64, "%s r%d", POSTGIS_LIB_VERSION, POSTGIS_SVN_REVISION);
ver[63] = '\0';

result = cstring2text(ver);
result = cstring_to_text(ver);
PG_RETURN_TEXT_P(result);
}

Expand All @@ -227,7 +228,7 @@ PG_FUNCTION_INFO_V1(postgis_libxml_version);
Datum postgis_libxml_version(PG_FUNCTION_ARGS)
{
char *ver = POSTGIS_LIBXML2_VERSION;
text *result = cstring2text(ver);
text *result = cstring_to_text(ver);
PG_RETURN_TEXT_P(result);
}

Expand Down Expand Up @@ -2345,7 +2346,7 @@ Datum LWGEOM_asEWKT(PG_FUNCTION_ARGS)
lwgeom_free(lwgeom);

/* Write to text and free the WKT */
result = cstring2text(wkt);
result = cstring_to_text(wkt);
pfree(wkt);

/* Return the text */
Expand Down Expand Up @@ -2676,7 +2677,7 @@ Datum ST_GeoHash(PG_FUNCTION_ARGS)
if ( ! geohash )
PG_RETURN_NULL();

result = cstring2text(geohash);
result = cstring_to_text(geohash);
pfree(geohash);

PG_RETURN_TEXT_P(result);
Expand Down
Loading