simplify and get rid of strtok()

This commit is contained in:
Axel Kohlmeyer
2021-03-15 11:28:25 -04:00
parent 23e4078b4e
commit dbf7b9c24a

View File

@ -150,39 +150,38 @@ DumpCustom::DumpCustom(LAMMPS *lmp, int narg, char **arg) :
ntypes = atom->ntypes; ntypes = atom->ntypes;
typenames = new char*[ntypes+1]; typenames = new char*[ntypes+1];
for (int itype = 1; itype <= ntypes; itype++) { for (int itype = 1; itype <= ntypes; itype++)
typenames[itype] = new char[2]; typenames[itype] = utils::strdup("C");
strcpy(typenames[itype],"C");
}
// setup format strings // setup format strings
vformat = new char*[nfield]; vformat = new char*[nfield];
std::string cols;
format_default = new char[4*nfield+1]; cols.clear();
format_default[0] = '\0';
for (int i = 0; i < nfield; i++) { for (int i = 0; i < nfield; i++) {
if (vtype[i] == Dump::INT) strcat(format_default,"%d "); if (vtype[i] == Dump::INT) cols += "%d ";
else if (vtype[i] == Dump::DOUBLE) strcat(format_default,"%g "); else if (vtype[i] == Dump::DOUBLE) cols += "%g ";
else if (vtype[i] == Dump::STRING) strcat(format_default,"%s "); else if (vtype[i] == Dump::STRING) cols += "%s ";
else if (vtype[i] == Dump::BIGINT) strcat(format_default,BIGINT_FORMAT " "); else if (vtype[i] == Dump::BIGINT) cols += BIGINT_FORMAT " ";
vformat[i] = nullptr; vformat[i] = nullptr;
} }
cols.resize(cols.size()-1);
format_default = utils::strdup(cols);
format_column_user = new char*[nfield]; format_column_user = new char*[nfield];
for (int i = 0; i < nfield; i++) format_column_user[i] = nullptr; for (int i = 0; i < nfield; i++) format_column_user[i] = nullptr;
// setup column string // setup column string
int n = 0; cols.clear();
for (int iarg = 0; iarg < nfield; iarg++) n += strlen(earg[iarg]) + 2;
columns = new char[n];
columns[0] = '\0';
for (int iarg = 0; iarg < nfield; iarg++) { for (int iarg = 0; iarg < nfield; iarg++) {
strcat(columns,earg[iarg]); cols += earg[iarg];
if (iarg+1 < nfield) strcat(columns," "); cols += " ";
} }
// remove trailing blank and copy
cols.resize(cols.size()-1);
columns = utils::strdup(cols);
} }
/* ---------------------------------------------------------------------- */ /* ---------------------------------------------------------------------- */
@ -263,44 +262,36 @@ void DumpCustom::init_style()
// format = copy of default or user-specified line format // format = copy of default or user-specified line format
delete [] format; delete [] format;
char *str; if (format_line_user) format = utils::strdup(format_line_user);
if (format_line_user) str = format_line_user; else format = utils::strdup(format_default);
else str = format_default;
int n = strlen(str) + 1;
format = new char[n];
strcpy(format,str);
// tokenize the format string and add space at end of each format element // tokenize the format string and add space at end of each format element
// if user-specified int/float format exists, use it instead // if user-specified int/float format exists, use it instead
// if user-specified column format exists, use it instead // if user-specified column format exists, use it instead
// lo priority = line, medium priority = int/float, hi priority = column // lo priority = line, medium priority = int/float, hi priority = column
char *ptr; auto words = utils::split_words(format);
for (int i = 0; i < nfield; i++) { if ((int) words.size() < nfield)
if (i == 0) ptr = strtok(format," \0"); error->all(FLERR,"Dump_modify format line is too short");
else ptr = strtok(nullptr," \0");
if (ptr == nullptr) error->all(FLERR,"Dump_modify format line is too short"); int i=0;
for (auto word : words) {
delete [] vformat[i]; delete [] vformat[i];
if (format_column_user[i]) { if (format_column_user[i])
vformat[i] = new char[strlen(format_column_user[i]) + 2]; vformat[i] = utils::strdup(std::string(format_column_user[i]) + " ");
strcpy(vformat[i],format_column_user[i]); else if (vtype[i] == Dump::INT && format_int_user)
} else if (vtype[i] == Dump::INT && format_int_user) { vformat[i] = utils::strdup(std::string(format_int_user) + " ");
vformat[i] = new char[strlen(format_int_user) + 2]; else if (vtype[i] == Dump::DOUBLE && format_float_user)
strcpy(vformat[i],format_int_user); vformat[i] = utils::strdup(std::string(format_float_user) + " ");
} else if (vtype[i] == Dump::DOUBLE && format_float_user) { else if (vtype[i] == Dump::BIGINT && format_bigint_user)
vformat[i] = new char[strlen(format_float_user) + 2]; vformat[i] = utils::strdup(std::string(format_int_user) + " ");
strcpy(vformat[i],format_float_user); else vformat[i] = utils::strdup(word + " ");
} else if (vtype[i] == Dump::BIGINT && format_bigint_user) {
vformat[i] = new char[strlen(format_bigint_user) + 2];
strcpy(vformat[i],format_bigint_user);
} else {
vformat[i] = new char[strlen(ptr) + 2];
strcpy(vformat[i],ptr);
}
if (i+1 < nfield) vformat[i] = strcat(vformat[i]," "); // remove trailing blank on last column's format
if (i == nfield-1) vformat[i][strlen(vformat[i])-1] = '\0';
++i;
} }
// setup boundary string // setup boundary string