Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
New function: xbt::Path() gets the CWD
[simgrid.git] / src / xbt / xbt_os_file.cpp
1 /* xbt_os_file.cpp -- portable interface to file-related functions          */
2
3 /* Copyright (c) 2017-2018. 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/file.hpp" /* this module */
10
11 #ifdef _WIN32
12 #include <windows.h>
13 #endif
14
15 #if HAVE_UNISTD_H
16 #include <unistd.h>
17 #endif
18
19 #include <cstring>
20 #include <libgen.h> /* POSIX dirname */
21
22 simgrid::xbt::Path::Path()
23 {
24 #if HAVE_UNISTD_H
25   char buffer[2048];
26   getcwd(buffer, 2048);
27   path_ = std::string(buffer);
28 #else
29   path_ = std::string(".");
30 #endif
31 }
32
33 std::string simgrid::xbt::Path::get_dir_name()
34 {
35   std::string p(path_);
36   char *res = dirname(&p[0]);
37   return std::string(res, strlen(res));
38 }
39
40 std::string simgrid::xbt::Path::get_base_name()
41 {
42   std::string p(path_);
43   char *res = basename(&p[0]);
44   return std::string(res, strlen(res));
45 }