Skip to content

Commit

Permalink
Update pcre to pcre2
Browse files Browse the repository at this point in the history
No idea if this runs stably...
  • Loading branch information
ryfactor committed Jan 24, 2025
1 parent 69cb3dd commit 6fec2d8
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 79 deletions.
22 changes: 11 additions & 11 deletions FeLib/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
include(FindPkgConfig)
if(PKG_CONFIG_FOUND)
pkg_check_modules(PCRE libpcre)
set(PCRE_LIBRARIES ${PCRE_LDFLAGS})
pkg_check_modules(PCRE2 libpcre2-dev)
set(PCRE2_LIBRARIES ${PCRE2_LDFLAGS})
endif()
if(NOT PCRE_FOUND)
if(NOT PCRE2_FOUND)
if(MSVC)
find_path(PCRE_INCLUDE_DIR NAMES pcre.h)
find_library(PCRE_LIBRARY pcre)
find_path(PCRE2_INCLUDE_DIR NAMES pcre2.h)
find_library(PCRE2_LIBRARY pcre2)
endif()
find_package(PCRE REQUIRED)
add_definitions(${PCRE_DEFINITIONS})
find_package(PCRE2 REQUIRED)
add_definitions(${PCRE2_DEFINITIONS})
endif()

file(GLOB FELIB_SOURCES Include/*.h Source/*.cpp)
Expand Down Expand Up @@ -71,7 +71,7 @@ if(MINGW)

install(CODE [[
file(GET_RUNTIME_DEPENDENCIES
LIBRARIES ${PNG_LIBRARIES} ${SDL2_LIBRARY} ${PCRE_LIBRARIES} ${SDL2_mixer_LIBRARY}
LIBRARIES ${PNG_LIBRARIES} ${SDL2_LIBRARY} ${PCRE2_LIBRARIES} ${SDL2_mixer_LIBRARY}
EXECUTABLES $<TARGET_FILE:ivan>
RESOLVED_DEPENDENCIES_VAR _r_deps
UNRESOLVED_DEPENDENCIES_VAR _u_deps
Expand Down Expand Up @@ -105,13 +105,13 @@ if(MSVC AND _VCPKG_INSTALLED_DIR)
endif()

target_include_directories(FeLib PUBLIC Include)
target_include_directories(FeLib SYSTEM PUBLIC ${SDL2_INCLUDE_DIR} PRIVATE ${PNG_INCLUDE_DIRS} ${PCRE_INCLUDE_DIRS} ${SDL2_mixer_INCLUDE_DIR})
target_include_directories(FeLib SYSTEM PUBLIC ${SDL2_INCLUDE_DIR} PRIVATE ${PNG_INCLUDE_DIRS} ${PCRE2_INCLUDE_DIRS} ${SDL2_mixer_INCLUDE_DIR})
target_include_directories(FeLib PUBLIC ../xbrzscale ../xbrzscale/xbrz)

if((NOT BUILD_SHARED_LIBS) AND BUILD_STATIC_LIBS)
target_link_libraries(FeLib ${PNG_LIBRARIES} ${SDL2_LIBRARIES} ${PCRE_LIBRARIES} ${SDL2_mixer_LIBRARY} -static-libgcc -static-libstdc++)
target_link_libraries(FeLib ${PNG_LIBRARIES} ${SDL2_LIBRARIES} ${PCRE2_LIBRARIES} ${SDL2_mixer_LIBRARY} -static-libgcc -static-libstdc++)
else()
target_link_libraries(FeLib ${PNG_LIBRARIES} ${SDL2_LIBRARY} ${PCRE_LIBRARIES} ${SDL2_mixer_LIBRARY})
target_link_libraries(FeLib ${PNG_LIBRARIES} ${SDL2_LIBRARY} ${PCRE2_LIBRARIES} ${SDL2_mixer_LIBRARY})
endif()

target_compile_options(FeLib PUBLIC ${SDL2_CFLAGS} ${SDL2_CFLAGS_OTHER})
Expand Down
36 changes: 24 additions & 12 deletions FeLib/Source/sfx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@

#include <cstdarg>
#include <cctype>
#include <pcre.h>
#define PCRE2_CODE_UNIT_WIDTH 8
#include <pcre2.h>

#include <ctype.h>

Expand Down Expand Up @@ -58,16 +59,20 @@ struct SoundFile
struct SoundInfo
{
std::vector<int> sounds;
std::unique_ptr<pcre*> re = std::unique_ptr<pcre*>(new (pcre*)());
std::unique_ptr<pcre_extra*> extra = std::unique_ptr<pcre_extra*>(new (pcre_extra*)(NULL));
//std::unique_ptr<pcre_code*> re = std::unique_ptr<pcre_code*>(new (pcre_code*)(NULL));
//std::unique_ptr<pcre_extra*> extra = std::unique_ptr<pcre_extra*>(new (pcre_extra*)(NULL));

std::unique_ptr<pcre2_code*> re = std::unique_ptr<pcre2_code*>(new (pcre2_code*)(NULL));
//std::unique_ptr<pcre2_code*> re (new pcre2_code*(NULL));
std::unique_ptr<pcre2_match_data*> match_data = std::unique_ptr<pcre2_match_data*>(new pcre2_match_data*(NULL));

SoundInfo() = default;
SoundInfo(SoundInfo&) = delete;
SoundInfo(SoundInfo&&) = default;
~SoundInfo()
{
if(re.get() && *re) free(*re);
if(extra.get() && *extra) pcre_free_study(*extra);
if(re.get() && *re) pcre2_code_free(*re);
//if(extra.get() && *extra) pcre_free_study(*extra);
}
};

Expand All @@ -91,8 +96,8 @@ festring getstr(FILE *f, truth word)
FILE *debf = NULL;
void soundeffects::initSound()
{
const char *error;
int erroffset;
int errorCode;
PCRE2_SIZE erroffset;

if(SoundState == 0)
{
Expand Down Expand Up @@ -174,10 +179,17 @@ void soundeffects::initSound()
}

// configure the regex
*si.re = pcre_compile(Pattern.CStr(), 0, &error, &erroffset, NULL);
if(debf && !*si.re) fprintf(debf, "PCRE compilation failed at expression offset %d: %s\n", erroffset, error);
if(*si.re) *si.extra = pcre_study(*si.re, 0, &error);
if(error) *si.extra = NULL;
*si.re = pcre2_compile(reinterpret_cast<const unsigned char *>(Pattern.CStr()), 0, 0, &errorCode, &erroffset, NULL);
if(debf && !*si.re) fprintf(debf, "PCRE compilation failed at expression offset %ld: %d\n", erroffset, errorCode);
//if(*si.re) *si.extra = pcre_study(*si.re, 0, &error);
//if(error) *si.extra = NULL;

if (*si.match_data.get() && *si.match_data)
{
pcre2_match_data_free(*si.match_data);
}

*si.match_data = pcre2_match_data_create_from_pattern(*si.re, NULL);

// configure the assigned files, now they are separated with ',' and the filename now accepts spaces.
festring FileName;
Expand Down Expand Up @@ -257,7 +269,7 @@ SoundFile* soundeffects::findMatchingSound(festring Buffer)
DBG1(Buffer.CStr());
for(int i = patterns.size() - 1; i >= 0; i--){
if(*patterns[i].re)
if(pcre_exec(*patterns[i].re, *patterns[i].extra, Buffer.CStr(), Buffer.GetSize(), 0, 0, NULL, 0) >= 0){
if(pcre2_match(*patterns[i].re, reinterpret_cast<const unsigned char *>(Buffer.CStr()), Buffer.GetSize(), 0, 0, *patterns[i].match_data, 0) >= 0){
SoundFile* p = &files[patterns[i].sounds[rand() % patterns[i].sounds.size()]];
DBG1(p->filename.CStr());
return p;
Expand Down
16 changes: 8 additions & 8 deletions Main/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
include(FindPkgConfig)
if(PKG_CONFIG_FOUND)
pkg_check_modules(PCRE libpcre)
set(PCRE_LIBRARIES ${PCRE_LDFLAGS})
pkg_check_modules(PCRE2 libpcre2)
set(PCRE2_LIBRARIES ${PCRE2_LDFLAGS})
endif()
if(NOT PCRE_FOUND)
if(MSVC)
find_path(PCRE_INCLUDE_DIR NAMES pcre.h)
find_library(PCRE_LIBRARY pcre)
find_path(PCRE2_INCLUDE_DIR NAMES pcre2.h)
find_library(PCRE2_LIBRARY pcre2)
endif()
find_package(PCRE REQUIRED)
add_definitions(${PCRE_DEFINITIONS})
find_package(PCRE2 REQUIRED)
add_definitions(${PCRE2_DEFINITIONS})
endif()

find_package(SDL2 REQUIRED)
Expand All @@ -33,8 +33,8 @@ set_source_files_properties(
PROPERTIES HEADER_FILE_ONLY TRUE)

add_executable(ivan ${IVAN_SOURCES} Resource/Ivan.rc)
target_include_directories(ivan PUBLIC Include ../Felib/Include ../audio ../fantasyname ../FastNoise ${PCRE_INCLUDE_DIRS} ${SDL2_mixer_INCLUDE_DIR})
target_link_libraries(ivan FeLib FeAudio xbrzscale fantasyname fastnoise ${PCRE_LIBRARIES} ${SDL2_mixer_LIBRARY})
target_include_directories(ivan PUBLIC Include ../Felib/Include ../audio ../fantasyname ../FastNoise ${PCRE2_INCLUDE_DIRS} ${SDL2_mixer_INCLUDE_DIR})
target_link_libraries(ivan FeLib FeAudio xbrzscale fantasyname fastnoise ${PCRE2_LIBRARIES} ${SDL2_mixer_LIBRARY})

if(MSVC AND _VCPKG_INSTALLED_DIR)
# Gum solution. Manually copy pcre.dll to where ivan.exe will end up.
Expand Down
27 changes: 17 additions & 10 deletions Main/Source/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
#include <vector>
#include <bitset>
#include <ctime>
#include <pcre.h>
#define PCRE2_CODE_UNIT_WIDTH 8
#include <pcre2.h>

#if defined(UNIX) || defined(__DJGPP__)
#include <sys/stat.h>
Expand Down Expand Up @@ -1325,7 +1326,8 @@ int game::RotateMapNotes()
}

std::vector<festring> afsAutoPickupMatch;
pcre *reAutoPickup=NULL;
pcre2_code *reAutoPickup=NULL;
pcre2_match_data *match_data = NULL;
void game::UpdateAutoPickUpMatching() //simple matching syntax
{
afsAutoPickupMatch.clear();
Expand All @@ -1341,24 +1343,29 @@ void game::UpdateAutoPickUpMatching() //simple matching syntax
}else{
//TODO test regex about: ignoring broken lanterns and bottles, ignore sticks on fire but pickup scrolls on fire
// static bool bDummyInit = [](){reAutoPickup=NULL;return true;}();
const char *errMsg;
int iErrOffset;
if(reAutoPickup)pcre_free(reAutoPickup);
reAutoPickup = pcre_compile(
ivanconfig::GetAutoPickUpMatching().CStr(), //pattern
int errCode;
PCRE2_SIZE iErrOffset;
if(reAutoPickup)pcre2_code_free(reAutoPickup);
reAutoPickup = pcre2_compile(
reinterpret_cast<const unsigned char *>(ivanconfig::GetAutoPickUpMatching().CStr()), //pattern
0, // PCRE2_ZERO_TERMINATED, // indicates the pattern is zero-terminated
0, //no options
&errMsg, &iErrOffset,
&errCode, &iErrOffset,
0); // default char tables
if (!reAutoPickup){
std::vector<festring> afsFullProblems;
afsFullProblems.push_back(festring(errMsg));
afsFullProblems.push_back(festring()+"error code:"+errCode);
afsFullProblems.push_back(festring()+"offset:"+iErrOffset);
bool bDummy = iosystem::AlertConfirmMsg("regex validation failed, if ignored will just not work at all",afsFullProblems,false);
}
match_data = pcre2_match_data_create_from_pattern(reAutoPickup, NULL);
}
}
bool game::IsAutoPickupMatch(cfestring fsName) {
return pcre_exec(reAutoPickup, 0, fsName.CStr(), fsName.GetSize(), 0, 0, NULL, 0) >= 0;
if (reAutoPickup == NULL) {
return false; // pattern not compiled
}
return pcre2_match(reAutoPickup, reinterpret_cast<const unsigned char *>(fsName.CStr()), fsName.GetSize(), 0, 0, match_data, 0) >= 0;
}
int game::CheckAutoPickup(square* sqr)
{
Expand Down
2 changes: 1 addition & 1 deletion Main/Source/message.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

#include <cstdarg>
#include <cctype>
#include <pcre.h>
//#include <pcre2.h>

#include "iconf.h"
#include "message.h"
Expand Down
37 changes: 0 additions & 37 deletions cmake/FindPCRE.cmake

This file was deleted.

37 changes: 37 additions & 0 deletions cmake/FindPCRE2.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Copyright (C) 2007-2009 LuaDist.
# Created by Peter Kapec <[email protected]>
# Redistribution and use of this file is allowed according to the terms of the MIT license.
# For details see the COPYRIGHT file distributed with LuaDist.
# Note:
# Searching headers and libraries is very simple and is NOT as powerful as scripts
# distributed with CMake, because LuaDist defines directories to search for.
# Everyone is encouraged to contact the author with improvements. Maybe this file
# becomes part of CMake distribution sometimes.

# - Find pcre2
# Find the native PCRE2 headers and libraries.
#
# PCRE2_INCLUDE_DIRS - where to find pcre2.h, etc.
# PCRE2_LIBRARIES - List of libraries when using pcre2.
# PCRE2_FOUND - True if pcre2 found.

# Look for the header file.
FIND_PATH(PCRE2_INCLUDE_DIR NAMES pcre2.h)

# Look for the library.
FIND_LIBRARY(PCRE2_LIBRARY NAMES pcre2-8)

# Handle the QUIETLY and REQUIRED arguments and set PCRE2_FOUND to TRUE if all listed variables are TRUE.
INCLUDE(FindPackageHandleStandardArgs)
FIND_PACKAGE_HANDLE_STANDARD_ARGS(PCRE2 DEFAULT_MSG PCRE2_LIBRARY PCRE2_INCLUDE_DIR)

# Copy the results to the output variables.
IF(PCRE2_FOUND)
SET(PCRE2_LIBRARIES ${PCRE2_LIBRARY})
SET(PCRE2_INCLUDE_DIRS ${PCRE2_INCLUDE_DIR})
ELSE(PCRE2_FOUND)
SET(PCRE2_LIBRARIES)
SET(PCRE2_INCLUDE_DIRS)
ENDIF(PCRE2_FOUND)

MARK_AS_ADVANCED(PCRE2_INCLUDE_DIRS PCRE2_LIBRARIES)

0 comments on commit 6fec2d8

Please sign in to comment.