complete refactor of file/atomfile variable parsing code

This commit is contained in:
Axel Kohlmeyer
2021-03-14 10:45:07 -04:00
parent 14abdade03
commit 194a551590

View File

@ -31,6 +31,7 @@
#include "random_mars.h" #include "random_mars.h"
#include "region.h" #include "region.h"
#include "thermo.h" #include "thermo.h"
#include "tokenizer.h"
#include "universe.h" #include "universe.h"
#include "update.h" #include "update.h"
@ -5112,19 +5113,17 @@ int VarReader::read_scalar(char *str)
if (me == 0) { if (me == 0) {
while (1) { while (1) {
if (fgets(str,MAXLINE,fp) == nullptr) n = 0; ptr = fgets(str,MAXLINE,fp);
else n = strlen(str); if (!ptr) { n=0; break; } // end of file
if (n == 0) break; // end of file ptr[strcspn(ptr,"#")] = '\0'; // strip comment
str[n-1] = '\0'; // strip newline ptr += strspn(ptr," \t\n\r\f"); // strip leading whitespace
if ((ptr = strchr(str,'#'))) *ptr = '\0'; // strip comment ptr[strcspn(ptr," \t\n\r\f")] = '\0'; // strip trailing whitespace
str += strspn(" \t\n\r\f"); // strip whitespace n = strlen(ptr) + 1;
str[strcspn(str," \t\n\r\f")] = '\0'; if (n == 1) continue; // skip if blank line
n = strlen(str) + 1;
if (n == 1) continue; // skip if blank
break; break;
} }
memmove(str,ptr,n); // move trimmed string back
} }
MPI_Bcast(&n,1,MPI_INT,0,world); MPI_Bcast(&n,1,MPI_INT,0,world);
if (n == 0) return 1; if (n == 0) return 1;
MPI_Bcast(str,n,MPI_CHAR,0,world); MPI_Bcast(str,n,MPI_CHAR,0,world);
@ -5157,22 +5156,20 @@ int VarReader::read_peratom()
char str[MAXLINE]; char str[MAXLINE];
if (me == 0) { if (me == 0) {
while (1) { while (1) {
if (fgets(str,MAXLINE,fp) == nullptr) n = 0; ptr = fgets(str,MAXLINE,fp);
else n = strlen(str); if (!ptr) { n=0; break; } // end of file
if (n == 0) break; // end of file ptr[strcspn(ptr,"#")] = '\0'; // strip comment
str[n-1] = '\0'; // strip newline ptr += strspn(ptr," \t\n\r\f"); // strip leading whitespace
if ((ptr = strchr(str,'#'))) *ptr = '\0'; // strip comment ptr[strcspn(ptr," \t\n\r\f")] = '\0'; // strip trailing whitespace
str += strspn(" \t\n\r\f"); // strip whitespace n = strlen(ptr) + 1;
str[strcspn(str," \t\n\r\f")] = '\0'; if (n == 1) continue; // skip if blank line
n = strlen(str) + 1;
if (n == 1) continue; // skip if blank
break; break;
} }
memmove(str,ptr,n); // move trimmed string back
} }
MPI_Bcast(&n,1,MPI_INT,0,world); MPI_Bcast(&n,1,MPI_INT,0,world);
if (n == 0) return 1; if (n == 0) return 1;
MPI_Bcast(str,n,MPI_CHAR,0,world); MPI_Bcast(str,n,MPI_CHAR,0,world);
bigint nlines = utils::bnumeric(FLERR,str,false,lmp); bigint nlines = utils::bnumeric(FLERR,str,false,lmp);
tagint map_tag_max = atom->map_tag_max; tagint map_tag_max = atom->map_tag_max;
@ -5187,9 +5184,17 @@ int VarReader::read_peratom()
for (i = 0; i < nchunk; i++) { for (i = 0; i < nchunk; i++) {
next = strchr(buf,'\n'); next = strchr(buf,'\n');
*next = '\0'; *next = '\0';
int rv = sscanf(buf,TAGINT_FORMAT " %lg",&tag,&value); try {
if (tag <= 0 || tag > map_tag_max || rv != 2) ValueTokenizer words(buf);
error->one(FLERR,"Invalid atom ID in variable file"); tag = words.next_bigint();
value = words.next_double();
} catch (TokenizerException &e) {
error->all(FLERR,fmt::format("Invalid atomfile line '{}': {}",
buf,e.what()));
}
if ((tag <= 0) || (tag > map_tag_max))
error->all(FLERR,fmt::format("Invalid atom ID {} in variable "
"file", tag));
if ((m = atom->map(tag)) >= 0) vstore[m] = value; if ((m = atom->map(tag)) >= 0) vstore[m] = value;
buf = next + 1; buf = next + 1;
} }