[postgis-commits] svn - r3026 - in trunk: liblwgeom liblwgeom/examples lwgeom

postgis-commits at postgis.refractions.net postgis-commits at postgis.refractions.net
Sun Sep 28 12:48:17 PDT 2008


Author: mcayland
Date: 2008-09-28 12:48:17 -0700 (Sun, 28 Sep 2008)
New Revision: 3026

Modified:
   trunk/liblwgeom/examples/unparser.c
   trunk/liblwgeom/liblwgeom.h
   trunk/liblwgeom/lwgeom.c
   trunk/liblwgeom/lwgparse.c
   trunk/liblwgeom/lwgunparse.c
   trunk/liblwgeom/wktparse.h
   trunk/lwgeom/lwgeom_functions_basic.c
   trunk/lwgeom/lwgeom_inout.c
   trunk/lwgeom/lwgeom_ogc.c
   trunk/lwgeom/lwgeom_pg.c
Log:
Update LWGEOM unparser to (E)WKT/WKB to resturn a LWGEOM_UNPARSER_RESULT structure instead of just the WKT/WKB character array. This is the same work done for r3023 but applied to the unparser instead.


Modified: trunk/liblwgeom/examples/unparser.c
===================================================================
--- trunk/liblwgeom/examples/unparser.c	2008-09-28 16:18:12 UTC (rev 3025)
+++ trunk/liblwgeom/examples/unparser.c	2008-09-28 19:48:17 UTC (rev 3026)
@@ -39,10 +39,11 @@
 	/*
 	 * An example to show how to call the WKT/WKB unparsers in liblwgeom
 	 */
+	LWGEOM_UNPARSER_RESULT lwg_unparser_result;
+	int result;
 
 	LWGEOM *lwgeom;
-	uchar *serialized_lwgeom, *wkt, *wkb;
-	size_t wkb_size;	
+	uchar *serialized_lwgeom;
 
 	POINTARRAY *pa;
 	POINT2D point2d;
@@ -69,14 +70,13 @@
 	serialized_lwgeom = lwgeom_serialize(lwgeom); 
 
 	/* Output the geometry in WKT and WKB */
-	wkt = serialized_lwgeom_to_ewkt(serialized_lwgeom, PARSER_CHECK_ALL);
-	printf("WKT format    : %s\n", wkt);
-	wkb = serialized_lwgeom_to_hexwkb(serialized_lwgeom, PARSER_CHECK_ALL, NDR, &wkb_size);
-	printf("HEXWKB format : %s\n\n", wkb); 
+	result = serialized_lwgeom_to_ewkt(&lwg_unparser_result, serialized_lwgeom, PARSER_CHECK_ALL);
+	printf("WKT format    : %s\n", lwg_unparser_result.wkoutput);
+	result = serialized_lwgeom_to_hexwkb(&lwg_unparser_result, serialized_lwgeom, PARSER_CHECK_ALL, NDR);
+	printf("HEXWKB format : %s\n\n", lwg_unparser_result.wkoutput); 
 
 	/* Free all of the allocated items */
-	lwfree(wkb);
-	lwfree(wkt);
+	lwfree(lwg_unparser_result.wkoutput);
 	lwfree(serialized_lwgeom);
 	pfree_point(testpoint);
 
@@ -105,14 +105,13 @@
 	serialized_lwgeom = lwgeom_serialize(lwgeom); 
 
 	/* Output the geometry in WKT and WKB */
-	wkt = serialized_lwgeom_to_ewkt(serialized_lwgeom, PARSER_CHECK_ALL);
-	printf("WKT format    : %s\n", wkt);
-	wkb = serialized_lwgeom_to_hexwkb(serialized_lwgeom, PARSER_CHECK_ALL, NDR, &wkb_size);
-	printf("HEXWKB format : %s\n\n", wkb); 
+	result = serialized_lwgeom_to_ewkt(&lwg_unparser_result, serialized_lwgeom, PARSER_CHECK_ALL);
+	printf("WKT format    : %s\n", lwg_unparser_result.wkoutput);
+	result = serialized_lwgeom_to_hexwkb(&lwg_unparser_result, serialized_lwgeom, PARSER_CHECK_ALL, NDR);
+	printf("HEXWKB format : %s\n\n", lwg_unparser_result.wkoutput); 
 
 	/* Free all of the allocated items */
