diff --git a/src/ADIOS/dump_atom_adios.cpp b/src/ADIOS/dump_atom_adios.cpp index a56f31b6c5..3e637b02d7 100644 --- a/src/ADIOS/dump_atom_adios.cpp +++ b/src/ADIOS/dump_atom_adios.cpp @@ -81,44 +81,18 @@ DumpAtomADIOS::~DumpAtomADIOS() void DumpAtomADIOS::openfile() { - if (multifile) { - // if one file per timestep, replace '*' with current timestep - char *filestar = strdup(filename); - char *filecurrent = new char[strlen(filestar) + 16]; - char *ptr = strchr(filestar, '*'); - *ptr = '\0'; - if (padflag == 0) - snprintf(filecurrent, sizeof(filecurrent), "%s" BIGINT_FORMAT "%s", - filestar, update->ntimestep, ptr + 1); - else { - char bif[8], pad[16]; - strcpy(bif, BIGINT_FORMAT); - snprintf(pad, sizeof(pad), "%%s%%0%d%s%%s", padflag, &bif[1]); - snprintf(filecurrent, sizeof(filecurrent), pad, filestar, - update->ntimestep, ptr + 1); - } - internal->fh = - internal->io.Open(filecurrent, adios2::Mode::Write, world); - if (!internal->fh) { - char str[128]; - snprintf(str, sizeof(str), "Cannot open dump file %s", filecurrent); - error->one(FLERR, str); - } - free(filestar); - delete[] filecurrent; - } else { - if (!singlefile_opened) { - internal->fh = - internal->io.Open(filename, adios2::Mode::Write, world); - if (!internal->fh) { - char str[128]; - snprintf(str, sizeof(str), "Cannot open dump file %s", - filename); - error->one(FLERR, str); - } - singlefile_opened = 1; - } + if (multifile) { + // if one file per timestep, replace '*' with current timestep + auto filecurrent = utils::star_subst(filename, update->ntimestep, padflag); + internal->fh = internal->io.Open(filecurrent, adios2::Mode::Write, world); + if (!internal->fh) error->one(FLERR, "Cannot open dump file {}", filecurrent); + } else { + if (!singlefile_opened) { + internal->fh = internal->io.Open(filename, adios2::Mode::Write, world); + if (!internal->fh) error->one(FLERR, "Cannot open dump file {}", filename); + singlefile_opened = 1; } + } } /* ---------------------------------------------------------------------- */ diff --git a/src/ADIOS/dump_custom_adios.cpp b/src/ADIOS/dump_custom_adios.cpp index a947f02d35..263f15349c 100644 --- a/src/ADIOS/dump_custom_adios.cpp +++ b/src/ADIOS/dump_custom_adios.cpp @@ -93,42 +93,18 @@ DumpCustomADIOS::~DumpCustomADIOS() void DumpCustomADIOS::openfile() { - if (multifile) { - // if one file per timestep, replace '*' with current timestep - char *filestar = strdup(filename); - char *filecurrent = new char[strlen(filestar) + 16]; - char *ptr = strchr(filestar, '*'); - *ptr = '\0'; - if (padflag == 0) - sprintf(filecurrent, "%s" BIGINT_FORMAT "%s", filestar, - update->ntimestep, ptr + 1); - else { - char bif[8], pad[16]; - strcpy(bif, BIGINT_FORMAT); - sprintf(pad, "%%s%%0%d%s%%s", padflag, &bif[1]); - sprintf(filecurrent, pad, filestar, update->ntimestep, ptr + 1); - } - internal->fh = - internal->io.Open(filecurrent, adios2::Mode::Write, world); - if (!internal->fh) { - char str[128]; - sprintf(str, "Cannot open dump file %s", filecurrent); - error->one(FLERR, str); - } - free(filestar); - delete[] filecurrent; - } else { - if (!singlefile_opened) { - internal->fh = - internal->io.Open(filename, adios2::Mode::Write, world); - if (!internal->fh) { - char str[128]; - sprintf(str, "Cannot open dump file %s", filename); - error->one(FLERR, str); - } - singlefile_opened = 1; - } + if (multifile) { + // if one file per timestep, replace '*' with current timestep + auto filecurrent = utils::star_subst(filename, update->ntimestep, padflag); + internal->fh = internal->io.Open(filecurrent, adios2::Mode::Write, world); + if (!internal->fh) error->one(FLERR, "Cannot open dump file {}", filecurrent); + } else { + if (!singlefile_opened) { + internal->fh = internal->io.Open(filename, adios2::Mode::Write, world); + if (!internal->fh) error->one(FLERR, "Cannot open dump file {}", filename); + singlefile_opened = 1; } + } } /* ---------------------------------------------------------------------- */ diff --git a/src/COMPRESS/dump_atom_gz.cpp b/src/COMPRESS/dump_atom_gz.cpp index 8bdc025436..3e15d6240d 100644 --- a/src/COMPRESS/dump_atom_gz.cpp +++ b/src/COMPRESS/dump_atom_gz.cpp @@ -46,19 +46,7 @@ void DumpAtomGZ::openfile() if (multiproc) filecurrent = multiname; if (multifile) { - char *filestar = filecurrent; - filecurrent = new char[strlen(filestar) + 16]; - char *ptr = strchr(filestar, '*'); - *ptr = '\0'; - if (padflag == 0) - sprintf(filecurrent, "%s" BIGINT_FORMAT "%s", filestar, update->ntimestep, ptr + 1); - else { - char bif[8], pad[16]; - strcpy(bif, BIGINT_FORMAT); - sprintf(pad, "%%s%%0%d%s%%s", padflag, &bif[1]); - sprintf(filecurrent, pad, filestar, update->ntimestep, ptr + 1); - } - *ptr = '*'; + filecurrent = utils::strdup(utils::star_subst(filecurrent, update->ntimestep, padflag)); if (maxfiles > 0) { if (numfiles < maxfiles) { nameslist[numfiles] = utils::strdup(filecurrent); diff --git a/src/COMPRESS/dump_atom_zstd.cpp b/src/COMPRESS/dump_atom_zstd.cpp index aa1c161d73..453efb6972 100644 --- a/src/COMPRESS/dump_atom_zstd.cpp +++ b/src/COMPRESS/dump_atom_zstd.cpp @@ -52,19 +52,7 @@ void DumpAtomZstd::openfile() if (multiproc) filecurrent = multiname; if (multifile) { - char *filestar = filecurrent; - filecurrent = new char[strlen(filestar) + 16]; - char *ptr = strchr(filestar, '*'); - *ptr = '\0'; - if (padflag == 0) - sprintf(filecurrent, "%s" BIGINT_FORMAT "%s", filestar, update->ntimestep, ptr + 1); - else { - char bif[8], pad[16]; - strcpy(bif, BIGINT_FORMAT); - sprintf(pad, "%%s%%0%d%s%%s", padflag, &bif[1]); - sprintf(filecurrent, pad, filestar, update->ntimestep, ptr + 1); - } - *ptr = '*'; + filecurrent = utils::strdup(utils::star_subst(filecurrent, update->ntimestep, padflag)); if (maxfiles > 0) { if (numfiles < maxfiles) { nameslist[numfiles] = utils::strdup(filecurrent); diff --git a/src/COMPRESS/dump_cfg_gz.cpp b/src/COMPRESS/dump_cfg_gz.cpp index 8d85e8cf83..dcf85e1fc5 100644 --- a/src/COMPRESS/dump_cfg_gz.cpp +++ b/src/COMPRESS/dump_cfg_gz.cpp @@ -48,19 +48,7 @@ void DumpCFGGZ::openfile() if (multiproc) filecurrent = multiname; if (multifile) { - char *filestar = filecurrent; - filecurrent = new char[strlen(filestar) + 16]; - char *ptr = strchr(filestar, '*'); - *ptr = '\0'; - if (padflag == 0) - sprintf(filecurrent, "%s" BIGINT_FORMAT "%s", filestar, update->ntimestep, ptr + 1); - else { - char bif[8], pad[16]; - strcpy(bif, BIGINT_FORMAT); - sprintf(pad, "%%s%%0%d%s%%s", padflag, &bif[1]); - sprintf(filecurrent, pad, filestar, update->ntimestep, ptr + 1); - } - *ptr = '*'; + filecurrent = utils::strdup(utils::star_subst(filecurrent, update->ntimestep, padflag)); if (maxfiles > 0) { if (numfiles < maxfiles) { nameslist[numfiles] = utils::strdup(filecurrent); diff --git a/src/COMPRESS/dump_cfg_zstd.cpp b/src/COMPRESS/dump_cfg_zstd.cpp index 6d70749479..8a3dc32e8d 100644 --- a/src/COMPRESS/dump_cfg_zstd.cpp +++ b/src/COMPRESS/dump_cfg_zstd.cpp @@ -54,19 +54,7 @@ void DumpCFGZstd::openfile() if (multiproc) filecurrent = multiname; if (multifile) { - char *filestar = filecurrent; - filecurrent = new char[strlen(filestar) + 16]; - char *ptr = strchr(filestar, '*'); - *ptr = '\0'; - if (padflag == 0) - sprintf(filecurrent, "%s" BIGINT_FORMAT "%s", filestar, update->ntimestep, ptr + 1); - else { - char bif[8], pad[16]; - strcpy(bif, BIGINT_FORMAT); - sprintf(pad, "%%s%%0%d%s%%s", padflag, &bif[1]); - sprintf(filecurrent, pad, filestar, update->ntimestep, ptr + 1); - } - *ptr = '*'; + filecurrent = utils::strdup(utils::star_subst(filecurrent, update->ntimestep, padflag)); if (maxfiles > 0) { if (numfiles < maxfiles) { nameslist[numfiles] = utils::strdup(filecurrent); diff --git a/src/COMPRESS/dump_custom_gz.cpp b/src/COMPRESS/dump_custom_gz.cpp index a8ef1ffe7c..42b15c4146 100644 --- a/src/COMPRESS/dump_custom_gz.cpp +++ b/src/COMPRESS/dump_custom_gz.cpp @@ -46,19 +46,7 @@ void DumpCustomGZ::openfile() if (multiproc) filecurrent = multiname; if (multifile) { - char *filestar = filecurrent; - filecurrent = new char[strlen(filestar) + 16]; - char *ptr = strchr(filestar, '*'); - *ptr = '\0'; - if (padflag == 0) - sprintf(filecurrent, "%s" BIGINT_FORMAT "%s", filestar, update->ntimestep, ptr + 1); - else { - char bif[8], pad[16]; - strcpy(bif, BIGINT_FORMAT); - sprintf(pad, "%%s%%0%d%s%%s", padflag, &bif[1]); - sprintf(filecurrent, pad, filestar, update->ntimestep, ptr + 1); - } - *ptr = '*'; + filecurrent = utils::strdup(utils::star_subst(filecurrent, update->ntimestep, padflag)); if (maxfiles > 0) { if (numfiles < maxfiles) { nameslist[numfiles] = utils::strdup(filecurrent); diff --git a/src/COMPRESS/dump_custom_zstd.cpp b/src/COMPRESS/dump_custom_zstd.cpp index c6c7e3592e..e3effe1744 100644 --- a/src/COMPRESS/dump_custom_zstd.cpp +++ b/src/COMPRESS/dump_custom_zstd.cpp @@ -56,20 +56,7 @@ void DumpCustomZstd::openfile() if (multiproc) filecurrent = multiname; if (multifile) { - char *filestar = filecurrent; - filecurrent = new char[strlen(filestar) + 16]; - char *ptr = strchr(filestar,'*'); - *ptr = '\0'; - if (padflag == 0) - sprintf(filecurrent,"%s" BIGINT_FORMAT "%s", - filestar,update->ntimestep,ptr+1); - else { - char bif[8],pad[16]; - strcpy(bif,BIGINT_FORMAT); - sprintf(pad,"%%s%%0%d%s%%s",padflag,&bif[1]); - sprintf(filecurrent,pad,filestar,update->ntimestep,ptr+1); - } - *ptr = '*'; + filecurrent = utils::strdup(utils::star_subst(filecurrent, update->ntimestep, padflag)); if (maxfiles > 0) { if (numfiles < maxfiles) { nameslist[numfiles] = utils::strdup(filecurrent); diff --git a/src/COMPRESS/dump_local_gz.cpp b/src/COMPRESS/dump_local_gz.cpp index c3669e6157..9c04f0cd61 100644 --- a/src/COMPRESS/dump_local_gz.cpp +++ b/src/COMPRESS/dump_local_gz.cpp @@ -46,19 +46,7 @@ void DumpLocalGZ::openfile() if (multiproc) filecurrent = multiname; if (multifile) { - char *filestar = filecurrent; - filecurrent = new char[strlen(filestar) + 16]; - char *ptr = strchr(filestar, '*'); - *ptr = '\0'; - if (padflag == 0) - sprintf(filecurrent, "%s" BIGINT_FORMAT "%s", filestar, update->ntimestep, ptr + 1); - else { - char bif[8], pad[16]; - strcpy(bif, BIGINT_FORMAT); - sprintf(pad, "%%s%%0%d%s%%s", padflag, &bif[1]); - sprintf(filecurrent, pad, filestar, update->ntimestep, ptr + 1); - } - *ptr = '*'; + filecurrent = utils::strdup(utils::star_subst(filecurrent, update->ntimestep, padflag)); if (maxfiles > 0) { if (numfiles < maxfiles) { nameslist[numfiles] = utils::strdup(filecurrent); diff --git a/src/COMPRESS/dump_local_zstd.cpp b/src/COMPRESS/dump_local_zstd.cpp index 63af89afcc..4e65702b0e 100644 --- a/src/COMPRESS/dump_local_zstd.cpp +++ b/src/COMPRESS/dump_local_zstd.cpp @@ -52,19 +52,7 @@ void DumpLocalZstd::openfile() if (multiproc) filecurrent = multiname; if (multifile) { - char *filestar = filecurrent; - filecurrent = new char[strlen(filestar) + 16]; - char *ptr = strchr(filestar, '*'); - *ptr = '\0'; - if (padflag == 0) - sprintf(filecurrent, "%s" BIGINT_FORMAT "%s", filestar, update->ntimestep, ptr + 1); - else { - char bif[8], pad[16]; - strcpy(bif, BIGINT_FORMAT); - sprintf(pad, "%%s%%0%d%s%%s", padflag, &bif[1]); - sprintf(filecurrent, pad, filestar, update->ntimestep, ptr + 1); - } - *ptr = '*'; + filecurrent = utils::strdup(utils::star_subst(filecurrent, update->ntimestep, padflag)); if (maxfiles > 0) { if (numfiles < maxfiles) { nameslist[numfiles] = utils::strdup(filecurrent); diff --git a/src/COMPRESS/dump_xyz_gz.cpp b/src/COMPRESS/dump_xyz_gz.cpp index 06561c6d9a..9bfead1c5c 100644 --- a/src/COMPRESS/dump_xyz_gz.cpp +++ b/src/COMPRESS/dump_xyz_gz.cpp @@ -45,19 +45,7 @@ void DumpXYZGZ::openfile() if (multiproc) filecurrent = multiname; if (multifile) { - char *filestar = filecurrent; - filecurrent = new char[strlen(filestar) + 16]; - char *ptr = strchr(filestar, '*'); - *ptr = '\0'; - if (padflag == 0) - sprintf(filecurrent, "%s" BIGINT_FORMAT "%s", filestar, update->ntimestep, ptr + 1); - else { - char bif[8], pad[16]; - strcpy(bif, BIGINT_FORMAT); - sprintf(pad, "%%s%%0%d%s%%s", padflag, &bif[1]); - sprintf(filecurrent, pad, filestar, update->ntimestep, ptr + 1); - } - *ptr = '*'; + filecurrent = utils::strdup(utils::star_subst(filecurrent, update->ntimestep, padflag)); if (maxfiles > 0) { if (numfiles < maxfiles) { nameslist[numfiles] = utils::strdup(filecurrent); diff --git a/src/COMPRESS/dump_xyz_zstd.cpp b/src/COMPRESS/dump_xyz_zstd.cpp index c4748b4ac3..bb2640b1b8 100644 --- a/src/COMPRESS/dump_xyz_zstd.cpp +++ b/src/COMPRESS/dump_xyz_zstd.cpp @@ -51,19 +51,7 @@ void DumpXYZZstd::openfile() if (multiproc) filecurrent = multiname; if (multifile) { - char *filestar = filecurrent; - filecurrent = new char[strlen(filestar) + 16]; - char *ptr = strchr(filestar, '*'); - *ptr = '\0'; - if (padflag == 0) - sprintf(filecurrent, "%s" BIGINT_FORMAT "%s", filestar, update->ntimestep, ptr + 1); - else { - char bif[8], pad[16]; - strcpy(bif, BIGINT_FORMAT); - sprintf(pad, "%%s%%0%d%s%%s", padflag, &bif[1]); - sprintf(filecurrent, pad, filestar, update->ntimestep, ptr + 1); - } - *ptr = '*'; + filecurrent = utils::strdup(utils::star_subst(filecurrent, update->ntimestep, padflag)); if (maxfiles > 0) { if (numfiles < maxfiles) { nameslist[numfiles] = utils::strdup(filecurrent); diff --git a/src/MOLFILE/dump_molfile.cpp b/src/MOLFILE/dump_molfile.cpp index 35e87f94af..bb45680395 100644 --- a/src/MOLFILE/dump_molfile.cpp +++ b/src/MOLFILE/dump_molfile.cpp @@ -274,29 +274,12 @@ void DumpMolfile::openfile() // if one file per timestep, replace '*' with current timestep - char *filecurrent = new char[strlen(filename) + 16]; - if (multifile == 0) { - strcpy(filecurrent,filename); - } else { - char *ptr = strchr(filename,'*'); - char *p1 = filename; - char *p2 = filecurrent; - while (p1 != ptr) - *p2++ = *p1++; + std::string filecurrent = filename; + if (multifile == 1) + filecurrent = utils::star_subst(filename, update->ntimestep, padflag); - if (padflag == 0) { - sprintf(p2,BIGINT_FORMAT "%s",update->ntimestep,ptr+1); - } else { - char bif[8],pad[16]; - strcpy(bif,BIGINT_FORMAT); - sprintf(pad,"%%0%d%s%%s",padflag,&bif[1]); - sprintf(p2,pad,update->ntimestep,ptr+1); - } - } - - if (mf->open(filecurrent,&natoms)) - error->one(FLERR,"Cannot open dump file"); - delete[] filecurrent; + if (mf->open(filecurrent.c_str(), &natoms)) + error->one(FLERR,"Cannot open dump file {}: {}", filecurrent, utils::getsyserror()); } } diff --git a/src/MPIIO/dump_atom_mpiio.cpp b/src/MPIIO/dump_atom_mpiio.cpp index 522950ab8b..9ba779924f 100644 --- a/src/MPIIO/dump_atom_mpiio.cpp +++ b/src/MPIIO/dump_atom_mpiio.cpp @@ -69,19 +69,7 @@ void DumpAtomMPIIO::openfile() filecurrent = filename; if (multifile) { - char *filestar = filecurrent; - filecurrent = new char[strlen(filestar) + 16]; - char *ptr = strchr(filestar, '*'); - *ptr = '\0'; - if (padflag == 0) { - sprintf(filecurrent, "%s" BIGINT_FORMAT "%s", filestar, update->ntimestep, ptr + 1); - } else { - char bif[8], pad[16]; - strcpy(bif, BIGINT_FORMAT); - sprintf(pad, "%%s%%0%d%s%%s", padflag, &bif[1]); - sprintf(filecurrent, pad, filestar, update->ntimestep, ptr + 1); - } - *ptr = '*'; + filecurrent = utils::strdup(utils::star_subst(filecurrent, update->ntimestep, padflag)); if (maxfiles > 0) { if (numfiles < maxfiles) { nameslist[numfiles] = new char[strlen(filecurrent) + 1]; @@ -98,8 +86,8 @@ void DumpAtomMPIIO::openfile() } if (append_flag) { // append open - int err = MPI_File_open(world, filecurrent, MPI_MODE_CREATE | MPI_MODE_APPEND | MPI_MODE_WRONLY, - MPI_INFO_NULL, &mpifh); + int err = MPI_File_open(world, filecurrent, MPI_MODE_CREATE | MPI_MODE_APPEND | + MPI_MODE_WRONLY, MPI_INFO_NULL, &mpifh); if (err != MPI_SUCCESS) error->one(FLERR, "Cannot open dump file {}: {}", filecurrent, utils::getsyserror()); @@ -112,8 +100,8 @@ void DumpAtomMPIIO::openfile() } else { // replace open - int err = - MPI_File_open(world, filecurrent, MPI_MODE_CREATE | MPI_MODE_WRONLY, MPI_INFO_NULL, &mpifh); + int err = MPI_File_open(world, filecurrent, MPI_MODE_CREATE | MPI_MODE_WRONLY, + MPI_INFO_NULL, &mpifh); if (err != MPI_SUCCESS) error->one(FLERR, "Cannot open dump file {}: {}", filecurrent, utils::getsyserror()); diff --git a/src/MPIIO/dump_cfg_mpiio.cpp b/src/MPIIO/dump_cfg_mpiio.cpp index 978709b787..88072d5adc 100644 --- a/src/MPIIO/dump_cfg_mpiio.cpp +++ b/src/MPIIO/dump_cfg_mpiio.cpp @@ -81,20 +81,7 @@ void DumpCFGMPIIO::openfile() filecurrent = filename; if (multifile) { - char *filestar = filecurrent; - filecurrent = new char[strlen(filestar) + 16]; - char *ptr = strchr(filestar,'*'); - *ptr = '\0'; - if (padflag == 0) - sprintf(filecurrent,"%s" BIGINT_FORMAT "%s", - filestar,update->ntimestep,ptr+1); - else { - char bif[8],pad[16]; - strcpy(bif,BIGINT_FORMAT); - sprintf(pad,"%%s%%0%d%s%%s",padflag,&bif[1]); - sprintf(filecurrent,pad,filestar,update->ntimestep,ptr+1); - } - *ptr = '*'; + filecurrent = utils::strdup(utils::star_subst(filecurrent, update->ntimestep, padflag)); if (maxfiles > 0) { if (numfiles < maxfiles) { nameslist[numfiles] = new char[strlen(filecurrent)+1]; @@ -111,12 +98,10 @@ void DumpCFGMPIIO::openfile() } if (append_flag) { // append open - int err = MPI_File_open( world, filecurrent, MPI_MODE_CREATE | MPI_MODE_APPEND | MPI_MODE_WRONLY , MPI_INFO_NULL, &mpifh); - if (err != MPI_SUCCESS) { - char str[128]; - sprintf(str,"Cannot open dump file %s",filecurrent); - error->one(FLERR,str); - } + int err = MPI_File_open( world, filecurrent, MPI_MODE_CREATE | MPI_MODE_APPEND | + MPI_MODE_WRONLY, MPI_INFO_NULL, &mpifh); + if (err != MPI_SUCCESS) error->one(FLERR, "Cannot open dump file {}", filecurrent); + int myrank; MPI_Comm_rank(world,&myrank); if (myrank == 0) @@ -125,15 +110,11 @@ void DumpCFGMPIIO::openfile() MPI_File_set_size(mpifh,mpifo+headerSize+sumFileSize); currentFileSize = mpifo+headerSize+sumFileSize; - } - else { // replace open + } else { // replace open - int err = MPI_File_open( world, filecurrent, MPI_MODE_CREATE | MPI_MODE_WRONLY , MPI_INFO_NULL, &mpifh); - if (err != MPI_SUCCESS) { - char str[128]; - sprintf(str,"Cannot open dump file %s",filecurrent); - error->one(FLERR,str); - } + int err = MPI_File_open( world, filecurrent, MPI_MODE_CREATE | MPI_MODE_WRONLY, + MPI_INFO_NULL, &mpifh); + if (err != MPI_SUCCESS) error->one(FLERR, "Cannot open dump file {}", filecurrent); mpifo = 0; MPI_File_set_size(mpifh,(MPI_Offset) (headerSize+sumFileSize)); diff --git a/src/MPIIO/dump_custom_mpiio.cpp b/src/MPIIO/dump_custom_mpiio.cpp index 766bd976bc..372b8705b9 100644 --- a/src/MPIIO/dump_custom_mpiio.cpp +++ b/src/MPIIO/dump_custom_mpiio.cpp @@ -36,7 +36,6 @@ using namespace LAMMPS_NS; -#define MAX_TEXT_HEADER_SIZE 4096 #define DUMP_BUF_CHUNK_SIZE 16384 #define DUMP_BUF_INCREMENT_SIZE 4096 @@ -71,7 +70,7 @@ DumpCustomMPIIO::~DumpCustomMPIIO() void DumpCustomMPIIO::openfile() { - if (singlefile_opened) { // single file already opened, so just return after resetting filesize + if (singlefile_opened) { // single file already opened, so just return after resetting filesize mpifo = currentFileSize; MPI_File_set_size(mpifh, mpifo + headerSize + sumFileSize); currentFileSize = mpifo + headerSize + sumFileSize; @@ -84,19 +83,7 @@ void DumpCustomMPIIO::openfile() filecurrent = filename; if (multifile) { - char *filestar = filecurrent; - filecurrent = new char[strlen(filestar) + 16]; - char *ptr = strchr(filestar, '*'); - *ptr = '\0'; - if (padflag == 0) { - sprintf(filecurrent, "%s" BIGINT_FORMAT "%s", filestar, update->ntimestep, ptr + 1); - } else { - char bif[8], pad[16]; - strcpy(bif, BIGINT_FORMAT); - sprintf(pad, "%%s%%0%d%s%%s", padflag, &bif[1]); - sprintf(filecurrent, pad, filestar, update->ntimestep, ptr + 1); - } - *ptr = '*'; + filecurrent = utils::strdup(utils::star_subst(filecurrent, update->ntimestep, padflag)); if (maxfiles > 0) { if (numfiles < maxfiles) { nameslist[numfiles] = new char[strlen(filecurrent) + 1]; diff --git a/src/MPIIO/dump_xyz_mpiio.cpp b/src/MPIIO/dump_xyz_mpiio.cpp index e4bfe4ef13..c976932b52 100644 --- a/src/MPIIO/dump_xyz_mpiio.cpp +++ b/src/MPIIO/dump_xyz_mpiio.cpp @@ -81,20 +81,7 @@ void DumpXYZMPIIO::openfile() filecurrent = filename; if (multifile) { - char *filestar = filecurrent; - filecurrent = new char[strlen(filestar) + 16]; - char *ptr = strchr(filestar,'*'); - *ptr = '\0'; - if (padflag == 0) - sprintf(filecurrent,"%s" BIGINT_FORMAT "%s", - filestar,update->ntimestep,ptr+1); - else { - char bif[8],pad[16]; - strcpy(bif,BIGINT_FORMAT); - sprintf(pad,"%%s%%0%d%s%%s",padflag,&bif[1]); - sprintf(filecurrent,pad,filestar,update->ntimestep,ptr+1); - } - *ptr = '*'; + filecurrent = utils::strdup(utils::star_subst(filecurrent, update->ntimestep, padflag)); if (maxfiles > 0) { if (numfiles < maxfiles) { nameslist[numfiles] = new char[strlen(filecurrent)+1]; @@ -111,12 +98,10 @@ void DumpXYZMPIIO::openfile() } if (append_flag) { // append open - int err = MPI_File_open( world, filecurrent, MPI_MODE_CREATE | MPI_MODE_APPEND | MPI_MODE_WRONLY , MPI_INFO_NULL, &mpifh); - if (err != MPI_SUCCESS) { - char str[128]; - sprintf(str,"Cannot open dump file %s",filecurrent); - error->one(FLERR,str); - } + int err = MPI_File_open( world, filecurrent, MPI_MODE_CREATE | MPI_MODE_APPEND | + MPI_MODE_WRONLY , MPI_INFO_NULL, &mpifh); + if (err != MPI_SUCCESS) error->one(FLERR, "Cannot open dump file {}",filecurrent); + int myrank; MPI_Comm_rank(world,&myrank); if (myrank == 0) @@ -124,20 +109,15 @@ void DumpXYZMPIIO::openfile() MPI_Bcast(&mpifo, 1, MPI_LMP_BIGINT, 0, world); MPI_File_set_size(mpifh,mpifo+headerSize+sumFileSize); currentFileSize = mpifo+headerSize+sumFileSize; - } - else { // replace open + } else { // replace open - int err = MPI_File_open( world, filecurrent, MPI_MODE_CREATE | MPI_MODE_WRONLY , MPI_INFO_NULL, &mpifh); - if (err != MPI_SUCCESS) { - char str[128]; - sprintf(str,"Cannot open dump file %s",filecurrent); - error->one(FLERR,str); - } + int err = MPI_File_open( world, filecurrent, MPI_MODE_CREATE | MPI_MODE_WRONLY, + MPI_INFO_NULL, &mpifh); + if (err != MPI_SUCCESS) error->one(FLERR, "Cannot open dump file {}",filecurrent); mpifo = 0; MPI_File_set_size(mpifh,(MPI_Offset) (headerSize+sumFileSize)); currentFileSize = (headerSize+sumFileSize); - } } @@ -221,8 +201,8 @@ void DumpXYZMPIIO::write() performEstimate = 0; write_data(nme,buf); - if (multifile) MPI_File_close(&mpifh); - if (multifile) delete [] filecurrent; + if (multifile) MPI_File_close(&mpifh); + if (multifile) delete[] filecurrent; } /* ---------------------------------------------------------------------- */ @@ -231,7 +211,7 @@ void DumpXYZMPIIO::init_style() { // format = copy of default or user-specified line format - delete [] format; + delete[] format; char *str; if (format_line_user) str = format_line_user; else str = format_default; @@ -246,10 +226,8 @@ void DumpXYZMPIIO::init_style() if (typenames == nullptr) { typenames = new char*[ntypes+1]; - for (int itype = 1; itype <= ntypes; itype++) { - typenames[itype] = new char[12]; - sprintf(typenames[itype],"%d",itype); - } + for (int itype = 1; itype <= ntypes; itype++) + typenames[itype] = utils::strdup(std::to_string(itype)); } // setup function ptr @@ -267,8 +245,7 @@ void DumpXYZMPIIO::write_header(bigint n) headerSize = 0; headerSize += sprintf(((char*)&((char*)headerBuffer)[headerSize]),BIGINT_FORMAT "\n",n); headerSize += sprintf(&((char*)headerBuffer)[headerSize],"Atoms. Timestep: " BIGINT_FORMAT "\n",update->ntimestep); - } - else { // write data + } else { // write data if (me == 0) MPI_File_write_at(mpifh,mpifo,headerBuffer,headerSize,MPI_CHAR,MPI_STATUS_IGNORE); diff --git a/src/NETCDF/dump_netcdf.cpp b/src/NETCDF/dump_netcdf.cpp index d68da35b0b..13dc869021 100644 --- a/src/NETCDF/dump_netcdf.cpp +++ b/src/NETCDF/dump_netcdf.cpp @@ -178,7 +178,7 @@ DumpNetCDF::DumpNetCDF(LAMMPS *lmp, int narg, char **arg) : for (int j = 0; j < DUMP_NC_MAX_DIMS; j++) { perat[inc].field[j] = -1; } - strncpy(perat[inc].name, mangled.c_str(), NC_FIELD_NAME_MAX); + strncpy(perat[inc].name, mangled.c_str(), NC_FIELD_NAME_MAX-1); n_perat++; } @@ -217,22 +217,9 @@ DumpNetCDF::~DumpNetCDF() void DumpNetCDF::openfile() { - char *filecurrent = filename; - if (multifile && !singlefile_opened) { - char *filestar = filecurrent; - filecurrent = new char[strlen(filestar) + 16]; - char *ptr = strchr(filestar,'*'); - *ptr = '\0'; - if (padflag == 0) - sprintf(filecurrent,"%s" BIGINT_FORMAT "%s", filestar,update->ntimestep,ptr+1); - else { - char bif[8],pad[16]; - strcpy(bif,BIGINT_FORMAT); - sprintf(pad,"%%s%%0%d%s%%s",padflag,&bif[1]); - sprintf(filecurrent,pad,filestar,update->ntimestep,ptr+1); - } - *ptr = '*'; - } + std::string filecurrent = filename; + if (multifile && !singlefile_opened) + filecurrent = utils::star_subst(filename, update->ntimestep, padflag); if (thermo && !singlefile_opened) { delete[] thermovar; @@ -291,7 +278,7 @@ void DumpNetCDF::openfile() if (singlefile_opened) return; singlefile_opened = 1; - NCERRX( nc_open(filecurrent, NC_WRITE, &ncid), filecurrent ); + NCERRX( nc_open(filecurrent.c_str(), NC_WRITE, &ncid), filecurrent.c_str() ); // dimensions NCERRX( nc_inq_dimid(ncid, NC_FRAME_STR, &frame_dim), NC_FRAME_STR ); @@ -356,7 +343,7 @@ void DumpNetCDF::openfile() if (singlefile_opened) return; singlefile_opened = 1; - NCERRX( nc_create(filecurrent, NC_64BIT_DATA, &ncid), filecurrent ); + NCERRX( nc_create(filecurrent.c_str(), NC_64BIT_DATA, &ncid), filecurrent.c_str() ); // dimensions NCERRX( nc_def_dim(ncid, NC_FRAME_STR, NC_UNLIMITED, &frame_dim), NC_FRAME_STR ); diff --git a/src/NETCDF/dump_netcdf_mpiio.cpp b/src/NETCDF/dump_netcdf_mpiio.cpp index ac281d26a3..dbf95953d7 100644 --- a/src/NETCDF/dump_netcdf_mpiio.cpp +++ b/src/NETCDF/dump_netcdf_mpiio.cpp @@ -177,7 +177,7 @@ DumpNetCDFMPIIO::DumpNetCDFMPIIO(LAMMPS *lmp, int narg, char **arg) : for (int j = 0; j < DUMP_NC_MPIIO_MAX_DIMS; j++) { perat[inc].field[j] = -1; } - strncpy(perat[inc].name, mangled.c_str(), NC_MPIIO_FIELD_NAME_MAX); + strncpy(perat[inc].name, mangled.c_str(), NC_MPIIO_FIELD_NAME_MAX-1); n_perat++; } @@ -214,22 +214,9 @@ DumpNetCDFMPIIO::~DumpNetCDFMPIIO() void DumpNetCDFMPIIO::openfile() { - char *filecurrent = filename; - if (multifile && !singlefile_opened) { - char *filestar = filecurrent; - filecurrent = new char[strlen(filestar) + 16]; - char *ptr = strchr(filestar,'*'); - *ptr = '\0'; - if (padflag == 0) - sprintf(filecurrent,"%s" BIGINT_FORMAT "%s", filestar,update->ntimestep,ptr+1); - else { - char bif[8],pad[16]; - strcpy(bif,BIGINT_FORMAT); - sprintf(pad,"%%s%%0%d%s%%s",padflag,&bif[1]); - sprintf(filecurrent,pad,filestar,update->ntimestep,ptr+1); - } - *ptr = '*'; - } + std::string filecurrent = filename; + if (multifile && !singlefile_opened) + filecurrent = utils::star_subst(filename, update->ntimestep, padflag); if (thermo && !singlefile_opened) { delete[] thermovar; @@ -287,7 +274,8 @@ void DumpNetCDFMPIIO::openfile() if (singlefile_opened) return; singlefile_opened = 1; - NCERRX( ncmpi_open(world, filecurrent, NC_WRITE, MPI_INFO_NULL, &ncid), filecurrent ); + NCERRX( ncmpi_open(world, filecurrent.c_str(), NC_WRITE, MPI_INFO_NULL, &ncid), + filecurrent.c_str() ); // dimensions NCERRX( ncmpi_inq_dimid(ncid, NC_FRAME_STR, &frame_dim), NC_FRAME_STR ); @@ -352,7 +340,8 @@ void DumpNetCDFMPIIO::openfile() if (singlefile_opened) return; singlefile_opened = 1; - NCERRX( ncmpi_create(world, filecurrent, NC_64BIT_DATA, MPI_INFO_NULL, &ncid), filecurrent ); + NCERRX( ncmpi_create(world, filecurrent.c_str(), NC_64BIT_DATA, MPI_INFO_NULL, &ncid), + filecurrent.c_str() ); // dimensions NCERRX( ncmpi_def_dim(ncid, NC_FRAME_STR, NC_UNLIMITED, &frame_dim), NC_FRAME_STR ); diff --git a/src/REAXFF/fix_reaxff_species.cpp b/src/REAXFF/fix_reaxff_species.cpp index 873ba9bbfd..45dec215df 100644 --- a/src/REAXFF/fix_reaxff_species.cpp +++ b/src/REAXFF/fix_reaxff_species.cpp @@ -644,29 +644,15 @@ void FixReaxFFSpecies::WriteFormulas(int Nmole, int Nspec) void FixReaxFFSpecies::OpenPos() { - char *filecurrent; - bigint ntimestep = update->ntimestep; - - filecurrent = (char*) malloc((strlen(filepos)+16)*sizeof(char)); - char *ptr = strchr(filepos,'*'); - *ptr = '\0'; - if (padflag == 0) - sprintf(filecurrent,"%s" BIGINT_FORMAT "%s",filepos,ntimestep,ptr+1); - else { - char bif[8],pad[16]; - strcpy(bif,BIGINT_FORMAT); - sprintf(pad,"%%s%%0%d%s%%s",padflag,&bif[1]); - sprintf(filecurrent,pad,filepos,ntimestep,ptr+1); - } - *ptr = '*'; - if (me == 0) { - pos = fopen(filecurrent, "w"); - if (pos == nullptr) error->one(FLERR,"Cannot open fix reaxff/species position file"); - } else pos = nullptr; + auto filecurrent = utils::star_subst(filepos, update->ntimestep, padflag); + pos = fopen(filecurrent.c_str(), "w"); + if (pos == nullptr) + error->one(FLERR, "Cannot open fix reaxff/species position file {}: {}", filecurrent, + utils::getsyserror()); + } else + pos = nullptr; multipos_opened = 1; - - free(filecurrent); } /* ---------------------------------------------------------------------- */ diff --git a/src/VTK/dump_vtk.cpp b/src/VTK/dump_vtk.cpp index d42699c271..e1b4d19713 100644 --- a/src/VTK/dump_vtk.cpp +++ b/src/VTK/dump_vtk.cpp @@ -1009,13 +1009,13 @@ void DumpVTK::write_data(int n, double *mybuf) /* ---------------------------------------------------------------------- */ void DumpVTK::setFileCurrent() { - delete [] filecurrent; + delete[] filecurrent; filecurrent = nullptr; char *filestar = filename; if (multiproc) { if (multiproc > 1) { // if dump_modify fileper or nfile was used - delete [] multiname_ex; + delete[] multiname_ex; multiname_ex = nullptr; char *ptr = strchr(filename,'%'); if (ptr) { @@ -1034,26 +1034,13 @@ void DumpVTK::setFileCurrent() { } if (multifile == 0) { - filecurrent = new char[strlen(filestar) + 1]; - strcpy(filecurrent, filestar); + filecurrent = utils::strdup(filestar); } else { - filecurrent = new char[strlen(filestar) + 16]; - char *ptr = strchr(filestar,'*'); - *ptr = '\0'; - if (padflag == 0) { - sprintf(filecurrent,"%s" BIGINT_FORMAT "%s", - filestar,update->ntimestep,ptr+1); - } else { - char bif[8],pad[16]; - strcpy(bif,BIGINT_FORMAT); - sprintf(pad,"%%s%%0%d%s%%s",padflag,&bif[1]); - sprintf(filecurrent,pad,filestar,update->ntimestep,ptr+1); - } - *ptr = '*'; + filecurrent = utils::strdup(utils::star_subst(filestar, update->ntimestep, padflag)); } // filename of domain box data file - delete [] domainfilecurrent; + delete[] domainfilecurrent; domainfilecurrent = nullptr; if (multiproc) { // remove '%' character @@ -1074,21 +1061,9 @@ void DumpVTK::setFileCurrent() { domainfilecurrent = new char[strlen(filestar) + 1]; strcpy(domainfilecurrent, filestar); } else { - domainfilecurrent = new char[strlen(filestar) + 16]; - char *ptr = strchr(filestar,'*'); - *ptr = '\0'; - if (padflag == 0) { - sprintf(domainfilecurrent,"%s" BIGINT_FORMAT "%s", - filestar,update->ntimestep,ptr+1); - } else { - char bif[8],pad[16]; - strcpy(bif,BIGINT_FORMAT); - sprintf(pad,"%%s%%0%d%s%%s",padflag,&bif[1]); - sprintf(domainfilecurrent,pad,filestar,update->ntimestep,ptr+1); - } - *ptr = '*'; + domainfilecurrent = utils::strdup(utils::star_subst(filestar, update->ntimestep, padflag)); } - delete [] filestar; + delete[] filestar; filestar = nullptr; } else { domainfilecurrent = new char[strlen(filecurrent) + 16]; @@ -1100,7 +1075,7 @@ void DumpVTK::setFileCurrent() { // filename of parallel file if (multiproc && me == 0) { - delete [] parallelfilecurrent; + delete[] parallelfilecurrent; parallelfilecurrent = nullptr; // remove '%' character and add 'p' to file extension @@ -1119,24 +1094,11 @@ void DumpVTK::setFileCurrent() { *ptr++= 0; if (multifile == 0) { - parallelfilecurrent = new char[strlen(filestar) + 1]; - strcpy(parallelfilecurrent, filestar); + parallelfilecurrent = utils::strdup(filestar); } else { - parallelfilecurrent = new char[strlen(filestar) + 16]; - char *ptr = strchr(filestar,'*'); - *ptr = '\0'; - if (padflag == 0) { - sprintf(parallelfilecurrent,"%s" BIGINT_FORMAT "%s", - filestar,update->ntimestep,ptr+1); - } else { - char bif[8],pad[16]; - strcpy(bif,BIGINT_FORMAT); - sprintf(pad,"%%s%%0%d%s%%s",padflag,&bif[1]); - sprintf(parallelfilecurrent,pad,filestar,update->ntimestep,ptr+1); - } - *ptr = '*'; + parallelfilecurrent = utils::strdup(utils::star_subst(filestar, update->ntimestep, padflag)); } - delete [] filestar; + delete[] filestar; filestar = nullptr; } } diff --git a/src/dump.cpp b/src/dump.cpp index f11f21f71e..2e6df77cd9 100644 --- a/src/dump.cpp +++ b/src/dump.cpp @@ -549,20 +549,7 @@ void Dump::openfile() if (multiproc) filecurrent = multiname; if (multifile) { - char *filestar = filecurrent; - filecurrent = new char[strlen(filestar) + 16]; - char *ptr = strchr(filestar,'*'); - *ptr = '\0'; - if (padflag == 0) - sprintf(filecurrent,"%s" BIGINT_FORMAT "%s", - filestar,update->ntimestep,ptr+1); - else { - char bif[8],pad[16]; - strcpy(bif,BIGINT_FORMAT); - sprintf(pad,"%%s%%0%d%s%%s",padflag,&bif[1]); - sprintf(filecurrent,pad,filestar,update->ntimestep,ptr+1); - } - *ptr = '*'; + filecurrent = utils::strdup(utils::star_subst(filecurrent, update->ntimestep, padflag)); if (maxfiles > 0) { if (numfiles < maxfiles) { nameslist[numfiles] = utils::strdup(filecurrent); @@ -594,7 +581,7 @@ void Dump::openfile() // delete string with timestep replaced - if (multifile) delete [] filecurrent; + if (multifile) delete[] filecurrent; } /* ----------------------------------------------------------------------