Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Define simgrid::xbt::Path to manage file names.
authorArnaud Giersch <arnaud.giersch@univ-fcomte.fr>
Wed, 25 Oct 2017 11:51:50 +0000 (13:51 +0200)
committerArnaud Giersch <arnaud.giersch@univ-fcomte.fr>
Wed, 25 Oct 2017 13:27:08 +0000 (15:27 +0200)
ChangeLog
include/xbt/file.hpp [new file with mode: 0644]
src/xbt/xbt_os_file.c [deleted file]
src/xbt/xbt_os_file.cpp [new file with mode: 0644]
tools/cmake/DefinePackages.cmake

index 7d1e8f9..89a432d 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -9,6 +9,7 @@ SimGrid (3.18) NOT RELEASED YET (target: December 24 2017)
    Storage::getCname() to get a char*.
 
  XBT
+ - Define class simgrid::xbt::Path to manage file names.
  - Removed unused functions:
    - xbt/file.h: xbt_getline()
 
diff --git a/include/xbt/file.hpp b/include/xbt/file.hpp
new file mode 100644 (file)
index 0000000..e642320
--- /dev/null
@@ -0,0 +1,33 @@
+/* Copyright (c) 2017. The SimGrid Team.
+ * All rights reserved.                                                     */
+
+/* This program is free software; you can redistribute it and/or modify it
+ * under the terms of the license (GNU LGPL) which comes with this package. */
+
+#ifndef XBT_FILE_HPP
+#define XBT_FILE_HPP
+
+#include <string>
+#include <xbt/base.h>
+
+namespace simgrid {
+namespace xbt {
+
+class Path {
+public:
+  explicit Path(const char* path): path_(path) {}
+  explicit Path(std::string path): path_(std::move(path)) {}
+
+  /** @brief Returns the full path name */
+  std::string getName() { return path_; }
+  /** @brief Returns the directory component of a path (reimplementation of POSIX dirname) */
+  std::string getDirname();
+  /** @brief Returns the file component of a path (reimplementation of POSIX basename) */
+  std::string getBasename();
+
+private:
+  std::string path_;
+};
+}}
+
+#endif                          /* XBT_FILE_HPP */
diff --git a/src/xbt/xbt_os_file.c b/src/xbt/xbt_os_file.c
deleted file mode 100644 (file)
index d7dbf02..0000000
+++ /dev/null
@@ -1,40 +0,0 @@
-/* xbt_os_file.c -- portable interface to file-related functions            */
-
-/* Copyright (c) 2007-2010, 2012-2017. The SimGrid Team.
- * All rights reserved.                                                     */
-
-/* This program is free software; you can redistribute it and/or modify it
- * under the terms of the license (GNU LGPL) which comes with this package. */
-
-#include "xbt/sysdep.h"
-#include "xbt/file.h"    /* this module */
-#include "xbt/log.h"
-#include "src/internal_config.h"
-
-#ifdef _WIN32
-#include <windows.h>
-#endif
-
-#include "libgen.h" /* POSIX dirname */
-
-/** @brief Returns the directory component of a path (reimplementation of POSIX dirname)
- *
- * The argument is never modified, and the returned value must be freed after use.
- */
-char *xbt_dirname(const char *path) {
-  char *tmp = xbt_strdup(path);
-  char *res = xbt_strdup(dirname(tmp));
-  free(tmp);
-  return res;
-}
-
-/** @brief Returns the file component of a path (reimplementation of POSIX basename)
- *
- * The argument is never modified, and the returned value must be freed after use.
- */
-char *xbt_basename(const char *path) {
-  char *tmp = xbt_strdup(path);
-  char *res = xbt_strdup(basename(tmp));
-  free(tmp);
-  return res;
-}
diff --git a/src/xbt/xbt_os_file.cpp b/src/xbt/xbt_os_file.cpp
new file mode 100644 (file)
index 0000000..d3e87df
--- /dev/null
@@ -0,0 +1,51 @@
+/* xbt_os_file.cpp -- portable interface to file-related functions            */
+
+/* Copyright (c) 2017. The SimGrid Team.
+ * All rights reserved.                                                     */
+
+/* This program is free software; you can redistribute it and/or modify it
+ * under the terms of the license (GNU LGPL) which comes with this package. */
+
+#include "xbt/file.hpp" /* this module */
+
+#include "xbt/file.h"
+#include "xbt/sysdep.h"
+
+#ifdef _WIN32
+#include <windows.h>
+#endif
+
+#include <cstring>
+#include <libgen.h> /* POSIX dirname */
+
+/** @brief Returns the directory component of a path (reimplementation of POSIX dirname)
+ *
+ * The argument is never modified, and the returned value must be freed after use.
+ */
+char *xbt_dirname(const char *path)
+{
+  return xbt_strdup(simgrid::xbt::Path(path).getDirname().c_str());
+}
+
+/** @brief Returns the file component of a path (reimplementation of POSIX basename)
+ *
+ * The argument is never modified, and the returned value must be freed after use.
+ */
+char *xbt_basename(const char *path)
+{
+  return xbt_strdup(simgrid::xbt::Path(path).getBasename().c_str());
+}
+
+std::string simgrid::xbt::Path::getDirname()
+{
+  std::string p(path_);
+  char *res = dirname(&p[0]);
+  return std::string(res, strlen(res));
+}
+
+std::string simgrid::xbt::Path::getBasename()
+{
+  std::string p(path_);
+  char *res = basename(&p[0]);
+  return std::string(res, strlen(res));
+}
index 29045aa..93f4930 100644 (file)
@@ -288,7 +288,7 @@ set(XBT_SRC
   src/xbt/xbt_log_layout_format.c
   src/xbt/xbt_log_layout_simple.c
   src/xbt/xbt_main.cpp
-  src/xbt/xbt_os_file.c
+  src/xbt/xbt_os_file.cpp
   src/xbt/xbt_os_synchro.cpp
   src/xbt/xbt_os_time.c
   src/xbt/xbt_replay.cpp
@@ -717,6 +717,7 @@ set(headers_to_install
   include/xbt/exception.hpp
   include/xbt/Extendable.hpp
   include/xbt/file.h
+  include/xbt/file.hpp
   include/xbt/functional.hpp
   include/xbt/function_types.h
   include/xbt/future.hpp