integrate platform sub-namespace into source code and documentation

this updates function calls to functions that have been moved from
the utils namepsace or the Info class to platform::
This commit is contained in:
Axel Kohlmeyer
2021-10-02 15:50:58 -04:00
parent 195fe81c60
commit 37bfe3d0ce
36 changed files with 207 additions and 473 deletions

View File

@ -165,44 +165,6 @@ std::string utils::getsyserror()
return std::string(strerror(errno));
}
/** On Linux the folder /proc/self/fd holds symbolic links to the actual
* pathnames associated with each open file descriptor of the current process.
* On MacOS the same kind of information can be obtained using ``fcntl(fd,F_GETPATH,buf)``.
* On Windows we use ``GetFinalPathNameByHandleA()`` which is available with
* Windows Vista and later.
*
* This function is used to provide a filename with error messages in functions
* where the filename is not passed as an argument, but the FILE * pointer.
*/
const char *utils::guesspath(char *buf, int len, FILE *fp)
{
memset(buf, 0, len);
#if defined(__linux__)
int fd = fileno(fp);
// get pathname from /proc or copy (unknown)
if (readlink(fmt::format("/proc/self/fd/{}", fd).c_str(), buf, len - 1) <= 0)
strncpy(buf, "(unknown)", len - 1);
#elif defined(__APPLE__)
int fd = fileno(fp);
char filepath[PATH_MAX];
if (fcntl(fd, F_GETPATH, filepath) != -1)
strncpy(buf, filepath, len - 1);
else
strncpy(buf, "(unknown)", len - 1);
#elif defined(_WIN32)
char filepath[MAX_PATH];
HANDLE h = (HANDLE) _get_osfhandle(_fileno(fp));
if (GetFinalPathNameByHandleA(h, filepath, PATH_MAX, FILE_NAME_NORMALIZED) > 0)
strncpy(buf, filepath, len - 1);
else
strncpy(buf, "(unknown)", len - 1);
#else
strncpy(buf, "(unknown)", len - 1);
#endif
return buf;
}
// read line into buffer. if line is too long keep reading until EOL or EOF
// but return only the first part with a newline at the end.
@ -256,7 +218,7 @@ void utils::sfgets(const char *srcname, int srcline, char *s, int size, FILE *fp
std::string errmsg;
// try to figure out the file name from the file pointer
if (!filename) filename = guesspath(buf, MAXPATHLENBUF, fp);
if (!filename) filename = platform::guesspath(fp, buf, MAXPATHLENBUF);
if (feof(fp)) {
errmsg = "Unexpected end of file while reading file '";
@ -285,7 +247,7 @@ void utils::sfread(const char *srcname, int srcline, void *s, size_t size, size_
std::string errmsg;
// try to figure out the file name from the file pointer
if (!filename) filename = guesspath(buf, MAXPATHLENBUF, fp);
if (!filename) filename = platform::guesspath(fp, buf, MAXPATHLENBUF);
if (feof(fp)) {
errmsg = "Unexpected end of file while reading file '";
@ -920,7 +882,7 @@ size_t utils::count_words(const std::string &text, const std::string &separators
size_t utils::trim_and_count_words(const std::string &text, const std::string &separators)
{
return utils::count_words(utils::trim_comment(text), separators);
return utils::count_words(trim_comment(text), separators);
}
/* ----------------------------------------------------------------------
@ -1065,71 +1027,6 @@ bool utils::is_id(const std::string &str)
return true;
}
/* ----------------------------------------------------------------------
strip off leading part of path, return just the filename
------------------------------------------------------------------------- */
std::string utils::path_basename(const std::string &path)
{
#if defined(_WIN32)
size_t start = path.find_last_of("/\\");
#else
size_t start = path.find_last_of('/');
#endif
if (start == std::string::npos) {
start = 0;
} else {
start += 1;
}
return path.substr(start);
}
/* ----------------------------------------------------------------------
Return only the leading part of a path, return just the directory
------------------------------------------------------------------------- */
std::string utils::path_dirname(const std::string &path)
{
#if defined(_WIN32)
size_t start = path.find_last_of("/\\");
#else
size_t start = path.find_last_of('/');
#endif
if (start == std::string::npos) return ".";
return path.substr(0, start);
}
/* ----------------------------------------------------------------------
join two paths
------------------------------------------------------------------------- */
std::string utils::path_join(const std::string &a, const std::string &b)
{
#if defined(_WIN32)
return fmt::format("{}\\{}", a, b);
#else
return fmt::format("{}/{}", a, b);
#endif
}
/* ----------------------------------------------------------------------
try to open file for reading
------------------------------------------------------------------------- */
bool utils::file_is_readable(const std::string &path)
{
FILE *fp = fopen(path.c_str(), "r");
if (fp) {
fclose(fp);
return true;
}
return false;
}
/* ----------------------------------------------------------------------
try to find potential file as specified by name
search current directory and the LAMMPS_POTENTIALS directory if
@ -1139,9 +1036,9 @@ bool utils::file_is_readable(const std::string &path)
std::string utils::get_potential_file_path(const std::string &path)
{
std::string filepath = path;
std::string filename = utils::path_basename(path);
std::string filename = platform::path_basename(path);
if (utils::file_is_readable(filepath)) {
if (platform::file_is_readable(filepath)) {
return filepath;
} else {
// try the environment variable directory
@ -1154,11 +1051,11 @@ std::string utils::get_potential_file_path(const std::string &path)
Tokenizer dirs(var, ":");
#endif
while (dirs.has_next()) {
auto pot = utils::path_basename(filepath);
auto pot = platform::path_basename(filepath);
auto dir = dirs.next();
filepath = utils::path_join(dir, pot);
filepath = platform::path_join(dir, pot);
if (utils::file_is_readable(filepath)) { return filepath; }
if (platform::file_is_readable(filepath)) { return filepath; }
}
}
}