replace #define with constrexpr for better compiler error reports on integer constants

This commit is contained in:
Axel Kohlmeyer
2021-08-17 09:42:08 -04:00
parent f24054f7b3
commit 0dbcb5dd66
2 changed files with 9 additions and 12 deletions

View File

@ -209,11 +209,11 @@ char *utils::fgets_trunc(char *buf, int size, FILE *fp)
return buf; return buf;
} }
#define MAXPATHLENBUF 1024
/* like fgets() but aborts with an error or EOF is encountered */ /* like fgets() but aborts with an error or EOF is encountered */
void utils::sfgets(const char *srcname, int srcline, char *s, int size, FILE *fp, void utils::sfgets(const char *srcname, int srcline, char *s, int size, FILE *fp,
const char *filename, Error *error) const char *filename, Error *error)
{ {
constexpr int MAXPATHLENBUF=1024;
char *rv = fgets(s, size, fp); char *rv = fgets(s, size, fp);
if (rv == nullptr) { // something went wrong if (rv == nullptr) { // something went wrong
char buf[MAXPATHLENBUF]; char buf[MAXPATHLENBUF];
@ -242,6 +242,7 @@ void utils::sfgets(const char *srcname, int srcline, char *s, int size, FILE *fp
void utils::sfread(const char *srcname, int srcline, void *s, size_t size, size_t num, FILE *fp, void utils::sfread(const char *srcname, int srcline, void *s, size_t size, size_t num, FILE *fp,
const char *filename, Error *error) const char *filename, Error *error)
{ {
constexpr int MAXPATHLENBUF=1024;
size_t rv = fread(s, size, num, fp); size_t rv = fread(s, size, num, fp);
if (rv != num) { // something went wrong if (rv != num) { // something went wrong
char buf[MAXPATHLENBUF]; char buf[MAXPATHLENBUF];
@ -1029,11 +1030,6 @@ bool utils::file_is_readable(const std::string &path)
search current directory and the LAMMPS_POTENTIALS directory if search current directory and the LAMMPS_POTENTIALS directory if
specified specified
------------------------------------------------------------------------- */ ------------------------------------------------------------------------- */
#if defined(_WIN32)
#define OS_PATH_VAR_SEP ";"
#else
#define OS_PATH_VAR_SEP ":"
#endif
std::string utils::get_potential_file_path(const std::string &path) std::string utils::get_potential_file_path(const std::string &path)
{ {
@ -1047,8 +1043,11 @@ std::string utils::get_potential_file_path(const std::string &path)
const char *var = getenv("LAMMPS_POTENTIALS"); const char *var = getenv("LAMMPS_POTENTIALS");
if (var != nullptr) { if (var != nullptr) {
Tokenizer dirs(var, OS_PATH_VAR_SEP); #if defined(_WIN32)
Tokenizer dirs(var, ";");
#else
Tokenizer dirs(var, ":");
#endif
while (dirs.has_next()) { while (dirs.has_next()) {
auto pot = utils::path_basename(filepath); auto pot = utils::path_basename(filepath);
auto dir = dirs.next(); auto dir = dirs.next();
@ -1060,7 +1059,6 @@ std::string utils::get_potential_file_path(const std::string &path)
} }
return ""; return "";
} }
#undef OS_PATH_VAR_SEP
/* ---------------------------------------------------------------------- /* ----------------------------------------------------------------------
read first line of potential file read first line of potential file

View File

@ -74,7 +74,8 @@ protected:
} }
}; };
#define MAX_BUF_SIZE 128 static constexpr int MAX_BUF_SIZE=128;
TEST_F(FileOperationsTest, safe_fgets) TEST_F(FileOperationsTest, safe_fgets)
{ {
char buf[MAX_BUF_SIZE]; char buf[MAX_BUF_SIZE];
@ -110,7 +111,6 @@ TEST_F(FileOperationsTest, safe_fgets)
fclose(fp); fclose(fp);
} }
#define MAX_BUF_SIZE 128
TEST_F(FileOperationsTest, fgets_trunc) TEST_F(FileOperationsTest, fgets_trunc)
{ {
char buf[MAX_BUF_SIZE]; char buf[MAX_BUF_SIZE];
@ -174,7 +174,6 @@ TEST_F(FileOperationsTest, fgets_trunc)
fclose(fp); fclose(fp);
} }
#define MAX_BUF_SIZE 128
TEST_F(FileOperationsTest, safe_fread) TEST_F(FileOperationsTest, safe_fread)
{ {
char buf[MAX_BUF_SIZE]; char buf[MAX_BUF_SIZE];