-	lwfree(wkb);
-	lwfree(wkt);
+	lwfree(lwg_unparser_result.wkoutput);
 	lwfree(serialized_lwgeom);
 	pfree_line(testline);
 
@@ -177,14 +176,13 @@
 	serialized_lwgeom = lwgeom_serialize(lwgeom); 
 
 	/* Output the geometry in WKT and WKB */
-	wkt = serialized_lwgeom_to_ewkt(serialized_lwgeom, PARSER_CHECK_NONE);
-	printf("WKT format    : %s\n", wkt);
-	wkb = serialized_lwgeom_to_hexwkb(serialized_lwgeom, PARSER_CHECK_NONE, NDR, &wkb_size);
-	printf("HEXWKB format : %s\n\n", wkb); 
+	result = serialized_lwgeom_to_ewkt(&lwg_unparser_result, serialized_lwgeom, PARSER_CHECK_NONE);
+	printf("WKT format    : %s\n", lwg_unparser_result.wkoutput);
+	result = serialized_lwgeom_to_hexwkb(&lwg_unparser_result, serialized_lwgeom, PARSER_CHECK_NONE, NDR);
+	printf("HEXWKB format : %s\n\n", lwg_unparser_result.wkoutput); 
 
 	/* Free all of the allocated items */
-	lwfree(wkb);
-	lwfree(wkt);
+	lwfree(lwg_unparser_result.wkoutput);
 	lwfree(serialized_lwgeom);
 	pfree_polygon(testpoly);
 }

Modified: trunk/liblwgeom/liblwgeom.h
===================================================================
--- trunk/liblwgeom/liblwgeom.h	2008-09-28 16:18:12 UTC (rev 3025)
+++ trunk/liblwgeom/liblwgeom.h	2008-09-28 19:48:17 UTC (rev 3026)
@@ -1121,10 +1121,18 @@
  */
 typedef struct struct_lwgeom_parser_result
 {
-	uchar *serialized_lwgeom;
-	int size;
+	uchar *serialized_lwgeom;	/* Pointer to serialized LWGEOM */
+	int size;			/* Size of serialized LWGEOM in bytes */
 } LWGEOM_PARSER_RESULT;
 
+/*
+ * Unparser result structure: returns the result of attempting to convert LWGEOM to (E)WKT/(E)WKB 
+ */
+typedef struct struct_lwgeom_unparser_result
+{
+	char *wkoutput;			/* Pointer to WKT or WKB output */
+	int size;			/* Size of serialized LWGEOM in bytes */
+} LWGEOM_UNPARSER_RESULT;
 
 /* Parser access routines */
 extern char *lwgeom_to_ewkt(LWGEOM *lwgeom, int flags);
@@ -1132,10 +1140,10 @@
 extern LWGEOM *lwgeom_from_ewkb(uchar *ewkb, int flags, size_t ewkblen);
 extern uchar *lwgeom_to_ewkb(LWGEOM *lwgeom, int flags, char byteorder, size_t *ewkblen);
 
-extern char *serialized_lwgeom_to_ewkt(uchar *serialized, int flags);
+extern int serialized_lwgeom_to_ewkt(LWGEOM_UNPARSER_RESULT *lwg_unparser_result, uchar *serialized, int flags);
 extern int serialized_lwgeom_from_ewkt(LWGEOM_PARSER_RESULT *lwg_parser_result, char *wkt_input, int flags);
-extern char *serialized_lwgeom_to_hexwkb(uchar *serialized, int flags, unsigned int byteorder, size_t *size);
-extern char *serialized_lwgeom_to_ewkb(uchar *serialized, int flags, unsigned int byteorder, size_t *size);
+extern int serialized_lwgeom_to_hexwkb(LWGEOM_UNPARSER_RESULT *lwg_unparser_result, uchar *serialized, int flags, unsigned int byteorder);
+extern int serialized_lwgeom_to_ewkb(LWGEOM_UNPARSER_RESULT *lwg_unparser_result, uchar *serialized, int flags, unsigned int byteorder);
 
 
 extern void *lwalloc(size_t size);

