Replace AtomVec::tokenize and use Tokenizer

This commit is contained in:
Richard Berger
2020-05-15 15:42:01 -04:00
parent d41927b056
commit 807130c771
3 changed files with 17 additions and 50 deletions

View File

@ -24,6 +24,7 @@
#include "memory.h"
#include "error.h"
#include "utils.h"
#include "tokenizer.h"
using namespace LAMMPS_NS;
using namespace MathConst;
@ -2457,11 +2458,10 @@ int AtomVec::process_fields(char *str, const char *default_str, Method *method)
}
// tokenize words in both strings
Tokenizer words(str, " ");
Tokenizer def_words(default_str, " ");
char *copy1,*copy2;
char **words,**defwords;
int nfield = tokenize(str,words,copy1);
int ndef = tokenize((char *) default_str,defwords,copy2);
int nfield = words.count();
// process fields one by one, add to index vector
@ -2472,14 +2472,15 @@ int AtomVec::process_fields(char *str, const char *default_str, Method *method)
int match;
for (int i = 0; i < nfield; i++) {
const std::string & field = words[i];
// find field in master Atom::peratom list
for (match = 0; match < nperatom; match++)
if (strcmp(words[i],peratom[match].name) == 0) break;
if (field == peratom[match].name) break;
if (match == nperatom) {
char str[128];
sprintf(str,"Peratom field %s not recognized",words[i]);
sprintf(str,"Peratom field %s not recognized", field.c_str());
error->all(FLERR,str);
}
index[i] = match;
@ -2489,56 +2490,24 @@ int AtomVec::process_fields(char *str, const char *default_str, Method *method)
for (match = 0; match < i; match++)
if (index[i] == index[match]) {
char str[128];
sprintf(str,"Peratom field %s is repeated",words[i]);
sprintf(str,"Peratom field %s is repeated", field.c_str());
error->all(FLERR,str);
}
// error if field is in default str
for (match = 0; match < ndef; match++)
if (strcmp(words[i],defwords[match]) == 0) {
for (auto & def_field : def_words)
if (def_field == field) {
char str[128];
sprintf(str,"Peratom field %s is a default",words[i]);
sprintf(str,"Peratom field %s is a default", field.c_str());
error->all(FLERR,str);
}
}
delete [] copy1;
delete [] copy2;
delete [] words;
delete [] defwords;
method->index = index;
return nfield;
}
/* ----------------------------------------------------------------------
tokenize str into white-space separated words
return nwords = number of words
return words = vector of ptrs to each word
also return copystr since words points into it, caller will delete copystr
------------------------------------------------------------------------- */
int AtomVec::tokenize(char *str, char **&words, char *&copystr)
{
int n = strlen(str) + 1;
copystr = new char[n];
strcpy(copystr,str);
int nword = atom->count_words(copystr);
words = new char*[nword];
nword = 0;
char *word = strtok(copystr," ");
while (word) {
words[nword++] = word;
word = strtok(NULL," ");
}
return nword;
}
/* ----------------------------------------------------------------------
create a method data structs for processing fields
------------------------------------------------------------------------- */

View File

@ -234,7 +234,6 @@ class AtomVec : protected Pointers {
int grow_nmax_bonus(int);
void setup_fields();
int process_fields(char *, const char *, Method *);
int tokenize(char *, char **&, char *&);
void create_method(int, Method *);
void init_method(Method *);
void destroy_method(Method *);

View File

@ -17,6 +17,7 @@
#include "comm.h"
#include "memory.h"
#include "error.h"
#include "tokenizer.h"
using namespace LAMMPS_NS;
@ -513,15 +514,15 @@ char *AtomVecHybrid::merge_fields(int inum, char *root,
// identify unique words in concatenated string
char *copystr;
char **words;
int nwords = tokenize(concat,words,copystr);
Tokenizer words(concat, " ");
int nwords = words.count();
int *unique = new int[nwords];
for (int i = 0; i < nwords; i++) {
unique[i] = 1;
for (int j = 0; j < i; j++)
if (strcmp(words[i],words[j]) == 0) unique[i] = 0;
if (words[i] == words[j]) unique[i] = 0;
}
// construct a new deduped string
@ -531,7 +532,7 @@ char *AtomVecHybrid::merge_fields(int inum, char *root,
for (int i = 0; i < nwords; i++) {
if (!unique[i]) continue;
strcat(dedup,words[i]);
strcat(dedup,words[i].c_str());
if (i < nwords-1) strcat(dedup," ");
}
@ -542,8 +543,6 @@ char *AtomVecHybrid::merge_fields(int inum, char *root,
// clean up
delete [] copystr;
delete [] words;
delete [] unique;
// return final concatenated, deduped string