Unverified Commit 62ee8d41 authored by Axel Kohlmeyer's avatar Axel Kohlmeyer
Browse files

get rid of local buffers and snprinf()

parent 4b58e33b
Loading
Loading
Loading
Loading
+26 −40
Original line number Diff line number Diff line
@@ -251,11 +251,9 @@ void Input::file(const char *filename)
      error->one(FLERR,"Too many nested levels of input scripts");

    infile = fopen(filename,"r");
    if (infile == NULL) {
      char str[128];
      snprintf(str,128,"Cannot open input script %s",filename);
      error->one(FLERR,str);
    }
    if (infile == NULL)
      error->one(FLERR,fmt::format("Cannot open input script {}",filename));

    infiles[nfile++] = infile;
  }

@@ -545,11 +543,10 @@ void Input::substitute(char *&str, char *&str2, int &max, int &max2, int flag)
        value = variable->retrieve(var);
      }

      if (value == NULL) {
        char str[128];
        snprintf(str,128,"Substitution for illegal variable %s",var);
        error->one(FLERR,str);
      }
      if (value == NULL)
        error->one(FLERR,fmt::format("Substitution for illegal "
                                     "variable {}",var));

      // check if storage in str2 needs to be expanded
      // re-initialize ptr and ptr2 to the point beyond the variable.

@@ -1038,11 +1035,9 @@ void Input::include()
      error->one(FLERR,"Too many nested levels of input scripts");

    infile = fopen(arg[0],"r");
    if (infile == NULL) {
      char str[128];
      snprintf(str,128,"Cannot open input script %s",arg[0]);
      error->one(FLERR,str);
    }
    if (infile == NULL)
      error->one(FLERR,fmt::format("Cannot open input script {}",arg[0]));

    infiles[nfile++] = infile;
  }

@@ -1073,11 +1068,9 @@ void Input::jump()
    else {
      if (infile && infile != stdin) fclose(infile);
      infile = fopen(arg[0],"r");
      if (infile == NULL) {
        char str[128];
        snprintf(str,128,"Cannot open input script %s",arg[0]);
        error->one(FLERR,str);
      }
      if (infile == NULL)
        error->one(FLERR,fmt::format("Cannot open input script {}",arg[0]));

      infiles[nfile-1] = infile;
    }
  }
@@ -1118,11 +1111,9 @@ void Input::log()
      if (appendflag) logfile = fopen(arg[0],"a");
      else logfile = fopen(arg[0],"w");

      if (logfile == NULL) {
        char str[128];
        snprintf(str,128,"Cannot open logfile %s",arg[0]);
        error->one(FLERR,str);
      }
      if (logfile == NULL)
        error->one(FLERR,fmt::format("Cannot open logfile {}",arg[0]));

    }
    if (universe->nworlds == 1) universe->ulogfile = logfile;
  }