Modified: trunk/liblwgeom/lwgeom.c
===================================================================
--- trunk/liblwgeom/lwgeom.c	2008-09-28 16:18:12 UTC (rev 3025)
+++ trunk/liblwgeom/lwgeom.c	2008-09-28 19:48:17 UTC (rev 3026)
@@ -459,14 +459,18 @@
 char *
 lwgeom_to_ewkt(LWGEOM *lwgeom, int flags)
 {
+	LWGEOM_UNPARSER_RESULT lwg_unparser_result;
 	uchar *serialized = lwgeom_serialize(lwgeom);
-	char *ret;
+	int result;
+
 	if ( ! serialized ) {
 		lwerror("Error serializing geom %p", lwgeom);
 	}
-	ret = unparse_WKT(serialized, lwalloc, lwfree, flags);
+
+	result = unparse_WKT(&lwg_unparser_result, serialized, lwalloc, lwfree, flags);
 	lwfree(serialized);
-	return ret;
+
+	return lwg_unparser_result.wkoutput;
 }
 
 /*
@@ -475,10 +479,14 @@
 char *
 lwgeom_to_hexwkb(LWGEOM *lwgeom, int flags, unsigned int byteorder)
 {
+	LWGEOM_UNPARSER_RESULT lwg_unparser_result;
 	uchar *serialized = lwgeom_serialize(lwgeom);
-	char *hexwkb = unparse_WKB(serialized, lwalloc, lwfree, flags, byteorder,NULL,1);
+	int result;
+
+	result = unparse_WKB(&lwg_unparser_result, serialized, lwalloc, lwfree, flags, byteorder,1);
+
 	lwfree(serialized);
-	return hexwkb;
+	return lwg_unparser_result.wkoutput;
 }
 
 /*
@@ -487,17 +495,19 @@
 uchar *
 lwgeom_to_ewkb(LWGEOM *lwgeom, int flags, char byteorder, size_t *outsize)
 {
+	LWGEOM_UNPARSER_RESULT lwg_unparser_result;
 	uchar *serialized = lwgeom_serialize(lwgeom);
+	int result;
 
 	/*
 	 * We cast return to "unsigned" char as we are
 	 * requesting a "binary" output, not HEX
 	 * (last argument set to 0)
 	 */
-	uchar *hexwkb = (uchar *)unparse_WKB(serialized, lwalloc, lwfree,
-		flags, byteorder, outsize, 0);
+	result = unparse_WKB(&lwg_unparser_result, serialized, lwalloc, lwfree,
+		flags, byteorder, 0);
 	lwfree(serialized);
-	return hexwkb;
+	return (uchar *)lwg_unparser_result.wkoutput;
 }
 
 /*
@@ -558,28 +568,40 @@
 /*
  * Return an alloced string
  */
-char *
-serialized_lwgeom_to_ewkt(uchar *serialized, int flags)
+int
+serialized_lwgeom_to_ewkt(LWGEOM_UNPARSER_RESULT *lwg_unparser_result, uchar *serialized, int flags)
 {
-	return unparse_WKT(serialized, lwalloc, lwfree, flags);
+	int result;
+
+	result = unparse_WKT(lwg_unparser_result, serialized, lwalloc, lwfree, flags);
+
+	return result;
 }
 
 /*
  * Return an alloced string
  */
-char *
-serialized_lwgeom_to_hexwkb(uchar *serialized, int flags, unsigned int byteorder, size_t *size)
+int
+serialized_lwgeom_to_hexwkb(LWGEOM_UNPARSER_RESULT *lwg_unparser_result, uchar *serialized, int flags, unsigned int byteorder)
 {
-	return unparse_WKB(serialized, lwalloc, lwfree, flags, byteorder, size, 1);
+	int result;
+
+	result = unparse_WKB(lwg_unparser_result, serialized, lwalloc, lwfree, flags, byteorder, 1);
+
+	return result;
 }
 
 /*
  * Return an alloced string
  */
-char *
-serialized_lwgeom_to_ewkb(uchar *serialized, int flags, unsigned int byteorder, size_t *size)
+int
+serialized_lwgeom_to_ewkb(LWGEOM_UNPARSER_RESULT *lwg_unparser_result, uchar *serialized, int flags, unsigned int byteorder)
 {
-	return unparse_WKB(serialized, lwalloc, lwfree, flags, byteorder, size, 0);
+	int result;
+
+	result = unparse_WKB(lwg_unparser_result, serialized, lwalloc, lwfree, flags, byteorder, 0);
+
+	return result;
 }
 
 

