Merge pull request #1537 from akohlmey/refactor-input-file-handling
Refactor input file processing flow of control
This commit is contained in:
@ -85,8 +85,9 @@ Input::Input(LAMMPS *lmp, int argc, char **argv) : Pointers(lmp)
|
|||||||
ifthenelse_flag = 0;
|
ifthenelse_flag = 0;
|
||||||
|
|
||||||
if (me == 0) {
|
if (me == 0) {
|
||||||
nfile = maxfile = 1;
|
nfile = 1;
|
||||||
infiles = (FILE **) memory->smalloc(sizeof(FILE *),"input:infiles");
|
maxfile = 16;
|
||||||
|
infiles = new FILE *[maxfile];
|
||||||
infiles[0] = infile;
|
infiles[0] = infile;
|
||||||
} else infiles = NULL;
|
} else infiles = NULL;
|
||||||
|
|
||||||
@ -138,7 +139,7 @@ Input::~Input()
|
|||||||
memory->sfree(work);
|
memory->sfree(work);
|
||||||
if (labelstr) delete [] labelstr;
|
if (labelstr) delete [] labelstr;
|
||||||
memory->sfree(arg);
|
memory->sfree(arg);
|
||||||
memory->sfree(infiles);
|
delete [] infiles;
|
||||||
delete variable;
|
delete variable;
|
||||||
|
|
||||||
delete command_map;
|
delete command_map;
|
||||||
@ -206,17 +207,7 @@ void Input::file()
|
|||||||
MPI_Bcast(&n,1,MPI_INT,0,world);
|
MPI_Bcast(&n,1,MPI_INT,0,world);
|
||||||
if (n == 0) {
|
if (n == 0) {
|
||||||
if (label_active) error->all(FLERR,"Label wasn't found in input script");
|
if (label_active) error->all(FLERR,"Label wasn't found in input script");
|
||||||
if (me == 0) {
|
break;
|
||||||
if (infile != stdin) {
|
|
||||||
fclose(infile);
|
|
||||||
infile = NULL;
|
|
||||||
}
|
|
||||||
nfile--;
|
|
||||||
}
|
|
||||||
MPI_Bcast(&nfile,1,MPI_INT,0,world);
|
|
||||||
if (nfile == 0) break;
|
|
||||||
if (me == 0) infile = infiles[nfile-1];
|
|
||||||
continue;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (n > maxline) reallocate(line,maxline,n);
|
if (n > maxline) reallocate(line,maxline,n);
|
||||||
@ -250,8 +241,8 @@ void Input::file()
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* ----------------------------------------------------------------------
|
/* ----------------------------------------------------------------------
|
||||||
process all input from filename
|
process all input from file at filename
|
||||||
called from library interface
|
mostly called from library interface
|
||||||
------------------------------------------------------------------------- */
|
------------------------------------------------------------------------- */
|
||||||
|
|
||||||
void Input::file(const char *filename)
|
void Input::file(const char *filename)
|
||||||
@ -261,21 +252,27 @@ void Input::file(const char *filename)
|
|||||||
// call to file() will close filename and decrement nfile
|
// call to file() will close filename and decrement nfile
|
||||||
|
|
||||||
if (me == 0) {
|
if (me == 0) {
|
||||||
if (nfile > 1)
|
if (nfile == maxfile)
|
||||||
error->one(FLERR,"Invalid use of library file() function");
|
error->one(FLERR,"Too many nested levels of input scripts");
|
||||||
|
|
||||||
if (infile && infile != stdin) fclose(infile);
|
|
||||||
infile = fopen(filename,"r");
|
infile = fopen(filename,"r");
|
||||||
if (infile == NULL) {
|
if (infile == NULL) {
|
||||||
char str[128];
|
char str[128];
|
||||||
snprintf(str,128,"Cannot open input script %s",filename);
|
snprintf(str,128,"Cannot open input script %s",filename);
|
||||||
error->one(FLERR,str);
|
error->one(FLERR,str);
|
||||||
}
|
}
|
||||||
infiles[0] = infile;
|
infiles[nfile++] = infile;
|
||||||
nfile = 1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// process contents of file
|
||||||
|
|
||||||
file();
|
file();
|
||||||
|
|
||||||
|
if (me == 0) {
|
||||||
|
fclose(infile);
|
||||||
|
nfile--;
|
||||||
|
infile = infiles[nfile-1];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* ----------------------------------------------------------------------
|
/* ----------------------------------------------------------------------
|
||||||
@ -1045,11 +1042,9 @@ void Input::include()
|
|||||||
error->all(FLERR,"Cannot use include command within an if command");
|
error->all(FLERR,"Cannot use include command within an if command");
|
||||||
|
|
||||||
if (me == 0) {
|
if (me == 0) {
|
||||||
if (nfile == maxfile) {
|
if (nfile == maxfile)
|
||||||
maxfile++;
|
error->one(FLERR,"Too many nested levels of input scripts");
|
||||||
infiles = (FILE **)
|
|
||||||
memory->srealloc(infiles,maxfile*sizeof(FILE *),"input:infiles");
|
|
||||||
}
|
|
||||||
infile = fopen(arg[0],"r");
|
infile = fopen(arg[0],"r");
|
||||||
if (infile == NULL) {
|
if (infile == NULL) {
|
||||||
char str[128];
|
char str[128];
|
||||||
@ -1058,6 +1053,16 @@ void Input::include()
|
|||||||
}
|
}
|
||||||
infiles[nfile++] = infile;
|
infiles[nfile++] = infile;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// process contents of file
|
||||||
|
|
||||||
|
file();
|
||||||
|
|
||||||
|
if (me == 0) {
|
||||||
|
fclose(infile);
|
||||||
|
nfile--;
|
||||||
|
infile = infiles[nfile-1];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* ---------------------------------------------------------------------- */
|
/* ---------------------------------------------------------------------- */
|
||||||
|
|||||||
Reference in New Issue
Block a user