Add ValueTokenizer

This commit is contained in:
Richard Berger
2020-05-18 16:40:11 -04:00
parent 46239e4577
commit 7ac0f869ef
3 changed files with 168 additions and 0 deletions

View File

@ -16,6 +16,7 @@
------------------------------------------------------------------------- */
#include "tokenizer.h"
#include "utils.h"
using namespace LAMMPS_NS;
@ -44,6 +45,14 @@ Tokenizer::iterator Tokenizer::end() {
return tokens.end();
}
Tokenizer::const_iterator Tokenizer::cbegin() const {
return tokens.cbegin();
}
Tokenizer::const_iterator Tokenizer::cend() const {
return tokens.cend();
}
const std::string & Tokenizer::operator[](size_t index) {
return tokens[index];
}
@ -51,3 +60,74 @@ const std::string & Tokenizer::operator[](size_t index) {
const size_t Tokenizer::count() const {
return tokens.size();
}
ValueTokenizer::ValueTokenizer(const std::string & str, const std::string & seperators) : tokens(str, seperators) {
current = tokens.begin();
}
bool ValueTokenizer::has_next() const {
return current != tokens.cend();
}
std::string ValueTokenizer::next_string() {
if (has_next()) {
std::string value = *current;
++current;
return value;
}
return "";
}
int ValueTokenizer::next_int() {
if (has_next()) {
if(!utils::is_integer(*current)) {
throw InvalidIntegerException(*current);
}
int value = atoi(current->c_str());
++current;
return value;
}
return 0;
}
bigint ValueTokenizer::next_bigint() {
if (has_next()) {
if(!utils::is_integer(*current)) {
throw InvalidIntegerException(*current);
}
bigint value = ATOBIGINT(current->c_str());
++current;
return value;
}
return 0;
}
tagint ValueTokenizer::next_tagint() {
if (current != tokens.end()) {
if(!utils::is_integer(*current)) {
throw InvalidIntegerException(*current);
}
tagint value = ATOTAGINT(current->c_str());
++current;
return value;
}
return 0;
}
double ValueTokenizer::next_double() {
if (current != tokens.end()) {
if(!utils::is_double(*current)) {
throw InvalidFloatException(*current);
}
double value = atof(current->c_str());
++current;
return value;
}
return 0.0;
}
void ValueTokenizer::skip(int ntokens) {
current = std::next(current, ntokens);
}