Modified: trunk/liblwgeom/lwgparse.c
===================================================================
--- trunk/liblwgeom/lwgparse.c	2008-09-28 16:18:12 UTC (rev 3025)
+++ trunk/liblwgeom/lwgparse.c	2008-09-28 19:48:17 UTC (rev 3026)
@@ -1135,6 +1135,7 @@
 	/* Setup the inital parser flags and empty the return struct */
 	parser_check_flags = flags;
 	lwg_parser_result->serialized_lwgeom = NULL;
+	lwg_parser_result->size = 0;
 
 	init_parser(geometry);
 

Modified: trunk/liblwgeom/lwgunparse.c
===================================================================
--- trunk/liblwgeom/lwgunparse.c	2008-09-28 16:18:12 UTC (rev 3025)
+++ trunk/liblwgeom/lwgunparse.c	2008-09-28 19:48:17 UTC (rev 3026)
@@ -44,7 +44,6 @@
 uchar* output_polygon_collection(uchar* geom,int suppress);
 uchar* output_polygon_ring_collection(uchar* geom,outfunc func,int supress);
 uchar* output_curve_collection(uchar* geom,outfunc func,int supress);
-uchar* output_collection_2(uchar* geom,int suppress);
 uchar* output_multipoint(uchar* geom,int suppress);
 uchar* output_compound(uchar* geom, int suppress);
 uchar* output_multisurface(uchar* geom, int suppress);
@@ -81,6 +80,11 @@
  */
 int unparser_check_flags;
 
+/*
+ * Unparser result structure
+ */
+LWGEOM_UNPARSER_RESULT *unparser_result;
+
 /*---------------------------------------------------------- */
 
 
@@ -565,18 +569,21 @@
 	return geom;
 }
 
-char *
-unparse_WKT(uchar* serialized, allocator alloc, freeor free, int flags)
+int
+unparse_WKT(LWGEOM_UNPARSER_RESULT *lwg_unparser_result, uchar* serialized, allocator alloc, freeor free, int flags)
 {
 
         LWDEBUGF(2, "unparse_WKT called with parser flags %d.", flags);
 
 	if (serialized==NULL)
-		return NULL;
+		return 0;
 
-	/* Setup the inital parser flags */
+	/* Setup the inital parser flags and empty the return struct */
         unparser_check_flags = flags;
+	lwg_unparser_result->wkoutput = NULL;
+        lwg_unparser_result->size = 0;
 
+	unparser_result = lwg_unparser_result;
 	local_malloc=alloc;
 	local_free=free;
 	len = 128;
@@ -585,7 +592,11 @@
 
 	output_wkt(serialized, 0);
 
-	return out_start;
+	/* Store the result in the struct */
+	lwg_unparser_result->wkoutput = out_start;
+	lwg_unparser_result->size = strlen(out_start);
+
+	return -1;
 }
 
 static char outchr[]={"0123456789ABCDEF" };
@@ -873,17 +884,20 @@
 	return geom;
 }
 
-char *
-unparse_WKB(uchar* serialized, allocator alloc, freeor free, int flags, char endian, size_t *outsize, uchar hex)
+int
+unparse_WKB(LWGEOM_UNPARSER_RESULT *lwg_unparser_result, uchar* serialized, allocator alloc, freeor free, int flags, char endian, uchar hex)
 {
 	LWDEBUGF(2, "unparse_WKB(%p,...) called with parser flags %d", serialized, flags);
 
-	if (serialized==NULL)
-		return NULL;
+	if (serialized==0)
+		return 0;
 
-	/* Setup the inital parser flags */
+	/* Setup the inital parser flags and empty the return struct */
         unparser_check_flags = flags;
+	lwg_unparser_result->wkoutput = NULL;
+	lwg_unparser_result->size = 0;
 
+	unparser_result = lwg_unparser_result;
 	local_malloc=alloc;
 	local_free=free;
 	len = 128;
@@ -918,9 +932,11 @@
 		*out_pos=0;
 	}
 
