Files
nethack/sys/share/cppregex.cpp
nhmall 3f93d54b66 some Makefile and hints tinkering
1. remove all window interface bits from compiler.370, and have
   the preceding include files set some variables to control
   the behavior of compiler.370 when it comes to c++.
2. some more common Makefile lines into sys/unix/hints/include/multiw-3.370.
3. make it so you can pass cppregex=1 on the Make command line to build with
   sys/share/cppregex.cpp instead of posixregex.c
4. fix sys/share/cppregex.cpp so that it will build with clang compiler
   (required an additional header include). I don't know if it would have
   worked with g++ without that change. The include can be placed into an #ifdef
   block if there's an issue with the change on other compilers.
5. Anything that needs to compile using c++ (Qt, sys/share/cppregex.cpp) can
   just ensure that CPLUSPLUS_NEEDED Makefile variable is set above the lines
   in compiler.370 to ensure that things get set up for c++. It no longer
   checks specifically for Qt. That is what sys/unix/hints/include/multiw-2.370
   does now.
2022-11-13 22:25:07 -05:00

88 lines
1.9 KiB
C++

/* NetHack 3.7 cppregex.cpp */
/* $NHDT-Date: 1596498279 2020/08/03 23:44:39 $ $NHDT-Branch: NetHack-3.7 $:$NHDT-Revision: 1.9 $ */
/* Copyright (c) Sean Hunt 2015. */
/* NetHack may be freely redistributed. See license for details. */
extern "C" {
#include "config.h"
#define CPPREGEX_C
#include "extern.h"
} // extern "C"
#include <regex>
#include <memory>
#include <cstring>
extern "C" { // rest of file
/* nhregex interface documented in sys/share/posixregex.c */
extern const char regex_id[] = "cppregex";
struct nhregex {
std::unique_ptr<std::regex> re;
std::unique_ptr<std::regex_error> err;
};
struct nhregex *
regex_init(void)
{
return new nhregex;
}
boolean
regex_compile(const char *s, struct nhregex *re)
{
if (!re)
return FALSE;
try {
re->re.reset(new std::regex(s, (std::regex::extended
| std::regex::nosubs
| std::regex::optimize)));
re->err.reset(nullptr);
return TRUE;
} catch (const std::regex_error& err) {
re->err.reset(new std::regex_error(err));
re->re.reset(nullptr);
return FALSE;
}
}
char *
regex_error_desc(struct nhregex *re, char *errbuf)
{
if (!re) {
Strcpy(errbuf, "no regexp");
} else if (!re->err) {
Strcpy(errbuf, "no explanation");
} else {
errbuf[0] = '\0';
(void) strncat(errbuf, re->err->what(), BUFSZ - 1);
if (!errbuf[0])
Strcpy(errbuf, "unspecified regexp error");
}
return errbuf;
}
boolean
regex_match(const char *s, struct nhregex *re)
{
if (!re->re)
return false;
try {
return regex_search(s, *re->re, std::regex_constants::match_any);
} catch (const std::regex_error& err) {
return false;
}
}
void
regex_free(struct nhregex *re)
{
delete re;
}
#undef CPPREGEX_C
} // extern "C"
/*cppregex.cpp*/