Merge branch 'master' into win32-x64-working
Conflicts: include/config.h src/options.c
This commit is contained in:
56
sys/share/cppregex.cpp
Normal file
56
sys/share/cppregex.cpp
Normal file
@@ -0,0 +1,56 @@
|
||||
/* NetHack 3.5 cppregex.cpp $NHDT-Date$ $NHDT-Branch$:$NHDT-Revision$ */
|
||||
/* Copyright (c) Sean Hunt 2015. */
|
||||
/* NetHack may be freely redistributed. See license for details. */
|
||||
|
||||
#include <regex>
|
||||
#include <memory>
|
||||
|
||||
/* nhregex interface documented in sys/share/posixregex.c */
|
||||
|
||||
extern "C" {
|
||||
#include <hack.h>
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
const char *regex_error_desc(struct nhregex *re) {
|
||||
if (re->err)
|
||||
return re->err->what();
|
||||
else
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
92
sys/share/posixregex.c
Normal file
92
sys/share/posixregex.c
Normal file
@@ -0,0 +1,92 @@
|
||||
/* NetHack 3.5 posixregex.c $NHDT-Date: 1428590280 2015/04/09 14:38:00 $ $NHDT-Branch: scshunt-regex $:$NHDT-Revision: 1.0 $ */
|
||||
/* Copyright (c) Sean Hunt 2015. */
|
||||
/* NetHack may be freely redistributed. See license for details. */
|
||||
|
||||
#include <hack.h>
|
||||
|
||||
#include <regex.h>
|
||||
|
||||
/* The nhregex interface is implemented by several source files. The
|
||||
* file to be used can be linked in by the build.
|
||||
*
|
||||
* The regex standard implemented should be POSIX extended regular
|
||||
* expressions, see:
|
||||
* http://pubs.opengroup.org/onlinepubs/009696899/basedefs/xbd_chap09.html
|
||||
* If an implementation uses an alternate expression format, this should
|
||||
* be clearly noted; this will result in incompatibility of config files
|
||||
* when NetHack is compiled with that implementation.
|
||||
*
|
||||
* struct nhregex
|
||||
* The nhregex structure is an opaque structure type containing the
|
||||
* information about a compiled regular expression.
|
||||
*
|
||||
* struct nhregex *regex_init(void)
|
||||
* Used to create a new instance of the nhregex structure. It is
|
||||
* uninitialized and can only safely be passed to regex_compile.
|
||||
*
|
||||
* boolean regex_compile(const char *s, struct nhregex *re)
|
||||
* Used to compile s into a regex and store it in re. Returns TRUE if
|
||||
* successful and FALSE otherwise. re is invalidated regardless of
|
||||
* success.
|
||||
*
|
||||
* const char *regex_error_desc(struct nhregex *re)
|
||||
* Used to retrieve an error description from an error created involving
|
||||
* re. Returns NULL if no description can be retrieved. The returned
|
||||
* string may be a static buffer and so is only valid until the next
|
||||
* call to regex_error_desc.
|
||||
*
|
||||
* boolean regex_match(const char *s, struct nhregex *re)
|
||||
* Used to determine if s (or any substring) matches the regex compiled
|
||||
* into re. Only valid if the most recent call to regex_compile on re
|
||||
* succeeded.
|
||||
*
|
||||
* void regex_free(struct nhregex *re)
|
||||
* Deallocate a regex object.
|
||||
*/
|
||||
|
||||
struct nhregex {
|
||||
regex_t re;
|
||||
int err;
|
||||
};
|
||||
|
||||
struct nhregex *regex_init() {
|
||||
return malloc (sizeof (struct nhregex));
|
||||
}
|
||||
|
||||
boolean regex_compile(const char *s, struct nhregex *re) {
|
||||
if (!re)
|
||||
return FALSE;
|
||||
if ((re->err = regcomp(&re->re, s, REG_EXTENDED | REG_NOSUB)))
|
||||
return FALSE;
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
const char *regex_error_desc(struct nhregex *re) {
|
||||
static char buf[BUFSZ];
|
||||
|
||||
if (!re || !re->err)
|
||||
return NULL;
|
||||
|
||||
/* FIXME: Using a static buffer here is not ideal, but avoids memory
|
||||
* leaks. Consider the allocation more carefully. */
|
||||
regerror(re->err, &re->re, buf, BUFSZ);
|
||||
|
||||
return buf;
|
||||
}
|
||||
|
||||
boolean regex_match(const char *s, struct nhregex *re) {
|
||||
if (!re)
|
||||
return FALSE;
|
||||
|
||||
int result;
|
||||
if ((result = regexec(&re->re, s, 0, NULL, 0))) {
|
||||
if (result != REG_NOMATCH)
|
||||
re->err = result;
|
||||
return FALSE;
|
||||
}
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
void regex_free(struct nhregex *re) {
|
||||
free(re);
|
||||
}
|
||||
Reference in New Issue
Block a user