-	if ( outsize ) *outsize = (out_pos-out_start);
+	/* Store the result in the struct */	
+	lwg_unparser_result->wkoutput = out_start;
+	lwg_unparser_result->size = (out_pos-out_start);
 
-	return out_start;
+	return -1;	
 }
 
 

Modified: trunk/liblwgeom/wktparse.h
===================================================================
--- trunk/liblwgeom/wktparse.h	2008-09-28 16:18:12 UTC (rev 3025)
+++ trunk/liblwgeom/wktparse.h	2008-09-28 19:48:17 UTC (rev 3026)
@@ -24,6 +24,12 @@
 {
         SERIALIZED_LWGEOM *serialized_lwgeom;
 } LWGEOM_PARSER_RESULT;
+
+typedef struct struct_lwgeom_unparser_result
+{
+        char *wkoutput;
+        int size;
+} LWGEOM_UNPARSER_RESULT;
 #endif
 typedef void* (*allocator)(size_t size);
 typedef void  (*freeor)(void* mem);
@@ -103,8 +109,8 @@
 
 int parse_lwg(LWGEOM_PARSER_RESULT *lwg_parser_result, const char* wkt, int flags, allocator allocfunc,report_error errfunc);
 int parse_lwgi(LWGEOM_PARSER_RESULT *lwg_parser_result, const char* wkt, int flags, allocator allocfunc,report_error errfunc);
-char* unparse_WKT(uchar* serialized, allocator alloc, freeor free, int flags);
-char* unparse_WKB(uchar* serialized, allocator alloc, freeor free, int flags, char endian, size_t *outsize, uchar hexform);
+int unparse_WKT(LWGEOM_UNPARSER_RESULT *lwg_unparser_result, uchar* serialized, allocator alloc, freeor free, int flags);
+int unparse_WKB(LWGEOM_UNPARSER_RESULT *lwg_unparser_result, uchar* serialized, allocator alloc, freeor free, int flags, char endian, uchar hexform);
 int lwg_parse_yyparse(void);
 int lwg_parse_yyerror(char* s);
 

