Skip to content

Commit

Permalink
Add support for pcre2. (#314)
Browse files Browse the repository at this point in the history
* Add support for pcre2. Fixes: #272
  • Loading branch information
sebastic authored and jmckenna committed Aug 2, 2024
1 parent dd260fd commit 5844d1a
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 9 deletions.
5 changes: 3 additions & 2 deletions .github/workflows/build-linux.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ jobs:
fi
if [[ 'maximal' =~ ${{ matrix.option }} ]]
then
sudo apt-get install -y libhiredis-dev libdb-dev libmapserver-dev
sudo apt-get install -y libhiredis-dev libdb-dev libmapserver-dev libpcre2-dev
fi
- name: Build MapCache
Expand All @@ -55,7 +55,8 @@ jobs:
-DWITH_TIFF=ON \
-DWITH_TIFF_WRITE_SUPPORT=ON \
-DWITH_GEOTIFF=ON \
-DWITH_PCRE=ON \
-DWITH_PCRE=OFF \
-DWITH_PCRE2=ON \
-DWITH_MAPSERVER=ON \
-DWITH_RIAK=OFF"
fi
Expand Down
14 changes: 14 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ option(WITH_TIFF "Use TIFFs as a cache backend" OFF)
option(WITH_TIFF_WRITE_SUPPORT "Enable (experimental) support for writable TIFF cache backends" OFF)
option(WITH_GEOTIFF "Allow GeoTIFF metadata creation for TIFF cache backends" OFF)
option(WITH_PCRE "Use PCRE for regex tests" OFF)
option(WITH_PCRE2 "Use PCRE2 for regex tests" OFF)
option(WITH_MAPSERVER "Enable (experimental) support for the mapserver library" OFF)
option(WITH_RIAK "Use Riak as a cache backend" OFF)
option(WITH_GDAL "Choose if GDAL raster support should be built in" ON)
Expand Down Expand Up @@ -205,6 +206,18 @@ if(WITH_PCRE)
endif(PCRE_FOUND)
endif (WITH_PCRE)

if(WITH_PCRE2)
find_package(PCRE2)
if(PCRE2_FOUND)
include_directories(${PCRE2_INCLUDE_DIR})
target_link_libraries(mapcache PCRE2::PCRE2-8)
set (USE_PCRE2 1)
add_definitions(-DPCRE2_CODE_UNIT_WIDTH=8)
else(PCRE2_FOUND)
report_optional_not_found(PCRE2)
endif(PCRE2_FOUND)
endif (WITH_PCRE2)

if(WITH_SQLITE)
find_package(SQLITE)
if(SQLITE_FOUND)
Expand Down Expand Up @@ -357,6 +370,7 @@ status_optional_component("TIFF" "${USE_TIFF}" "${TIFF_LIBRARY}")
status_optional_component("GeoTIFF" "${USE_GEOTIFF}" "${GEOTIFF_LIBRARY}")
status_optional_component("Experimental TIFF write support" "${USE_TIFF_WRITE}" "${TIFF_LIBRARY}")
status_optional_component("PCRE" "${USE_PCRE}" "${PCRE_LIBRARY}")
status_optional_component("PCRE2" "${USE_PCRE2}" "${PCRE2-8_LIBRARY}")
status_optional_component("Experimental mapserver support" "${USE_MAPSERVER}" "${MAPSERVER_LIBRARY}")
status_optional_component("RIAK" "${USE_RIAK}" "${RIAK_LIBRARY}")
status_optional_component("GDAL" "${USE_GDAL}" "${GDAL_LIBRARY}")
Expand Down
2 changes: 1 addition & 1 deletion cmake/FindBerkeleyDB.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -83,5 +83,5 @@ endif (BERKELEYDB_FIND_VERSION AND BERKELEYDB_FOUND_TMP)
set(BERKELEYDB_INCLUDE_DIRS ${BERKELEYDB_INCLUDE_DIR})
set(BERKELEYDB_LIBRARIES ${BERKELEYDB_LIBRARY})
include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(BERKELEYDB DEFAULT_MSG BERKELEYDB_LIBRARY BERKELEYDB_INCLUDE_DIR)
find_package_handle_standard_args(BerkeleyDB DEFAULT_MSG BERKELEYDB_LIBRARY BERKELEYDB_INCLUDE_DIR)
mark_as_advanced(BERKELEYDB_LIBRARY BERKELEYDB_INCLUDE_DIR)
27 changes: 27 additions & 0 deletions cmake/FindPCRE2.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Distributed under the OSI-approved BSD 3-Clause License. See accompanying
# file COPYING-CMAKE-SCRIPTS or https://cmake.org/licensing for details.

#.rst
# FindPCRE2
# ~~~~~~~~~
# Copyright (C) 2017-2018, Hiroshi Miura
#
# Find the native PCRE2 headers and libraries.

find_path(PCRE2_INCLUDE_DIR NAMES pcre2.h)
find_library(PCRE2-8_LIBRARY NAMES pcre2-8 pcre2-8d pcre2-8-static pcre2-8-staticd NAMES_PER_DIR)
include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(PCRE2
REQUIRED_VARS PCRE2-8_LIBRARY PCRE2_INCLUDE_DIR)
mark_as_advanced(PCRE2_INCLUDE_DIR PCRE2-8_LIBRARY)
if(PCRE2_FOUND)
list(APPEND PCRE2_LIBRARIES "${PCRE2-8_LIBRARY}")
set(PCRE2_INCLUDE_DIRS "${PCRE2_INCLUDE_DIR}")
if(NOT TARGET PCRE2::PCRE2-8)
add_library(PCRE2::PCRE2-8 UNKNOWN IMPORTED)
set_target_properties(PCRE2::PCRE2-8 PROPERTIES
INTERFACE_INCLUDE_DIRECTORIES "${PCRE2_INCLUDE_DIR}"
IMPORTED_LINK_INTERFACE_LANGUAGES "C"
IMPORTED_LOCATION "${PCRE2-8_LIBRARY}")
endif()
endif()
2 changes: 1 addition & 1 deletion cmake/FindPixman.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@ FIND_LIBRARY(PIXMAN_LIBRARY
set(PIXMAN_INCLUDE_DIRS ${PIXMAN_INCLUDE_DIR})
set(PIXMAN_LIBRARIES ${PIXMAN_LIBRARY})
include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(PIXMAN DEFAULT_MSG PIXMAN_LIBRARY PIXMAN_INCLUDE_DIR)
find_package_handle_standard_args(Pixman DEFAULT_MSG PIXMAN_LIBRARY PIXMAN_INCLUDE_DIR)
mark_as_advanced(PIXMAN_LIBRARY PIXMAN_INCLUDE_DIR)
1 change: 1 addition & 0 deletions include/mapcache-config.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#cmakedefine USE_TIFF_WRITE 1
#cmakedefine USE_GEOTIFF 1
#cmakedefine USE_PCRE 1
#cmakedefine USE_PCRE2 1
#cmakedefine USE_MAPSERVER 1
#cmakedefine USE_RIAK 1
#cmakedefine USE_GDAL 1
Expand Down
31 changes: 26 additions & 5 deletions lib/dimension.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@
#include <apr_strings.h>
#include <math.h>
#include <sys/types.h>
#ifdef USE_PCRE
#if defined(USE_PCRE2)
#include <pcre2.h>
#elif defined(USE_PCRE)
#include <pcre.h>
#else
#include <regex.h>
Expand All @@ -51,7 +53,9 @@ struct mapcache_dimension_values {
struct mapcache_dimension_regex {
mapcache_dimension dimension;
char *regex_string;
#ifdef USE_PCRE
#if defined(USE_PCRE2)
pcre2_code *pcregex;
#elif defined(USE_PCRE)
pcre *pcregex;
#else
regex_t *regex;
Expand Down Expand Up @@ -127,7 +131,13 @@ static apr_array_header_t* _mapcache_dimension_regex_get_entries_for_value(mapca
{
mapcache_dimension_regex *dimension = (mapcache_dimension_regex*)dim;
apr_array_header_t *values = apr_array_make(ctx->pool,1,sizeof(char*));
#ifdef USE_PCRE
#if defined(USE_PCRE2)
pcre2_match_data *match_data;
int rc = pcre2_match(dimension->pcregex,(PCRE2_SPTR)value,strlen(value),0,0,match_data,NULL);
if(rc>0) {
APR_ARRAY_PUSH(values,char*) = apr_pstrdup(ctx->pool,value);
}
#elif defined(USE_PCRE)
int ovector[30];
int rc = pcre_exec(dimension->pcregex,NULL,value,strlen(value),0,0,ovector,30);
if(rc>0) {
Expand Down Expand Up @@ -168,7 +178,18 @@ static void _mapcache_dimension_regex_parse_xml(mapcache_context *ctx, mapcache_
ctx->set_error(ctx,400,"failed to parse %s regex: no <regex> child supplied",dim->class_name);
return;
}
#ifdef USE_PCRE
#if defined(USE_PCRE2)
{
int pcre_err;
PCRE2_SIZE *pcre_offset;
dimension->pcregex = pcre2_compile((PCRE2_SPTR8)dimension->regex_string,strlen(dimension->regex_string), 0, &pcre_err, pcre_offset, NULL);
if(!dimension->pcregex) {
ctx->set_error(ctx,400,"failed to compile regular expression \"%s\" for %s \"%s\": %d",
dimension->regex_string,dim->class_name,dim->name,pcre_err);
return;
}
}
#elif defined(USE_PCRE)
{
const char *pcre_err;
int pcre_offset;
Expand Down Expand Up @@ -294,7 +315,7 @@ mapcache_dimension* mapcache_dimension_regex_create(mapcache_context *ctx, apr_p
mapcache_dimension_regex *dimension = apr_pcalloc(pool, sizeof(mapcache_dimension_regex));
dimension->dimension.type = MAPCACHE_DIMENSION_REGEX;
dimension->dimension.class_name = "dimension";
#ifndef USE_PCRE
#if !defined(USE_PCRE) && !defined(USE_PCRE2)
dimension->regex = (regex_t*)apr_pcalloc(pool, sizeof(regex_t));
#endif
dimension->dimension._get_entries_for_value = _mapcache_dimension_regex_get_entries_for_value;
Expand Down

0 comments on commit 5844d1a

Please sign in to comment.