@@ -1197,11 +1188,9 @@ void Input::print()
        if (fp != NULL) fclose(fp);
        if (strcmp(arg[iarg],"file") == 0) fp = fopen(arg[iarg+1],"w");
        else fp = fopen(arg[iarg+1],"a");
        if (fp == NULL) {
          char str[128];
          snprintf(str,128,"Cannot open print file %s",arg[iarg+1]);
          error->one(FLERR,str);
        }
        if (fp == NULL)
          error->one(FLERR,fmt::format("Cannot open print file {}",
                                       arg[iarg+1]));
      }
      iarg += 2;
    } else if (strcmp(arg[iarg],"screen") == 0) {
@@ -1810,18 +1799,15 @@ void Input::pair_style()
{
  if (narg < 1) error->all(FLERR,"Illegal pair_style command");
  if (force->pair) {
    std::string style = arg[0];
    int match = 0;
    if (strcmp(arg[0],force->pair_style) == 0) match = 1;
    if (style == force->pair_style) match = 1;
    if (!match && lmp->suffix_enable) {
      char estyle[256];
      if (lmp->suffix) {
        snprintf(estyle,256,"%s/%s",arg[0],lmp->suffix);
        if (strcmp(estyle,force->pair_style) == 0) match = 1;
      }
      if (lmp->suffix2) {
        snprintf(estyle,256,"%s/%s",arg[0],lmp->suffix2);
        if (strcmp(estyle,force->pair_style) == 0) match = 1;
      }
      if (lmp->suffix)
        if (style + "/" + lmp->suffix == force->pair_style) match = 1;

      if (lmp->suffix2)
        if (style + "/" + lmp->suffix2 == force->pair_style) match = 1;
    }
    if (match) {
      force->pair->settings(narg-1,&arg[1]);
+3 −5
Original line number Diff line number Diff line
@@ -436,11 +436,9 @@ LAMMPS::LAMMPS(int narg, char **arg, MPI_Comm communicator) :
    if (universe->me == 0) {
      if (inflag == 0) infile = stdin;
      else infile = fopen(arg[inflag],"r");
      if (infile == NULL) {
        char str[128];
        snprintf(str,128,"Cannot open input script %s",arg[inflag]);
        error->one(FLERR,str);
      }
      if (infile == NULL)
        error->one(FLERR,fmt::format("Cannot open input script {}",
                                     arg[inflag]));
    }

    if ((universe->me == 0) && !helpflag)
+10 −18
Original line number Diff line number Diff line
@@ -41,6 +41,7 @@
#include "neighbor.h"
#include "neigh_list.h"
#include "neigh_request.h"
#include "fmt/format.h"

#if defined(LAMMPS_EXCEPTIONS)
#include "exceptions.h"
@@ -1582,14 +1583,11 @@ void lammps_create_atoms(void *ptr, int n, tagint *id, int *type,

    // warn if new natoms is not correct

    if (lmp->atom->natoms != natoms_prev + n) {
      char str[128];
      snprintf(str, 128, "Library warning in lammps_create_atoms, "
              "invalid total atoms " BIGINT_FORMAT " " BIGINT_FORMAT,
              lmp->atom->natoms,natoms_prev+n);
      if (lmp->comm->me == 0)
        lmp->error->warning(FLERR,str);
    }
    if ((lmp->atom->natoms != natoms_prev + n) && (lmp->comm->me == 0))
      lmp->error->warning(FLERR,fmt::format("Library warning in "
                                            "lammps_create_atoms: "
                                            "invalid total atoms {} vs. {}",
                                            lmp->atom->natoms,natoms_prev+n));
  }
  END_CAPTURE
}
@@ -1607,19 +1605,13 @@ void lammps_set_fix_external_callback(void *ptr, char *id, FixExternalFnPtr call
  BEGIN_CAPTURE
  {
    int ifix = lmp->modify->find_fix(id);
    if (ifix < 0) {
      char str[128];
      snprintf(str, 128, "Can not find fix with ID '%s'!", id);
      lmp->error->all(FLERR,str);
    }
    if (ifix < 0)
      lmp->error->all(FLERR,fmt::format("Can not find fix with ID '{}'!", id));

    Fix *fix = lmp->modify->fix[ifix];

    if (strcmp("external",fix->style) != 0){
      char str[128];
      snprintf(str, 128, "Fix '%s' is not of style external!", id);
      lmp->error->all(FLERR,str);
    }
    if (strcmp("external",fix->style) != 0)
      lmp->error->all(FLERR,fmt::format("Fix '{}' is not of style external!", id));

    FixExternal * fext = (FixExternal*) fix;
    fext->set_callback(callback, caller);
+9 −16
Original line number Diff line number Diff line
@@ -14,6 +14,7 @@
#include "memory.h"
#include <cstdlib>
#include "error.h"
#include "fmt/format.h"

#if defined(LMP_USER_INTEL) && defined(__INTEL_COMPILER)
#ifndef LMP_INTEL_NO_TBB
@@ -56,12 +57,9 @@ void *Memory::smalloc(bigint nbytes, const char *name)
#else
  void *ptr = malloc(nbytes);
#endif
  if (ptr == NULL) {
    char str[128];
    sprintf(str,"Failed to allocate " BIGINT_FORMAT " bytes for array %s",
            nbytes,name);
    error->one(FLERR,str);
  }
  if (ptr == NULL)
    error->one(FLERR,fmt::format("Failed to allocate {} bytes for array {}",
                                 nbytes,name));
  return ptr;
}

@@ -92,12 +90,9 @@ void *Memory::srealloc(void *ptr, bigint nbytes, const char *name)
#else
  ptr = realloc(ptr,nbytes);
#endif
  if (ptr == NULL) {
    char str[128];
    sprintf(str,"Failed to reallocate " BIGINT_FORMAT " bytes for array %s",
            nbytes,name);
    error->one(FLERR,str);
  }
  if (ptr == NULL)
    error->one(FLERR,fmt::format("Failed to reallocate {} bytes for array {}",
                                 nbytes,name));
  return ptr;
}

@@ -121,8 +116,6 @@ void Memory::sfree(void *ptr)

void Memory::fail(const char *name)
{
  char str[128];
  snprintf(str,128,
           "Cannot create/grow a vector/array of pointers for %s",name);
  error->one(FLERR,str);
  error->one(FLERR,fmt::format("Cannot create/grow a vector/array of "
                               "pointers for {}",name));
}
+7 −12
Original line number Diff line number Diff line
@@ -28,6 +28,7 @@
#include "memory.h"
#include "error.h"
#include "utils.h"
#include "fmt/format.h"

using namespace LAMMPS_NS;
using namespace FixConst;
@@ -245,21 +246,15 @@ void Modify::init()
  // error if any fix or compute is using a dynamic group when not allowed

  for (i = 0; i < nfix; i++)
    if (!fix[i]->dynamic_group_allow && group->dynamic[fix[i]->igroup]) {
      char str[128];
      snprintf(str,128,
               "Fix %s does not allow use of dynamic group",fix[i]->id);
      error->all(FLERR,str);
    }
    if (!fix[i]->dynamic_group_allow && group->dynamic[fix[i]->igroup])
      error->all(FLERR,fmt::format("Fix {} does not allow use with a "
                                   "dynamic group",fix[i]->id));

  for (i = 0; i < ncompute; i++)
    if (!compute[i]->dynamic_group_allow &&
        group->dynamic[compute[i]->igroup]) {
      char str[128];
      snprintf(str,128,"Compute %s does not allow use of dynamic group",
               fix[i]->id);
      error->all(FLERR,str);
    }
        group->dynamic[compute[i]->igroup])
      error->all(FLERR,fmt::format("Compute {} does not allow use with a "
                                   "dynamic group",compute[i]->id));

  // warn if any particle is time integrated more than once

Loading