[postgis-commits] svn - r3373 - in trunk: liblwgeom lwgeom regress

postgis-commits at postgis.refractions.net postgis-commits at postgis.refractions.net
Wed Dec 10 04:32:46 PST 2008


Author: mcayland
Date: 2008-12-10 04:32:40 -0800 (Wed, 10 Dec 2008)
New Revision: 3373

Modified:
   trunk/liblwgeom/lwutil.c
   trunk/lwgeom/lwgeom_pg.c
   trunk/regress/regress_expected
   trunk/regress/wkt_expected
Log:
Add new lwmessage_truncate() function to liblwgeom that will truncate a string to a maximum number of characters, adding a "..." to indicate where the string has been trimmed. Truncation can be specified to occur from either the start or end of the string as required. Also update the parser error handler to use the new function.


Modified: trunk/liblwgeom/lwutil.c
===================================================================
--- trunk/liblwgeom/lwutil.c	2008-12-09 23:29:31 UTC (rev 3372)
+++ trunk/liblwgeom/lwutil.c	2008-12-10 12:32:40 UTC (rev 3373)
@@ -271,6 +271,65 @@
 	LWDEBUGF(3, "output: %s", str);
 }
 
+
+/*
+ * Returns a new string which contains a maximum of maxlength characters starting 
+ * from startpos and finishing at endpos. If the string is truncated then the 
+ * first or last characters are replaced by "..." as appropriate.
+ *
+ * The caller should specify start or end truncation by setting the truncdirection
+ * parameter as follows:
+ *    0 - start truncation (i.e. characters are removed from the beginning)
+ *    1 - end trunctation (i.e. characters are removed from the end)
+ */  
+
+char *lwmessage_truncate(char *str, int startpos, int endpos, int maxlength, int truncdirection)
+{
+	char *output;
+	char *outstart;
+
+	/* Allocate space for new string */
+	output = lwalloc(maxlength + 1);
+	output[0] = '\0';
+
+	/* Start truncation */
+	if (truncdirection == 0)
+	{
+		/* Calculate the start position */
+		if (endpos - startpos <= maxlength) 
+		{
+			outstart = str + startpos;
+			strncat(output, outstart, endpos - startpos); 
+		}		
+		else
+		{
+			/* Add "..." prefix */
+			outstart = str + endpos - maxlength + 3;
+			strncpy(output, "...", 4);
+			strncat(output, outstart, maxlength - 3); 
+		}
+	}
+
+	/* End truncation */
+	if (truncdirection == 1)
+	{
+		/* Calculate the end position */
+		if (endpos - startpos <= maxlength)
+		{
+			strncat(output, str, endpos - startpos);
+		}	
+		else
+		{
+			/* Add "..." suffix */
+			strncat(output, str, maxlength - 3);
+			strncat(output, "...", 3); 
+		}
+	}
+
+	return output;
+}
+
+
 char
 getMachineEndian(void)
 {

Modified: trunk/lwgeom/lwgeom_pg.c
===================================================================
--- trunk/lwgeom/lwgeom_pg.c	2008-12-09 23:29:31 UTC (rev 3372)
+++ trunk/lwgeom/lwgeom_pg.c	2008-12-10 12:32:40 UTC (rev 3373)
@@ -29,26 +29,11 @@
 void
 pg_parser_errhint(LWGEOM_PARSER_RESULT *lwg_parser_result)
 {
-	char hintbuffer[128];
-	char *hintstart;
-	char *hintfinish; 
+	char *hintbuffer;
 
-	/* Generate the HINT string as the last 40 chars up until the error location */
-	hintbuffer[0] = '\0';
-	hintfinish = (char *)lwg_parser_result->wkinput + lwg_parser_result->errlocation;	
-
-	if (lwg_parser_result->errlocation <= 40)
-		hintstart = (char *)lwg_parser_result->wkinput;
-	else
-		hintstart = hintfinish - 40;
-
-	/* If we are not at the start of the buffer, prefix with "..." */
-	if (hintstart != lwg_parser_result->wkinput)
-		strncpy(hintbuffer, "...", 4);
+	/* Return a copy of the input string start truncated at the error location */
+	hintbuffer = lwmessage_truncate((char *)lwg_parser_result->wkinput, 0, lwg_parser_result->errlocation, 40, 0);
 	
-	/* Append to the existing string */
-	strncat(hintbuffer, hintstart, hintfinish-hintstart);
-	
 	/* Only display the parser position if the location is > 0; this provides a nicer output when the first token
 	   within the input stream cannot be matched */	
 	if (lwg_parser_result->errlocation > 0)

Modified: trunk/regress/regress_expected
===================================================================
--- trunk/regress/regress_expected	2008-12-09 23:29:31 UTC (rev 3372)
+++ trunk/regress/regress_expected	2008-12-10 12:32:40 UTC (rev 3373)
@@ -62,9 +62,9 @@
 ERROR:  geometry contains non-closed rings
 HINT:  "POLYGON( (0 0, 10 0, 10 10, 0 10)" <-- parse error at position 33 within geometry
 ERROR:  geometry contains non-closed rings
-HINT:  "... 10 10, 0 10, 0 0),(5 5, 7 5, 7 7 , 5 7)" <-- parse error at position 60 within geometry
+HINT:  "... 10, 0 10, 0 0),(5 5, 7 5, 7 7 , 5 7)" <-- parse error at position 60 within geometry
 ERROR:  parse error - invalid geometry
-HINT:  "...TILINESTRING((0 0, 1 1),(0 0, 1 1, 2 2,)" <-- parse error at position 43 within geometry
+HINT:  "...INESTRING((0 0, 1 1),(0 0, 1 1, 2 2,)" <-- parse error at position 43 within geometry
 ERROR:  parse error - invalid geometry
 HINT:  "POINT(1 2 3," <-- parse error at position 12 within geometry
 ERROR:  parse error - invalid geometry

Modified: trunk/regress/wkt_expected
===================================================================
--- trunk/regress/wkt_expected	2008-12-09 23:29:31 UTC (rev 3372)
+++ trunk/regress/wkt_expected	2008-12-10 12:32:40 UTC (rev 3373)
@@ -27,9 +27,9 @@
 MULTILINESTRING((0 0,2 0))
 MULTILINESTRING((0 0,2 0),(1 1,2 2))
 ERROR:  parse error - invalid geometry
-HINT:  "...LINESTRING((0 0, 2 0), (1 1, 2 2), EMPTY" <-- parse error at position 45 within geometry
+HINT:  "...ESTRING((0 0, 2 0), (1 1, 2 2), EMPTY" <-- parse error at position 45 within geometry
 ERROR:  parse error - invalid geometry
-HINT:  "...INESTRING((0 0, 2 0), (1 1, 2 2), (EMPTY" <-- parse error at position 46 within geometry
+HINT:  "...STRING((0 0, 2 0), (1 1, 2 2), (EMPTY" <-- parse error at position 46 within geometry
 GEOMETRYCOLLECTION EMPTY
 ERROR:  parse error - invalid geometry
 HINT:  "POLYGON(EMPTY" <-- parse error at position 13 within geometry



More information about the postgis-commits mailing list