Modified: trunk/lwgeom/lwgeom_functions_basic.c
===================================================================
--- trunk/lwgeom/lwgeom_functions_basic.c	2008-09-28 16:18:12 UTC (rev 3025)
+++ trunk/lwgeom/lwgeom_functions_basic.c	2008-09-28 19:48:17 UTC (rev 3026)
@@ -2903,34 +2903,35 @@
 PG_FUNCTION_INFO_V1(LWGEOM_asEWKT);
 Datum LWGEOM_asEWKT(PG_FUNCTION_ARGS)
 {
+	LWGEOM_UNPARSER_RESULT lwg_unparser_result;
 	PG_LWGEOM *lwgeom;
-	char *result_cstring;
-	int len;
-	char *result,*loc_wkt;
+	int len, result;
+	char *lwgeom_result,*loc_wkt;
 	/*char *semicolonLoc; */
 
 	lwgeom = (PG_LWGEOM *)PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
-	result_cstring =  serialized_lwgeom_to_ewkt(SERIALIZED_FORM(lwgeom), PARSER_CHECK_ALL);
 
+	result = serialized_lwgeom_to_ewkt(&lwg_unparser_result, SERIALIZED_FORM(lwgeom), PARSER_CHECK_ALL);
+
 #if 0
-	semicolonLoc = strchr(result_cstring,';');
+	semicolonLoc = strchr(lwg_unparser_result.wkb,';');
 
 	/*loc points to start of wkt */
-	if (semicolonLoc == NULL) loc_wkt = result_cstring;
+	if (semicolonLoc == NULL) loc_wkt = lwg_unparser_result.wkoutput;
 	else loc_wkt = semicolonLoc +1;
 #endif
-	loc_wkt = result_cstring;
+	loc_wkt = lwg_unparser_result.wkoutput;
 
 	len = strlen(loc_wkt);
-	result = palloc(len + VARHDRSZ);
-    SET_VARSIZE(result, len + VARHDRSZ);
+	lwgeom_result = palloc(len + VARHDRSZ);
+	SET_VARSIZE(lwgeom_result, len + VARHDRSZ);
 
-	memcpy(VARDATA(result), loc_wkt, len);
+	memcpy(VARDATA(lwgeom_result), loc_wkt, len);
 
-	pfree(result_cstring);
+	pfree(lwg_unparser_result.wkoutput);
 	PG_FREE_IF_COPY(lwgeom, 0);
 
-	PG_RETURN_POINTER(result);
+	PG_RETURN_POINTER(lwgeom_result);
 }
 
 /*

Modified: trunk/lwgeom/lwgeom_inout.c
===================================================================
--- trunk/lwgeom/lwgeom_inout.c	2008-09-28 16:18:12 UTC (rev 3025)
+++ trunk/lwgeom/lwgeom_inout.c	2008-09-28 19:48:17 UTC (rev 3026)
@@ -95,13 +95,14 @@
 PG_FUNCTION_INFO_V1(LWGEOM_out);
 Datum LWGEOM_out(PG_FUNCTION_ARGS)
 {
+	LWGEOM_UNPARSER_RESULT lwg_unparser_result;	
 	PG_LWGEOM *lwgeom;
-	char *result;
+	int result;
 
 	lwgeom = (PG_LWGEOM *)  PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
-	result = serialized_lwgeom_to_hexwkb(SERIALIZED_FORM(lwgeom), PARSER_CHECK_ALL, -1, NULL);
+	result = serialized_lwgeom_to_hexwkb(&lwg_unparser_result, SERIALIZED_FORM(lwgeom), PARSER_CHECK_ALL, -1);
 
-	PG_RETURN_CSTRING(result);
+	PG_RETURN_CSTRING(lwg_unparser_result.wkoutput);
 }
 
 /*
@@ -110,10 +111,10 @@
 PG_FUNCTION_INFO_V1(LWGEOM_asHEXEWKB);
 Datum LWGEOM_asHEXEWKB(PG_FUNCTION_ARGS)
 {
+	LWGEOM_UNPARSER_RESULT lwg_unparser_result;
 	PG_LWGEOM *lwgeom;
-	char *result;
+	int result;
 	text *text_result;
-	size_t size;
 	text *type;
 	unsigned int byteorder=-1;
 
@@ -139,12 +140,12 @@
 		}
 	}
 
-	result = serialized_lwgeom_to_hexwkb(SERIALIZED_FORM(lwgeom), PARSER_CHECK_ALL, byteorder, &size);
+	result = serialized_lwgeom_to_hexwkb(&lwg_unparser_result, SERIALIZED_FORM(lwgeom), PARSER_CHECK_ALL, byteorder);
 
-	text_result = palloc(size+VARHDRSZ);
-	memcpy(VARDATA(text_result),result,size);
-	SET_VARSIZE(text_result, size+VARHDRSZ);
-	pfree(result);
+	text_result = palloc(lwg_unparser_result.size+VARHDRSZ);
+	memcpy(VARDATA(text_result),lwg_unparser_result.wkoutput,lwg_unparser_result.size);
+	SET_VARSIZE(text_result, lwg_unparser_result.size+VARHDRSZ);
+	pfree(lwg_unparser_result.wkoutput);
 
 	PG_RETURN_POINTER(text_result);
 
@@ -160,18 +161,18 @@
 PG_FUNCTION_INFO_V1(LWGEOM_to_text);
 Datum LWGEOM_to_text(PG_FUNCTION_ARGS)
 {
+	LWGEOM_UNPARSER_RESULT lwg_unparser_result;
 	PG_LWGEOM *lwgeom;
-	char *result;
+	int result;
 	text *text_result;
-	size_t size;
 
 	lwgeom = (PG_LWGEOM *)  PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
-	result = serialized_lwgeom_to_hexwkb(SERIALIZED_FORM(lwgeom), PARSER_CHECK_ALL, -1, &size);
+	result = serialized_lwgeom_to_hexwkb(&lwg_unparser_result, SERIALIZED_FORM(lwgeom), PARSER_CHECK_ALL, -1);
 
-	text_result = palloc(size+VARHDRSZ);
-	memcpy(VARDATA(text_result),result,size);
-	SET_VARSIZE(text_result, size+VARHDRSZ);
-	pfree(result);
+	text_result = palloc(lwg_unparser_result.size+VARHDRSZ);
+	memcpy(VARDATA(text_result),lwg_unparser_result.wkoutput,lwg_unparser_result.size);
+	SET_VARSIZE(text_result, lwg_unparser_result.size+VARHDRSZ);
+	pfree(lwg_unparser_result.wkoutput);
 
 	PG_RETURN_POINTER(text_result);
 }
@@ -227,18 +228,18 @@
 {
 /* #define BINARY_FROM_HEX 1 */
 
