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