ArgInfo class for simpler parsing of compute, fix, variable references
This commit is contained in:
74
src/arg_info.cpp
Normal file
74
src/arg_info.cpp
Normal file
@ -0,0 +1,74 @@
|
||||
/* ----------------------------------------------------------------------
|
||||
LAMMPS - Large-scale Atomic/Molecular Massively Parallel Simulator
|
||||
https://lammps.sandia.gov/, Sandia National Laboratories
|
||||
Steve Plimpton, sjplimp@sandia.gov
|
||||
|
||||
Copyright (2003) Sandia Corporation. Under the terms of Contract
|
||||
DE-AC04-94AL85000 with Sandia Corporation, the U.S. Government retains
|
||||
certain rights in this software. This software is distributed under
|
||||
the GNU General Public License.
|
||||
|
||||
See the README file in the top-level LAMMPS directory.
|
||||
------------------------------------------------------------------------- */
|
||||
|
||||
#include "arg_info.h"
|
||||
|
||||
#include <stdexcept>
|
||||
#include <cstring>
|
||||
|
||||
using namespace LAMMPS_NS;
|
||||
|
||||
ArgInfo::ArgInfo(const std::string &arg, int allowed)
|
||||
: type(NONE), dim(0), index1(-1), index2(-1)
|
||||
{
|
||||
if ((arg.size() > 2) && (arg[1] == '_')) {
|
||||
if ((arg[0] == 'c') && (allowed & COMPUTE)) type = COMPUTE;
|
||||
else if ((arg[0] == 'f') && (allowed & FIX)) type = FIX;
|
||||
else if ((arg[0] == 'v') && (allowed & VARIABLE)) type = VARIABLE;
|
||||
else {
|
||||
name = arg;
|
||||
return;
|
||||
}
|
||||
|
||||
std::size_t has_idx1 = arg.find('[',2);
|
||||
if (has_idx1 != std::string::npos) {
|
||||
name = arg.substr(2,has_idx1-2);
|
||||
dim = 1;
|
||||
|
||||
std::size_t has_idx2 = arg.find('[',has_idx1+1);
|
||||
if (has_idx2 != std::string::npos) {
|
||||
dim = 2;
|
||||
|
||||
if (arg[arg.size()-1] != ']') {
|
||||
type = UNKNOWN;
|
||||
} else {
|
||||
try {
|
||||
index2 = std::stoi(arg.substr(has_idx2+1,arg.size()-(has_idx2+2)));
|
||||
} catch (std::invalid_argument &) {
|
||||
type = UNKNOWN;
|
||||
}
|
||||
}
|
||||
} else has_idx2 = arg.size();
|
||||
|
||||
if (arg[has_idx2-1] != ']') {
|
||||
type = UNKNOWN;
|
||||
} else {
|
||||
try {
|
||||
index1 = std::stoi(arg.substr(has_idx1+1,arg.size()-(has_idx2+2)));
|
||||
} catch (std::invalid_argument &) {
|
||||
type = UNKNOWN;
|
||||
}
|
||||
}
|
||||
} else name = arg.substr(2);
|
||||
} else name = arg;
|
||||
}
|
||||
|
||||
/* ---------------------------------------------------------------------- */
|
||||
|
||||
char *ArgInfo::copy_name()
|
||||
{
|
||||
char *dest = new char[name.size()+1];
|
||||
strcpy(dest,name.c_str());
|
||||
return dest;
|
||||
}
|
||||
|
||||
71
src/arg_info.h
Normal file
71
src/arg_info.h
Normal file
@ -0,0 +1,71 @@
|
||||
/* -*- c++ -*- ----------------------------------------------------------
|
||||
LAMMPS - Large-scale Atomic/Molecular Massively Parallel Simulator
|
||||
http://lammps.sandia.gov, Sandia National Laboratories
|
||||
Steve Plimpton, sjplimp@sandia.gov
|
||||
|
||||
Copyright (2003) Sandia Corporation. Under the terms of Contract
|
||||
DE-AC04-94AL85000 with Sandia Corporation, the U.S. Government retains
|
||||
certain rights in this software. This software is distributed under
|
||||
the GNU General Public License.
|
||||
|
||||
See the README file in the top-level LAMMPS directory.
|
||||
------------------------------------------------------------------------- */
|
||||
|
||||
#ifndef LMP_ARG_INFO_H
|
||||
#define LMP_ARG_INFO_H
|
||||
|
||||
/*! \file arg_info.h */
|
||||
|
||||
#include <string>
|
||||
|
||||
namespace LAMMPS_NS {
|
||||
class ArgInfo {
|
||||
public:
|
||||
enum {
|
||||
ERROR =-2,
|
||||
UNKNOWN =-1,
|
||||
NONE = 0,
|
||||
X = 1<<0,
|
||||
V = 1<<1,
|
||||
F = 1<<2,
|
||||
COMPUTE = 1<<3,
|
||||
FIX = 1<<4,
|
||||
VARIABLE = 1<<5,
|
||||
KEYWORD = 1<<6,
|
||||
TYPE = 1<<7,
|
||||
MOLECULE = 1<<8,
|
||||
DNAME = 1<<9,
|
||||
INAME = 1<<10,
|
||||
DENSITY_NUMBER = 1<<11,
|
||||
DENSITY_MASS = 1<<12,
|
||||
MASS = 1<<13,
|
||||
TEMPERATURE = 1<<14,
|
||||
BIN1D = 1<<15,
|
||||
BIN2D = 1<<16,
|
||||
BIN3D = 1<<17,
|
||||
BINSPHERE = 1<<18,
|
||||
BINCYLINDER = 1<<19
|
||||
};
|
||||
|
||||
ArgInfo(const std::string &arg, int allowed=COMPUTE|FIX|VARIABLE);
|
||||
virtual ~ArgInfo() {}
|
||||
|
||||
public:
|
||||
int get_type() const { return type; }
|
||||
int get_dim() const { return dim; }
|
||||
int get_index1() const { return index1; }
|
||||
int get_index2() const { return index2; }
|
||||
const char *get_name() const { return name.c_str(); }
|
||||
char *copy_name();
|
||||
|
||||
private:
|
||||
std::string name;
|
||||
int type, dim, index1, index2;
|
||||
|
||||
// disabled standard methods
|
||||
ArgInfo() {}
|
||||
ArgInfo(const ArgInfo &) {}
|
||||
void operator=(const ArgInfo &) {}
|
||||
};
|
||||
}
|
||||
#endif
|
||||
Reference in New Issue
Block a user