+	LWGEOM_UNPARSER_RESULT lwg_unparser_result;
 	PG_LWGEOM *lwgeom_input; /* SRID=#;<hexized wkb> */
-	char *hexized_wkb; /* hexized_wkb_srid w/o srid */
-	char *result; /* wkb */
+	char *lwgeom_result;
+	int result;
 	int size_result;
 #ifdef BINARY_FROM_HEX
-	char *hexized_wkb_srid;
+	char *hexized_wkb; /* hexized_wkb_srid w/o srid */
 	char *semicolonLoc;
 	int t;
 #endif /* BINARY_FROM_HEX */
 	text *type;
 	unsigned int byteorder=-1;
-	size_t size;
 
 #ifdef PROFILE
 	profstart(PROF_QRUN);
@@ -267,13 +268,13 @@
 	lwgeom_input = (PG_LWGEOM *)PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
 
 #ifdef BINARY_FROM_HEX
-	hexized_wkb_srid = serialized_lwgeom_to_hexwkb(SERIALIZED_FORM(lwgeom_input), byteorder, &size);
+	result = serialized_lwgeom_to_hexwkb(&lwg_unparser_result, SERIALIZED_FORM(lwgeom_input), byteorder);
 
-	LWDEBUGF(3, "in WKBFromLWGEOM with WKB = '%s'", hexized_wkb_srid);
+	LWDEBUGF(3, "in WKBFromLWGEOM with WKB = '%s'", lwg_unparser_result.wkoutput);
 
-	hexized_wkb = hexized_wkb_srid;
+	hexized_wkb_srid = lwg_unparser_result.wkoutput;
 
-	semicolonLoc = strchr(hexized_wkb_srid,';');
+	semicolonLoc = strchr(lwg_unparser_result.wkoutput,';');
 	if (semicolonLoc != NULL)
 	{
 		hexized_wkb = (semicolonLoc+1);
@@ -282,27 +283,27 @@
 	LWDEBUGF(3, "in WKBFromLWGEOM with WKB (with no 'SRID=#;' = '%s'",
 		hexized_wkb);
 
-	size_result = size/2 + VARHDRSZ;
-	result = palloc(size_result);
-	SET_VARSIZE(result, size_result);
+	size_result = lwg_unparser_result.size/2 + VARHDRSZ;
+	lwgeom_result = palloc(size_result);
+	SET_VARSIZE(lwgeom_result, size_result);
 
 	/* have a hexized string, want to make it binary */
-	for (t=0; t< (size/2); t++)
+	for (t=0; t< (lwgeom_unparser_result.size/2); t++)
 	{
-		((uchar *) VARDATA(result))[t] = parse_hex(  hexized_wkb + (t*2) );
+		((uchar *) VARDATA(lwgeom_result))[t] = parse_hex(  hexized_wkb + (t*2) );
 	}
 
-	pfree(hexized_wkb_srid);
+	pfree(hexized_wkb);
 
 #else /* ndef BINARY_FROM_HEX */
 
-	hexized_wkb = serialized_lwgeom_to_ewkb(SERIALIZED_FORM(lwgeom_input), PARSER_CHECK_ALL, byteorder, &size);
+	result = serialized_lwgeom_to_ewkb(&lwg_unparser_result, SERIALIZED_FORM(lwgeom_input), PARSER_CHECK_ALL, byteorder);
 
-	size_result = size+VARHDRSZ;
-	result = palloc(size_result);
-	SET_VARSIZE(result, size_result);
-	memcpy(VARDATA(result), hexized_wkb, size);
-	pfree(hexized_wkb);
+	size_result = lwg_unparser_result.size+VARHDRSZ;
+	lwgeom_result = palloc(size_result);
+	SET_VARSIZE(lwgeom_result, size_result);
+	memcpy(VARDATA(lwgeom_result), lwg_unparser_result.wkoutput, lwg_unparser_result.size);
+	pfree(lwg_unparser_result.wkoutput);
 
 #endif
 
@@ -312,9 +313,9 @@
 #endif
 
 	LWDEBUGF(3, "Output size is %lu (comp: %lu)",
-		VARSIZE(result), (unsigned long)size);
+		VARSIZE(lwgeom_result), (unsigned long)size);
 
