Skip to content

Commit 0b85061

Browse files
authored
Add support for pcre2. (#314)
* Add support for pcre2. Fixes: #272
1 parent 609bda0 commit 0b85061

File tree

7 files changed

+74
-10
lines changed

7 files changed

+74
-10
lines changed

.github/workflows/build-linux.yml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ jobs:
2828
fi
2929
if [[ 'maximal' =~ ${{ matrix.option }} ]]
3030
then
31-
sudo apt-get install -y libhiredis-dev libdb-dev libmapserver-dev
31+
sudo apt-get install -y libhiredis-dev libdb-dev libmapserver-dev libpcre2-dev
3232
fi
3333
3434
- name: Build MapCache
@@ -55,7 +55,8 @@ jobs:
5555
-DWITH_TIFF=ON \
5656
-DWITH_TIFF_WRITE_SUPPORT=ON \
5757
-DWITH_GEOTIFF=ON \
58-
-DWITH_PCRE=ON \
58+
-DWITH_PCRE=OFF \
59+
-DWITH_PCRE2=ON \
5960
-DWITH_MAPSERVER=ON \
6061
-DWITH_RIAK=OFF"
6162
fi

CMakeLists.txt

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
cmake_minimum_required (VERSION 2.6)
1+
cmake_minimum_required (VERSION 3.0)
22

33
project (MapCache C)
44

@@ -97,6 +97,7 @@ option(WITH_TIFF "Use TIFFs as a cache backend" OFF)
9797
option(WITH_TIFF_WRITE_SUPPORT "Enable (experimental) support for writable TIFF cache backends" OFF)
9898
option(WITH_GEOTIFF "Allow GeoTIFF metadata creation for TIFF cache backends" OFF)
9999
option(WITH_PCRE "Use PCRE for regex tests" OFF)
100+
option(WITH_PCRE2 "Use PCRE2 for regex tests" OFF)
100101
option(WITH_MAPSERVER "Enable (experimental) support for the mapserver library" OFF)
101102
option(WITH_RIAK "Use Riak as a cache backend" OFF)
102103
option(WITH_GDAL "Choose if GDAL raster support should be built in" ON)
@@ -205,6 +206,18 @@ if(WITH_PCRE)
205206
endif(PCRE_FOUND)
206207
endif (WITH_PCRE)
207208

209+
if(WITH_PCRE2)
210+
find_package(PCRE2)
211+
if(PCRE2_FOUND)
212+
include_directories(${PCRE2_INCLUDE_DIR})
213+
target_link_libraries(mapcache PCRE2::PCRE2-8)
214+
set (USE_PCRE2 1)
215+
add_definitions(-DPCRE2_CODE_UNIT_WIDTH=8)
216+
else(PCRE2_FOUND)
217+
report_optional_not_found(PCRE2)
218+
endif(PCRE2_FOUND)
219+
endif (WITH_PCRE2)
220+
208221
if(WITH_SQLITE)
209222
find_package(SQLITE)
210223
if(SQLITE_FOUND)
@@ -357,6 +370,7 @@ status_optional_component("TIFF" "${USE_TIFF}" "${TIFF_LIBRARY}")
357370
status_optional_component("GeoTIFF" "${USE_GEOTIFF}" "${GEOTIFF_LIBRARY}")
358371
status_optional_component("Experimental TIFF write support" "${USE_TIFF_WRITE}" "${TIFF_LIBRARY}")
359372
status_optional_component("PCRE" "${USE_PCRE}" "${PCRE_LIBRARY}")
373+
status_optional_component("PCRE2" "${USE_PCRE2}" "${PCRE2-8_LIBRARY}")
360374
status_optional_component("Experimental mapserver support" "${USE_MAPSERVER}" "${MAPSERVER_LIBRARY}")
361375
status_optional_component("RIAK" "${USE_RIAK}" "${RIAK_LIBRARY}")
362376
status_optional_component("GDAL" "${USE_GDAL}" "${GDAL_LIBRARY}")

cmake/FindBerkeleyDB.cmake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,5 +83,5 @@ endif (BERKELEYDB_FIND_VERSION AND BERKELEYDB_FOUND_TMP)
8383
set(BERKELEYDB_INCLUDE_DIRS ${BERKELEYDB_INCLUDE_DIR})
8484
set(BERKELEYDB_LIBRARIES ${BERKELEYDB_LIBRARY})
8585
include(FindPackageHandleStandardArgs)
86-
find_package_handle_standard_args(BERKELEYDB DEFAULT_MSG BERKELEYDB_LIBRARY BERKELEYDB_INCLUDE_DIR)
86+
find_package_handle_standard_args(BerkeleyDB DEFAULT_MSG BERKELEYDB_LIBRARY BERKELEYDB_INCLUDE_DIR)
8787
mark_as_advanced(BERKELEYDB_LIBRARY BERKELEYDB_INCLUDE_DIR)

cmake/FindPCRE2.cmake

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Distributed under the OSI-approved BSD 3-Clause License. See accompanying
2+
# file COPYING-CMAKE-SCRIPTS or https://cmake.org/licensing for details.
3+
4+
#.rst
5+
# FindPCRE2
6+
# ~~~~~~~~~
7+
# Copyright (C) 2017-2018, Hiroshi Miura
8+
#
9+
# Find the native PCRE2 headers and libraries.
10+
11+
find_path(PCRE2_INCLUDE_DIR NAMES pcre2.h)
12+
find_library(PCRE2-8_LIBRARY NAMES pcre2-8 pcre2-8d pcre2-8-static pcre2-8-staticd NAMES_PER_DIR)
13+
include(FindPackageHandleStandardArgs)
14+
find_package_handle_standard_args(PCRE2
15+
REQUIRED_VARS PCRE2-8_LIBRARY PCRE2_INCLUDE_DIR)
16+
mark_as_advanced(PCRE2_INCLUDE_DIR PCRE2-8_LIBRARY)
17+
if(PCRE2_FOUND)
18+
list(APPEND PCRE2_LIBRARIES "${PCRE2-8_LIBRARY}")
19+
set(PCRE2_INCLUDE_DIRS "${PCRE2_INCLUDE_DIR}")
20+
if(NOT TARGET PCRE2::PCRE2-8)
21+
add_library(PCRE2::PCRE2-8 UNKNOWN IMPORTED)
22+
set_target_properties(PCRE2::PCRE2-8 PROPERTIES
23+
INTERFACE_INCLUDE_DIRECTORIES "${PCRE2_INCLUDE_DIR}"
24+
IMPORTED_LINK_INTERFACE_LANGUAGES "C"
25+
IMPORTED_LOCATION "${PCRE2-8_LIBRARY}")
26+
endif()
27+
endif()

cmake/FindPixman.cmake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,5 @@ FIND_LIBRARY(PIXMAN_LIBRARY
2121
set(PIXMAN_INCLUDE_DIRS ${PIXMAN_INCLUDE_DIR})
2222
set(PIXMAN_LIBRARIES ${PIXMAN_LIBRARY})
2323
include(FindPackageHandleStandardArgs)
24-
find_package_handle_standard_args(PIXMAN DEFAULT_MSG PIXMAN_LIBRARY PIXMAN_INCLUDE_DIR)
24+
find_package_handle_standard_args(Pixman DEFAULT_MSG PIXMAN_LIBRARY PIXMAN_INCLUDE_DIR)
2525
mark_as_advanced(PIXMAN_LIBRARY PIXMAN_INCLUDE_DIR)

include/mapcache-config.h.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#cmakedefine USE_TIFF_WRITE 1
1414
#cmakedefine USE_GEOTIFF 1
1515
#cmakedefine USE_PCRE 1
16+
#cmakedefine USE_PCRE2 1
1617
#cmakedefine USE_MAPSERVER 1
1718
#cmakedefine USE_RIAK 1
1819
#cmakedefine USE_GDAL 1

lib/dimension.c

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@
3131
#include <apr_strings.h>
3232
#include <math.h>
3333
#include <sys/types.h>
34-
#ifdef USE_PCRE
34+
#if defined(USE_PCRE2)
35+
#include <pcre2.h>
36+
#elif defined(USE_PCRE)
3537
#include <pcre.h>
3638
#else
3739
#include <regex.h>
@@ -51,7 +53,9 @@ struct mapcache_dimension_values {
5153
struct mapcache_dimension_regex {
5254
mapcache_dimension dimension;
5355
char *regex_string;
54-
#ifdef USE_PCRE
56+
#if defined(USE_PCRE2)
57+
pcre2_code *pcregex;
58+
#elif defined(USE_PCRE)
5559
pcre *pcregex;
5660
#else
5761
regex_t *regex;
@@ -127,7 +131,13 @@ static apr_array_header_t* _mapcache_dimension_regex_get_entries_for_value(mapca
127131
{
128132
mapcache_dimension_regex *dimension = (mapcache_dimension_regex*)dim;
129133
apr_array_header_t *values = apr_array_make(ctx->pool,1,sizeof(char*));
130-
#ifdef USE_PCRE
134+
#if defined(USE_PCRE2)
135+
pcre2_match_data *match_data;
136+
int rc = pcre2_match(dimension->pcregex,(PCRE2_SPTR)value,strlen(value),0,0,match_data,NULL);
137+
if(rc>0) {
138+
APR_ARRAY_PUSH(values,char*) = apr_pstrdup(ctx->pool,value);
139+
}
140+
#elif defined(USE_PCRE)
131141
int ovector[30];
132142
int rc = pcre_exec(dimension->pcregex,NULL,value,strlen(value),0,0,ovector,30);
133143
if(rc>0) {
@@ -168,7 +178,18 @@ static void _mapcache_dimension_regex_parse_xml(mapcache_context *ctx, mapcache_
168178
ctx->set_error(ctx,400,"failed to parse %s regex: no <regex> child supplied",dim->class_name);
169179
return;
170180
}
171-
#ifdef USE_PCRE
181+
#if defined(USE_PCRE2)
182+
{
183+
int pcre_err;
184+
PCRE2_SIZE *pcre_offset;
185+
dimension->pcregex = pcre2_compile((PCRE2_SPTR8)dimension->regex_string,strlen(dimension->regex_string), 0, &pcre_err, pcre_offset, NULL);
186+
if(!dimension->pcregex) {
187+
ctx->set_error(ctx,400,"failed to compile regular expression \"%s\" for %s \"%s\": %d",
188+
dimension->regex_string,dim->class_name,dim->name,pcre_err);
189+
return;
190+
}
191+
}
192+
#elif defined(USE_PCRE)
172193
{
173194
const char *pcre_err;
174195
int pcre_offset;
@@ -294,7 +315,7 @@ mapcache_dimension* mapcache_dimension_regex_create(mapcache_context *ctx, apr_p
294315
mapcache_dimension_regex *dimension = apr_pcalloc(pool, sizeof(mapcache_dimension_regex));
295316
dimension->dimension.type = MAPCACHE_DIMENSION_REGEX;
296317
dimension->dimension.class_name = "dimension";
297-
#ifndef USE_PCRE
318+
#if !defined(USE_PCRE) && !defined(USE_PCRE2)
298319
dimension->regex = (regex_t*)apr_pcalloc(pool, sizeof(regex_t));
299320
#endif
300321
dimension->dimension._get_entries_for_value = _mapcache_dimension_regex_get_entries_for_value;

0 commit comments

Comments
 (0)