#include "utils.h"
#include <cstring>
#include <cstdlib>
#include "lammps.h"
#include "error.h"
extern "C"
{
static int re_match(const char *text, const char *pattern);
}
using namespace LAMMPS_NS;
bool utils::strmatch(std::string text, std::string pattern)
{
const int pos = re_match(text.c_str(),pattern.c_str());
return (pos >= 0);
}
int utils::cfvarg(std::string mode, const char *arg, char *&cfv_id)
{
int rv = utils::NONE;
cfv_id = NULL;
if (!arg) return rv;
if (utils::strmatch(arg,std::string("^[") + mode + "]_")) {
if (*arg == 'c') rv = utils::COMPUTE;
else if (*arg == 'f') rv = utils::FIX;
else if (*arg == 'v') rv = utils::VARIABLE;
else return rv;
arg += 2;
int n = strlen(arg)+1;
cfv_id = new char[n];
strcpy(cfv_id,arg);
}
return rv;
}
void utils::sfgets(const char *srcname, int srcline, char *s, int size,
FILE *fp, const char *filename, Error *error)
{
char *rv = fgets(s,size,fp);
if (rv == NULL) { std::string errmsg;
if (feof(fp)) {
errmsg = "Unexpected end of file while reading file '";
} else if (ferror(fp)) {
errmsg = "Unexpected error while reading file '";
} else {
errmsg = "Unexpected short read while reading file '";
}
errmsg += filename;
errmsg += "'";
if (error) error->one(srcname,srcline,errmsg.c_str());
if (s) *s = '\0'; }
return;
}
std::string utils::check_packages_for_style(std::string style,
std::string name, LAMMPS *lmp)
{
std::string errmsg = "Unrecognized " + style + " style '" + name + "'";
const char *pkg = lmp->match_style(style.c_str(),name.c_str());
if (pkg) {
errmsg += " is part of the " + std::string(pkg) + " package";
if (lmp->is_installed_pkg(pkg))
errmsg += ", but seems to be missing because of a dependency";
else
errmsg += " which is not enabled in this LAMMPS binary.";
}
return errmsg;
}
double utils::numeric(const char *file, int line, const char *str,
bool do_abort, LAMMPS *lmp)
{
int n = 0;
if (str) n = strlen(str);
if (n == 0) {
if (do_abort)
lmp->error->one(file,line,"Expected floating point parameter instead of"
" NULL or empty string in input script or data file");
else
lmp->error->all(file,line,"Expected floating point parameter instead of"
" NULL or empty string in input script or data file");
}
for (int i = 0; i < n; i++) {
if (isdigit(str[i])) continue;
if (str[i] == '-' || str[i] == '+' || str[i] == '.') continue;
if (str[i] == 'e' || str[i] == 'E') continue;
std::string msg("Expected floating point parameter instead of '");
msg += str;
msg += "' in input script or data file";
if (do_abort)
lmp->error->one(file,line,msg.c_str());
else
lmp->error->all(file,line,msg.c_str());
}
return atof(str);
}
int utils::inumeric(const char *file, int line, const char *str,
bool do_abort, LAMMPS *lmp)
{
int n = 0;
if (str) n = strlen(str);
if (n == 0) {
if (do_abort)
lmp->error->one(file,line,"Expected integer parameter instead of "
"NULL or empty string in input script or data file");
else
lmp->error->all(file,line,"Expected integer parameter instead of "
"NULL or empty string in input script or data file");
}
for (int i = 0; i < n; i++) {
if (isdigit(str[i]) || str[i] == '-' || str[i] == '+') continue;
std::string msg("Expected integer parameter instead of '");
msg += str;
msg += "' in input script or data file";
if (do_abort)
lmp->error->one(file,line,msg.c_str());
else
lmp->error->all(file,line,msg.c_str());
}
return atoi(str);
}
bigint utils::bnumeric(const char *file, int line, const char *str,
bool do_abort, LAMMPS *lmp)
{
int n = 0;
if (str) n = strlen(str);
if (n == 0) {
if (do_abort)
lmp->error->one(file,line,"Expected integer parameter instead of "
"NULL or empty string in input script or data file");
else
lmp->error->all(file,line,"Expected integer parameter instead of "
"NULL or empty string in input script or data file");
}
for (int i = 0; i < n; i++) {
if (isdigit(str[i]) || str[i] == '-' || str[i] == '+') continue;
std::string msg("Expected integer parameter instead of '");
msg += str;
msg += "' in input script or data file";
if (do_abort)
lmp->error->one(file,line,msg.c_str());
else
lmp->error->all(file,line,msg.c_str());
}
return ATOBIGINT(str);
}
tagint utils::tnumeric(const char *file, int line, const char *str,
bool do_abort, LAMMPS *lmp)
{
int n = 0;
if (str) n = strlen(str);
if (n == 0) {
if (do_abort)
lmp->error->one(file,line,"Expected integer parameter instead of "
"NULL or empty string in input script or data file");
else
lmp->error->all(file,line,"Expected integer parameter instead of "
"NULL or empty string in input script or data file");
}
for (int i = 0; i < n; i++) {
if (isdigit(str[i]) || str[i] == '-' || str[i] == '+') continue;
std::string msg("Expected integer parameter instead of '");
msg += str;
msg += "' in input script or data file";
if (do_abort)
lmp->error->one(file,line,msg.c_str());
else
lmp->error->all(file,line,msg.c_str());
}
return ATOTAGINT(str);
}
extern "C" {
typedef struct regex_t *re_t;
static re_t re_compile(const char *pattern);
static int re_matchp(const char *text, re_t pattern);
#define MAX_REGEXP_OBJECTS 30
#define MAX_CHAR_CLASS_LEN 40
enum { UNUSED, DOT, BEGIN, END, QUESTIONMARK, STAR, PLUS,
CHAR, CHAR_CLASS, INV_CHAR_CLASS, DIGIT, NOT_DIGIT,
INTEGER, NOT_INTEGER, FLOAT, NOT_FLOAT,
ALPHA, NOT_ALPHA, WHITESPACE, NOT_WHITESPACE };
typedef struct regex_t {
unsigned char type;
union {
unsigned char ch;
unsigned char *ccl;
};
} regex_t;
static int matchpattern(regex_t *pattern, const char *text);
static int matchcharclass(char c, const char *str);
static int matchstar(regex_t p, regex_t *pattern, const char *text);
static int matchplus(regex_t p, regex_t *pattern, const char *text);
static int matchone(regex_t p, char c);
static int matchdigit(char c);
static int matchint(char c);
static int matchfloat(char c);
static int matchalpha(char c);
static int matchwhitespace(char c);
static int matchmetachar(char c, const char *str);
static int matchrange(char c, const char *str);
static int ismetachar(char c);
int re_match(const char *text, const char *pattern)
{
return re_matchp(text, re_compile(pattern));
}
int re_matchp(const char *text, re_t pattern)
{
if (pattern != 0) {
if (pattern[0].type == BEGIN) {
return ((matchpattern(&pattern[1], text)) ? 0 : -1);
} else {
int idx = -1;
do {
idx += 1;
if (matchpattern(pattern, text)) {
if (text[0] == '\0')
return -1;
return idx;
}
}
while (*text++ != '\0');
}
}
return -1;
}
re_t re_compile(const char *pattern)
{
static regex_t re_compiled[MAX_REGEXP_OBJECTS];
static unsigned char ccl_buf[MAX_CHAR_CLASS_LEN];
int ccl_bufidx = 1;
char c;
int i = 0;
int j = 0;
while (pattern[i] != '\0' && (j+1 < MAX_REGEXP_OBJECTS)) {
c = pattern[i];
switch (c) {
case '^': { re_compiled[j].type = BEGIN; } break;
case '$': { re_compiled[j].type = END; } break;
case '.': { re_compiled[j].type = DOT; } break;
case '*': { re_compiled[j].type = STAR; } break;
case '+': { re_compiled[j].type = PLUS; } break;
case '?': { re_compiled[j].type = QUESTIONMARK; } break;
case '\\': {
if (pattern[i+1] != '\0') {
i += 1;
switch (pattern[i]) {
case 'd': { re_compiled[j].type = DIGIT; } break;
case 'D': { re_compiled[j].type = NOT_DIGIT; } break;
case 'i': { re_compiled[j].type = INTEGER; } break;
case 'I': { re_compiled[j].type = NOT_INTEGER; } break;
case 'f': { re_compiled[j].type = FLOAT; } break;
case 'F': { re_compiled[j].type = NOT_FLOAT; } break;
case 'w': { re_compiled[j].type = ALPHA; } break;
case 'W': { re_compiled[j].type = NOT_ALPHA; } break;
case 's': { re_compiled[j].type = WHITESPACE; } break;
case 'S': { re_compiled[j].type = NOT_WHITESPACE; } break;
default: {
re_compiled[j].type = CHAR;
re_compiled[j].ch = pattern[i];
} break;
}
}
} break;
case '[': {
int buf_begin = ccl_bufidx;
if (pattern[i+1] == '^') {
re_compiled[j].type = INV_CHAR_CLASS;
i += 1;
} else {
re_compiled[j].type = CHAR_CLASS;
}
while ((pattern[++i] != ']') && (pattern[i] != '\0')) {
if (pattern[i] == '\\') {
if (ccl_bufidx >= MAX_CHAR_CLASS_LEN - 1) {
return 0;
}
ccl_buf[ccl_bufidx++] = pattern[i++];
} else if (ccl_bufidx >= MAX_CHAR_CLASS_LEN) {
return 0;
}
ccl_buf[ccl_bufidx++] = pattern[i];
}
if (ccl_bufidx >= MAX_CHAR_CLASS_LEN) {
return 0;
}
ccl_buf[ccl_bufidx++] = 0;
re_compiled[j].ccl = &ccl_buf[buf_begin];
} break;
default: {
re_compiled[j].type = CHAR;
re_compiled[j].ch = c;
} break;
}
i += 1;
j += 1;
}
re_compiled[j].type = UNUSED;
return (re_t) re_compiled;
}
static int matchdigit(char c)
{
return ((c >= '0') && (c <= '9'));
}
static int matchint(char c)
{
return (matchdigit(c) || (c == '-') || (c == '+'));
}
static int matchfloat(char c)
{
return (matchint(c) || (c == '.') || (c == 'e') || (c == 'E'));
}
static int matchalpha(char c)
{
return ((c >= 'a') && (c <= 'z')) || ((c >= 'A') && (c <= 'Z'));
}
static int matchwhitespace(char c)
{
return ((c == ' ') || (c == '\t') || (c == '\n') || (c == '\r') || (c == '\f') || (c == '\v'));
}
static int matchalphanum(char c)
{
return ((c == '_') || matchalpha(c) || matchdigit(c));
}
static int matchrange(char c, const char *str)
{
return ((c != '-') && (str[0] != '\0')
&& (str[0] != '-') && (str[1] == '-')
&& (str[1] != '\0') && (str[2] != '\0')
&& ((c >= str[0]) && (c <= str[2])));
}
static int ismetachar(char c)
{
return ((c == 's') || (c == 'S')
|| (c == 'w') || (c == 'W')
|| (c == 'd') || (c == 'D'));
}
static int matchmetachar(char c, const char *str)
{
switch (str[0]) {
case 'd': return matchdigit(c);
case 'D': return !matchdigit(c);
case 'i': return matchint(c);
case 'I': return !matchint(c);
case 'f': return matchfloat(c);
case 'F': return !matchfloat(c);
case 'w': return matchalphanum(c);
case 'W': return !matchalphanum(c);
case 's': return matchwhitespace(c);
case 'S': return !matchwhitespace(c);
default: return (c == str[0]);
}
}
static int matchcharclass(char c, const char *str)
{
do {
if (matchrange(c, str)) {
return 1;
} else if (str[0] == '\\') {
str += 1;
if (matchmetachar(c, str)) {
return 1;
} else if ((c == str[0]) && !ismetachar(c)) {
return 1;
}
} else if (c == str[0]) {
if (c == '-') {
return ((str[-1] == '\0') || (str[1] == '\0'));
} else {
return 1;
}
}
}
while (*str++ != '\0');
return 0;
}
static int matchone(regex_t p, char c)
{
switch (p.type) {
case DOT: return 1;
case CHAR_CLASS: return matchcharclass(c, (const char *)p.ccl);
case INV_CHAR_CLASS: return !matchcharclass(c, (const char *)p.ccl);
case DIGIT: return matchdigit(c);
case NOT_DIGIT: return !matchdigit(c);
case INTEGER: return matchint(c);
case NOT_INTEGER: return !matchint(c);
case FLOAT: return matchfloat(c);
case NOT_FLOAT: return !matchfloat(c);
case ALPHA: return matchalphanum(c);
case NOT_ALPHA: return !matchalphanum(c);
case WHITESPACE: return matchwhitespace(c);
case NOT_WHITESPACE: return !matchwhitespace(c);
default: return (p.ch == c);
}
}
static int matchstar(regex_t p, regex_t *pattern, const char *text)
{
do {
if (matchpattern(pattern, text))
return 1;
}
while ((text[0] != '\0') && matchone(p, *text++));
return 0;
}
static int matchplus(regex_t p, regex_t *pattern, const char *text)
{
while ((text[0] != '\0') && matchone(p, *text++)) {
if (matchpattern(pattern, text))
return 1;
}
return 0;
}
static int matchquestion(regex_t p, regex_t *pattern, const char *text)
{
if (p.type == UNUSED)
return 1;
if (matchpattern(pattern, text))
return 1;
if (*text && matchone(p, *text++))
return matchpattern(pattern, text);
return 0;
}
static int matchpattern(regex_t *pattern, const char *text)
{
do {
if ((pattern[0].type == UNUSED) || (pattern[1].type == QUESTIONMARK)) {
return matchquestion(pattern[0], &pattern[2], text);
} else if (pattern[1].type == STAR) {
return matchstar(pattern[0], &pattern[2], text);
} else if (pattern[1].type == PLUS) {
return matchplus(pattern[0], &pattern[2], text);
} else if ((pattern[0].type == END) && pattern[1].type == UNUSED) {
return (text[0] == '\0');
}
}
while ((text[0] != '\0') && matchone(*pattern++, *text++));
return 0;
}
}