Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Update copyright lines.
[simgrid.git] / src / xbt / xbt_os_file.cpp
1 /* xbt_os_file.cpp -- portable interface to file-related functions          */
2
3 /* Copyright (c) 2017-2021. The SimGrid Team. All rights reserved.          */
4
5 /* This program is free software; you can redistribute it and/or modify it
6  * under the terms of the license (GNU LGPL) which comes with this package. */
7
8 #include "src/internal_config.h"
9 #include "xbt/asserts.h"
10 #include "xbt/file.hpp" /* this module */
11
12 #ifdef _WIN32
13 #include <windows.h>
14 #endif
15
16 #if HAVE_UNISTD_H
17 #include <array>
18 #include <cerrno>
19 #include <unistd.h>
20 #endif
21
22 #include <cstring>
23 #include <libgen.h> /* POSIX dirname */
24
25 simgrid::xbt::Path::Path()
26 {
27 #if HAVE_UNISTD_H
28   std::array<char, 2048> buffer;
29   const char* cwd = getcwd(buffer.data(), 2048);
30   xbt_assert(cwd != nullptr, "Error during getcwd: %s", strerror(errno));
31   path_ = std::string(cwd);
32 #else
33   path_ = std::string(".");
34 #endif
35 }
36
37 std::string simgrid::xbt::Path::get_dir_name() const
38 {
39   std::string p(path_);
40   const char* res = dirname(&p[0]);
41   return std::string(res, strlen(res));
42 }
43
44 std::string simgrid::xbt::Path::get_base_name() const
45 {
46   std::string p(path_);
47   const char* res = basename(&p[0]);
48   return std::string(res, strlen(res));
49 }