From 97e69abcda9ddc5aae87e50f9ec6c22117b5c358 Mon Sep 17 00:00:00 2001 From: Axel Kohlmeyer Date: Thu, 4 Jun 2020 10:45:24 -0400 Subject: [PATCH] get rid of snprintf() and local buffers in atom style creation --- src/KOKKOS/atom_kokkos.cpp | 3 ++- src/KOKKOS/atom_kokkos.h | 2 +- src/atom.cpp | 27 ++++++++++++--------------- src/atom.h | 4 ++-- 4 files changed, 17 insertions(+), 19 deletions(-) diff --git a/src/KOKKOS/atom_kokkos.cpp b/src/KOKKOS/atom_kokkos.cpp index 49fdbb95bb..e826a7c392 100644 --- a/src/KOKKOS/atom_kokkos.cpp +++ b/src/KOKKOS/atom_kokkos.cpp @@ -340,7 +340,8 @@ void AtomKokkos::sync_modify(ExecutionSpace execution_space, modified(execution_space,datamask_modify); } -AtomVec *AtomKokkos::new_avec(const char *style, int trysuffix, int &sflag) +AtomVec *AtomKokkos::new_avec(const std::string &style, + int trysuffix, int &sflag) { AtomVec* avec = Atom::new_avec(style,trysuffix,sflag); if (!avec->kokkosable) diff --git a/src/KOKKOS/atom_kokkos.h b/src/KOKKOS/atom_kokkos.h index 0ae032032a..82ea55beb1 100644 --- a/src/KOKKOS/atom_kokkos.h +++ b/src/KOKKOS/atom_kokkos.h @@ -74,7 +74,7 @@ class AtomKokkos : public Atom { virtual void deallocate_topology(); void sync_modify(ExecutionSpace, unsigned int, unsigned int); private: - class AtomVec *new_avec(const char *, int, int &); + class AtomVec *new_avec(const std::string &, int, int &); }; template diff --git a/src/atom.cpp b/src/atom.cpp index 134def74a3..98c1e391c4 100644 --- a/src/atom.cpp +++ b/src/atom.cpp @@ -16,6 +16,7 @@ #include #include #include +#include #include "style_atom.h" #include "atom_vec.h" #include "atom_vec_ellipsoid.h" @@ -681,7 +682,7 @@ void Atom::set_atomflag_defaults() called from lammps.cpp, input script, restart file, replicate ------------------------------------------------------------------------- */ -void Atom::create_avec(const char *style, int narg, char **arg, int trysuffix) +void Atom::create_avec(const std::string &style, int narg, char **arg, int trysuffix) { delete [] atom_style; if (avec) delete avec; @@ -704,16 +705,14 @@ void Atom::create_avec(const char *style, int narg, char **arg, int trysuffix) avec->grow(1); if (sflag) { - char estyle[256]; - if (sflag == 1) snprintf(estyle,256,"%s/%s",style,lmp->suffix); - else snprintf(estyle,256,"%s/%s",style,lmp->suffix2); - int n = strlen(estyle) + 1; - atom_style = new char[n]; - strcpy(atom_style,estyle); + std::string estyle = style + "/"; + if (sflag == 1) estyle += lmp->suffix; + else estyle += lmp->suffix2; + atom_style = new char[estyle.size()+1]; + strcpy(atom_style,estyle.c_str()); } else { - int n = strlen(style) + 1; - atom_style = new char[n]; - strcpy(atom_style,style); + atom_style = new char[style.size()+1]; + strcpy(atom_style,style.c_str()); } // if molecular system: @@ -731,13 +730,12 @@ void Atom::create_avec(const char *style, int narg, char **arg, int trysuffix) generate an AtomVec class, first with suffix appended ------------------------------------------------------------------------- */ -AtomVec *Atom::new_avec(const char *style, int trysuffix, int &sflag) +AtomVec *Atom::new_avec(const std::string &style, int trysuffix, int &sflag) { if (trysuffix && lmp->suffix_enable) { if (lmp->suffix) { sflag = 1; - char estyle[256]; - snprintf(estyle,256,"%s/%s",style,lmp->suffix); + std::string estyle = style + "/" + lmp->suffix; if (avec_map->find(estyle) != avec_map->end()) { AtomVecCreator avec_creator = (*avec_map)[estyle]; return avec_creator(lmp); @@ -746,8 +744,7 @@ AtomVec *Atom::new_avec(const char *style, int trysuffix, int &sflag) if (lmp->suffix2) { sflag = 2; - char estyle[256]; - snprintf(estyle,256,"%s/%s",style,lmp->suffix2); + std::string estyle = style + "/" + lmp->suffix2; if (avec_map->find(estyle) != avec_map->end()) { AtomVecCreator avec_creator = (*avec_map)[estyle]; return avec_creator(lmp); diff --git a/src/atom.h b/src/atom.h index b94189e6e1..fca682aff6 100644 --- a/src/atom.h +++ b/src/atom.h @@ -262,8 +262,8 @@ class Atom : protected Pointers { void add_peratom_change_columns(const char *, int); void add_peratom_vary(const char *, void *, int, int *, void *, int collength=0); - void create_avec(const char *, int, char **, int); - virtual class AtomVec *new_avec(const char *, int, int &); + void create_avec(const std::string &, int, char **, int); + virtual class AtomVec *new_avec(const std::string &, int, int &); void init(); void setup();