Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
1e1e9f82e8cf284377f349983e8d34d4caa010ba
[simgrid.git] / src / xbt / xbt_os_file.cpp
1 /* xbt_os_file.cpp -- portable interface to file-related functions            */
2
3 /* Copyright (c) 2017. The SimGrid Team.
4  * All rights reserved.                                                     */
5
6 /* This program is free software; you can redistribute it and/or modify it
7  * under the terms of the license (GNU LGPL) which comes with this package. */
8
9 #include "xbt/file.hpp" /* this module */
10
11 #include "xbt/file.h"
12 #include "xbt/sysdep.h"
13
14 #ifdef _WIN32
15 #include <windows.h>
16 #endif
17
18 #include <cstring>
19 #include <libgen.h> /* POSIX dirname */
20
21 /** @brief Returns the file component of a path (reimplementation of POSIX basename)
22  *
23  * The argument is never modified, and the returned value must be freed after use.
24  */
25 char *xbt_basename(const char *path)
26 {
27   return xbt_strdup(simgrid::xbt::Path(path).getBasename().c_str());
28 }
29
30 std::string simgrid::xbt::Path::getDirname()
31 {
32   std::string p(path_);
33   char *res = dirname(&p[0]);
34   return std::string(res, strlen(res));
35 }
36
37 std::string simgrid::xbt::Path::getBasename()
38 {
39   std::string p(path_);
40   char *res = basename(&p[0]);
41   return std::string(res, strlen(res));
42 }