[niepce: 9/12] fwk: path_dirname() remove the trailing slash. Return '.' if no path.



commit 2a800ec2897e19c89ffc84230d240fbbadc9c095
Author: Hubert Figuière <hub figuiere net>
Date:   Wed Oct 21 08:54:06 2015 -0400

    fwk: path_dirname() remove the trailing slash. Return '.' if no path.
    
    Add tests.
    The behaviour is closer to the dirname(1) command.

 src/fwk/utils/pathutils.cpp       |    6 +++---
 src/fwk/utils/pathutils.hpp       |   12 +++++-------
 src/fwk/utils/t/testpathutils.cpp |   10 +++++++---
 3 files changed, 15 insertions(+), 13 deletions(-)
---
diff --git a/src/fwk/utils/pathutils.cpp b/src/fwk/utils/pathutils.cpp
index a08d61a..b78f51d 100644
--- a/src/fwk/utils/pathutils.cpp
+++ b/src/fwk/utils/pathutils.cpp
@@ -1,7 +1,7 @@
 /*
  * niepce - fwk/utils/pathutils.cpp
  *
- * Copyright (C) 2009 Hubert Figuiere
+ * Copyright (C) 2009-2015 Hubert Figuière
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -71,9 +71,9 @@ std::string path_dirname(const std::string & path)
 {
     auto slash_idx = path.find_last_of("/");
     if(slash_idx == std::string::npos) {
-        return "";
+        return ".";
     }
-    return std::string(path.cbegin(), path.cbegin() + slash_idx + 1);
+    return std::string(path.cbegin(), path.cbegin() + slash_idx);
 }
 
 /** return the extension of a path
diff --git a/src/fwk/utils/pathutils.hpp b/src/fwk/utils/pathutils.hpp
index 9c8454a..979bd5a 100644
--- a/src/fwk/utils/pathutils.hpp
+++ b/src/fwk/utils/pathutils.hpp
@@ -1,7 +1,7 @@
 /*
  * niepce - fwk/utils/pathutils.hpp
  *
- * Copyright (C) 2009 Hubert Figuiere
+ * Copyright (C) 2009-2015 Hubert Figuière
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -18,9 +18,7 @@
  */
 
 
-
-#ifndef __FWK_PATHUTILS_HPP_
-#define __FWK_PATHUTILS_HPP_
+#pragma once
 
 #include <string>
 
@@ -36,8 +34,9 @@ std::string path_basename(const std::string & path);
  */
 std::string path_stem(const std::string & path);
 
-/** return the dirname of a path. Assume the last component is a file.
-    /foo/bar/baz.txt returns /foo/bar/
+/** return the dirname of a path and trailing slash removed.
+    Assume the last component is a file.
+    /foo/bar/baz.txt returns /foo/bar
  */
 std::string path_dirname(const std::string & path);
 
@@ -66,4 +65,3 @@ void path_remove_recursive(const std::string & path);
   fill-column:99
   End:
 */
-#endif
diff --git a/src/fwk/utils/t/testpathutils.cpp b/src/fwk/utils/t/testpathutils.cpp
index b018e2f..1b6ec6a 100644
--- a/src/fwk/utils/t/testpathutils.cpp
+++ b/src/fwk/utils/t/testpathutils.cpp
@@ -1,7 +1,7 @@
 /*
  * niepce - utils/testpathutils.cpp
  *
- * Copyright (C) 2009 Hubert Figuiere
+ * Copyright (C) 2009-2015 Hubert Figuière
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -31,12 +31,16 @@ int test_main( int, char *[] )             // note the name!
   BOOST_CHECK(fwk::path_basename("/foo/bar/baz.txt") == "baz.txt");
 
   BOOST_CHECK(fwk::path_stem("/foo/bar/baz.txt") == "/foo/bar/baz");
-  
+
+  BOOST_CHECK(fwk::path_dirname("/foo/bar/") == "/foo/bar");
+  BOOST_CHECK(fwk::path_dirname("/foo/bar/baz.txt") == "/foo/bar");
+  BOOST_CHECK(fwk::path_dirname("baz.txt") == ".");
+
   BOOST_CHECK(fwk::path_extension("/foo/bar/baz.txt") == ".txt");
   BOOST_CHECK(fwk::path_extension("img_2114.xmp") == ".xmp");
 
   BOOST_CHECK(fwk::path_replace_extension("/foo/bar/baz.txt", ".xmp") == "/foo/bar/baz.xmp");
 
-       return 0;
+  return 0;
 }
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]