From 0b6f67024b3858744dbcfc22a5f83ac078361f08 Mon Sep 17 00:00:00 2001 From: Mark Cave-Ayland Date: Tue, 22 Sep 2009 11:13:33 +0000 Subject: [PATCH] Remove dependency on regex library by implementing the same functionality using standard C functions. Passes regression tests for me. git-svn-id: http://svn.osgeo.org/postgis/trunk@4520 b70326c6-7e19-0410-871a-916f4a2858ee --- configure.ac | 3 - liblwgeom/cunit/Makefile.in | 2 +- liblwgeom/g_util.c | 166 ++++++++++++++++++++---------------- postgis/Makefile.in | 2 +- 4 files changed, 94 insertions(+), 79 deletions(-) diff --git a/configure.ac b/configure.ac index be1e796ec..c2370b5c7 100644 --- a/configure.ac +++ b/configure.ac @@ -536,11 +536,8 @@ CPPFLAGS="$PGSQL_CPPFLAGS $GEOS_CPPFLAGS $PROJ_CPPFLAGS" dnl AC_MSG_RESULT([CPPFLAGS: $CPPFLAGS]) SHLIB_LINK="$PGSQL_LDFLAGS $GEOS_LDFLAGS $PROJ_LDFLAGS -lgeos_c -lproj" -AC_CHECK_FUNCS(regexec, REGEX_LIBS= , [AC_CHECK_LIB(regex, regexec, REGEX_LIBS="$REGEX_LIBS -lregex")]) - AC_SUBST([SHLIB_LINK]) dnl AC_MSG_RESULT([SHLIB_LINK: $SHLIB_LINK]) -AC_SUBST([REGEX_LIBS]) dnl Output the relevant files AC_OUTPUT([liblwgeom/Makefile liblwgeom/cunit/Makefile postgis/Makefile postgis/sqldefines.h loader/Makefile topology/Makefile regress/Makefile doc/Makefile doc/html/image_src/Makefile]) diff --git a/liblwgeom/cunit/Makefile.in b/liblwgeom/cunit/Makefile.in index 93a6343a1..0a0520191 100644 --- a/liblwgeom/cunit/Makefile.in +++ b/liblwgeom/cunit/Makefile.in @@ -13,7 +13,7 @@ CC=@CC@ CFLAGS=@CFLAGS@ @WARNFLAGS@ -CUNIT_LDFLAGS=@CUNIT_LDFLAGS@ @REGEX_LIBS@ +CUNIT_LDFLAGS=@CUNIT_LDFLAGS@ CUNIT_CPPFLAGS=@CUNIT_CPPFLAGS@ -I.. OBJS= \ diff --git a/liblwgeom/g_util.c b/liblwgeom/g_util.c index 85043e005..6dfd16965 100644 --- a/liblwgeom/g_util.c +++ b/liblwgeom/g_util.c @@ -9,8 +9,57 @@ * **********************************************************************/ +#include + #include "libgeom.h" +/* Structure for the type array */ +struct geomtype_struct +{ + char *typename; + int type; + int z; + int m; +}; + +/* Type array. Note that the order of this array is important in + that any typename in the list must *NOT* occur within an entry + before it. Otherwise if we search for "POINT" at the top of the + list we would also match MULTIPOINT, for example. */ + +struct geomtype_struct geomtype_struct_array[28] = +{ + { "GEOMETRYCOLLECTIONZM", COLLECTIONTYPE, 1, 1 }, + { "GEOMETRYCOLLECTIONZ", COLLECTIONTYPE, 1, 0 }, + { "GEOMETRYCOLLECTIONM", COLLECTIONTYPE, 0, 1 }, + { "GEOMETRYCOLLECTION", COLLECTIONTYPE, 0, 0 }, + { "MULTILINESTRINGZM", MULTILINETYPE, 1, 1 }, + { "MULTILINESTRINGZ", MULTILINETYPE, 1, 0 }, + { "MULTILINESTRINGM", MULTILINETYPE, 0, 1 }, + { "MULTILINESTRING", MULTILINETYPE, 0, 0 }, + { "MULTIPOLYGONZM", MULTIPOLYGONTYPE, 1, 1 }, + { "MULTIPOLYGONZ", MULTIPOLYGONTYPE, 1, 0 }, + { "MULTIPOLYGONM", MULTIPOLYGONTYPE, 0, 1 }, + { "MULTIPOLYGON", MULTIPOLYGONTYPE, 0, 0 }, + { "MULTIPOINTZM", MULTIPOINTTYPE, 1, 1 }, + { "MULTIPOINTZ", MULTIPOINTTYPE, 1, 0 }, + { "MULTIPOINTM", MULTIPOINTTYPE, 0, 1 }, + { "MULTIPOINT", MULTIPOINTTYPE, 0, 0 }, + { "LINESTRINGZM", LINETYPE, 1, 1 }, + { "LINESTRINGZ", LINETYPE, 1, 0 }, + { "LINESTRINGM", LINETYPE, 0, 1 }, + { "LINESTRING", LINETYPE, 0, 0 }, + { "POLYGONZM", POLYGONTYPE, 1, 1 }, + { "POLYGONZ", POLYGONTYPE, 1, 0 }, + { "POLYGONM", POLYGONTYPE, 0, 1 }, + { "POLYGON", POLYGONTYPE, 0, 0 }, + { "POINTZM", POINTTYPE, 1, 1 }, + { "POINTZ", POINTTYPE, 1, 0 }, + { "POINTM", POINTTYPE, 0, 1 }, + { "POINT", POINTTYPE, 0, 0 } +}; + + uchar gflags(int hasz, int hasm, int geodetic) { unsigned char flags = 0; @@ -31,13 +80,9 @@ uchar gflags(int hasz, int hasm, int geodetic) */ int geometry_type_from_string(char *str, int *type, int *z, int *m) { - regex_t rx_point; - regex_t rx_linestring; - regex_t rx_polygon; - regex_t rx_geometrycollection; - regex_t rx_geometry; - size_t rx_nmatch = 5; - regmatch_t rx_matchptr[5]; + char *tmpstr; + int tmpstartpos, tmpendpos; + int i; assert(str); assert(type); @@ -49,81 +94,54 @@ int geometry_type_from_string(char *str, int *type, int *z, int *m) *z = 0; *m = 0; - regcomp(&rx_point, "^ *(st_)?(multi)?point(z)?(m)? *$", REG_ICASE | REG_EXTENDED); - regcomp(&rx_linestring, "^ *(st_)?(multi)?linestring(z)?(m)? *$", REG_ICASE | REG_EXTENDED); - regcomp(&rx_polygon, "^ *(st_)?(multi)?polygon(z)?(m)? *$", REG_ICASE | REG_EXTENDED); - regcomp(&rx_geometrycollection, "^ *(st_)?geometrycollection(z)?(m)? *$", REG_ICASE | REG_EXTENDED); - regcomp(&rx_geometry, "^ *(st_)?geometry(z)?(m)? *$", REG_ICASE | REG_EXTENDED); - - if( ! regexec(&rx_point, str, rx_nmatch, rx_matchptr, 0) ) - { - *type = POINTTYPE; - if(rx_matchptr[2].rm_so != -1) /* MULTI */ - *type = MULTIPOINTTYPE; - - if(rx_matchptr[3].rm_so != -1) /* Z */ - *z = 1; - - if(rx_matchptr[4].rm_so != -1) /* M */ - *m = 1; - - return G_SUCCESS; - } - if( ! regexec(&rx_linestring, str, rx_nmatch, rx_matchptr, 0) ) + /* Locate any leading/trailing spaces */ + tmpstartpos = 0; + for (i = 0; i < strlen(str); i++) { - *type = LINETYPE; - if(rx_matchptr[2].rm_so != -1) /* MULTI */ - *type = MULTILINETYPE; - - if(rx_matchptr[3].rm_so != -1) /* Z */ - *z = 1; - - if(rx_matchptr[4].rm_so != -1) /* M */ - *m = 1; - - return G_SUCCESS; - } - if( ! regexec(&rx_polygon, str, rx_nmatch, rx_matchptr, 0) ) + if (str[i] != ' ') + { + tmpstartpos = i; + break; + } + } + + tmpendpos = strlen(str) - 1; + for (i = strlen(str) - 1; i >= 0; i--) { - *type = POLYGONTYPE; - if(rx_matchptr[2].rm_so != -1) /* MULTI */ - *type = MULTIPOLYGONTYPE; - - if(rx_matchptr[3].rm_so != -1) /* Z */ - *z = 1; - - if(rx_matchptr[4].rm_so != -1) /* M */ - *m = 1; - - return G_SUCCESS; - } - if( ! regexec(&rx_geometrycollection, str, rx_nmatch, rx_matchptr, 0) ) + if (str[i] != ' ') + { + tmpendpos = i; + break; + } + } + + /* Copy and convert to upper case for comparison */ + tmpstr = lwalloc(tmpendpos - tmpstartpos + 2); + for (i = tmpstartpos; i <= tmpendpos; i++) + tmpstr[i - tmpstartpos] = toupper(str[i]); + + /* Add NULL to terminate */ + tmpstr[i - tmpstartpos] = '\0'; + + /* Now check for the type */ + for (i = 0; i < 28; i++) { - *type = COLLECTIONTYPE; + if (!strcmp(tmpstr, geomtype_struct_array[i].typename)) + { + *type = geomtype_struct_array[i].type; + *z = geomtype_struct_array[i].z; + *m = geomtype_struct_array[i].m; - if(rx_matchptr[2].rm_so != -1) /* Z */ - *z = 1; - - if(rx_matchptr[3].rm_so != -1) /* M */ - *m = 1; + lwfree(tmpstr); - return G_SUCCESS; - } - if( ! regexec(&rx_geometry, str, rx_nmatch, rx_matchptr, 0) ) - { - *type = 0; /* Generic geometry type. */ + return G_SUCCESS; + } + + } - if(rx_matchptr[2].rm_so != -1) /* Z */ - *z = 1; - - if(rx_matchptr[3].rm_so != -1) /* M */ - *m = 1; + lwfree(tmpstr); - return G_SUCCESS; - } - return G_FAILURE; - } diff --git a/postgis/Makefile.in b/postgis/Makefile.in index 75fec41f1..fc66cbdd4 100644 --- a/postgis/Makefile.in +++ b/postgis/Makefile.in @@ -62,7 +62,7 @@ OBJS=$(PG_OBJS) # older version of PostGIS, rather than with the static liblwgeom.a # supplied with newer versions of PostGIS PG_CPPFLAGS+=@CPPFLAGS@ -I../liblwgeom -SHLIB_LINK+=@SHLIB_LINK@ ../liblwgeom/liblwgeom.a @REGEX_LIBS@ +SHLIB_LINK+=@SHLIB_LINK@ ../liblwgeom/liblwgeom.a # Extra files to remove during 'make clean' EXTRA_CLEAN=$(SQL_OBJS) -- 2.49.0