-	PG_RETURN_POINTER(result);
+	PG_RETURN_POINTER(lwgeom_result);
 }
 
 /* puts a bbox inside the geometry */

Modified: trunk/lwgeom/lwgeom_ogc.c
===================================================================
--- trunk/lwgeom/lwgeom_ogc.c	2008-09-28 16:18:12 UTC (rev 3025)
+++ trunk/lwgeom/lwgeom_ogc.c	2008-09-28 19:48:17 UTC (rev 3026)
@@ -1065,11 +1065,11 @@
 PG_FUNCTION_INFO_V1(LWGEOM_asText);
 Datum LWGEOM_asText(PG_FUNCTION_ARGS)
 {
+	LWGEOM_UNPARSER_RESULT lwg_unparser_result;
 	PG_LWGEOM *lwgeom;
 	PG_LWGEOM *ogclwgeom;
-	char *result_cstring;
-	int len;
-	char *result,*loc_wkt;
+	int len, result;
+	char *lwgeom_result,*loc_wkt;
 	char *semicolonLoc;
         
 	POSTGIS_DEBUG(2, "LWGEOM_asText called.");
@@ -1080,25 +1080,25 @@
 	ogclwgeom = (PG_LWGEOM *)DatumGetPointer(DirectFunctionCall1(
 		LWGEOM_force_2d, PointerGetDatum(lwgeom)));
 
-	result_cstring =  serialized_lwgeom_to_ewkt(SERIALIZED_FORM(ogclwgeom), PARSER_CHECK_ALL);
+	result =  serialized_lwgeom_to_ewkt(&lwg_unparser_result, SERIALIZED_FORM(ogclwgeom), PARSER_CHECK_ALL);
 
-	semicolonLoc = strchr(result_cstring,';');
+	semicolonLoc = strchr(lwg_unparser_result.wkoutput,';');
 
 	/* loc points to start of wkt */
-	if (semicolonLoc == NULL) loc_wkt = result_cstring;
+	if (semicolonLoc == NULL) loc_wkt = lwg_unparser_result.wkoutput;
 	else loc_wkt = semicolonLoc +1;
 
 	len = strlen(loc_wkt)+VARHDRSZ;
-	result = palloc(len);
-	SET_VARSIZE(result, len);
+	lwgeom_result = palloc(len);
+	SET_VARSIZE(lwgeom_result, len);
 
-	memcpy(VARDATA(result), loc_wkt, len-VARHDRSZ);
+	memcpy(VARDATA(lwgeom_result), loc_wkt, len-VARHDRSZ);
 
-	pfree(result_cstring);
+	pfree(lwg_unparser_result.wkoutput);
 	PG_FREE_IF_COPY(lwgeom, 0);
 	if ( ogclwgeom != lwgeom ) pfree(ogclwgeom);
 
-	PG_RETURN_POINTER(result);
+	PG_RETURN_POINTER(lwgeom_result);
 }
 
 /* convert LWGEOM to wkb (in BINARY format) */

Modified: trunk/lwgeom/lwgeom_pg.c
===================================================================
--- trunk/lwgeom/lwgeom_pg.c	2008-09-28 16:18:12 UTC (rev 3025)
+++ trunk/lwgeom/lwgeom_pg.c	2008-09-28 19:48:17 UTC (rev 3026)
@@ -274,8 +274,14 @@
 char *
 pglwgeom_to_ewkb(PG_LWGEOM *geom, int flags, char byteorder, size_t *outsize)
 {
+	LWGEOM_UNPARSER_RESULT lwg_unparser_result;
+	int result;
 	uchar *srl = &(geom->type);
-	return serialized_lwgeom_to_ewkb(srl, flags, byteorder, outsize);
+
+	result = serialized_lwgeom_to_ewkb(&lwg_unparser_result, srl, flags, byteorder);
+
+	*outsize = lwg_unparser_result.size;
+	return lwg_unparser_result.wkoutput;
 }
 
 /*



More information about the postgis-commits mailing list